: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
import { MOCK_DEFAULT_TEAM } from 'lib/api.mock'
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import posthog from 'posthog-js'
import api from 'lib/api'
import { FEATURE_FLAGS } from 'lib/constants'
import { dayjs } from 'lib/dayjs'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { playerSidebarLogic } from 'scenes/session-recordings/player/sidebar/playerSidebarLogic'
import { teamLogic } from 'scenes/teamLogic'
import { ExperimentMetricType, NodeKind } from '~/queries/schema/schema-general'
import { initKeaTests } from '~/test/init'
import {
Experiment,
FilterLogicalOperator,
PropertyFilterType,
PropertyOperator,
RecordingDurationFilter,
SessionRecordingSidebarTab,
TeamType,
} from '~/types'
import {
experimentsInSessionExposureRetrieve,
experimentsSessionBucketsCreate,
experimentsSessionContextsCreate,
experimentsSessionEventDeltasCreate,
} from 'products/experiments/frontend/generated/api'
import { visionScannersList } from 'products/replay_vision/frontend/generated/api'
import {
FUNNEL_DATA_WAREHOUSE_COMPLETION_REASON,
FUNNEL_SERVER_SIDE_COMPLETION_REASON,
RETENTION_UNLINKABLE_REASON,
} from '../utils'
import { viewRecordingsLinkabilityLogic } from '../viewRecordingsLinkabilityLogic'
import {
type ExperimentRecordingsNarrowingAction,
type ExperimentReplayRecording,
ExperimentReplayListEmptyReason,
experimentReplayTabLogic,
} from './experimentReplayTabLogic'
jest.mock('lib/utils/product-intents', () => ({
addProductIntentForCrossSell: jest.fn().mockResolvedValue(null),
}))
jest.mock('products/experiments/frontend/generated/api', () => ({
experimentsInSessionExposureRetrieve: jest.fn(),
experimentsSessionContextsCreate: jest.fn().mockResolvedValue({ results: [] }),
experimentsSessionBucketsCreate: jest.fn(),
experimentsSessionEventDeltasCreate: jest.fn(),
}))
jest.mock('products/replay_vision/frontend/generated/api', () => ({
visionScannersList: jest.fn().mockResolvedValue({ results: [] }),
}))
const BUCKET_RESPONSE = {
session_ids: ['bucket-1', 'bucket-2'],
truncated: false,
considered_metrics: [{ metric_uuid: 'metric-purchase', metric_name: 'Purchase' }],
excluded_metrics: [],
// Read against today, like the run windows below. A fixed scan window would age past the
// project's retention and start naming a different empty reason.
date_from: dayjs()
.subtract(30 * 24, 'hour')
.toISOString(),
date_to: dayjs().toISOString(),
filter_test_accounts: true,
}
/** A playlist page as the tab receives it, narrowed to what the watch cards read off it. */
const loadedPage = (ids: string[]): ExperimentReplayRecording[] => ids.map((id) => ({ id, recording_duration: 120 }))
const DELTA_RESPONSE = {
cards: [
{
kind: 'behavior',
event: 'pricing_faq',
variant: 'test',
strength: 'far_more',
metric_name: null,
recording_count: 2,
session_ids: ['card-session-1', 'card-session-2'],
highlights: [{ session_id: 'card-session-2', reason: '3 rage clicks' }],
},
],
variants: [
{ key: 'control', persons: 100, sessions: 140 },
{ key: 'test', persons: 100, sessions: 138 },
],
multiple_variant_persons: 0,
multiple_variant_handling: 'exclude',
metric_events: [],
date_from: '2026-01-01T00:00:00Z',
date_to: '2026-02-01T00:00:00Z',
filter_test_accounts: true,
used_exposure_fallback: false,
sessions_truncated: false,
events_truncated: false,
min_variant_persons: 50,
max_card_recordings: 20,
dropped_duplicate_cards: 0,
too_early: false,
empty_reason: null,
}
const PURCHASE_METRIC = {
kind: NodeKind.ExperimentMetric,
metric_type: ExperimentMetricType.MEAN,
uuid: 'metric-purchase',
name: 'Purchase',
source: { kind: NodeKind.EventsNode, event: 'purchase' },
}
const FUNNEL_METRIC = {
kind: NodeKind.ExperimentMetric,
metric_type: ExperimentMetricType.FUNNEL,
uuid: 'metric-funnel',
name: 'Checkout funnel',
series: [
{ kind: NodeKind.EventsNode, event: 'server_side_step' },
{ kind: NodeKind.EventsNode, event: 'client_step' },
],
}
const EXPERIMENT = {
id: 42,
feature_flag_key: 'my-flag',
start_date: '2026-01-01T00:00:00Z',
end_date: '2026-02-01T00:00:00Z',
exposure_criteria: { filterTestAccounts: true },
metrics: [PURCHASE_METRIC],
metrics_secondary: [FUNNEL_METRIC],
feature_flag: {
filters: {
multivariate: {
variants: [
{ key: 'control', rollout_percentage: 50 },
{ key: 'test', rollout_percentage: 50 },
],
},
},
},
} as unknown as Experiment
// A flag that aggregates by group exposes groups rather than persons, which is what the comparison
// would have to match against recordings.
const GROUP_AGGREGATED_EXPERIMENT = {
...EXPERIMENT,
id: 53,
feature_flag: {
...EXPERIMENT.feature_flag,
filters: { ...EXPERIMENT.feature_flag?.filters, aggregation_group_type_index: 0 },
},
} as unknown as Experiment
const REFUSAL_DETAIL = "This experiment aggregates by group, so its exposures can't be matched to persons' recordings."
const ALL_LINKABLE = {
$feature_flag_called: true,
purchase: true,
server_side_step: true,
client_step: true,
}
type InSessionExposureResponse = {
available: boolean
unavailable_reason: string | null
uses_stamped_fallback: boolean
}
// The common case: in-session evidence is the exposure event itself, and the scope can answer.
const IN_SESSION_AVAILABLE: InSessionExposureResponse = {
available: true,
unavailable_reason: null,
uses_stamped_fallback: false,
}
// Exposure narrowing lives in `experiment_exposure`, not the filter tree, so an unfiltered
// tab carries an empty group.
const EMPTY_FILTER_GROUP = {
type: FilterLogicalOperator.And,
values: [{ type: FilterLogicalOperator.And, values: [] }],
}
// Run windows are relative: the reasons are read against the retention period and against today,
// so a fixed date would start naming a different reason as it aged. Counted in hours, so a run in
// a zone with daylight saving still lands a whole number of days back.
const daysAgo = (days: number): string =>
dayjs()
.subtract(days * 24, 'hour')
.toISOString()
const listsRendered = (captureSpy: jest.SpyInstance, experimentId: number): any[] =>
captureSpy.mock.calls.filter(
([event, properties]) =>
event === 'experiment recordings list rendered' && (properties as any)?.experiment_id === experimentId
)
type TabLogic = ReturnType<typeof experimentReplayTabLogic.build>
// The three narrowings the viewer controls, shared by the cases below and by the narrowing-action
// cases under them, so the two tables cannot end up describing different tabs.
const selectTestVariant = (logic: TabLogic): void => logic.actions.setSelectedVariantKey('test')
const narrowToInSession = (logic: TabLogic): void => logic.actions.setExposureScope('in_session')
const addFilterBarFilter = (logic: TabLogic): void =>
logic.actions.playlistFiltersChanged({
...logic.values.recordingsFilters,
filter_group: {
type: FilterLogicalOperator.And,
values: [
{
type: FilterLogicalOperator.And,
values: [{ id: '$pageview', name: '$pageview', type: 'events', order: 0 }],
},
],
},
})
interface EmptyReasonCase {
reason: ExperimentReplayListEmptyReason
/** One per case: the logic is keyed per experiment, and each case mounts its own. */
experimentId: number
experiment: Partial<Experiment>
team?: Partial<TeamType>
setup?: (logic: TabLogic) => void
}
// The team's retention period is the mock default of 30 days, which the run windows are set against.
const EMPTY_REASON_CASES: EmptyReasonCase[] = [
{
reason: ExperimentReplayListEmptyReason.ReplayDisabled,
experimentId: 121,
experiment: {},
team: { session_recording_opt_in: false },
},
{
reason: ExperimentReplayListEmptyReason.NotLaunched,
experimentId: 122,
experiment: { start_date: null, end_date: null },
},
{
reason: ExperimentReplayListEmptyReason.MetricFilterMatchedNothing,
experimentId: 123,
experiment: { start_date: daysAgo(10), end_date: null },
setup: (logic) => {
;(experimentsSessionBucketsCreate as jest.Mock).mockResolvedValue({ ...BUCKET_RESPONSE, session_ids: [] })
logic.actions.setMetricSelected('metric-purchase', true)
logic.actions.setMetricFilterMode('no_metric_activity')
},
},
{
// Sessions matched the metric, none of them has a recording.
reason: ExperimentReplayListEmptyReason.MetricFilterMatchedNothing,
experimentId: 146,
experiment: { start_date: daysAgo(10), end_date: null },
setup: (logic) => {
;(experimentsSessionBucketsCreate as jest.Mock).mockResolvedValue({
...BUCKET_RESPONSE,
session_ids: ['bucket-session'],
})
logic.actions.setMetricSelected('metric-purchase', true)
logic.actions.setMetricFilterMode('no_metric_activity')
},
},
{
// A running experiment whose exposures stopped. The filter's window is anchored back
// there, so retention explains the empty list and changing the filter cannot.
reason: ExperimentReplayListEmptyReason.EndedPastRetention,
experimentId: 148,
experiment: { start_date: daysAgo(120), end_date: null },
setup: (logic) => {
;(experimentsSessionBucketsCreate as jest.Mock).mockResolvedValue({
...BUCKET_RESPONSE,
session_ids: [],
date_to: daysAgo(60),
})
logic.actions.setMetricSelected('metric-purchase', true)
logic.actions.setMetricFilterMode('no_metric_activity')
},
},
{
reason: ExperimentReplayListEmptyReason.FiltersNarrowed,
experimentId: 147,
experiment: { start_date: daysAgo(10), end_date: null },
setup: addFilterBarFilter,
},
{
reason: ExperimentReplayListEmptyReason.EndedPastRetention,
experimentId: 124,
experiment: { start_date: daysAgo(200), end_date: daysAgo(60) },
},
{
reason: ExperimentReplayListEmptyReason.TooEarly,
experimentId: 125,
experiment: { start_date: daysAgo(1), end_date: null },
},
// The same young run, narrowed by the viewer. A list this young is usually empty for every
// variant, every scope and every filter, so the age of the run stays the reason and the
// narrowing is not named on a guess. The banner hands back the narrowing's way out instead,
// which the narrowing-action cases below cover.
{
reason: ExperimentReplayListEmptyReason.TooEarly,
experimentId: 149,
experiment: { start_date: daysAgo(1), end_date: null },
setup: selectTestVariant,
},
{
reason: ExperimentReplayListEmptyReason.TooEarly,
experimentId: 150,
experiment: { start_date: daysAgo(1), end_date: null },
setup: narrowToInSession,
},
{
reason: ExperimentReplayListEmptyReason.TooEarly,
experimentId: 151,
experiment: { start_date: daysAgo(1), end_date: null },
setup: addFilterBarFilter,
},
{
reason: ExperimentReplayListEmptyReason.VariantHasNone,
experimentId: 141,
experiment: { start_date: daysAgo(10), end_date: daysAgo(2) },
setup: selectTestVariant,
},
{
reason: ExperimentReplayListEmptyReason.InSessionHasNone,
experimentId: 142,
experiment: { start_date: daysAgo(10), end_date: daysAgo(2) },
setup: narrowToInSession,
},
{
reason: ExperimentReplayListEmptyReason.UnknownInWindow,
experimentId: 127,
experiment: { start_date: daysAgo(10), end_date: daysAgo(2) },
},
{
// Still running past the retention period: its retained days are inside retention, so this
// is unexplained rather than a retention loss.
reason: ExperimentReplayListEmptyReason.UnknownInWindow,
experimentId: 128,
experiment: { start_date: daysAgo(60), end_date: null },
},
]
// The way out the banner has to offer for each narrowing. Read on a young run, where the reason is
// the age of the run rather than the narrowing, so this is the only thing that gets a viewer who
// narrowed the list back out of it.
const NARROWING_ACTION_CASES: {
narrowing: string
experimentId: number
team?: Partial<TeamType>
setup?: (logic: TabLogic) => void
reason: ExperimentReplayListEmptyReason
action: ExperimentRecordingsNarrowingAction | null
}[] = [
{
narrowing: 'a filter added above',
experimentId: 152,
setup: addFilterBarFilter,
reason: ExperimentReplayListEmptyReason.TooEarly,
action: 'clear_filters',
},
{
narrowing: 'a selected variant',
experimentId: 153,
setup: selectTestVariant,
reason: ExperimentReplayListEmptyReason.TooEarly,
action: 'show_all_variants',
},
{
narrowing: 'the in-session scope',
experimentId: 154,
setup: narrowToInSession,
reason: ExperimentReplayListEmptyReason.TooEarly,
action: 'all_sessions',
},
{ narrowing: 'nothing', experimentId: 155, reason: ExperimentReplayListEmptyReason.TooEarly, action: null },
{
// Replay off names its own cause, and its banner offers the settings link alone. The
// variant still narrows the tab, so reading the narrowing rather than the reason would
// report a button this viewer was never given, and every reason's click-through rate would
// be measured against renders that offered nothing.
narrowing: 'a selected variant under replay off',
experimentId: 156,
team: { session_recording_opt_in: false },
setup: selectTestVariant,
reason: ExperimentReplayListEmptyReason.ReplayDisabled,
action: null,
},
]
describe('experimentReplayTabLogic', () => {
let logic: ReturnType<typeof experimentReplayTabLogic.build>
let seenTogetherSpy: jest.SpyInstance
beforeEach(() => {
// The facet reducer is persisted; clear so no test inherits another's selection.
localStorage.clear()
initKeaTests()
;(experimentsSessionContextsCreate as jest.Mock).mockClear()
;(experimentsSessionBucketsCreate as jest.Mock).mockClear()
;(experimentsSessionBucketsCreate as jest.Mock).mockResolvedValue(BUCKET_RESPONSE)
;(experimentsSessionEventDeltasCreate as jest.Mock).mockClear()
;(experimentsSessionEventDeltasCreate as jest.Mock).mockResolvedValue(DELTA_RESPONSE)
;(visionScannersList as jest.Mock).mockClear()
;(visionScannersList as jest.Mock).mockResolvedValue({ results: [] })
;(experimentsInSessionExposureRetrieve as jest.Mock).mockClear()
;(experimentsInSessionExposureRetrieve as jest.Mock).mockResolvedValue(IN_SESSION_AVAILABLE)
seenTogetherSpy = jest.spyOn(api.propertyDefinitions, 'seenTogether')
seenTogetherSpy.mockResolvedValue(ALL_LINKABLE)
logic = experimentReplayTabLogic({ experiment: EXPERIMENT })
logic.mount()
})
afterEach(() => {
jest.restoreAllMocks()
})
it('defaults to the "All" facet and lists the experiment variants', async () => {
await expectLogic(logic).toMatchValues({
selectedVariantKey: null,
variantKeys: ['control', 'test'],
})
})
it('pins the person-scoped exposure filter to the run window', async () => {
await expectLogic(logic).toFinishAllListeners()
const { recordingsFilters } = logic.values
// RecordingsQuery defaults to "-3d", which would silently hide older exposed sessions,
// so the window must come from the experiment.
expect(recordingsFilters.date_from).toBe('2026-01-01T00:00:00Z')
expect(recordingsFilters.date_to).toBe('2026-02-01T00:00:00Z')
expect(recordingsFilters.filter_test_accounts).toBe(true)
// All facet: the population is the server-resolved exposure filter, and nothing
// event-shaped stands in for it in the filter tree.
expect(recordingsFilters.experiment_exposure).toEqual({ experiment_id: 42 })
expect(recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
// Criteria without the key exclude test accounts from the results, so the filter bar must show them excluded.
const withoutKey = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 69, exposure_criteria: {} } as Experiment,
})
withoutKey.mount()
expect(withoutKey.values.recordingsFilters.filter_test_accounts).toBe(true)
withoutKey.unmount()
})
it('keeps the selected variant across remounts, in step with the playlist persisting its filters', async () => {
await expectLogic(logic, () => {
logic.actions.setSelectedVariantKey('test')
}).toMatchValues({ selectedVariantKey: 'test' })
logic.unmount()
const remounted = experimentReplayTabLogic({ experiment: EXPERIMENT })
remounted.mount()
expect(remounted.values.selectedVariantKey).toBe('test')
remounted.unmount()
})
it('falls back to "All" when the persisted variant no longer exists', async () => {
await expectLogic(logic, () => {
logic.actions.setSelectedVariantKey('test')
}).toMatchValues({ selectedVariantKey: 'test' })
logic.unmount()
// Same experiment id, so the persisted "test" rehydrates — but the variant has since been renamed.
const renamed = {
...EXPERIMENT,
feature_flag: {
filters: {
multivariate: {
variants: [
{ key: 'control', rollout_percentage: 50 },
{ key: 'test_v2', rollout_percentage: 50 },
],
},
},
},
} as unknown as Experiment
const remounted = experimentReplayTabLogic({ experiment: renamed })
remounted.mount()
expect(remounted.values.selectedVariantKey).toBe('test')
expect(remounted.values.effectiveVariantKey).toBeNull()
// The stale key must not leak into the query; the filter falls back to all variants.
expect(remounted.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 42 })
remounted.unmount()
})
it('narrows the filter to the selected variant, keeping the run window', async () => {
await expectLogic(logic, () => {
logic.actions.setSelectedVariantKey('test')
}).toFinishAllListeners()
const { recordingsFilters } = logic.values
expect(recordingsFilters.date_from).toBe('2026-01-01T00:00:00Z')
expect(recordingsFilters.experiment_exposure).toEqual({ experiment_id: 42, variant: 'test' })
})
it('defaults to all sessions and narrows when in-session exposure is available', async () => {
await expectLogic(logic).toFinishAllListeners()
expect(logic.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 42 })
logic.actions.setExposureScope('in_session')
expect(logic.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 42, in_session: true })
})
it('holds the in-session narrowing out of the query until the availability check lands', async () => {
// Sent before the backend confirms availability, in_session can hit a refusal for an
// experiment whose exposure can't be pinned to a session; exposure-only is the correct
// superset until then.
let resolveCheck!: (response: InSessionExposureResponse) => void
;(experimentsInSessionExposureRetrieve as jest.Mock).mockReturnValue(
new Promise((resolve) => (resolveCheck = resolve))
)
const pending = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 51 } as Experiment })
pending.mount()
pending.actions.setExposureScope('in_session')
expect(pending.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 51 })
// The playlist waits too: mounted now it would fire the all-sessions listing only to
// replace it the moment the scope confirms.
expect(pending.values.playlistHeldForChecks).toBe(true)
resolveCheck(IN_SESSION_AVAILABLE)
await expectLogic(pending).toFinishAllListeners()
expect(pending.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 51, in_session: true })
expect(pending.values.playlistHeldForChecks).toBe(false)
pending.unmount()
})
it('disables in-session and stays on all sessions when the backend reports it unavailable', async () => {
// The backend refuses in_session for experiments whose exposure can't be pinned to a
// session (activation, or a custom event with no session-linked stand-in, or a fallback
// scan too large for the project). The tab mirrors that from the same check: the option is
// disabled with the reason, and a picked or persisted choice falls back to all sessions
// instead of drawing a backend 400.
;(experimentsInSessionExposureRetrieve as jest.Mock).mockResolvedValue({
available: false,
unavailable_reason:
'This experiment uses an activation event, so its exposure can span more than one session.',
uses_stamped_fallback: false,
})
const unavailable = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 52 } as Experiment })
unavailable.mount()
await expectLogic(unavailable).toFinishAllListeners()
unavailable.actions.setExposureScope('in_session')
expect(unavailable.values.exposureInSessionUnavailableReason).not.toBeNull()
expect(unavailable.values.effectiveExposureScope).toBe('all_exposed')
expect(unavailable.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 52 })
unavailable.unmount()
})
it('falls back to all sessions when the availability check fails', async () => {
// A failed check can't confirm the scope is safe to send, so the option isn't disabled (the
// failure is transient) but the query still holds at the all-sessions superset, never
// sending a narrowing the backend might refuse.
;(experimentsInSessionExposureRetrieve as jest.Mock).mockRejectedValue(new Error('network error'))
const failed = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 54 } as Experiment })
failed.mount()
await expectLogic(failed).toFinishAllListeners()
failed.actions.setExposureScope('in_session')
expect(failed.values.exposureInSessionUnavailableReason).toBeNull()
expect(failed.values.effectiveExposureScope).toBe('all_exposed')
expect(failed.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 54 })
await expectLogic(failed).toFinishAllListeners()
// Picking the scope again retries the check, so recovery doesn't wait for a remount.
;(experimentsInSessionExposureRetrieve as jest.Mock).mockResolvedValue(IN_SESSION_AVAILABLE)
failed.actions.setExposureScope('in_session')
await expectLogic(failed).toFinishAllListeners()
expect(failed.values.effectiveExposureScope).toBe('in_session')
expect(failed.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 54, in_session: true })
failed.unmount()
})
it('narrows but labels sessions as flag-active when the backend evidence is the stamped fallback', async () => {
// Server-side default exposure: the event carries no session id, so the backend matches on
// the stamped $feature/<key> property. The narrowing still applies (tighter than all
// sessions), but the copy must say the flag was active, not that the exposure was captured.
;(experimentsInSessionExposureRetrieve as jest.Mock).mockResolvedValue({
available: true,
unavailable_reason: null,
uses_stamped_fallback: true,
})
const fallback = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 43 } as Experiment })
fallback.mount()
await expectLogic(fallback).toFinishAllListeners()
fallback.actions.setExposureScope('in_session')
expect(fallback.values.inSessionExposure?.uses_stamped_fallback).toBe(true)
expect(fallback.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 43, in_session: true })
expect(fallback.values.recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
fallback.unmount()
})
it('ANDs each selected metric filter onto the exposure filter, and ignores unknown metric uuids', async () => {
await expectLogic(logic).toFinishAllListeners()
expect(logic.values.metricOptions.map((option) => option.uuid)).toEqual(['metric-purchase', 'metric-funnel'])
logic.actions.setMetricSelected('metric-purchase', true)
expect(logic.values.effectiveMetricUuids).toEqual(['metric-purchase'])
expect(logic.values.recordingsFilters.filter_group.values).toEqual([
{
type: FilterLogicalOperator.And,
values: [{ id: 'purchase', name: 'purchase', type: 'events', properties: [] }],
},
])
// A second selection narrows further: both metrics' primary events AND together.
logic.actions.setMetricSelected('metric-funnel', true)
expect(logic.values.effectiveMetricUuids).toEqual(['metric-purchase', 'metric-funnel'])
expect(logic.values.recordingsFilters.filter_group.values).toEqual([
{
type: FilterLogicalOperator.And,
values: [
{ id: 'purchase', name: 'purchase', type: 'events', properties: [] },
{ id: 'server_side_step', name: 'server_side_step', type: 'events', properties: [] },
],
},
])
logic.actions.setMetricSelected('metric-purchase', false)
logic.actions.setMetricSelected('metric-funnel', false)
// A persisted uuid whose metric has since been removed must not leak into the query.
logic.actions.setMetricSelected('ghost', true)
expect(logic.values.effectiveMetricUuids).toEqual([])
expect(logic.values.recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
})
it('lists a retention metric as unmatchable instead of dropping it silently', async () => {
// A retention metric yields no session filter (its return visit lands in a later session).
// Dropping it from the list reads as the metric having been forgotten, so it stays listed
// with a reason — and must never reach the query, which would only narrow to nothing.
const withRetention = experimentReplayTabLogic({
experiment: {
...EXPERIMENT,
id: 46,
metrics_secondary: [
...(EXPERIMENT.metrics_secondary ?? []),
{
kind: NodeKind.ExperimentMetric,
metric_type: ExperimentMetricType.RETENTION,
uuid: 'metric-retention',
name: '7-day retention',
start_event: { kind: NodeKind.EventsNode, event: '$pageview' },
completion_event: { kind: NodeKind.EventsNode, event: '$pageview' },
},
],
} as unknown as Experiment,
})
withRetention.mount()
await expectLogic(withRetention).toFinishAllListeners()
expect(withRetention.values.metricOptions.find((option) => option.uuid === 'metric-retention')).toMatchObject({
name: '7-day retention',
unlinkable: true,
unlinkableReason: RETENTION_UNLINKABLE_REASON,
})
withRetention.actions.setMetricSelected('metric-retention', true)
expect(withRetention.values.effectiveMetricUuids).toEqual([])
expect(withRetention.values.recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
withRetention.unmount()
})
it('disables a fully server-side metric and never ANDs it into the query', async () => {
seenTogetherSpy.mockResolvedValue({ ...ALL_LINKABLE, purchase: false })
const serverSideMetric = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 44 } as Experiment })
serverSideMetric.mount()
await expectLogic(serverSideMetric).toFinishAllListeners()
expect(serverSideMetric.values.metricOptions.find((option) => option.uuid === 'metric-purchase')).toMatchObject(
{ unlinkable: true }
)
serverSideMetric.actions.setMetricSelected('metric-purchase', true)
expect(serverSideMetric.values.effectiveMetricUuids).toEqual([])
// The unlinkable event would zero the whole AND-combined query — it must never appear.
expect(serverSideMetric.values.recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
serverSideMetric.unmount()
})
it('keeps a partially linkable metric selectable but drops its unlinkable step from the query', async () => {
seenTogetherSpy.mockResolvedValue({ ...ALL_LINKABLE, server_side_step: false })
const partiallyLinkable = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 45 } as Experiment })
partiallyLinkable.mount()
await expectLogic(partiallyLinkable).toFinishAllListeners()
expect(partiallyLinkable.values.metricOptions.find((option) => option.uuid === 'metric-funnel')).toMatchObject({
unlinkable: false,
})
partiallyLinkable.actions.setMetricSelected('metric-funnel', true)
await expectLogic(partiallyLinkable).toFinishAllListeners()
// A multi-source metric resolves server-side, where the unmatchable step is simply one
// OR variant that never matches a session — so it drops out without narrowing anything.
expect(experimentsSessionBucketsCreate).toHaveBeenLastCalledWith(expect.any(String), 45, {
bucket: 'fired_any',
metric_uuids: ['metric-funnel'],
variant: null,
})
expect(partiallyLinkable.values.recordingsFilters.session_ids).toEqual(['bucket-1', 'bucket-2'])
partiallyLinkable.unmount()
})
it('resolves one multi-source metric server-side, and one single-source metric client-side', async () => {
await expectLogic(logic).toFinishAllListeners()
// A single event is expressible as a filter: exact, uncapped, no request.
logic.actions.setMetricSelected('metric-purchase', true)
await expectLogic(logic).toFinishAllListeners()
expect(experimentsSessionBucketsCreate).not.toHaveBeenCalled()
expect(logic.values.recordingsFilters.session_ids).toBeUndefined()
expect(logic.values.recordingsFilters.filter_group.values).toEqual([
{
type: FilterLogicalOperator.And,
values: [{ id: 'purchase', name: 'purchase', type: 'events', properties: [] }],
},
])
// Several events are not: a recordings query carries one operator for its whole tree, so
// the filter could only match the metric's first event and would silently miss the rest.
logic.actions.setMetricSelected('metric-purchase', false)
logic.actions.setMetricSelected('metric-funnel', true)
await expectLogic(logic).toFinishAllListeners()
expect(experimentsSessionBucketsCreate).toHaveBeenLastCalledWith(expect.any(String), 42, {
bucket: 'fired_any',
metric_uuids: ['metric-funnel'],
variant: null,
})
expect(logic.values.recordingsFilters.session_ids).toEqual(['bucket-1', 'bucket-2'])
})
it('skips metrics without a uuid — the persisted selection needs a stable identity', async () => {
// A positional stand-in id would re-attach a persisted selection to a *different* metric
// once the metric list is edited, silently filtering the playlist on the wrong event.
const withoutUuid = experimentReplayTabLogic({
experiment: {
...EXPERIMENT,
id: 47,
metrics: [{ ...PURCHASE_METRIC, uuid: undefined }],
metrics_secondary: [],
} as unknown as Experiment,
})
withoutUuid.mount()
await expectLogic(withoutUuid).toFinishAllListeners()
expect(withoutUuid.values.metricOptions).toEqual([])
withoutUuid.unmount()
})
it('holds metric filters out of the query until the linkability check lands', async () => {
// Applying a persisted selection before linkability is known can fire an exposure+metric
// query that can only be empty (server-side-only metric), flashing a false empty state.
let resolveSeenTogether!: (map: Record<string, boolean>) => void
seenTogetherSpy.mockReturnValue(new Promise((resolve) => (resolveSeenTogether = resolve)))
const pending = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 48 } as Experiment })
pending.mount()
pending.actions.setMetricSelected('metric-purchase', true)
expect(pending.values.recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
resolveSeenTogether(ALL_LINKABLE)
await expectLogic(pending).toFinishAllListeners()
expect(pending.values.recordingsFilters.filter_group.values).toEqual([
{
type: FilterLogicalOperator.And,
values: [{ id: 'purchase', name: 'purchase', type: 'events', properties: [] }],
},
])
pending.unmount()
})
it('reports the tab view once, after the linkability and availability checks have both settled', async () => {
// Reported from `afterMount` instead, every view would carry the fail-open defaults and
// null scope fields, an experiment whose exposure event can never match recordings would
// look like a healthy one, and empty-list sessions could not be split by exposure scope.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
// Scoped to this experiment: the logic mounted in `beforeEach` reports its own view too.
const tabViews = (): any[] =>
captureSpy.mock.calls.filter(
([event, properties]) =>
event === 'experiment recordings tab viewed' && (properties as any)?.experiment_id === 51
)
let resolveSeenTogether!: (map: Record<string, boolean>) => void
seenTogetherSpy.mockReturnValue(new Promise((resolve) => (resolveSeenTogether = resolve)))
let resolveAvailability!: (response: InSessionExposureResponse) => void
;(experimentsInSessionExposureRetrieve as jest.Mock).mockReturnValue(
new Promise((resolve) => (resolveAvailability = resolve))
)
const pending = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 51 } as Experiment })
pending.mount()
expect(tabViews()).toHaveLength(0)
// The linkability half settled, but the scope fields aren't known yet, so no report.
resolveSeenTogether({ ...ALL_LINKABLE, purchase: false })
await expectLogic(pending).toDispatchActions(['loadSeenTogetherSuccess', 'reportTabViewed'])
expect(tabViews()).toHaveLength(0)
resolveAvailability({ ...IN_SESSION_AVAILABLE, uses_stamped_fallback: true })
await expectLogic(pending).toFinishAllListeners()
expect(tabViews()).toHaveLength(1)
expect(tabViews()[0][1]).toMatchObject({
experiment_id: 51,
variant_count: 2,
metric_count: 2,
linkable_metric_count: 1,
exposure_scope: 'all_exposed',
// The pair that says this visit had a list to show, so a visit with no list rendered
// after it is a failure or a bounce rather than an experiment that never had one.
experiment_status: 'stopped',
list_unavailable_reason: null,
in_session_available: true,
in_session_unavailable_reason: null,
in_session_uses_stamped_fallback: true,
// The default test flags leave the shelf off, so this view never saw the toggle.
behavior_comparison_available: false,
behavior_comparison_unavailable_reason: null,
})
// The check is shared with the metrics tab and reloads when the experiment's metrics change,
// so a second result while this tab is open must not count as a second view.
pending.actions.loadSeenTogetherSuccess(ALL_LINKABLE)
await expectLogic(pending).toFinishAllListeners()
expect(tabViews()).toHaveLength(1)
pending.unmount()
})
it('flushes the tab view at unmount when the availability check has not settled', async () => {
// A bounce before the availability round trip completes must still count as a view; the
// verdict fields stay null, meaning unknown rather than unavailable.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const tabViews = (): any[] =>
captureSpy.mock.calls.filter(
([event, properties]) =>
event === 'experiment recordings tab viewed' && (properties as any)?.experiment_id === 55
)
;(experimentsInSessionExposureRetrieve as jest.Mock).mockReturnValue(new Promise(() => {}))
const bounced = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 55 } as Experiment })
bounced.mount()
await expectLogic(bounced).toDispatchActions(['loadSeenTogetherSuccess'])
expect(tabViews()).toHaveLength(0)
bounced.unmount()
expect(tabViews()).toHaveLength(1)
expect(tabViews()[0][1]).toMatchObject({
experiment_id: 55,
in_session_available: null,
in_session_uses_stamped_fallback: null,
})
})
it('reports the tab view when the linkability check had already failed before the tab opened', async () => {
// The shared check can settle as a failure while the user is still on the metrics tab.
// No linkability load action follows once this tab mounts, so the availability check's
// completion is what sends the report, with the fail-open defaults, the same posture as
// a failure that lands while the tab is open.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const tabViews = (): any[] =>
captureSpy.mock.calls.filter(
([event, properties]) =>
event === 'experiment recordings tab viewed' && (properties as any)?.experiment_id === 52
)
seenTogetherSpy.mockRejectedValue(new Error('network error'))
const experiment = { ...EXPERIMENT, id: 52 } as Experiment
// Stands in for the metrics tab, which holds the shared logic mounted across tab switches.
const linkability = viewRecordingsLinkabilityLogic({ experiment })
linkability.mount()
await expectLogic(linkability).toFinishAllListeners()
expect(tabViews()).toHaveLength(0)
const opened = experimentReplayTabLogic({ experiment })
opened.mount()
await expectLogic(opened).toFinishAllListeners()
expect(tabViews()).toHaveLength(1)
expect(tabViews()[0][1]).toMatchObject({
experiment_id: 52,
linkable_metric_count: 2,
})
opened.unmount()
linkability.unmount()
})
it('applies metric filters when the linkability check fails — fail open, not permanently gated', async () => {
seenTogetherSpy.mockRejectedValue(new Error('network error'))
const failed = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 49 } as Experiment })
failed.mount()
failed.actions.setMetricSelected('metric-purchase', true)
await expectLogic(failed).toFinishAllListeners()
expect(failed.values.recordingsFilters.filter_group.values).toEqual([
{
type: FilterLogicalOperator.And,
values: [{ id: 'purchase', name: 'purchase', type: 'events', properties: [] }],
},
])
failed.unmount()
})
it('prefetches session contexts for a loaded recordings page', async () => {
logic.actions.recordingsLoaded(loadedPage(['s1', 's2']))
await expectLogic(logic).toFinishAllListeners()
expect(experimentsSessionContextsCreate).toHaveBeenCalledWith(expect.any(String), {
session_ids: ['s1', 's2'],
})
})
it('re-warms the rest of the page when a recording is opened', async () => {
// Opening before any page has loaded must not fire an empty batch (the backend 400s it).
logic.actions.recordingOpened('s1')
await expectLogic(logic).toFinishAllListeners()
expect(experimentsSessionContextsCreate).not.toHaveBeenCalled()
logic.actions.recordingsLoaded(loadedPage(['s1', 's2', 's3']))
await expectLogic(logic).toFinishAllListeners()
// The server-side cache TTL runs from prefetch time, so each open must re-warm the
// page — without this, a user who watches one recording past the TTL opens every
// later one cold. The opened id is excluded: the player is fetching it right now.
logic.actions.recordingOpened('s2')
await expectLogic(logic).toFinishAllListeners()
expect(experimentsSessionContextsCreate).toHaveBeenCalledTimes(2)
expect((experimentsSessionContextsCreate as jest.Mock).mock.calls[1][1].session_ids).toEqual(['s1', 's3'])
})
it('caps a batch at the backend limit', async () => {
// Over-cap ids must be sliced, not sent — the backend 400s the whole batch above its cap.
logic.actions.recordingsLoaded(loadedPage(Array.from({ length: 25 }, (_, index) => `session-${index}`)))
await expectLogic(logic).toFinishAllListeners()
expect(experimentsSessionContextsCreate).toHaveBeenCalledTimes(1)
expect((experimentsSessionContextsCreate as jest.Mock).mock.calls[0][1].session_ids).toHaveLength(20)
})
it.each(EMPTY_REASON_CASES)(
'reports $reason for a list that came back empty',
async ({ reason, experimentId, experiment, team, setup }) => {
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
teamLogic.actions.loadCurrentTeamSuccess({ ...MOCK_DEFAULT_TEAM, ...team })
const empty = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: experimentId, ...experiment } as Experiment,
})
empty.mount()
setup?.(empty)
await expectLogic(empty).toFinishAllListeners()
empty.actions.recordingsLoaded([])
await expectLogic(empty).toFinishAllListeners()
expect(listsRendered(captureSpy, experimentId)).toHaveLength(1)
expect(listsRendered(captureSpy, experimentId)[0][1]).toMatchObject({
result_count: 0,
empty_reason: reason,
})
empty.unmount()
}
)
it.each(NARROWING_ACTION_CASES)(
'reports $action as the way out of a young run narrowed by $narrowing',
async ({ experimentId, team, setup, reason, action }) => {
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
teamLogic.actions.loadCurrentTeamSuccess({ ...MOCK_DEFAULT_TEAM, ...team })
const young = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: experimentId, start_date: daysAgo(1), end_date: null } as Experiment,
})
young.mount()
setup?.(young)
await expectLogic(young).toFinishAllListeners()
young.actions.recordingsLoaded([])
await expectLogic(young).toFinishAllListeners()
// The banner and the report both resolve the action from the reason, so a viewer who
// was handed a way out and a render counted as offering one cannot come apart.
expect(listsRendered(captureSpy, experimentId)[0][1]).toMatchObject({
empty_reason: reason,
narrowing_action: action,
})
young.unmount()
}
)
it('reports no reason for the hidden-recordings action, and the reason for the others', async () => {
// `show_hidden` is offered when rows came back and the browser hid them, so the list is not
// empty. Sending the reason there would count a cause of emptiness against a list that had
// recordings, and every reason's click-through rate would be measured against it.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
teamLogic.actions.loadCurrentTeamSuccess(MOCK_DEFAULT_TEAM)
const empty = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 143, start_date: daysAgo(1), end_date: null } as Experiment,
})
empty.mount()
await expectLogic(empty).toFinishAllListeners()
empty.actions.listEmptyActionClicked('show_hidden')
empty.actions.listEmptyActionClicked('replay_settings')
await expectLogic(empty).toFinishAllListeners()
const clicks = captureSpy.mock.calls.filter(
([event, properties]) =>
event === 'experiment recordings empty state action clicked' &&
(properties as any)?.experiment_id === 143
)
expect(clicks.map(([, properties]) => properties as any)).toMatchObject([
{ action: 'show_hidden', empty_reason: null, days_since_start: 1 },
{
action: 'replay_settings',
empty_reason: ExperimentReplayListEmptyReason.TooEarly,
days_since_start: 1,
},
])
empty.unmount()
})
it('reports a list with rows, with no reason and the facets it was narrowed by', async () => {
// The empty reason names a plausible cause of emptiness, so on a list with rows it would
// read as a fault the tab found. The facets match `experiment recording opened`, so an
// empty list and an opened recording are comparable per facet.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
teamLogic.actions.loadCurrentTeamSuccess({ ...MOCK_DEFAULT_TEAM, session_recording_retention_period: '90d' })
const filled = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 111, start_date: daysAgo(10), end_date: daysAgo(2) } as Experiment,
})
filled.mount()
filled.actions.setSelectedVariantKey('test')
await expectLogic(filled).toFinishAllListeners()
filled.actions.recordingsLoaded(loadedPage(['s1', 's2']))
await expectLogic(filled).toFinishAllListeners()
expect(listsRendered(captureSpy, 111)).toHaveLength(1)
expect(listsRendered(captureSpy, 111)[0][1]).toEqual({
experiment_id: 111,
result_count: 2,
empty_reason: null,
narrowing_action: null,
days_since_start: 10,
days_since_end: 2,
retention_period: '90d',
replay_opt_in: true,
duration_filter_active: true,
duration_filter_key: 'active_seconds',
duration_filter_seconds: 5,
duration_filter_operator: 'gt',
duration_filter_count: 1,
duration_filter_customized: false,
filters_customized: false,
hide_viewed_recordings: 'off',
exposure_linkable: true,
variant: 'test',
exposure_scope: 'all_exposed',
metric_filter_mode: 'fired_all',
selected_metric_count: 0,
is_bucketed: false,
watch_card_kind: null,
entry_point: null,
metric_unavailable_reason: null,
})
filled.unmount()
})
it.each([
{ verdict: 'not_launched', experimentId: 160, experiment: { start_date: null, end_date: null } },
{ verdict: 'group_aggregated', experimentId: 161, experiment: GROUP_AGGREGATED_EXPERIMENT },
{
verdict: 'no_variants',
experimentId: 162,
experiment: { feature_flag: { filters: { multivariate: { variants: [] } } } },
},
{ verdict: null, experimentId: 163, experiment: {} },
])('resolves $verdict as the list verdict', async ({ verdict, experimentId, experiment }) => {
// Every verdict but null is a request `resolve_exposure_linkage` refuses outright. The tab
// used to mount the list anyway, so a group-aggregated experiment answered every visit
// with an error toast and the playlist's generic banner.
const checked = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, ...experiment, id: experimentId } as Experiment,
})
checked.mount()
await expectLogic(checked).toFinishAllListeners()
expect(checked.values.listUnavailableReason).toBe(verdict)
checked.unmount()
})
it('reports a failed first page with the backend reason, and clears it on the next list', async () => {
// The playlist's own banner says "Error while trying to load recordings." for every
// failure, so without the detail neither the viewer nor a reader of the event can tell a
// refusal this experiment can never escape from a wait-and-retry.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const failures = (): any[] =>
captureSpy.mock.calls.filter(
([event, properties]) =>
event === 'experiment recordings list failed' && (properties as any)?.experiment_id === 164
)
const failing = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 164, start_date: daysAgo(4), end_date: null } as Experiment,
})
failing.mount()
await expectLogic(failing).toFinishAllListeners()
failing.actions.recordingsLoadFailed({ status: 400, detail: REFUSAL_DETAIL }, true)
await expectLogic(failing).toFinishAllListeners()
expect(failing.values.listLoadError).toEqual({ status: 400, detail: REFUSAL_DETAIL })
expect(failures()).toHaveLength(1)
expect(failures()[0][1]).toMatchObject({
status: 400,
error_detail: REFUSAL_DETAIL,
days_since_start: 4,
variant: null,
exposure_scope: 'all_exposed',
})
// A later page failing is paging against a list that already has rows on screen.
failing.actions.recordingsLoadFailed({ status: 500, detail: 'Server error' }, false)
await expectLogic(failing).toFinishAllListeners()
expect(failures()).toHaveLength(1)
failing.actions.recordingsLoaded(loadedPage(['s1']))
await expectLogic(failing).toFinishAllListeners()
expect(failing.values.listLoadError).toBeNull()
failing.unmount()
})
it.each([
{ visit: 'that never saw a list', experimentId: 165, experiment: {}, setup: undefined, abandoned: 1 },
{
visit: 'whose first page rendered',
experimentId: 166,
experiment: {},
setup: (tab: TabLogic) => tab.actions.recordingsLoaded(loadedPage(['s1'])),
abandoned: 0,
},
{
visit: 'whose first page failed',
experimentId: 167,
experiment: {},
setup: (tab: TabLogic) => tab.actions.recordingsLoadFailed({ status: 400, detail: REFUSAL_DETAIL }, true),
abandoned: 0,
},
{
visit: 'that never had a list to load',
experimentId: 168,
experiment: { start_date: null, end_date: null },
setup: undefined,
abandoned: 0,
},
])(
'reports $abandoned abandoned list for a visit $visit',
async ({ experimentId, experiment, setup, abandoned }) => {
// The playlist drops a load at its breakpoint on unmount without dispatching success or
// failure, so a viewer who clicks through to another tab produces a tab view and nothing
// else. That bounce is most of the tab's unexplained visits, and only this event counts it.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const bounced = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: experimentId, ...experiment } as Experiment,
})
bounced.mount()
await expectLogic(bounced).toFinishAllListeners()
setup?.(bounced)
await expectLogic(bounced).toFinishAllListeners()
bounced.unmount()
const abandons = captureSpy.mock.calls.filter(
([event, properties]) =>
event === 'experiment recordings list abandoned' &&
(properties as any)?.experiment_id === experimentId
)
expect(abandons).toHaveLength(abandoned)
if (abandoned > 0) {
expect(abandons[0][1]).toMatchObject({
held_for_checks: false,
bucket_loading: false,
exposure_scope: 'all_exposed',
ms_on_tab: expect.any(Number),
})
}
}
)
it.each([
{
answer: 'a bucket that fails',
experimentId: 169,
bucket: null,
renders: 1,
},
{
answer: 'a bucket that matches nothing',
experimentId: 170,
bucket: { ...BUCKET_RESPONSE, session_ids: [] },
renders: 1,
},
{
answer: 'a bucket that changes the session set',
experimentId: 171,
bucket: BUCKET_RESPONSE,
renders: 1,
},
])(
'reports $renders render for an empty first page followed by $answer',
async ({ experimentId, bucket, renders }) => {
// An empty first page while the bucket loads is held back, because the bucket's answer
// reloads the list. When the answer leaves `session_ids` as it found them the playlist
// compares by value and never reloads, so the held-back page has to be replayed or the
// visit reports no list at all. When the answer does change them the reload reports the
// render itself, and replaying as well would count one list twice.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
;(experimentsSessionBucketsCreate as jest.Mock).mockImplementation(() =>
bucket ? Promise.resolve(bucket) : Promise.reject(new Error('bucket failed'))
)
const racing = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: experimentId, start_date: daysAgo(10), end_date: null } as Experiment,
})
racing.mount()
racing.actions.setMetricSelected('metric-purchase', true)
racing.actions.setMetricFilterMode('no_metric_activity')
// The bucket load debounces, so this page lands while the answer is still out.
racing.actions.recordingsLoaded([])
expect(listsRendered(captureSpy, experimentId)).toHaveLength(0)
await expectLogic(racing).toFinishAllListeners()
// The playlist reloads on a changed session set, which is the caller's next first page.
if (bucket?.session_ids.length) {
racing.actions.recordingsLoaded(loadedPage(bucket.session_ids))
await expectLogic(racing).toFinishAllListeners()
}
expect(listsRendered(captureSpy, experimentId)).toHaveLength(renders)
racing.unmount()
}
)
it('drops a held-back empty page once a first page fails, rather than replaying it', async () => {
// The visit ends on the failure, so the caption and the report both have to describe it.
// Replayed, the held-back page reported a render for a list that failed and cleared the
// caption underneath the playlist's own error banner.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
let resolveBucket!: (response: unknown) => void
;(experimentsSessionBucketsCreate as jest.Mock).mockImplementation(
() => new Promise((resolve) => (resolveBucket = resolve))
)
const failing = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 174, start_date: daysAgo(10), end_date: null } as Experiment,
})
failing.mount()
failing.actions.setMetricSelected('metric-purchase', true)
failing.actions.setMetricFilterMode('no_metric_activity')
// The bucket load debounces, so both of these land while the answer is still out.
failing.actions.recordingsLoaded([])
failing.actions.recordingsLoadFailed({ status: 500, detail: 'Server error' }, true)
while ((experimentsSessionBucketsCreate as jest.Mock).mock.calls.length === 0) {
await new Promise((resolve) => setTimeout(resolve, 25))
}
// An answer that matches the session set the dropped page ran under, which is the case the
// replay exists for: the playlist compares by value, so it never reloads.
resolveBucket({ ...BUCKET_RESPONSE, session_ids: [] })
await expectLogic(failing).toFinishAllListeners()
expect(listsRendered(captureSpy, 174)).toHaveLength(0)
expect(failing.values.listLoadError).toEqual({ status: 500, detail: 'Server error' })
failing.unmount()
})
it('asks for no session bucket when it states that no list is available', async () => {
// The metric filter is one of three paths that reach the bucket without a list on screen:
// a results-row link, a multi-event metric's default click, and a mode a previous visit
// persisted. Each one used to spend a ClickHouse scan on a session set nothing can show.
const grouped = experimentReplayTabLogic({
experiment: { ...GROUP_AGGREGATED_EXPERIMENT, id: 172 } as Experiment,
})
grouped.mount()
grouped.actions.setMetricSelected('metric-purchase', true)
grouped.actions.setMetricFilterMode('no_metric_activity')
await expectLogic(grouped).toFinishAllListeners()
expect(grouped.values.sessionBucketRequest).toBeNull()
expect(experimentsSessionBucketsCreate).not.toHaveBeenCalled()
grouped.unmount()
})
it('still reports the group aggregation for a draft experiment', async () => {
// The list verdict takes the first refusal in the backend's order, so a draft's verdict is
// `not_launched` whatever its flag does. Read from that verdict, the shelf's own reason
// would go null on exactly the experiments it describes.
const draftGrouped = experimentReplayTabLogic({
experiment: {
...GROUP_AGGREGATED_EXPERIMENT,
id: 173,
start_date: null,
end_date: null,
} as Experiment,
})
draftGrouped.mount()
await expectLogic(draftGrouped).toFinishAllListeners()
expect(draftGrouped.values.listUnavailableReason).toBe('not_launched')
expect(draftGrouped.values.behaviorComparisonUnavailableReason).toBe('group_aggregated')
draftGrouped.unmount()
})
it('reports the entry point a deep link set, and clears it when the viewer moves a facet', async () => {
// The entry point is what separates a list a results row opened from one somebody narrowed
// by hand, so an empty rate can be read per entry point. Left set after a manual change, it
// would credit the results row with lists it never asked for. The dropped-metric reason is
// read the same way, and it is also what the tab's caption explains the list with.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
teamLogic.actions.loadCurrentTeamSuccess(MOCK_DEFAULT_TEAM)
router.actions.push('/experiments/63', {
tab: 'recordings',
variant: 'test',
entry: 'results_button',
metric_unavailable: 'no_uuid',
})
const fromResults = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 63, start_date: daysAgo(10), end_date: daysAgo(2) } as Experiment,
})
fromResults.mount()
await expectLogic(fromResults).toFinishAllListeners()
expect(fromResults.values.droppedMetricReason).toBe('no_uuid')
// Left in the URL it would re-apply on the next remount and caption a list the viewer has
// since narrowed themselves.
expect(router.values.searchParams).toEqual({ tab: 'recordings' })
fromResults.actions.recordingsLoaded(loadedPage(['s1']))
await expectLogic(fromResults).toFinishAllListeners()
expect(listsRendered(captureSpy, 63)[0][1]).toMatchObject({
entry_point: 'results_button',
metric_unavailable_reason: 'no_uuid',
variant: 'test',
})
fromResults.actions.setSelectedVariantKey(null)
fromResults.actions.recordingsLoaded(loadedPage(['s1']))
await expectLogic(fromResults).toFinishAllListeners()
expect(fromResults.values.droppedMetricReason).toBeNull()
expect(listsRendered(captureSpy, 63)[1][1]).toMatchObject({
entry_point: null,
metric_unavailable_reason: null,
variant: null,
})
fromResults.unmount()
})
it('drops the entry point once the viewer narrows the list from the playlist bar', async () => {
// A filter added in the playlist bar narrows the list past what the link asked for, so the
// results row must stop being credited with it. The variant facet is left alone here, since
// moving one is the other way to clear the entry point and would hide this one failing.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
teamLogic.actions.loadCurrentTeamSuccess(MOCK_DEFAULT_TEAM)
router.actions.push('/experiments/67', {
tab: 'recordings',
variant: 'test',
entry: 'results_button',
metric_unavailable: 'no_uuid',
})
const fromResults = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 67, start_date: daysAgo(10), end_date: daysAgo(2) } as Experiment,
})
fromResults.mount()
await expectLogic(fromResults).toFinishAllListeners()
fromResults.actions.playlistFiltersChanged({
...fromResults.values.recordingsFilters,
filter_group: {
type: FilterLogicalOperator.And,
values: [
{
type: FilterLogicalOperator.And,
values: [{ id: '$pageview', name: '$pageview', type: 'events', order: 0 }],
},
],
},
})
fromResults.actions.recordingsLoaded(loadedPage(['s1']))
await expectLogic(fromResults).toFinishAllListeners()
expect(fromResults.values.entryPoint).toBe('results_button')
// The reducer keeps the reason, so the caption still explains the list the link opened.
expect(fromResults.values.droppedMetricReason).toBe('no_uuid')
expect(listsRendered(captureSpy, 67)[0][1]).toMatchObject({
entry_point: null,
metric_unavailable_reason: null,
variant: 'test',
})
fromResults.unmount()
})
it('applies a deep link once and takes its params out of the URL', async () => {
router.actions.push('/experiments/64', {
tab: 'recordings',
variant: 'test',
metric_uuid: 'metric-purchase',
metric_filter: 'no_metric_activity',
entry: 'results_menu',
})
const deepLinked = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 64 } as Experiment })
deepLinked.mount()
await expectLogic(deepLinked).toFinishAllListeners()
expect(deepLinked.values.selectedVariantKey).toBe('test')
expect(deepLinked.values.effectiveMetricUuids).toEqual(['metric-purchase'])
expect(deepLinked.values.metricFilterMode).toBe('no_metric_activity')
// Which control opened the tab has to survive the trip, or a menu selection and a plain
// button click become the same row in the report.
expect(deepLinked.values.entryPoint).toBe('results_menu')
// One request for the three facets: they arrive in one dispatch, and afterMount asks for
// the same bucket, so a second call here means the two are no longer collapsing.
expect(experimentsSessionBucketsCreate).toHaveBeenCalledTimes(1)
expect(experimentsSessionBucketsCreate).toHaveBeenLastCalledWith(expect.any(String), 64, {
bucket: 'no_metric_activity',
metric_uuids: ['metric-purchase'],
variant: 'test',
})
// The tab the link named stays; the three it consumed go, so a later remount reads the
// persisted state instead of applying the link again.
expect(router.values.searchParams).toEqual({ tab: 'recordings' })
deepLinked.unmount()
})
it('leaves a change made after a deep link in place when the tab remounts', async () => {
router.actions.push('/experiments/65', {
tab: 'recordings',
metric_uuid: 'metric-purchase',
metric_filter: 'no_metric_activity',
})
const deepLinked = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 65 } as Experiment })
deepLinked.mount()
await expectLogic(deepLinked).toFinishAllListeners()
deepLinked.actions.setMetricFilterMode('fired_all')
deepLinked.unmount()
// The tab bar renders only the active tab, so this logic unmounts on a tab switch and
// mounts again on return. A link still in the URL would re-apply and undo the change.
const remounted = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 65 } as Experiment })
remounted.mount()
await expectLogic(remounted).toFinishAllListeners()
expect(remounted.values.metricFilterMode).toBe('fired_all')
remounted.unmount()
})
it('starts a deep link from the whole exposed set, whatever scope the last visit left', async () => {
// The scope persists, and 'in_session' narrows to the sessions carrying exposure evidence.
// Left in place it would cut the population the row's label promised, with nothing on
// screen saying why.
const earlier = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 68 } as Experiment })
earlier.mount()
await expectLogic(earlier).toFinishAllListeners()
earlier.actions.setExposureScope('in_session')
await expectLogic(earlier).toMatchValues({ effectiveExposureScope: 'in_session' })
earlier.unmount()
router.actions.push('/experiments/68', { tab: 'recordings', variant: 'test', entry: 'results_button' })
const fromResults = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 68 } as Experiment })
fromResults.mount()
await expectLogic(fromResults).toFinishAllListeners()
expect(fromResults.values.effectiveExposureScope).toBe('all_exposed')
expect(fromResults.values.recordingsFilters.experiment_exposure).toEqual({
experiment_id: 68,
variant: 'test',
})
fromResults.unmount()
})
it('degrades a deep link the experiment cannot answer to the tab defaults', async () => {
router.actions.push('/experiments/66', {
tab: 'recordings',
variant: 'nope',
metric_uuid: 'nope',
metric_filter: 'nope',
})
const stale = experimentReplayTabLogic({ experiment: { ...EXPERIMENT, id: 66 } as Experiment })
stale.mount()
await expectLogic(stale).toFinishAllListeners()
// A link that names a renamed variant, a deleted metric, or a mode that no longer exists
// has to land on the tab's own defaults rather than on a stuck filter or a refused request.
expect(stale.values.effectiveVariantKey).toBeNull()
// The facet persists, so a variant the experiment doesn't have must never be written: it
// would outlive this visit and show as a selection on the next one.
expect(stale.values.selectedVariantKey).toBeNull()
expect(stale.values.effectiveMetricUuids).toEqual([])
expect(stale.values.metricFilterMode).toBe('fired_all')
expect(experimentsSessionBucketsCreate).not.toHaveBeenCalled()
stale.unmount()
})
it.each([
{
name: 'a floor the viewer raised, on another duration key',
duration: [
{
type: PropertyFilterType.Recording,
key: 'duration',
value: 60,
operator: PropertyOperator.GreaterThan,
} as RecordingDurationFilter,
],
expected: {
duration_filter_active: true,
duration_filter_key: 'duration',
duration_filter_seconds: 60,
duration_filter_operator: 'gt',
duration_filter_count: 1,
duration_filter_customized: true,
},
},
{
name: 'a floor the viewer removed',
duration: [],
expected: {
duration_filter_active: false,
duration_filter_key: null,
duration_filter_seconds: null,
duration_filter_operator: null,
duration_filter_count: 0,
duration_filter_customized: true,
},
},
{
name: 'a duration set with a second, stricter entry',
duration: [
{
type: PropertyFilterType.Recording,
key: 'active_seconds',
value: 5,
operator: PropertyOperator.GreaterThan,
} as RecordingDurationFilter,
{
type: PropertyFilterType.Recording,
key: 'active_seconds',
value: 30,
operator: PropertyOperator.GreaterThan,
} as RecordingDurationFilter,
],
expected: {
duration_filter_active: true,
duration_filter_key: 'active_seconds',
duration_filter_seconds: 5,
duration_filter_operator: 'gt',
duration_filter_count: 2,
duration_filter_customized: true,
},
},
])('reports $name on an empty list', async ({ duration, expected }) => {
// Replay applies its default floor to every list, so a report that only said a duration
// filter was present cannot tell the floor everyone gets from one the viewer chose. The
// viewer edits it in the playlist's own filter bar, so the report has to read that rather
// than the filters the tab pushed down.
//
// The last case is the fail-safe one: the reported key, threshold, and operator describe
// the first entry, so a set whose first entry is the default must still report as
// customized, and its count must say that the report names part of the set.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
logic.actions.playlistFiltersChanged({ ...logic.values.recordingsFilters, duration })
logic.actions.recordingsLoaded([])
await expectLogic(logic).toFinishAllListeners()
expect(listsRendered(captureSpy, 42)).toHaveLength(1)
expect(listsRendered(captureSpy, 42)[0][1]).toMatchObject(expected)
})
it('tells a refused metric filter apart from one that matched nothing, once it has answered', async () => {
// Both leave the list with an empty session set, since a refusal must never widen the list
// back out. Folded together, a broken endpoint would inflate the count of filters that
// legitimately match nothing — the reason a reader would act on.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
;(experimentsSessionBucketsCreate as jest.Mock).mockReturnValue(new Promise(() => {}))
const failing = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 113, start_date: daysAgo(10), end_date: null } as Experiment,
})
failing.mount()
failing.actions.setMetricSelected('metric-purchase', true)
failing.actions.setMetricFilterMode('no_metric_activity')
// While the request is out the list is empty because the filter hasn't answered yet, and
// the caption says so. Reported, that provisional empty would be counted twice.
failing.actions.recordingsLoaded([])
expect(listsRendered(captureSpy, 113)).toHaveLength(0)
;(experimentsSessionBucketsCreate as jest.Mock).mockRejectedValue(
Object.assign(new Error('boom'), { detail: 'Pick exactly one funnel metric' })
)
failing.actions.loadSessionBucket()
await expectLogic(failing).toFinishAllListeners()
// A failed bucket leaves the session set empty, exactly as the dropped page found it, so
// the playlist never reloads and the answer alone has to produce the report.
expect(listsRendered(captureSpy, 113)).toHaveLength(1)
expect(listsRendered(captureSpy, 113)[0][1]).toMatchObject({
result_count: 0,
empty_reason: 'metric_filter_failed',
is_bucketed: true,
})
failing.unmount()
})
it('leaves the pages scrolling adds out of the report', async () => {
// Counted too, one visit would look like several lists, and a later page that comes back
// empty would read as an empty list rather than the end of a list with rows.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
logic.actions.recordingsLoaded([], false)
await expectLogic(logic).toFinishAllListeners()
expect(listsRendered(captureSpy, 42)).toHaveLength(0)
})
it('reports the exposure event as unlinkable when it is never seen with a session id', async () => {
// The residual "in window, replay on, still empty" bucket is the one the tab can't explain,
// and an exposure event captured without a session id is the likeliest cause in it.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
seenTogetherSpy.mockResolvedValue({ ...ALL_LINKABLE, $feature_flag_called: false })
const unlinkable = experimentReplayTabLogic({
experiment: { ...EXPERIMENT, id: 112, start_date: daysAgo(10), end_date: null } as Experiment,
})
unlinkable.mount()
await expectLogic(unlinkable).toFinishAllListeners()
unlinkable.actions.recordingsLoaded([])
await expectLogic(unlinkable).toFinishAllListeners()
expect(listsRendered(captureSpy, 112)[0][1]).toMatchObject({
empty_reason: 'unknown_in_window',
exposure_linkable: false,
days_since_end: null,
})
unlinkable.unmount()
})
it('offers saved/shared metrics in the facet, deduped by uuid', async () => {
const savedMetric = {
query: {
kind: NodeKind.ExperimentMetric,
metric_type: ExperimentMetricType.MEAN,
uuid: 'metric-saved',
name: 'Signups',
source: { kind: NodeKind.EventsNode, event: 'signup' },
},
}
const withSaved = experimentReplayTabLogic({
experiment: {
...EXPERIMENT,
id: 46,
// Same saved metric linked twice (e.g. primary + secondary) must yield one chip.
saved_metrics: [savedMetric, savedMetric],
} as unknown as Experiment,
})
withSaved.mount()
await expectLogic(withSaved).toFinishAllListeners()
expect(withSaved.values.metricOptions.map((option) => option.uuid)).toEqual([
'metric-purchase',
'metric-funnel',
'metric-saved',
])
withSaved.unmount()
})
it('hands the list to the bucket endpoint for filters the recordings query cannot express', async () => {
await expectLogic(logic, () => {
logic.actions.setSelectedVariantKey('test')
logic.actions.setMetricSelected('metric-purchase', true)
logic.actions.setMetricSelected('metric-funnel', true)
logic.actions.setMetricFilterMode('fired_any')
}).toFinishAllListeners()
expect(experimentsSessionBucketsCreate).toHaveBeenLastCalledWith(expect.any(String), 42, {
bucket: 'fired_any',
metric_uuids: ['metric-purchase', 'metric-funnel'],
variant: 'test',
})
const { recordingsFilters } = logic.values
expect(recordingsFilters.session_ids).toEqual(['bucket-1', 'bucket-2'])
// The returned set already encodes the metric condition; ANDing the event filters back in
// would narrow it a second time. The person-scoped filter stays alongside: bucket ids say
// the session saw the experiment, not that its person is in the analysis population.
expect(recordingsFilters.experiment_exposure).toEqual({ experiment_id: 42, variant: 'test' })
expect(recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
})
it.each(['fired_any', 'no_metric_activity', 'funnel_dropoff', 'funnel_completed'] as const)(
'leaves the list untouched when %s has no metric to apply',
async (mode) => {
await expectLogic(logic).toFinishAllListeners()
const before = logic.values.recordingsFilters
await expectLogic(logic, () => logic.actions.setMetricFilterMode(mode)).toFinishAllListeners()
// The shared playlist refetches on any deep change to its filters, so a mode with
// nothing ticked has to leave them alone. Otherwise picking the mode reloads the list
// and answers a question the unticked checkboxes never asked.
expect(logic.values.recordingsFilters).toEqual(before)
expect(experimentsSessionBucketsCreate).not.toHaveBeenCalled()
}
)
it('follows the playlist\'s own "Show all" back to the unbucketed list', async () => {
await expectLogic(logic, () => {
logic.actions.setMetricSelected('metric-purchase', true)
logic.actions.setMetricFilterMode('no_metric_activity')
}).toFinishAllListeners()
expect(logic.values.recordingsFilters.session_ids).toEqual(['bucket-1', 'bucket-2'])
// The shared playlist clears session_ids itself; without following it the tab would push
// the same ids straight back and the control would look broken.
await expectLogic(logic, () => {
logic.actions.playlistFiltersChanged({ ...logic.values.recordingsFilters, session_ids: undefined })
}).toMatchValues({ metricFilterMode: 'fired_all' })
expect(logic.values.recordingsFilters.session_ids).toBeUndefined()
})
it("matches the funnel's last step for finished funnels, without asking the endpoint", async () => {
await expectLogic(logic, () => {
logic.actions.setMetricSelected('metric-funnel', true)
logic.actions.setMetricFilterMode('funnel_completed')
}).toFinishAllListeners()
// Every other mode matches a funnel on its entry step, which is where a session starts the
// funnel rather than finishes it. Completion is an ordinary event filter on the last step,
// so it stays exact and uncapped instead of going to the capped bucket endpoint.
expect(logic.values.sessionBucketRequest).toBeNull()
expect(experimentsSessionBucketsCreate).not.toHaveBeenCalled()
expect(logic.values.recordingsFilters.session_ids).toBeUndefined()
expect(logic.values.recordingsFilters.filter_group.values).toEqual([
{
type: FilterLogicalOperator.And,
values: [{ id: 'client_step', name: 'client_step', type: 'events', properties: [] }],
},
])
})
it('takes one funnel metric for drop-off and leaves the rest unselectable', async () => {
await expectLogic(logic, () => {
logic.actions.setMetricSelected('metric-purchase', true)
logic.actions.setMetricSelected('metric-funnel', true)
logic.actions.setMetricFilterMode('funnel_dropoff')
}).toFinishAllListeners()
// A mean metric isn't a funnel, and drop-off takes exactly one funnel at a time.
expect(logic.values.effectiveMetricUuids).toEqual(['metric-funnel'])
expect(experimentsSessionBucketsCreate).toHaveBeenLastCalledWith(expect.any(String), 42, {
bucket: 'funnel_dropoff',
metric_uuids: ['metric-funnel'],
variant: null,
})
})
it.each([
[
'server-side',
[
{ kind: NodeKind.EventsNode, event: 'client_step' },
{ kind: NodeKind.EventsNode, event: 'server_side_step' },
],
FUNNEL_SERVER_SIDE_COMPLETION_REASON,
],
[
'data warehouse',
[
{ kind: NodeKind.EventsNode, event: 'client_step' },
{ kind: NodeKind.EventsNode, event: 'purchase' },
{ kind: NodeKind.ExperimentDataWarehouseNode, table_name: 'stripe_charges' },
],
FUNNEL_DATA_WAREHOUSE_COMPLETION_REASON,
],
])('refuses drop-off when the funnel finishes on a %s step', async (_name, series, expectedReason) => {
seenTogetherSpy.mockResolvedValue({ ...ALL_LINKABLE, server_side_step: false })
const unmatchableFinish = experimentReplayTabLogic({
experiment: {
...EXPERIMENT,
id: 50,
metrics_secondary: [{ ...FUNNEL_METRIC, series }],
} as unknown as Experiment,
})
unmatchableFinish.mount()
await expectLogic(unmatchableFinish, () => {
unmatchableFinish.actions.setMetricSelected('metric-funnel', true)
unmatchableFinish.actions.setMetricFilterMode('funnel_dropoff')
}).toFinishAllListeners()
// The other steps stay matchable, so the metric is selectable in every other mode.
// Drop-off is the one that reads the last step, and a completion no recording can show
// would return every exposed session as not having finished.
expect(unmatchableFinish.values.metricOptions.find((option) => option.uuid === 'metric-funnel')).toMatchObject({
unlinkable: false,
dropoffReason: expectedReason,
})
expect(unmatchableFinish.values.effectiveMetricUuids).toEqual([])
// Nothing is asked of the endpoint, which would refuse this funnel anyway.
expect(unmatchableFinish.values.sessionBucketRequest).toBeNull()
expect(unmatchableFinish.values.recordingsFilters.session_ids).toBeUndefined()
// Both funnel modes read the same last step, so the one that filters client-side has to
// refuse the funnel too rather than matching on a step that isn't the completion.
await expectLogic(unmatchableFinish, () =>
unmatchableFinish.actions.setMetricFilterMode('funnel_completed')
).toFinishAllListeners()
expect(unmatchableFinish.values.effectiveMetricUuids).toEqual([])
expect(unmatchableFinish.values.recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
unmatchableFinish.unmount()
})
it('asks the endpoint for drop-off on a single-step funnel', async () => {
const oneStepFunnel = experimentReplayTabLogic({
experiment: {
...EXPERIMENT,
id: 51,
metrics_secondary: [
{ ...FUNNEL_METRIC, series: [{ kind: NodeKind.EventsNode, event: 'client_step' }] },
],
} as unknown as Experiment,
})
oneStepFunnel.mount()
await expectLogic(oneStepFunnel, () => {
oneStepFunnel.actions.setMetricSelected('metric-funnel', true)
oneStepFunnel.actions.setMetricFilterMode('funnel_dropoff')
}).toFinishAllListeners()
// The funnel's first step is the exposure, so one series step is a complete funnel. A
// single-event metric must not fall back to the client-side filter path other modes use.
expect(oneStepFunnel.values.metricOptions.find((option) => option.uuid === 'metric-funnel')).toMatchObject({
dropoffReason: null,
})
expect(experimentsSessionBucketsCreate).toHaveBeenLastCalledWith(expect.any(String), 51, {
bucket: 'funnel_dropoff',
metric_uuids: ['metric-funnel'],
variant: null,
})
oneStepFunnel.unmount()
})
it('keeps the list empty when the bucket fails, rather than widening it silently', async () => {
;(experimentsSessionBucketsCreate as jest.Mock).mockRejectedValue(new Error('boom'))
await expectLogic(logic, () => {
logic.actions.setMetricSelected('metric-purchase', true)
logic.actions.setMetricSelected('metric-funnel', true)
logic.actions.setMetricFilterMode('fired_any')
}).toFinishAllListeners()
// Falling back to no session_ids would show every exposed session under a label that
// promises a narrower set. The message is kept, not just a flag: a rejected request
// usually says what to fix.
expect(logic.values.sessionBucketError).toBe('boom')
expect(logic.values.recordingsFilters.session_ids).toEqual([])
})
it('reports a bucket load once it is the one the list shows', async () => {
// The bucket events have never fired in production — every open so far used the default
// mode, which never asks the endpoint. This pins that the wiring works when one does.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const bucketLoads = (): any[] =>
captureSpy.mock.calls.filter(([event]) => event === 'experiment recordings bucket loaded')
await expectLogic(logic, () => {
logic.actions.setMetricSelected('metric-funnel', true)
logic.actions.setMetricFilterMode('fired_any')
}).toFinishAllListeners()
expect(bucketLoads()).toHaveLength(1)
expect(bucketLoads()[0][1]).toEqual({
experiment_id: 42,
bucket: 'fired_any',
metric_count: 1,
session_count: 2,
truncated: false,
considered_metric_count: 1,
excluded_metric_count: 0,
duration_ms: expect.any(Number),
})
})
it('reports a bucket failure with what the endpoint said', async () => {
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const bucketEvents = (): any[] =>
captureSpy.mock.calls.filter(([event]) => (event as string).startsWith('experiment recordings bucket'))
;(experimentsSessionBucketsCreate as jest.Mock).mockRejectedValue(
Object.assign(new Error('boom'), { detail: 'Pick exactly one funnel metric' })
)
await expectLogic(logic, () => {
logic.actions.setMetricSelected('metric-funnel', true)
logic.actions.setMetricFilterMode('fired_any')
}).toFinishAllListeners()
expect(bucketEvents()).toHaveLength(1)
expect(bucketEvents()[0][0]).toBe('experiment recordings bucket failed')
// The endpoint's detail names what to fix; the generic message would hide it from us.
expect(bucketEvents()[0][1]).toEqual({
experiment_id: 42,
bucket: 'fired_any',
metric_count: 1,
duration_ms: expect.any(Number),
error: 'Pick exactly one funnel metric',
})
})
it('keeps a picked mode that has nothing to filter on yet', async () => {
// The tab pushes no session_ids until an eligible metric is picked, and the playlist echoes
// that back through onFiltersChange. Reading it as the user clearing the bucket bounced the
// mode straight back to "Fired all" the moment it was picked.
const oneStepFunnel = experimentReplayTabLogic({
experiment: {
...EXPERIMENT,
id: 47,
metrics_secondary: [{ ...FUNNEL_METRIC, series: [FUNNEL_METRIC.series[0]] }],
} as unknown as Experiment,
})
oneStepFunnel.mount()
await expectLogic(oneStepFunnel, () => {
oneStepFunnel.actions.setMetricFilterMode('funnel_dropoff')
}).toFinishAllListeners()
oneStepFunnel.actions.playlistFiltersChanged(oneStepFunnel.values.recordingsFilters)
expect(oneStepFunnel.values.metricFilterMode).toBe('funnel_dropoff')
// No funnel with two matchable steps, so nothing is asked of the endpoint either.
expect(oneStepFunnel.values.sessionBucketRequest).toBeNull()
expect(experimentsSessionBucketsCreate).not.toHaveBeenCalled()
oneStepFunnel.unmount()
})
it("lists metrics in the experiment page's order, and never hides one missing from it", async () => {
// The ordering arrays are the metrics page's display order. Sorting on them keeps the two
// surfaces in step; filtering on them would silently drop a metric whose uuid never made
// it into the array (older experiments, or a row written outside the API).
const ordered = experimentReplayTabLogic({
experiment: {
...EXPERIMENT,
id: 48,
metrics: [PURCHASE_METRIC, FUNNEL_METRIC],
metrics_secondary: [],
primary_metrics_ordered_uuids: ['metric-funnel'],
} as unknown as Experiment,
})
ordered.mount()
await expectLogic(ordered).toFinishAllListeners()
expect(ordered.values.metricOptions.map((option) => option.uuid)).toEqual(['metric-funnel', 'metric-purchase'])
ordered.unmount()
})
it('lands the player sidebar on Overview, and hands it back when the tab goes away', () => {
// Held mounted across the unmount below, standing in for a player that outlives this tab —
// the only case in which the reset has anything to do.
const sidebar = playerSidebarLogic()
sidebar.mount()
// Recordings are opened from here to see what the experiment did to a session, which is what
// the Overview tab shows. Landing on Inspector instead hides it behind a click.
expect(sidebar.values.defaultTab).toBe(SessionRecordingSidebarTab.OVERVIEW)
logic.unmount()
expect(sidebar.values.defaultTab).toBe(SessionRecordingSidebarTab.INSPECTOR)
sidebar.unmount()
})
it('runs the variant comparison only once the panel is opened', async () => {
// It is the heaviest read on this tab and most visits don't want it, so mounting the tab
// must not fire it.
expect(experimentsSessionEventDeltasCreate).not.toHaveBeenCalled()
await expectLogic(logic, () => {
logic.actions.toggleBehaviorComparison()
}).toFinishAllListeners()
expect(experimentsSessionEventDeltasCreate).toHaveBeenCalledTimes(1)
expect(logic.values.sessionEventDeltas).toEqual(DELTA_RESPONSE)
})
it('asks for no comparison when the experiment aggregates by group, and reports why', async () => {
// The backend answers a group-aggregated experiment with the same 400 every time, so each
// open would spend a heavy request on a refusal the tab can name in advance. The tab view
// carries that reason, which is how a disabled toggle is told apart from one nobody opened.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const tabViews = (): any[] =>
captureSpy.mock.calls.filter(
([event, properties]) =>
event === 'experiment recordings tab viewed' && (properties as any)?.experiment_id === 53
)
featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.EXPERIMENT_BEHAVIOR_COMPARISON]: true })
const grouped = experimentReplayTabLogic({ experiment: GROUP_AGGREGATED_EXPERIMENT })
grouped.mount()
await expectLogic(grouped, () => {
grouped.actions.toggleBehaviorComparison()
}).toFinishAllListeners()
expect(grouped.values.behaviorComparisonUnavailableReason).toBe('group_aggregated')
expect(experimentsSessionEventDeltasCreate).not.toHaveBeenCalled()
expect(tabViews()).toHaveLength(1)
expect(tabViews()[0][1]).toMatchObject({
behavior_comparison_available: true,
behavior_comparison_unavailable_reason: 'group_aggregated',
})
grouped.unmount()
})
it.each([
{
failure: 'a refusal the backend states on purpose',
rejection: Object.assign(new Error('Request failed'), { status: 400, detail: REFUSAL_DETAIL }),
status: 400,
message: REFUSAL_DETAIL,
callsAfterReopen: 1,
},
{
failure: 'a request that may pass on a second attempt',
rejection: new Error('Failed to fetch'),
status: null,
message: 'Failed to fetch',
callsAfterReopen: 2,
},
])('keeps the status beside the message for $failure', async ({ rejection, status, message, callsAfterReopen }) => {
// The status is what splits the two states the shelf renders, and what decides whether
// reopening asks again. A refusal leaves no deltas behind, so without the status the
// reopen path sends the same request and gets the same refusal back.
;(experimentsSessionEventDeltasCreate as jest.Mock).mockRejectedValue(rejection)
await expectLogic(logic, () => {
logic.actions.toggleBehaviorComparison()
}).toFinishAllListeners()
expect(logic.values.sessionEventDeltasError).toBe(message)
expect(logic.values.sessionEventDeltasErrorStatus).toBe(status)
await expectLogic(logic, () => {
logic.actions.toggleBehaviorComparison()
logic.actions.toggleBehaviorComparison()
}).toFinishAllListeners()
expect(experimentsSessionEventDeltasCreate).toHaveBeenCalledTimes(callsAfterReopen)
})
it('reports the population the comparison covered, not just what it found', async () => {
// An empty reason on its own cannot be read: 'no_separation' over sixty people and over
// twelve thousand ask for different answers. So the report carries the denominator, the
// stretch of enrollment it came from, and whether the cap left older enrollees out.
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
await expectLogic(logic, () => {
logic.actions.toggleBehaviorComparison()
}).toFinishAllListeners()
expect(captureSpy).toHaveBeenCalledWith('experiment behavior comparison loaded', {
experiment_id: 42,
too_early: false,
empty_reason: null,
behavior_cards: 1,
friction_cards: 0,
variant_only_cards: 0,
metric_cards: 0,
dropped_duplicate_cards: 0,
used_exposure_fallback: false,
duration_ms: expect.any(Number),
compared_persons: 200,
compared_variants: 2,
compared_enrollment_hours: 744,
sessions_truncated: false,
events_truncated: false,
experiment_ended: true,
// Read off the fixture rather than hardcoded, so the assertion still states the same
// distance as real time moves past the run window.
days_since_start: dayjs().diff(dayjs(EXPERIMENT.start_date), 'day'),
})
})
it('does not fire a duplicate comparison when the shelf is closed and reopened mid-load', async () => {
let resolveLoad: (value: unknown) => void = () => {}
;(experimentsSessionEventDeltasCreate as jest.Mock).mockImplementation(
() => new Promise((resolve) => (resolveLoad = resolve))
)
logic.actions.toggleBehaviorComparison()
await expectLogic(logic).toDispatchActions(['loadSessionEventDeltas'])
logic.actions.toggleBehaviorComparison()
logic.actions.toggleBehaviorComparison()
await expectLogic(logic).toMatchValues({ sessionEventDeltasLoading: true })
expect(experimentsSessionEventDeltasCreate).toHaveBeenCalledTimes(1)
resolveLoad(DELTA_RESPONSE)
await expectLogic(logic).toFinishAllListeners()
expect(logic.values.sessionEventDeltas).toEqual(DELTA_RESPONSE)
})
it("shows a selected card's recordings, in the variant the card belongs to", async () => {
await expectLogic(logic, () => {
logic.actions.setMetricSelected('metric-purchase', true)
logic.actions.setMetricFilterMode('no_metric_activity')
}).toFinishAllListeners()
expect(logic.values.recordingsFilters.session_ids).toEqual(['bucket-1', 'bucket-2'])
expect(logic.values.recordingsFilters.duration).not.toEqual([])
await expectLogic(logic, () => {
logic.actions.selectWatchCard(DELTA_RESPONSE.cards[0] as any)
}).toFinishAllListeners()
// The variant facet follows the card, the capped bucket is left behind so the card's own
// session set takes its place, and the card survives that mode change rather than being
// cleared by it. The metric selection goes with the mode: kept, "didn't fire Purchase"
// would silently have become "fired Purchase".
expect(logic.values.selectedVariantKey).toBe('test')
expect(logic.values.metricFilterMode).toBe('fired_all')
expect(logic.values.selectedMetricUuids).toEqual([])
expect(logic.values.recordingsFilters.session_ids).toEqual(['card-session-1', 'card-session-2'])
// The card's sessions are picked with no duration floor, so the default active-seconds
// filter must not thin out the list the card's count promises.
expect(logic.values.recordingsFilters.duration).toEqual([])
// The card's ids already encode the event condition, so no event filter is added on top;
// the population definition rides `experiment_exposure`, following the card's variant.
expect(logic.values.recordingsFilters.filter_group).toEqual(EMPTY_FILTER_GROUP)
expect(logic.values.recordingsFilters.experiment_exposure).toEqual({ experiment_id: 42, variant: 'test' })
})
it.each([
['the variant facet moves', (): void => logic.actions.setSelectedVariantKey('control')],
['the exposure scope moves', (): void => logic.actions.setExposureScope('in_session')],
['a metric is picked', (): void => logic.actions.setMetricSelected('metric-purchase', true)],
['the shelf is closed', (): void => logic.actions.toggleBehaviorComparison()],
])('drops the selected card when %s', async (_name: string, moveFacet: () => void) => {
await expectLogic(logic, () => {
logic.actions.toggleBehaviorComparison()
logic.actions.selectWatchCard(DELTA_RESPONSE.cards[0] as any)
}).toFinishAllListeners()
expect(logic.values.recordingsFilters.session_ids).toEqual(['card-session-1', 'card-session-2'])
await expectLogic(logic, moveFacet).toFinishAllListeners()
// Left stacked these contradict rather than compose: the card's ids are one variant's and
// AND to an empty list under another, a metric picked on top is dropped from the query
// while still reading as applied, and a closed shelf leaves no way to deselect.
expect(logic.values.selectedWatchCard).toBeNull()
expect(logic.values.recordingsFilters.session_ids).toBeUndefined()
})
it("follows the playlist when the card's recordings are cleared there", async () => {
await expectLogic(logic, () => {
logic.actions.selectWatchCard(DELTA_RESPONSE.cards[0] as any)
}).toFinishAllListeners()
// Without following it the tab would push the same session ids straight back, and the
// playlist's own "Show all" control would look broken.
await expectLogic(logic, () => {
logic.actions.playlistFiltersChanged({
...logic.values.recordingsFilters,
session_ids: undefined,
})
}).toMatchValues({ selectedWatchCard: null })
})
it('loads the scanners watching this experiment, scoped by experiment_id', async () => {
// Guards the back-link on the Recordings tab: it must query the scanners endpoint with this
// experiment's id (dropping the filter would list every scanner in the project) and surface
// the name, type, and monthly observation count each row shows.
logic.unmount()
featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.VISION_EXPERIMENT_SCANNER]: true })
;(visionScannersList as jest.Mock).mockResolvedValue({
results: [
{
id: 's1',
name: 'Checkout confusion',
scanner_type: 'classifier',
enabled: true,
observations_this_month: 50,
},
{
id: 's2',
name: 'Rage clicks',
scanner_type: 'summarizer',
enabled: false,
observations_this_month: 3,
},
{
id: 's3',
name: 'Checkout redesign (#42)',
scanner_type: 'experiment',
enabled: false,
scanner_config: { experiment_id: 42, start_on_launch: true },
observations_this_month: 0,
},
],
})
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const withScanners = experimentReplayTabLogic({ experiment: EXPERIMENT })
withScanners.mount()
await expectLogic(withScanners)
.toFinishAllListeners()
.toMatchValues({
// Only an off scanner that waits for launch says so; a scanner turned off by hand does not.
linkedScanners: [
{
id: 's1',
name: 'Checkout confusion',
scannerType: 'classifier',
observationsThisMonth: 50,
startsAtLaunch: false,
},
{
id: 's2',
name: 'Rage clicks',
scannerType: 'summarizer',
observationsThisMonth: 3,
startsAtLaunch: false,
},
{
id: 's3',
name: 'Checkout redesign (#42)',
scannerType: 'experiment',
observationsThisMonth: 0,
startsAtLaunch: true,
},
],
})
expect(visionScannersList).toHaveBeenCalledWith(expect.any(String), { experiment_id: '42' })
// The adoption funnel reads which entry point was shown from this event.
expect(captureSpy).toHaveBeenCalledWith('experiment recordings scanner entry point shown', {
experiment_id: 42,
state: 'scanners_listed',
scanner_count: 3,
})
captureSpy.mockRestore()
withScanners.unmount()
})
it('does not query scanners when the experiment scanner flag is off', async () => {
// The card only renders behind the flag, so the lookup must not fire for the many users who
// open the Recordings tab without it. The default test flags leave the flag off.
logic.unmount()
;(visionScannersList as jest.Mock).mockClear()
const withoutFlag = experimentReplayTabLogic({ experiment: EXPERIMENT })
withoutFlag.mount()
await expectLogic(withoutFlag).toFinishAllListeners()
expect(visionScannersList).not.toHaveBeenCalled()
withoutFlag.unmount()
})
it('marks the scanners lookup loading while in flight, so the tab shows a skeleton not the banner', async () => {
// The skeleton branch keys off linkedScannersLoading. Without the loading flag, the tab would
// flash the cross-sell banner (linkedScanners is [] until the fetch resolves) before the card.
logic.unmount()
featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.VISION_EXPERIMENT_SCANNER]: true })
let resolve: (value: unknown) => void = () => {}
;(visionScannersList as jest.Mock).mockReturnValue(new Promise((r) => (resolve = r)))
const loadingLogic = experimentReplayTabLogic({ experiment: EXPERIMENT })
loadingLogic.mount()
await expectLogic(loadingLogic).toMatchValues({ linkedScannersLoading: true })
resolve({ results: [] })
await expectLogic(loadingLogic).toFinishAllListeners().toMatchValues({ linkedScannersLoading: false })
loadingLogic.unmount()
})
it('degrades to no back-link when the scanner lookup fails', async () => {
// The tab must render even if the lookup errors, so the loader swallows to an empty list.
logic.unmount()
featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.VISION_EXPERIMENT_SCANNER]: true })
;(visionScannersList as jest.Mock).mockRejectedValue(new Error('boom'))
const captureSpy = jest.spyOn(posthog, 'capture').mockReturnValue(undefined as any)
const withError = experimentReplayTabLogic({ experiment: EXPERIMENT })
withError.mount()
await expectLogic(withError).toFinishAllListeners().toMatchValues({ linkedScanners: [] })
// With no scanners the tab offers the set-up card, and the shown event must say so.
expect(captureSpy).toHaveBeenCalledWith(
'experiment recordings scanner entry point shown',
expect.objectContaining({ state: 'set_up_offered', scanner_count: 0 })
)
captureSpy.mockRestore()
withError.unmount()
})
})