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.

4.7 KB
Raw Blame
import {
    type ExperimentExposureHealthFinding,
    type ExperimentExposureHealthFindingCode,
    type ExperimentExposureQueryResponse,
    NodeKind,
} from '~/queries/schema/schema-general'

import type { ExperimentHealthApi, ExperimentHealthFindingApi } from '../generated/api.schemas'
import { healthIssues, healthPanelFindings } from './healthPanelFindings'

const serverFinding = (
    code: ExperimentHealthFindingApi['code'],
    severity: ExperimentHealthFindingApi['severity'] = 'warning'
): ExperimentHealthFindingApi => ({
    code,
    subcode: null,
    reason: null,
    severity,
    title: code,
    detail: code,
    evidence: {},
    actions: [],
    diagnostic_ref: null,
    check: 'no_metric',
})

const exposureFinding = (code: ExperimentExposureHealthFindingCode): ExperimentExposureHealthFinding => ({
    code,
    subcode: null,
    reason: null,
    severity: 'warning',
    title: code,
    detail: code,
    evidence: {},
    actions: [],
    diagnostic_ref: null,
})

const UNEVEN_EXPOSURES = {
    kind: NodeKind.ExperimentExposureQuery,
    timeseries: [
        { variant: 'control', days: ['2026-10-01', '2026-10-02'], exposure_counts: [300, 600] },
        { variant: 'test', days: ['2026-10-01', '2026-10-02'], exposure_counts: [200, 400] },
    ],
    total_exposures: { control: 600, test: 400 },
    date_range: { date_from: '2026-10-01', date_to: '2026-10-02' },
    health_findings: [exposureFinding('srm'), exposureFinding('bias_risk_multiple_excluded')],
} satisfies ExperimentExposureQueryResponse

describe('healthPanelFindings', () => {
    test.each<{
        name: string
        health: ExperimentHealthApi | null
        exposures: ExperimentExposureQueryResponse | null
        isExperimentDraft: boolean
        expected: string[] | null
    }>([
        {
            name: 'no panel without server findings, so the page keeps its own warnings',
            health: null,
            exposures: UNEVEN_EXPOSURES,
            isExperimentDraft: false,
            expected: null,
        },
        {
            name: 'no exposure finding before the exposures load',
            health: { findings: [serverFinding('no_metric')], checks: [] },
            exposures: null,
            isExperimentDraft: false,
            expected: ['no_metric'],
        },
        {
            name: 'the findings of the exposure answer join those of the experiment read',
            health: { findings: [serverFinding('no_metric')], checks: [] },
            exposures: UNEVEN_EXPOSURES,
            isExperimentDraft: false,
            expected: ['no_metric', 'srm', 'bias_risk_multiple_excluded'],
        },
        {
            name: 'a draft shows no finding of the exposure answer that a reset left in the page',
            health: { findings: [serverFinding('flag_live_before_launch')], checks: [] },
            exposures: UNEVEN_EXPOSURES,
            isExperimentDraft: true,
            expected: ['flag_live_before_launch'],
        },
        {
            name: 'an info finding sorts below the warnings of both sources',
            health: {
                findings: [serverFinding('forced_variant_release_condition', 'info'), serverFinding('no_metric')],
                checks: [],
            },
            exposures: UNEVEN_EXPOSURES,
            isExperimentDraft: false,
            expected: ['no_metric', 'srm', 'bias_risk_multiple_excluded', 'forced_variant_release_condition'],
        },
        {
            name: 'a code the experiment read sent is not added again from the exposure answer',
            health: { findings: [serverFinding('bias_risk_multiple_excluded')], checks: [] },
            exposures: UNEVEN_EXPOSURES,
            isExperimentDraft: false,
            expected: ['bias_risk_multiple_excluded', 'srm'],
        },
    ])('$name', ({ health, exposures, isExperimentDraft, expected }) => {
        expect(healthPanelFindings(health, exposures, isExperimentDraft)?.map(({ code }) => code) ?? null).toEqual(
            expected
        )
    })

    test.each<{ name: string; findings: ExperimentHealthFindingApi[]; expected: string[] }>([
        {
            name: 'the chip counts no info finding',
            findings: [serverFinding('forced_variant_release_condition', 'info')],
            expected: [],
        },
        {
            name: 'the chip counts warnings next to an info finding',
            findings: [serverFinding('forced_variant_release_condition', 'info'), serverFinding('no_metric')],
            expected: ['no_metric'],
        },
    ])('$name', ({ findings, expected }) => {
        expect(
            healthIssues(healthPanelFindings({ findings, checks: [] }, null, false)).map(({ code }) => code)
        ).toEqual(expected)
    })
})