: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 { expectLogic, partial } from 'kea-test-utils'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { useMocks } from '~/mocks/jest'
import { propertyDefinitionsModel } from '~/models/propertyDefinitionsModel'
import { initKeaTests } from '~/test/init'
import { PropertyDefinition, PropertyDefinitionState, PropertyDefinitionType, PropertyType } from '~/types'
const propertyDefinitions: PropertyDefinition[] = [
{
id: 'an id',
name: 'no property type',
description: 'a description',
type: PropertyDefinitionType.Event,
},
{
id: 'an id',
name: 'a string',
description: 'a description',
property_type: PropertyType.String,
type: PropertyDefinitionType.Event,
},
{
id: 'an id',
name: '$time',
description: 'a description',
property_type: PropertyType.DateTime,
type: PropertyDefinitionType.Event,
},
{
id: 'an id',
name: '$timestamp',
description: 'a description',
property_type: PropertyType.DateTime,
type: PropertyDefinitionType.Event,
},
]
const groupPropertyDefinitions: PropertyDefinition[] = [
{
id: 'an id',
name: 'no property type',
description: 'a description',
type: PropertyDefinitionType.Group,
},
{
id: 'an id',
name: 'a string',
description: 'a description',
property_type: PropertyType.String,
type: PropertyDefinitionType.Group,
},
{
id: 'an id',
name: '$time',
description: 'a description',
property_type: PropertyType.DateTime,
type: PropertyDefinitionType.Group,
},
]
describe('the property definitions model', () => {
let logic: ReturnType<typeof propertyDefinitionsModel.build>
let featureFlagsLogic: ReturnType<typeof featureFlagLogic.build>
beforeEach(async () => {
useMocks({
get: {
'/api/projects/:team_id/property_definitions/': ({ request }) => {
const url = new URL(request.url)
const propertiesToFind = (url.searchParams.get('properties') || '').split(',')
if (propertiesToFind[0] === 'network error') {
return [500, { detail: 'simulated network error' }]
}
const filteredPropertyDefinitions =
url.searchParams.get('type') === 'group' && url.searchParams.get('group_type_index') !== null
? groupPropertyDefinitions
: propertyDefinitions
const foundProperties = filteredPropertyDefinitions.filter(
(p) => propertiesToFind.length === 0 || propertiesToFind.includes(p.name)
)
return [
200,
{
count: foundProperties.length,
results: foundProperties,
next: undefined,
},
]
},
},
})
initKeaTests()
featureFlagsLogic = featureFlagLogic()
featureFlagsLogic.mount()
logic = propertyDefinitionsModel()
logic.mount()
})
describe('loading properties', () => {
it('can load property definitions', async () => {
await expectLogic(logic, () => {
logic.actions.loadPropertyDefinitions(['a string'], PropertyDefinitionType.Event)
})
.toDispatchActions([
logic.actionCreators.updatePropertyDefinitions({
'event/a string': PropertyDefinitionState.Pending,
}),
logic.actionCreators.fetchAllPendingDefinitions(),
logic.actionCreators.updatePropertyDefinitions({
'event/a string': PropertyDefinitionState.Loading,
}),
logic.actionCreators.updatePropertyDefinitions({
'event/a string': propertyDefinitions.find(
({ name }) => name === 'a string'
) as PropertyDefinition,
}),
])
.toMatchValues({
propertyDefinitionStorage: partial({
'event/a string': propertyDefinitions.find(({ name }) => name === 'a string'),
}),
})
expect(logic.values.propertyDefinitionsByType('event')).toEqual([
{
description: 'Duration of the session',
id: '$session_duration',
is_seen_on_filtered_events: false,
is_numerical: true,
name: '$session_duration',
property_type: 'Duration',
},
propertyDefinitions.find(({ name }) => name === 'a string'),
])
})
it('can load group property definitions when correct group type index is provided', async () => {
await expectLogic(logic, () => {
logic.actions.loadPropertyDefinitions(['a string'], PropertyDefinitionType.Group, 1)
})
.toDispatchActions([
logic.actionCreators.updatePropertyDefinitions({
'group/1/a string': PropertyDefinitionState.Pending,
}),
logic.actionCreators.fetchAllPendingDefinitions(),
logic.actionCreators.updatePropertyDefinitions({
'group/1/a string': PropertyDefinitionState.Loading,
}),
logic.actionCreators.updatePropertyDefinitions({
'group/1/a string': groupPropertyDefinitions.find(
({ name }) => name === 'a string'
) as PropertyDefinition,
}),
])
.toMatchValues({
propertyDefinitionStorage: partial({
'group/1/a string': groupPropertyDefinitions.find(({ name }) => name === 'a string'),
}),
})
// invalid or wrong group type, should not return any properties
expect(logic.values.propertyDefinitionsByType('group')).toEqual([])
expect(logic.values.propertyDefinitionsByType('group', 0)).toEqual([])
expect(logic.values.propertyDefinitionsByType('group', 1)).toEqual([
groupPropertyDefinitions.find(({ name }) => name === 'a string'),
])
})
it('handles network errors', async () => {
// run twice to assure errors get retried
for (let i = 0; i < 2; i++) {
await expectLogic(logic, () => {
logic.actions.loadPropertyDefinitions(['network error'], PropertyDefinitionType.Event)
})
.toDispatchActions([
logic.actionCreators.updatePropertyDefinitions({
'event/network error': PropertyDefinitionState.Pending,
}),
logic.actionCreators.fetchAllPendingDefinitions(),
logic.actionCreators.updatePropertyDefinitions({
'event/network error': PropertyDefinitionState.Error,
}),
])
.toMatchValues({
propertyDefinitionStorage: partial({ 'event/network error': PropertyDefinitionState.Error }),
})
}
})
it('handles missing definitions', async () => {
await expectLogic(logic, () => {
logic.actions.loadPropertyDefinitions(['this is not there'], PropertyDefinitionType.Event)
})
.toDispatchActions([
logic.actionCreators.updatePropertyDefinitions({
'event/this is not there': PropertyDefinitionState.Pending,
}),
logic.actionCreators.fetchAllPendingDefinitions(),
logic.actionCreators.updatePropertyDefinitions({
'event/this is not there': PropertyDefinitionState.Missing,
}),
])
.toMatchValues({
propertyDefinitionStorage: partial({ 'event/this is not there': PropertyDefinitionState.Missing }),
})
})
it('marks unfetchable definition types as missing without wedging the queue', async () => {
// account_custom_property has no definitions endpoint; a stale key for it (e.g. from
// a saved view for a deleted definition) must not block other types from resolving.
await expectLogic(logic, () => {
logic.actions.loadPropertyDefinitions(
['11111111-2222-3333-4444-555555555555'],
PropertyDefinitionType.AccountCustomProperty
)
logic.actions.loadPropertyDefinitions(['a string'], PropertyDefinitionType.Event)
})
.toFinishAllListeners()
.toMatchValues({
propertyDefinitionStorage: partial({
'account_custom_property/11111111-2222-3333-4444-555555555555': PropertyDefinitionState.Missing,
'event/a string': partial({ name: 'a string' }),
}),
})
})
it('drops a definitions response that arrives after the logic is gone', async () => {
let releaseRequest: (() => void) | undefined
let requestStarted: () => void
let requestAnswered: () => void
const inFlight = new Promise<void>((resolve) => (requestStarted = resolve))
const answered = new Promise<void>((resolve) => (requestAnswered = resolve))
useMocks({
get: {
'/api/projects/:team_id/property_definitions/': async () => {
requestStarted()
await new Promise<void>((release) => (releaseRequest = release))
requestAnswered()
return [200, { count: 0, results: [], next: undefined }]
},
},
})
const detachedPathErrors: unknown[] = []
const captureRejection = (reason: unknown): void => {
if (String((reason as Error)?.message).includes('Can not find path')) {
detachedPathErrors.push(reason)
}
}
process.on('unhandledRejection', captureRejection)
try {
logic.actions.loadPropertyDefinitions(['a string'], PropertyDefinitionType.Event)
await inFlight
// permanentlyMount() holds a reference of its own, so one unmount leaves it mounted.
while (propertyDefinitionsModel.findMounted()) {
logic.unmount()
}
releaseRequest?.()
await answered
// The listener resumes on the turn the response lands, and node reports an
// unhandled rejection at the end of the turn that produced it.
await new Promise((resolve) => setImmediate(resolve))
await new Promise((resolve) => setImmediate(resolve))
} finally {
process.off('unhandledRejection', captureRejection)
}
expect(detachedPathErrors).toEqual([])
})
it('handles local definitions', async () => {
await expectLogic(logic, () => {
logic.actions.loadPropertyDefinitions(['$session_duration'], PropertyDefinitionType.Event)
})
.toFinishAllListeners()
.toNotHaveDispatchedActions(['updatePropertyDefinitions'])
.toMatchValues({
propertyDefinitionStorage: {
'event/$session_duration': partial({ name: '$session_duration' }),
'session/snapshot_source': partial({ name: 'snapshot_source' }),
'event_metadata/$group_0': {
id: '$group_0',
name: 'organization',
property_type: 'String',
type: 'event_metadata',
},
'event_metadata/$group_1': {
id: '$group_1',
name: 'instance',
property_type: 'String',
type: 'event_metadata',
},
'event_metadata/$group_2': {
id: '$group_2',
name: 'project',
property_type: 'String',
type: 'event_metadata',
},
'event_metadata/distinct_id': partial({ name: 'distinct_id' }),
'event_metadata/event': partial({ name: 'event' }),
'event_metadata/person_id': partial({ name: 'person_id' }),
'event_metadata/person_mode': partial({ name: 'person_mode' }),
'event_metadata/timestamp': partial({
name: 'timestamp',
}),
'person_metadata/created_at': {
id: 'created_at',
name: 'created_at',
property_type: 'DateTime',
type: 'person_metadata',
},
'resource/assignee': partial({ name: 'assignee' }),
'resource/first_seen': partial({ name: 'first_seen' }),
},
})
})
})
describe('lazy loading', () => {
it('lazy loads a property with getPropertyDefinition()', async () => {
expect(logic.values.getPropertyDefinition('$time', PropertyDefinitionType.Event)).toEqual(null)
await expectLogic(logic).toFinishAllListeners()
expect(logic.values.getPropertyDefinition('$time', PropertyDefinitionType.Event)).toEqual(
partial({ name: '$time', property_type: 'DateTime' })
)
})
it('lazy loads a property with describeProperty()', async () => {
expect(logic.values.describeProperty('$time', PropertyDefinitionType.Event)).toEqual(null)
await expectLogic(logic).toFinishAllListeners()
expect(logic.values.describeProperty('$time', PropertyDefinitionType.Event)).toEqual('DateTime')
})
it('lazy loads a property with formatPropertyValueForDisplay()', async () => {
expect(logic.values.formatPropertyValueForDisplay('$time', 1661332948)).toEqual('1661332948')
await expectLogic(logic).toFinishAllListeners()
expect(logic.values.formatPropertyValueForDisplay('$time', 1661332948)).toEqual('2022-08-24 09:22:28')
})
it('does not refetch missing properties', async () => {
expect(logic.values.describeProperty('not a prop', PropertyDefinitionType.Event)).toEqual(null)
await expectLogic(logic)
.delay(15)
.toFinishAllListeners()
.toDispatchActions([
'loadPropertyDefinitions',
'fetchAllPendingDefinitions',
'updatePropertyDefinitions',
'updatePropertyDefinitions',
])
.toMatchValues({
propertyDefinitionStorage: partial({ 'event/not a prop': PropertyDefinitionState.Missing }),
})
expect(logic.values.describeProperty('not a prop', PropertyDefinitionType.Event)).toEqual(null)
await expectLogic(logic)
.delay(15)
.toFinishAllListeners()
.toNotHaveDispatchedActions(['loadPropertyDefinitions'])
.toMatchValues({
propertyDefinitionStorage: partial({ 'event/not a prop': PropertyDefinitionState.Missing }),
})
})
it('works with different types', async () => {
expect(logic.values.describeProperty('not a prop', PropertyDefinitionType.Event)).toEqual(null)
await expectLogic(logic)
.delay(15)
.toFinishAllListeners()
.toMatchValues({
propertyDefinitionStorage: partial({ 'event/not a prop': PropertyDefinitionState.Missing }),
})
expect(logic.values.describeProperty('$time', PropertyDefinitionType.Person)).toEqual(null)
await expectLogic(logic)
.delay(15)
.toFinishAllListeners()
.toMatchValues({
propertyDefinitionStorage: partial({
'event/not a prop': PropertyDefinitionState.Missing,
'person/$time': partial({ name: '$time', property_type: 'DateTime' }),
}),
})
})
})
describe('formatting properties', () => {
beforeEach(async () => {
await expectLogic(() => {
logic.actions.loadPropertyDefinitions(
['a string', '$timestamp', 'no property type'],
PropertyDefinitionType.Event
)
}).toFinishAllListeners()
})
describe('formatting simple properties', () => {
it('does not describe a property that has no server provided type', () => {
expect(logic.values.describeProperty('no property type', PropertyDefinitionType.Event)).toBeNull()
})
it('does not describe a property that has not yet been cached', () => {
expect(logic.values.describeProperty('not yet cached', PropertyDefinitionType.Event)).toBeNull()
})
it('does describe a property that has a server provided type', () => {
expect(logic.values.describeProperty('a string', PropertyDefinitionType.Event)).toEqual('String')
expect(logic.values.describeProperty('$timestamp', PropertyDefinitionType.Event)).toEqual('DateTime')
})
it('can format a property with no formatting needs for display', () => {
expect(logic.values.formatPropertyValueForDisplay('a string', '1641368752.908')).toEqual(
'1641368752.908'
)
})
it('can format an unknown property for display', () => {
expect(
logic.values.formatPropertyValueForDisplay('not a known property type', '1641368752.908')
).toEqual('1641368752.908')
})
it('can format an null property key for display', () => {
expect(logic.values.formatPropertyValueForDisplay(null, '1641368752.908')).toEqual('1641368752.908')
})
})
describe('formatting datetime properties', () => {
it('can format a unix timestamp as seconds with fractional part for display', () => {
expect(logic.values.formatPropertyValueForDisplay('$timestamp', '1641368752.908')).toEqual(
'2022-01-05 07:45:52'
)
})
it('can format a unix timestamp as milliseconds for display', () => {
expect(logic.values.formatPropertyValueForDisplay('$timestamp', '1641368752908')).toEqual(
'2022-01-05 07:45:52'
)
})
it('can format a unix timestamp as seconds for display', () => {
expect(logic.values.formatPropertyValueForDisplay('$timestamp', '1641368752')).toEqual(
'2022-01-05 07:45:52'
)
})
it('can format a date string for display', () => {
expect(logic.values.formatPropertyValueForDisplay('$timestamp', '2022-01-05')).toEqual('2022-01-05')
})
it('can format a datetime string for display', () => {
expect(logic.values.formatPropertyValueForDisplay('$timestamp', '2022-01-05 07:45:52')).toEqual(
'2022-01-05 07:45:52'
)
})
it('can format an array of datetime string for display', () => {
expect(
logic.values.formatPropertyValueForDisplay('$timestamp', ['1641368752.908', 1641368752.908])
).toEqual(['2022-01-05 07:45:52', '2022-01-05 07:45:52'])
})
})
describe('formatting duration properties', () => {
it('can format a number to duration', () => {
expect(logic.values.formatPropertyValueForDisplay('$session_duration', 60)).toEqual('00:01:00')
})
it('can format a string to duration', () => {
expect(logic.values.formatPropertyValueForDisplay('$session_duration', '60')).toEqual('00:01:00')
})
it('handles non numbers', () => {
expect(logic.values.formatPropertyValueForDisplay('$session_duration', 'blah')).toEqual('blah')
})
})
it('can format a null value for display', () => {
expect(logic.values.formatPropertyValueForDisplay('$timestamp', null)).toEqual(null)
expect(logic.values.formatPropertyValueForDisplay('$timestamp', undefined)).toEqual(null)
})
})
describe('loading property values', () => {
it.each([
[
'valid array results',
{ results: [{ name: 'chrome' }, { name: 'firefox' }], refreshing: false },
[{ name: 'chrome' }, { name: 'firefox' }],
],
['non-array object in results', { results: { some: 'object' }, refreshing: false }, []],
['null results', { results: null, refreshing: false }, []],
['missing results key', { refreshing: false }, []],
])('handles %s without crashing and stores correct values', async (_label, apiResponse, expectedValues) => {
useMocks({
get: {
'/api/event/values/': () => [200, apiResponse],
},
})
await expectLogic(logic, () => {
logic.actions.loadPropertyValues({
endpoint: undefined,
type: PropertyDefinitionType.Event,
propertyKey: 'browser',
eventNames: [],
newInput: undefined,
})
}).toFinishAllListeners()
expect(logic.values.options['browser'].values).toEqual(expectedValues)
})
it('handles API errors by setting error state and stopping loading', async () => {
useMocks({
get: {
'/api/event/values/': () => [503, { detail: 'Service Unavailable' }],
},
})
await expectLogic(logic, () => {
logic.actions.loadPropertyValues({
endpoint: undefined,
type: PropertyDefinitionType.Event,
propertyKey: 'browser',
eventNames: [],
newInput: undefined,
})
})
.toDispatchActions(['setOptionsLoading', 'setOptionsError'])
.toFinishAllListeners()
expect(logic.values.options['browser'].status).toEqual('error')
expect(logic.values.options['browser'].refreshing).toEqual(false)
})
})
describe('local property values', () => {
it.each([
[
'log_entry/level',
PropertyDefinitionType.LogEntry,
'level',
[
{ id: 0, name: 'info' },
{ id: 1, name: 'warn' },
{ id: 2, name: 'error' },
],
],
[
'resource/severity',
PropertyDefinitionType.Resource,
'severity',
[
{ id: 0, name: 'low' },
{ id: 1, name: 'medium' },
{ id: 2, name: 'high' },
{ id: 3, name: 'critical' },
],
],
] as const)(
'returns local options for %s without a network request',
async (_, type, propertyKey, expectedValues) => {
let networkCalled = false
useMocks({
get: {
[`/api/${type}/values`]: () => {
networkCalled = true
return [200, { results: [], refreshing: false }]
},
},
})
await expectLogic(logic, () => {
logic.actions.loadPropertyValues({
endpoint: undefined,
type,
propertyKey,
newInput: undefined,
})
}).toFinishAllListeners()
expect(networkCalled).toBe(false)
expect(logic.values.options[propertyKey].values).toEqual(expectedValues)
}
)
it('does not fetch from a nonexistent endpoint for log_entry properties without local options', async () => {
let networkCalled = false
useMocks({
get: {
'/api/log_entry/values': () => {
networkCalled = true
return [200, { results: [], refreshing: false }]
},
},
})
await expectLogic(logic, () => {
logic.actions.loadPropertyValues({
endpoint: undefined,
type: PropertyDefinitionType.LogEntry,
propertyKey: 'message',
newInput: undefined,
})
}).toFinishAllListeners()
expect(networkCalled).toBe(false)
})
})
describe('loadPropertyValues', () => {
it('includes refresh=force_cache in the URL when polling', async () => {
let capturedUrl: string | undefined
useMocks({
get: {
'/api/event/values': ({ request }) => {
capturedUrl = request.url
return [200, { results: [], refreshing: false }]
},
},
})
await expectLogic(logic, () => {
logic.actions.loadPropertyValues({
endpoint: undefined,
type: PropertyDefinitionType.Event,
newInput: undefined,
propertyKey: 'browser',
refresh: 'force_cache',
})
}).toFinishAllListeners()
expect(capturedUrl).toContain('refresh=force_cache')
})
it('does not include refresh in the URL by default', async () => {
let capturedUrl: string | undefined
useMocks({
get: {
'/api/event/values': ({ request }) => {
capturedUrl = request.url
return [200, { results: [], refreshing: false }]
},
},
})
await expectLogic(logic, () => {
logic.actions.loadPropertyValues({
endpoint: undefined,
type: PropertyDefinitionType.Event,
newInput: undefined,
propertyKey: 'browser',
})
}).toFinishAllListeners()
expect(capturedUrl).not.toContain('refresh=')
})
it('sends refresh=force_cache on the follow-up poll after a refreshing response', async () => {
let pollCallback: (() => void) | null = null
const capturedUrls: string[] = []
// Intercept only the 2000ms polling timer; let all other timers run normally
const originalSetTimeout = global.setTimeout.bind(global)
jest.spyOn(global, 'setTimeout').mockImplementation(((
fn: TimerHandler,
delay?: number,
...args: unknown[]
) => {
if (delay === 2000) {
pollCallback = () => (fn as (...a: unknown[]) => unknown)(...args)
return 0 as unknown as ReturnType<typeof setTimeout>
}
return originalSetTimeout(fn, delay, ...args)
}) as typeof setTimeout)
useMocks({
get: {
'/api/event/values': ({ request }) => {
capturedUrls.push(request.url)
return [200, { results: [], refreshing: capturedUrls.length === 1 }]
},
},
})
await expectLogic(logic, () => {
logic.actions.loadPropertyValues({
endpoint: undefined,
type: PropertyDefinitionType.Event,
newInput: undefined,
propertyKey: 'browser',
})
}).toFinishAllListeners()
jest.restoreAllMocks()
expect(pollCallback).not.toBeNull()
await expectLogic(logic, () => pollCallback!()).toFinishAllListeners()
expect(capturedUrls).toHaveLength(2)
expect(capturedUrls[1]).toContain('refresh=force_cache')
})
})
})