Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.0 KB
Raw Blame
import type { HogFlowBatchJobApi } from 'products/workflows/frontend/generated/api.schemas'

import { getBroadcastStatus } from './broadcastsLogic'

const flow = (
    status: string,
    origin_product: string | null = 'broadcasts'
): Parameters<typeof getBroadcastStatus>[0] => ({ status, origin_product })
const withJob = (status: string): { latestBatchJob: HogFlowBatchJobApi; totals: Record<string, number> } => ({
    latestBatchJob: { status } as HogFlowBatchJobApi,
    totals: {},
})

describe('broadcast status', () => {
    it.each([
        ['a draft, whatever its runs say', flow('draft'), withJob('completed'), 'draft'],
        ['an archived broadcast', flow('archived'), withJob('completed'), 'archived'],
        ['a run still going', flow('active'), withJob('active'), 'sending'],
        ['a run that finished', flow('active'), { ...withJob('completed'), hasPendingSchedule: false }, 'sent'],
        ['a finished run whose schedules did not load', flow('active'), withJob('completed'), 'unknown'],
        // A terminal run used to fall through to the no-run fallback and read as "scheduled",
        // telling the sender another send was pending when nothing was coming.
        ['a run that failed', flow('active'), withJob('failed'), 'failed'],
        ['a run that was cancelled', flow('active'), withJob('cancelled'), 'failed'],
        ['no run yet', flow('active'), { latestBatchJob: null, totals: {} }, 'scheduled'],
        [
            'no run and no schedule to come',
            flow('active'),
            { latestBatchJob: null, totals: {}, hasPendingSchedule: false },
            'failed',
        ],
        [
            'a workflow waiting for a send started through the API',
            flow('active', null),
            { latestBatchJob: null, totals: {}, hasPendingSchedule: false },
            'scheduled',
        ],
        [
            'a recurring broadcast between runs',
            flow('active'),
            { ...withJob('completed'), hasPendingSchedule: true },
            'scheduled',
        ],
        [
            'a recurring broadcast whose last run failed',
            flow('active'),
            { ...withJob('failed'), hasPendingSchedule: true },
            'failed',
        ],
        // Runs not loaded yet, or failed to load, used to read as "scheduled" for a broadcast that already sent.
        ['a live broadcast whose runs have not loaded', flow('active'), undefined, 'unknown'],
        [
            'a run the server marks as partly failed',
            flow('active'),
            { ...withJob('partially_failed'), hasPendingSchedule: false },
            'partially_failed',
        ],
        [
            'a recurring broadcast whose last run partly failed',
            flow('active'),
            { ...withJob('partially_failed'), hasPendingSchedule: true },
            'scheduled',
        ],
    ])('reads %s as %s', (_name, broadcast, details, expected) => {
        expect(getBroadcastStatus(broadcast, details)).toBe(expected)
    })
})