: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(replay-vision): adding tests for new vision UI (#62632)
Hayne Ryu8d5985f19904
diff --git a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.tsindex 41589117aa59..a49f76f648aa 100644--- a/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts+++ b/products/replay_vision/frontend/replay_scanners/replayScannerLogic.test.ts@@ -84,6 +84,27 @@ describe('replayScannerLogic', () => {}),})})++ it('clears the showScannerErrors flag so stale validation does not bleed into the new type', async () => {+ logic.actions.submitScanner()+ await expectLogic(logic).toMatchValues({ showScannerErrors: true })+ logic.actions.setScannerType('summarizer')+ await expectLogic(logic).toMatchValues({ showScannerErrors: false })+ })+ })++ describe('submit intent', () => {+ it('advance intent routes to /triggers without calling the API', async () => {+ router.actions.push('/replay-vision/new/configure')+ logic.actions.setScannerValues({+ name: 'Test scanner',+ scanner_config: { prompt: 'Q?' },+ })+ logic.actions.setSubmitIntent('advance')+ await expectLogic(logic, () => logic.actions.submitScanner()).toFinishAllListeners()+ expect(router.values.location.pathname).toContain('/replay-vision/new/triggers')+ expect(logic.values.submitIntent).toBe('save')+ })})describe('validation errors', () => {diff --git a/products/replay_vision/frontend/replay_scanners/scannerEditorSceneLogic.test.ts b/products/replay_vision/frontend/replay_scanners/scannerEditorSceneLogic.test.tsnew file mode 100644index 000000000000..48f37a91ae1c--- /dev/null+++ b/products/replay_vision/frontend/replay_scanners/scannerEditorSceneLogic.test.ts@@ -0,0 +1,99 @@+import { router } from 'kea-router'+import { expectLogic } from 'kea-test-utils'++import { urls } from 'scenes/urls'++import { initKeaTests } from '~/test/init'++import { scannerEditorSceneLogic } from './scannerEditorSceneLogic'++describe('scannerEditorSceneLogic', () => {+ let logic: ReturnType<typeof scannerEditorSceneLogic.build>++ beforeEach(() => {+ initKeaTests()+ logic = scannerEditorSceneLogic()+ logic.mount()+ })++ afterEach(() => {+ logic?.unmount()+ })++ describe('URL → state', () => {+ it('reflects the template step for a new scanner', async () => {+ router.actions.push(urls.replayVisionScannerTemplate('new'))+ await expectLogic(logic).toMatchValues({+ scannerId: 'new',+ step: 'template',+ isNew: true,+ })+ })++ it('reflects the configure step for a new scanner', async () => {+ router.actions.push(urls.replayVisionScannerConfigure('new'))+ await expectLogic(logic).toMatchValues({+ scannerId: 'new',+ step: 'configure',+ isNew: true,+ })+ })++ it('reflects the triggers step for an existing scanner', async () => {+ router.actions.push(urls.replayVisionScannerTriggers('abc-123'))+ await expectLogic(logic).toMatchValues({+ scannerId: 'abc-123',+ step: 'triggers',+ isNew: false,+ })+ })++ it('redirects /:id/template → /:id/configure for existing scanners', async () => {+ router.actions.push(urls.replayVisionScannerTemplate('abc-123'))+ await expectLogic(logic).toMatchValues({+ scannerId: 'abc-123',+ step: 'configure',+ isNew: false,+ })+ expect(router.values.location.pathname.endsWith(urls.replayVisionScannerConfigure('abc-123'))).toBe(true)+ })+ })++ describe('visibleSteps', () => {+ it('shows all three steps for a new scanner', async () => {+ router.actions.push(urls.replayVisionScannerConfigure('new'))+ await expectLogic(logic).toMatchValues({+ visibleSteps: ['template', 'configure', 'triggers'],+ })+ })++ it('hides the template step for an existing scanner', async () => {+ router.actions.push(urls.replayVisionScannerConfigure('abc-123'))+ await expectLogic(logic).toMatchValues({+ visibleSteps: ['configure', 'triggers'],+ })+ })+ })++ describe('breadcrumbs', () => {+ it('labels the new-scanner trail when creating', async () => {+ router.actions.push(urls.replayVisionScannerConfigure('new'))+ await expectLogic(logic).toMatchValues({+ breadcrumbs: [+ expect.objectContaining({ key: 'replay-vision', name: 'Replay vision' }),+ expect.objectContaining({ key: 'new-scanner', name: 'New scanner' }),+ ],+ })+ })++ it('labels the scanner trail when editing', async () => {+ router.actions.push(urls.replayVisionScannerConfigure('abc-123'))+ await expectLogic(logic).toMatchValues({+ breadcrumbs: [+ expect.objectContaining({ key: 'replay-vision', name: 'Replay vision' }),+ expect.objectContaining({ key: 'scanner-abc-123', name: 'Scanner' }),+ ],+ })+ })+ })+})diff --git a/products/replay_vision/frontend/replay_scanners/scannerTemplates.test.ts b/products/replay_vision/frontend/replay_scanners/scannerTemplates.test.tsnew file mode 100644index 000000000000..6b9ac4b3fa88--- /dev/null+++ b/products/replay_vision/frontend/replay_scanners/scannerTemplates.test.ts@@ -0,0 +1,52 @@+import { defaultScannerTemplates, findScannerTemplate, newScanner } from './scannerTemplates'++describe('findScannerTemplate', () => {+ it('returns the matching template by key', () => {+ const first = defaultScannerTemplates[0]+ expect(findScannerTemplate(first.key)).toBe(first)+ })++ it.each([+ { label: 'undefined', input: undefined as string | undefined },+ { label: 'empty string', input: '' as string | undefined },+ { label: 'unknown key', input: 'nonexistent-key-xyz' as string | undefined },+ ])('returns undefined for $label', ({ input }) => {+ expect(findScannerTemplate(input)).toBeUndefined()+ })+})++describe('newScanner', () => {+ it.each([+ { label: 'null', input: null as string | null | undefined },+ { label: 'omitted', input: undefined as string | null | undefined },+ { label: 'unknown key', input: 'nonexistent-key-xyz' as string | null | undefined },+ ])('falls back to a blank monitor scanner when templateKey is $label', ({ input }) => {+ expect(newScanner(input)).toMatchObject({+ scanner_type: 'monitor',+ scanner_config: { prompt: '' },+ name: '',+ })+ })++ it('blank scanner carries the expected default id, enabled, and sampling fields', () => {+ expect(newScanner(null)).toMatchObject({+ id: 'new',+ enabled: true,+ sampling_rate: 1,+ description: '',+ })+ })++ it.each(defaultScannerTemplates.map((t) => [t.key, t]))(+ 'applies the %s template config and metadata when picked',+ (_key, template) => {+ const scanner = newScanner(template.key)+ expect(scanner).toMatchObject({+ name: template.scanner_name,+ description: template.scanner_description,+ scanner_type: template.scanner_type,+ scanner_config: template.scanner_config,+ })+ }+ )+})diff --git a/products/replay_vision/frontend/utils/observation.test.ts b/products/replay_vision/frontend/utils/observation.test.tsnew file mode 100644index 000000000000..4e1eefaf6437--- /dev/null+++ b/products/replay_vision/frontend/utils/observation.test.ts@@ -0,0 +1,79 @@+import type { ReplayObservationApi } from '../generated/api.schemas'+import { readFixedTags, readFreeformTags, readModelOutput, readScore, readTags, readVerdict } from './observation'++function makeObservation(modelOutput: Record<string, unknown> | null | undefined): ReplayObservationApi {+ return {+ id: 'o1',+ scanner_result:+ modelOutput === undefined ? undefined : ({ model_output: modelOutput } as Record<string, unknown>),+ } as unknown as ReplayObservationApi+}++describe('readModelOutput', () => {+ it('returns the model_output object when present', () => {+ expect(readModelOutput(makeObservation({ score: 7 }))).toEqual({ score: 7 })+ })++ it('returns null when scanner_result is absent', () => {+ expect(readModelOutput(makeObservation(undefined))).toBeNull()+ })++ it.each([null, 'hello', 42, true])('returns null when model_output is %p', (badShape) => {+ expect(readModelOutput(makeObservation(badShape as Record<string, unknown> | null))).toBeNull()+ })+})++describe('readScore', () => {+ it('returns the numeric score', () => {+ expect(readScore(makeObservation({ score: 7.5 }))).toBe(7.5)+ })++ it.each(['7', null, undefined, true, [1, 2]])('returns null for non-numeric score %p', (badScore) => {+ expect(readScore(makeObservation({ score: badScore }))).toBeNull()+ })++ it('returns null when model_output is absent', () => {+ expect(readScore(makeObservation(undefined))).toBeNull()+ })+})++describe('readVerdict', () => {+ it.each(['yes', 'no', 'inconclusive'] as const)('accepts %s', (verdict) => {+ expect(readVerdict(makeObservation({ verdict }))).toBe(verdict)+ })++ it.each(['Yes', 'YES', 'maybe', '', 1, null])('rejects non-canonical verdict %p', (bad) => {+ expect(readVerdict(makeObservation({ verdict: bad }))).toBeNull()+ })+})++describe('readFixedTags / readFreeformTags / readTags', () => {+ it('returns the fixed tag array', () => {+ expect(readFixedTags(makeObservation({ tags: ['bug', 'frustration'] }))).toEqual(['bug', 'frustration'])+ })++ it('filters non-string entries from the fixed tag array', () => {+ expect(readFixedTags(makeObservation({ tags: ['bug', 42, null, 'frustration'] }))).toEqual([+ 'bug',+ 'frustration',+ ])+ })++ it('returns an empty array when tags is missing or wrong shape', () => {+ expect(readFixedTags(makeObservation({}))).toEqual([])+ expect(readFixedTags(makeObservation({ tags: 'bug' }))).toEqual([])+ expect(readFixedTags(makeObservation(undefined))).toEqual([])+ })++ it('reads freeform tags from tags_freeform', () => {+ expect(readFreeformTags(makeObservation({ tags_freeform: ['custom'] }))).toEqual(['custom'])+ })++ it('concatenates fixed then freeform in readTags', () => {+ expect(readTags(makeObservation({ tags: ['bug'], tags_freeform: ['custom', 'note'] }))).toEqual([+ 'bug',+ 'custom',+ 'note',+ ])+ })+})diff --git a/products/replay_vision/frontend/utils/quotaProjection.test.ts b/products/replay_vision/frontend/utils/quotaProjection.test.tsnew file mode 100644index 000000000000..0a18de74a9f4--- /dev/null+++ b/products/replay_vision/frontend/utils/quotaProjection.test.ts@@ -0,0 +1,91 @@+import { dayjs } from 'lib/dayjs'++import type { VisionQuotaApi } from '../generated/api.schemas'+import { QUOTA_WARN_THRESHOLD, projectQuota, quotaUx } from './quotaProjection'++function makeQuota(overrides: Partial<VisionQuotaApi> = {}): VisionQuotaApi {+ const now = dayjs()+ return {+ monthly_quota: 10_000,+ usage_this_month: 0,+ remaining: 10_000,+ exhausted: false,+ period_start: now.subtract(10, 'day').toISOString(),+ period_end: now.add(20, 'day').toISOString(),+ ...overrides,+ } as VisionQuotaApi+}++describe('projectQuota', () => {+ it('returns the empty projection when quota is null or unbounded', () => {+ expect(projectQuota(null)).toMatchObject({ status: 'safe', daysRemaining: 0 })+ expect(projectQuota(makeQuota({ monthly_quota: 0 }))).toMatchObject({ status: 'safe', combinedDailyRate: 0 })+ })++ it('safe when current burn projects well under the cap', () => {+ const proj = projectQuota(makeQuota({ usage_this_month: 1_000, remaining: 9_000 }))+ expect(proj.status).toBe('safe')+ expect(proj.projectedPeriodEndRatio).toBeLessThan(QUOTA_WARN_THRESHOLD)+ })++ it('warning when projection crosses the warn threshold but stays under cap', () => {+ // 3,000 used over 10 days → 300/day. Projected to end at 9,000 (90% of cap) without exhausting.+ const proj = projectQuota(makeQuota({ usage_this_month: 3_000, remaining: 7_000 }))+ expect(proj.status).toBe('warning')+ expect(proj.projectedPeriodEndRatio).toBeGreaterThanOrEqual(QUOTA_WARN_THRESHOLD)+ expect(proj.capReachInPeriod).toBe(false)+ })++ it('danger when projected to exhaust before period end', () => {+ const proj = projectQuota(makeQuota({ usage_this_month: 9_000, remaining: 1_000 }))+ expect(proj.status).toBe('danger')+ expect(proj.capReachInPeriod).toBe(true)+ expect(proj.capReachDate).not.toBeNull()+ })++ it('danger when explicitly exhausted regardless of historical burn', () => {+ const proj = projectQuota(makeQuota({ usage_this_month: 10_000, remaining: 0, exhausted: true }))+ expect(proj.status).toBe('danger')+ })++ it('projection is unconfident before 3 days have elapsed', () => {+ const now = dayjs()+ const proj = projectQuota(+ makeQuota({+ period_start: now.subtract(1, 'day').toISOString(),+ period_end: now.add(29, 'day').toISOString(),+ })+ )+ expect(proj.projectionConfident).toBe(false)+ })++ it('adds scanner projected monthly on top of historical burn', () => {+ const noScanner = projectQuota(makeQuota({ usage_this_month: 1_000, remaining: 9_000 }))+ const withScanner = projectQuota(makeQuota({ usage_this_month: 1_000, remaining: 9_000 }), 6_000)+ expect(withScanner.combinedDailyRate).toBeGreaterThan(noScanner.combinedDailyRate)+ expect(withScanner.projectedPeriodEndRatio).toBeGreaterThan(noScanner.projectedPeriodEndRatio)+ })+})++describe('quotaUx', () => {+ it('returns nothing when no quota is configured', () => {+ expect(quotaUx(null)).toEqual({})+ expect(quotaUx(makeQuota({ monthly_quota: 0 }))).toEqual({})+ })++ it('blocks with a disabledReason when exhausted', () => {+ const ux = quotaUx(makeQuota({ usage_this_month: 10_000, remaining: 0, exhausted: true }))+ expect(ux.disabledReason).toMatch(/quota reached/i)+ expect(ux.tooltip).toBeUndefined()+ })++ it('shows a tooltip near the warn threshold but does not block', () => {+ const ux = quotaUx(makeQuota({ usage_this_month: 8_500, remaining: 1_500 }))+ expect(ux.disabledReason).toBeUndefined()+ expect(ux.tooltip).toContain('1,500')+ })++ it('returns nothing while usage is well under the threshold', () => {+ expect(quotaUx(makeQuota({ usage_this_month: 1_000, remaining: 9_000 }))).toEqual({})+ })+})