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.

5.0 KB
Raw Blame
import { AlertCalculationInterval, AlertConditionType, InsightThresholdType } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'

import type { AlertFormType } from './alertFormLogic'
import { getAlertFormValidationErrors, THRESHOLD_BOUNDS_FORM_ERROR, thresholdAlertHasBounds } from './alertFormSchema'

const baseAlert: AlertFormType = {
    name: 'My alert',
    enabled: true,
    created_by: null,
    created_at: '',
    config: {
        type: 'TrendsAlertConfig',
        series_index: 0,
        check_ongoing_interval: false,
    },
    threshold: {
        configuration: {
            type: InsightThresholdType.ABSOLUTE,
            bounds: { upper: 100 },
        },
    },
    condition: { type: AlertConditionType.ABSOLUTE_VALUE },
    subscribed_users: [],
    checks: [],
    calculation_interval: AlertCalculationInterval.DAILY,
    skip_weekend: false,
    schedule_restriction: null,
    detector_config: null,
    insight: 42,
}

describe('alertFormSchema', () => {
    beforeEach(() => {
        initKeaTests()
    })

    it('returns no errors for a valid threshold alert', () => {
        expect(getAlertFormValidationErrors(baseAlert)).toEqual({})
    })

    test.each([-1, 101, 1.5])('rejects evaluation delay %s', (delay) => {
        expect(
            getAlertFormValidationErrors({ ...baseAlert, evaluation_delay_intervals: delay }).evaluation_delay_intervals
        ).toBeTruthy()
    })

    test.each([
        ['rejects', AlertConditionType.ABSOLUTE_VALUE, { upper: 100 }, true],
        ['allows a stale flag on', AlertConditionType.RELATIVE_DECREASE, { upper: 100 }, false],
        ['allows a stale flag on', AlertConditionType.ABSOLUTE_VALUE, { lower: 5 }, false],
    ])('%s the ongoing interval with a delay on %s %o', (_, conditionType, bounds, rejected) => {
        expect(
            !!getAlertFormValidationErrors({
                ...baseAlert,
                evaluation_delay_intervals: 2,
                condition: { type: conditionType },
                threshold: { configuration: { type: InsightThresholdType.ABSOLUTE, bounds } },
                config: { type: 'TrendsAlertConfig', series_index: 0, check_ongoing_interval: true },
            }).evaluation_delay_intervals
        ).toBe(rejected)
    })

    it('requires a name', () => {
        expect(getAlertFormValidationErrors({ ...baseAlert, name: '' }).name).toBe('You need to give your alert a name')
    })

    it('requires at least one threshold bound for non-detector alerts', () => {
        const errors = getAlertFormValidationErrors({
            ...baseAlert,
            threshold: {
                configuration: {
                    type: InsightThresholdType.ABSOLUTE,
                    bounds: {},
                },
            },
        })
        expect(errors.threshold).toBe(THRESHOLD_BOUNDS_FORM_ERROR)
        expect(
            thresholdAlertHasBounds({
                ...baseAlert,
                threshold: { configuration: { type: InsightThresholdType.ABSOLUTE, bounds: {} } },
            })
        ).toBe(false)
    })

    it('requires the lower threshold to be below the upper threshold', () => {
        const errors = getAlertFormValidationErrors({
            ...baseAlert,
            threshold: {
                configuration: {
                    type: InsightThresholdType.ABSOLUTE,
                    bounds: { lower: 5, upper: 1 },
                },
            },
        })

        expect(errors.threshold).toBe('The “Less than” value must be lower than the “More than” value')
    })

    it('rejects negative thresholds for relative conditions', () => {
        const errors = getAlertFormValidationErrors({
            ...baseAlert,
            condition: { type: AlertConditionType.RELATIVE_DECREASE },
            threshold: {
                configuration: {
                    type: InsightThresholdType.ABSOLUTE,
                    bounds: { upper: -1 },
                },
            },
        })

        expect(errors.threshold).toBe('Enter zero or a positive change value')
    })

    it('treats cleared threshold inputs as missing bounds', () => {
        expect(
            thresholdAlertHasBounds({
                ...baseAlert,
                threshold: {
                    configuration: {
                        type: InsightThresholdType.ABSOLUTE,
                        bounds: { lower: '' as unknown as number, upper: '' as unknown as number },
                    },
                },
            })
        ).toBe(false)
    })

    it.each([{}, null])('skips threshold bounds when detector_config is set and bounds are %p', (bounds) => {
        expect(
            getAlertFormValidationErrors({
                ...baseAlert,
                detector_config: { type: 'zscore', threshold: 3 },
                threshold: {
                    configuration: {
                        type: InsightThresholdType.ABSOLUTE,
                        bounds,
                    },
                },
            })
        ).toEqual({})
    })
})