: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): adding tests for replay vision FE (#58855)
Hayne Ryub556ebd331be
diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex adf501379b55..b047fe149d83 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -275,7 +275,7 @@ importers:version: 3.12.1jest:specifier: 'catalog:'- version: 29.7.0(@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))+ version: 29.7.0(@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))oxfmt:specifier: 'catalog:'version: 0.35.0@@ -448,7 +448,7 @@ importers:version: 7.6.4(@babel/[email protected])(@swc/[email protected])([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected])([email protected])'@storybook/test-runner':specifier: ^0.16.0- version: 0.16.0(@types/[email protected])([email protected])+ version: 0.16.0(@types/[email protected])([email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))'@storybook/theming':specifier: ^7.6.4@@ -1309,7 +1309,7 @@ importers:version: 29.7.0jest-image-snapshot:specifier: ^6.1.0- version: 6.4.0([email protected](@types/[email protected]))+ version: 6.4.0([email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])))kea-typegen:specifier: 3.6.2-leakfix.5version: 3.6.2-leakfix.5([email protected])@@ -1804,7 +1804,7 @@ importers:version: 7.6.24([email protected])([email protected]([email protected]))([email protected])([email protected])([email protected])([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected])([email protected]))'@storybook/test-runner':specifier: ^0.16.0- version: 0.16.0(@types/[email protected])([email protected])+ version: 0.16.0(@types/[email protected])([email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))'@storybook/theming':specifier: ^7.6.20@@ -1825,7 +1825,7 @@ importers:version: 4.7.0([email protected](@types/[email protected])([email protected])([email protected])([email protected])([email protected]))jest-image-snapshot:specifier: ^6.4.0- version: 6.4.0([email protected](@types/[email protected]))+ version: 6.4.0([email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])))storybook:specifier: ^7.6.4version: 7.6.4([email protected])@@ -3172,6 +3172,10 @@ importers:typescript:specifier: 6.0.3version: 6.0.3+ devDependencies:+ kea-test-utils:+ specifier: 'catalog:'+ version: 0.2.4([email protected]([email protected]))products/revenue_analytics:dependencies:@@ -29259,6 +29263,41 @@ snapshots:- ts-node- utf-8-validate+ '@jest/[email protected]([email protected](@swc/[email protected])(@types/[email protected])([email protected]))':+ dependencies:+ '@jest/console': 29.7.0+ '@jest/reporters': 29.7.0+ '@jest/test-result': 29.7.0+ '@jest/transform': 29.7.0+ '@jest/types': 29.6.3+ '@types/node': 22.18.8+ ansi-escapes: 4.3.2+ chalk: 4.1.2+ ci-info: 3.8.0+ exit: 0.1.2+ graceful-fs: 4.2.11+ jest-changed-files: 29.7.0+ jest-config: 29.7.0(@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))+ jest-haste-map: 29.7.0+ jest-message-util: 29.7.0+ jest-regex-util: 29.6.3+ jest-resolve: 29.7.0+ jest-resolve-dependencies: 29.7.0+ jest-runner: 29.7.0+ jest-runtime: 29.7.0+ jest-snapshot: 29.7.0+ jest-util: 29.7.0+ jest-validate: 29.7.0+ jest-watcher: 29.7.0+ micromatch: 4.0.8+ pretty-format: 29.7.0+ slash: 3.0.0+ strip-ansi: 6.0.1+ transitivePeerDependencies:+ - babel-plugin-macros+ - supports-color+ - ts-node+'@jest/[email protected]([email protected](@swc/[email protected])(@types/[email protected])([email protected]))':dependencies:'@jest/console': 29.7.0@@ -34694,7 +34733,7 @@ snapshots:- encoding- supports-color+ '@storybook/[email protected](@types/[email protected])([email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))':dependencies:'@babel/core': 7.29.0'@babel/generator': 7.29.1@@ -38676,6 +38715,21 @@ snapshots:safe-buffer: 5.2.1sha.js: 2.4.12+ [email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])):+ dependencies:+ '@jest/types': 29.6.3+ chalk: 4.1.2+ exit: 0.1.2+ graceful-fs: 4.2.11+ jest-config: 29.7.0(@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))+ jest-util: 29.7.0+ prompts: 2.4.2+ transitivePeerDependencies:+ - '@types/node'+ - babel-plugin-macros+ - supports-color+ - ts-node+[email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])):dependencies:'@jest/types': 29.6.3@@ -42149,6 +42203,25 @@ snapshots:- ts-node- utf-8-validate+ [email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])):+ dependencies:+ '@jest/core': 29.7.0([email protected](@swc/[email protected])(@types/[email protected])([email protected]))+ '@jest/test-result': 29.7.0+ '@jest/types': 29.6.3+ chalk: 4.1.2+ create-jest: 29.7.0(@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))+ exit: 0.1.2+ import-local: 3.2.0+ jest-config: 29.7.0(@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))+ jest-util: 29.7.0+ jest-validate: 29.7.0+ yargs: 17.7.2+ transitivePeerDependencies:+ - '@types/node'+ - babel-plugin-macros+ - supports-color+ - ts-node+[email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])):dependencies:'@jest/core': 29.7.0([email protected](@swc/[email protected])(@types/[email protected])([email protected]))@@ -42221,6 +42294,37 @@ snapshots:- supports-color- utf-8-validate+ [email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])):+ dependencies:+ '@babel/core': 7.29.0+ '@jest/test-sequencer': 29.7.0+ '@jest/types': 29.6.3+ babel-jest: 29.7.0(@babel/[email protected])+ chalk: 4.1.2+ ci-info: 3.8.0+ deepmerge: 4.3.1+ glob: 7.2.3+ graceful-fs: 4.2.11+ jest-circus: 29.7.0+ jest-environment-node: 29.7.0+ jest-get-type: 29.6.3+ jest-regex-util: 29.6.3+ jest-resolve: 29.7.0+ jest-runner: 29.7.0+ jest-util: 29.7.0+ jest-validate: 29.7.0+ micromatch: 4.0.8+ parse-json: 5.2.0+ pretty-format: 29.7.0+ slash: 3.0.0+ strip-json-comments: 3.1.1+ optionalDependencies:+ '@types/node': 22.18.8+ transitivePeerDependencies:+ - babel-plugin-macros+ - supports-color+[email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])):dependencies:'@babel/core': 7.29.0@@ -42471,7 +42575,7 @@ snapshots:optionalDependencies:fsevents: 2.3.3+ [email protected]([email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))):dependencies:chalk: 4.1.2get-stdin: 5.0.1@@ -43127,6 +43231,18 @@ snapshots:- ts-node- utf-8-validate+ [email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])):+ dependencies:+ '@jest/core': 29.7.0([email protected](@swc/[email protected])(@types/[email protected])([email protected]))+ '@jest/types': 29.6.3+ import-local: 3.2.0+ jest-cli: 29.7.0(@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected]))+ transitivePeerDependencies:+ - '@types/node'+ - babel-plugin-macros+ - supports-color+ - ts-node+[email protected](@types/[email protected])([email protected](@swc/[email protected])(@types/[email protected])([email protected])):dependencies:'@jest/core': 29.7.0([email protected](@swc/[email protected])(@types/[email protected])([email protected]))@@ -49469,6 +49585,27 @@ snapshots:tslib: 2.8.1typescript: 6.0.3+ dependencies:+ '@cspotcode/source-map-support': 0.8.1+ '@tsconfig/node10': 1.0.9+ '@tsconfig/node12': 1.0.11+ '@tsconfig/node14': 1.0.3+ '@tsconfig/node16': 1.0.3+ '@types/node': 22.18.8+ acorn: 8.15.0+ acorn-walk: 8.3.2+ arg: 4.1.3+ create-require: 1.1.1+ diff: 4.0.2+ make-error: 1.3.6+ typescript: 6.0.3+ v8-compile-cache-lib: 3.0.1+ yn: 3.1.1+ optionalDependencies:+ '@swc/core': 1.11.4+ optional: true+dependencies:'@cspotcode/source-map-support': 0.8.1diff --git a/products/replay_vision/frontend/replay_lenses/ReplayLens.tsx b/products/replay_vision/frontend/replay_lenses/ReplayLens.tsxindex 8a540fbd97dc..60003e97b0c8 100644--- a/products/replay_vision/frontend/replay_lenses/ReplayLens.tsx+++ b/products/replay_vision/frontend/replay_lenses/ReplayLens.tsx@@ -9,6 +9,7 @@ import {LemonSwitch,LemonTab,LemonTabs,+ LemonTag,LemonTextArea,} from '@posthog/lemon-ui'@@ -68,22 +69,32 @@ export function ReplayLensSceneComponent({ tabId }: { tabId: string }): JSX.Elem<LemonTextArea placeholder="What this lens looks for and why." minRows={2} /></Field>- <Field name="lens_type" label="Lens type">- <LemonSelect- value={lens.lens_type}- onChange={(v) => setLensType(v)}- options={LENS_TYPE_OPTIONS.map((opt) => ({- value: opt.value,- label: opt.label,- labelInMenu: (- <div className="flex flex-col">- <span className="font-medium">{opt.label}</span>- <span className="text-xs text-muted">{opt.description}</span>- </div>- ),- }))}- />- </Field>+ {isNew ? (+ <Field name="lens_type" label="Lens type">+ <LemonSelect+ value={lens.lens_type}+ onChange={(v) => setLensType(v)}+ options={LENS_TYPE_OPTIONS.map((opt) => ({+ value: opt.value,+ label: opt.label,+ labelInMenu: (+ <div className="flex flex-col">+ <span className="font-medium">{opt.label}</span>+ <span className="text-xs text-muted">{opt.description}</span>+ </div>+ ),+ }))}+ />+ </Field>+ ) : (+ <div className="space-y-1">+ <label className="block text-sm font-medium">Lens type</label>+ <LemonTag type="option">+ {LENS_TYPE_OPTIONS.find((o) => o.value === lens.lens_type)?.label ?? lens.lens_type}+ </LemonTag>+ <div className="text-xs text-muted">Lens type is fixed after creation.</div>+ </div>+ )}<LensTypeConfigEditor lensId={lensId} tabId={tabId} />diff --git a/products/replay_vision/frontend/replay_lenses/replayLensLogic.test.ts b/products/replay_vision/frontend/replay_lenses/replayLensLogic.test.tsnew file mode 100644index 000000000000..6e3143d4b793--- /dev/null+++ b/products/replay_vision/frontend/replay_lenses/replayLensLogic.test.ts@@ -0,0 +1,142 @@+import { expectLogic } from 'kea-test-utils'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import { replayLensLogic } from './replayLensLogic'+import { ClassifierLens, ScorerLens } from './types'++describe('replayLensLogic', () => {+ let logic: ReturnType<typeof replayLensLogic.build>++ beforeEach(() => {+ useMocks({+ get: {+ '/api/environments/:team/vision/lenses/:id/': () => [404, {}],+ '/api/environments/:team/vision/lenses/:id/observations/': { results: [] },+ },+ })+ initKeaTests()+ logic = replayLensLogic({ id: 'new', tabId: 'test' })+ logic.mount()+ })++ afterEach(() => {+ logic?.unmount()+ })++ describe('form defaults', () => {+ it('new lens starts as monitor with empty prompt and default sampling', () => {+ expect(logic.values.lens).toMatchObject({+ id: 'new',+ name: '',+ enabled: true,+ lens_type: 'monitor',+ lens_config: { prompt: '' },+ sampling_rate: 1,+ })+ })+ })++ describe('setLensType', () => {+ it.each([+ { type: 'monitor' as const, expectedConfig: { prompt: '' } },+ { type: 'summarizer' as const, expectedConfig: { prompt: '', length: 'medium' } },+ { type: 'classifier' as const, expectedConfig: { prompt: '', tags: [], multi_label: true } },+ { type: 'scorer' as const, expectedConfig: { prompt: '', scale: { min: 0, max: 10 } } },+ { type: 'indexer' as const, expectedConfig: { prompt: '' } },+ ])(+ 'switching to $type replaces lens_config with the default for that type',+ async ({ type, expectedConfig }) => {+ await expectLogic(logic, () => logic.actions.setLensType(type)).toMatchValues({+ lens: expect.objectContaining({ lens_type: type, lens_config: expectedConfig }),+ })+ }+ )++ it('does not preserve old prompt across type changes', async () => {+ logic.actions.setLensValues({ lens_config: { prompt: 'Was there a refund?' } })+ await expectLogic(logic, () => logic.actions.setLensType('summarizer')).toMatchValues({+ lens: expect.objectContaining({ lens_config: { prompt: '', length: 'medium' } }),+ })+ })+ })++ describe('validation errors', () => {+ it.each([+ {+ name: 'flags missing name',+ setup: () => undefined,+ expectedErrors: { name: 'Name is required' },+ },+ {+ name: 'flags missing prompt',+ setup: () => undefined,+ expectedErrors: { lens_config: expect.objectContaining({ prompt: 'Prompt is required' }) },+ },+ {+ name: 'flags sampling rate outside (0, 1]',+ setup: () => logic.actions.setLensValues({ sampling_rate: 0 }),+ expectedErrors: { sampling_rate: expect.any(String) },+ },+ {+ name: 'flags scorer scale when min >= max',+ setup: () => {+ logic.actions.setLensType('scorer')+ logic.actions.setLensValues({+ lens_config: {+ prompt: 'rate this',+ scale: { min: 10, max: 5 },+ } as ScorerLens['lens_config'],+ })+ },+ expectedErrors: {+ lens_config: expect.objectContaining({ scale: expect.stringContaining('greater than') }),+ },+ },+ ])('$name', async ({ setup, expectedErrors }) => {+ setup()+ await expectLogic(logic).toMatchValues({+ lensValidationErrors: expect.objectContaining(expectedErrors),+ })+ })++ it('passes when all required fields are filled', async () => {+ logic.actions.setLensType('classifier')+ logic.actions.setLensValues({+ name: 'My lens',+ sampling_rate: 0.1,+ lens_config: { prompt: 'Categorize', tags: ['a'], multi_label: true } as ClassifierLens['lens_config'],+ })+ await expectLogic(logic).toMatchValues({+ isLensValid: true,+ })+ })+ })++ describe('hasUnsavedChanges', () => {+ it('is false when no original lens is loaded', () => {+ expect(logic.values.hasUnsavedChanges).toBe(false)+ })++ it('is false when current matches original', async () => {+ logic.actions.loadLensSuccess({+ ...logic.values.lens!,+ id: 'abc',+ name: 'Loaded',+ })+ await expectLogic(logic).toMatchValues({ hasUnsavedChanges: false })+ })++ it('is true after a form edit', async () => {+ logic.actions.loadLensSuccess({+ ...logic.values.lens!,+ id: 'abc',+ name: 'Loaded',+ })+ await expectLogic(logic, () => logic.actions.setLensValues({ name: 'Edited' })).toMatchValues({+ hasUnsavedChanges: true,+ })+ })+ })+})diff --git a/products/replay_vision/frontend/replay_lenses/replayLensSceneLogic.ts b/products/replay_vision/frontend/replay_lenses/replayLensSceneLogic.tsindex d468122a5d5f..9829d3b42ce3 100644--- a/products/replay_vision/frontend/replay_lenses/replayLensSceneLogic.ts+++ b/products/replay_vision/frontend/replay_lenses/replayLensSceneLogic.ts@@ -61,7 +61,8 @@ export const replayLensSceneLogic = kea<replayLensSceneLogicType>([tabAwareActionToUrl(({ values }) => ({setActiveTab: () => {- const tab = values.activeTab === 'configuration' ? undefined : values.activeTab+ const defaultTab: EditorTab = values.lensId === 'new' ? 'configuration' : 'observations'+ const tab = values.activeTab === defaultTab ? undefined : values.activeTabreturn [urls.replayVision(values.lensId),{ ...router.values.searchParams, tab },@@ -78,7 +79,8 @@ export const replayLensSceneLogic = kea<replayLensSceneLogicType>([actions.setLensId(lensId)}const raw = typeof searchParams.tab === 'string' ? searchParams.tab : ''- const tab: EditorTab = (ALL_EDITOR_TABS as string[]).includes(raw) ? (raw as EditorTab) : 'configuration'+ const defaultTab: EditorTab = lensId === 'new' ? 'configuration' : 'observations'+ const tab: EditorTab = (ALL_EDITOR_TABS as string[]).includes(raw) ? (raw as EditorTab) : defaultTabif (tab !== values.activeTab) {actions.setActiveTab(tab)}diff --git a/products/replay_vision/frontend/replay_lenses/replayLensesLogic.test.ts b/products/replay_vision/frontend/replay_lenses/replayLensesLogic.test.tsnew file mode 100644index 000000000000..1a8c60a6e894--- /dev/null+++ b/products/replay_vision/frontend/replay_lenses/replayLensesLogic.test.ts@@ -0,0 +1,134 @@+import { expectLogic } from 'kea-test-utils'++import { useMocks } from '~/mocks/jest'+import { initKeaTests } from '~/test/init'++import { replayLensesLogic } from './replayLensesLogic'+import { LensConfig, LensType, ReplayLens } from './types'++function defaultConfigForType(lensType: LensType): LensConfig {+ if (lensType === 'summarizer') {+ return { prompt: 'Summarize this session.', length: 'medium' }+ }+ if (lensType === 'classifier') {+ return { prompt: 'Tag this session.', tags: [], multi_label: true }+ }+ if (lensType === 'scorer') {+ return { prompt: 'Score this session.', scale: { min: 0, max: 10 } }+ }+ return { prompt: 'Did the user struggle?' }+}++function makeLens(overrides: Partial<ReplayLens> = {}): ReplayLens {+ const lensType: LensType = overrides.lens_type ?? 'monitor'+ const base = {+ id: 'lens-1',+ name: 'Confused checkout',+ description: '',+ enabled: true,+ sampling_rate: 0.1,+ query: null,+ provider: 'google',+ model: 'gemini-3-flash',+ emits_signals: false,+ lens_version: 1,+ last_swept_at: '2026-05-12T00:00:00Z',+ created_at: '2026-05-12T00:00:00Z',+ updated_at: '2026-05-12T00:00:00Z',+ created_by: null,+ lens_type: lensType,+ lens_config: defaultConfigForType(lensType),+ }+ return { ...base, ...overrides } as ReplayLens+}++describe('replayLensesLogic', () => {+ let logic: ReturnType<typeof replayLensesLogic.build>++ beforeEach(() => {+ useMocks({+ get: {+ '/api/environments/:team/vision/lenses/': { results: [] },+ '/api/environments/:team/vision/quota/': () => [404, {}],+ },+ })+ initKeaTests()+ logic = replayLensesLogic({ tabId: 'test' })+ logic.mount()+ })++ afterEach(() => {+ logic?.unmount()+ })++ const lenses: ReplayLens[] = [+ makeLens({ id: 'a', name: 'Confused checkout', lens_type: 'monitor', enabled: true }),+ makeLens({ id: 'b', name: 'Power user behavior', lens_type: 'classifier', enabled: false }),+ makeLens({ id: 'c', name: 'Refund summarizer', lens_type: 'summarizer', enabled: true }),+ ]++ it.each([+ { name: 'no filters returns all', search: '', enabled: [], types: [], expected: ['a', 'b', 'c'] },+ { name: 'search by name', search: 'checkout', enabled: [], types: [], expected: ['a'] },+ { name: 'search by prompt fragment', search: 'struggle', enabled: [], types: [], expected: ['a'] },+ { name: 'enabled filter', search: '', enabled: ['enabled' as const], types: [], expected: ['a', 'c'] },+ { name: 'disabled filter', search: '', enabled: ['disabled' as const], types: [], expected: ['b'] },+ {+ name: 'lens type filter',+ search: '',+ enabled: [],+ types: ['classifier' as LensType, 'summarizer' as LensType],+ expected: ['b', 'c'],+ },+ {+ name: 'combined search + enabled',+ search: 'refund',+ enabled: ['enabled' as const],+ types: [],+ expected: ['c'],+ },+ ])('filteredLenses: $name', async ({ search, enabled, types, expected }) => {+ await expectLogic(logic, () => {+ logic.actions.loadLensesSuccess(lenses)+ logic.actions.setSearch(search)+ logic.actions.setEnabledFilter(enabled)+ logic.actions.setLensTypeFilter(types)+ }).toMatchValues({+ filteredLenses: lenses.filter((l) => expected.includes(l.id)),+ })+ })++ it('hasActiveFilters tracks any active filter', async () => {+ await expectLogic(logic).toMatchValues({ hasActiveFilters: false })++ await expectLogic(logic, () => logic.actions.setSearch('foo')).toMatchValues({ hasActiveFilters: true })+ await expectLogic(logic, () => logic.actions.setSearch('')).toMatchValues({ hasActiveFilters: false })++ await expectLogic(logic, () => logic.actions.setEnabledFilter(['enabled'])).toMatchValues({+ hasActiveFilters: true,+ })+ })++ it('clearFilters resets all filter state', async () => {+ logic.actions.setSearch('foo')+ logic.actions.setEnabledFilter(['enabled'])+ logic.actions.setLensTypeFilter(['monitor'])+ await expectLogic(logic).toMatchValues({ hasActiveFilters: true })++ await expectLogic(logic, () => logic.actions.clearFilters()).toMatchValues({+ search: '',+ enabledFilter: [],+ lensTypeFilter: [],+ hasActiveFilters: false,+ })+ })++ it('toggleLensEnabledSuccess flips the lens row', async () => {+ await expectLogic(logic, () => {+ logic.actions.loadLensesSuccess(lenses)+ logic.actions.toggleLensEnabledSuccess('a')+ }).toMatchValues({+ lenses: expect.arrayContaining([expect.objectContaining({ id: 'a', enabled: false })]),+ })+ })+})diff --git a/products/replay_vision/package.json b/products/replay_vision/package.jsonindex ab173b2e79ae..6273612e47a1 100644--- a/products/replay_vision/package.json+++ b/products/replay_vision/package.json@@ -8,6 +8,9 @@"kea-forms": "catalog:","kea-router": "catalog:"},+ "devDependencies": {+ "kea-test-utils": "catalog:"+ },"peerDependencies": {"@posthog/icons": "*","@posthog/lemon-ui": "*",