: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 { DataTableNode, NodeKind } from '~/queries/schema/schema-general'
import { PropertyFilterType, PropertyOperator } from '~/types'
import {
FilterIdentifier,
PersonData,
aiObservabilityColumnRenderers,
createPersonFilter,
getEventData,
getFilterIdentifier,
getTracesUrlWithPersonFilter,
} from './aiObservabilityColumnRenderers'
import { aiObservabilityGlobalColumnRenderers } from './aiObservabilityGlobalColumns'
describe('aiObservabilityColumnRenderers', () => {
describe('aiObservabilityGlobalColumnRenderers', () => {
// The global keys are listed by hand so that tables can skip the renderer module. A namespaced
// renderer missing from that list would stop applying outside the AI observability scenes.
it('lists every namespaced renderer', () => {
const namespacedKeys = Object.keys(aiObservabilityColumnRenderers).filter(
(key) => key.startsWith('properties.$ai_') || key.startsWith('__llm_')
)
expect(Object.keys(aiObservabilityGlobalColumnRenderers).sort()).toEqual(namespacedKeys.sort())
})
})
describe('getEventData', () => {
// Regression: without traceId + timestamp, useAIData can never fetch stripped heavy props,
// so Input/Output cells fall back to empty instead of loading the full payload.
it('extracts trace coordinates from object-format records', () => {
const eventData = getEventData({
uuid: 'event-1',
timestamp: '2026-04-30T10:00:00Z',
properties: {
$ai_trace_id: 'trace-1',
$ai_input: [{ role: 'user', content: 'hi' }],
$ai_output_choices: [{ role: 'assistant', content: 'hello' }],
},
})
expect(eventData).toEqual({
uuid: 'event-1',
input: [{ role: 'user', content: 'hi' }],
output: [{ role: 'assistant', content: 'hello' }],
traceId: 'trace-1',
timestamp: '2026-04-30T10:00:00Z',
})
})
it('extracts trace coordinates from array-format records by select column position', () => {
const query: DataTableNode = {
kind: NodeKind.DataTableNode,
source: {
kind: NodeKind.EventsQuery,
select: [
'uuid',
'properties.$ai_input[-1]',
'properties.$ai_output_choices',
'properties.$ai_trace_id',
'timestamp',
],
},
}
const eventData = getEventData(
['event-1', 'stripped-input', 'stripped-output', 'trace-1', '2026-04-30T10:00:00Z'],
query
)
expect(eventData).toEqual({
uuid: 'event-1',
input: 'stripped-input',
output: 'stripped-output',
traceId: 'trace-1',
timestamp: '2026-04-30T10:00:00Z',
})
})
})
describe('getFilterIdentifier', () => {
it.each<[string, PersonData | null | undefined, FilterIdentifier | null]>([
['returns null when person is null', null, null],
['returns null when person is undefined', undefined, null],
[
'returns null when person has no identifiable properties',
{ distinct_id: undefined, properties: {} },
null,
],
[
'returns email when person has email property',
{ distinct_id: 'user-123', properties: { email: '[email protected]', username: 'testuser' } },
{ type: 'email', value: '[email protected]' },
],
[
'returns username when person has username but no email',
{ distinct_id: 'user-123', properties: { username: 'testuser' } },
{ type: 'username', value: 'testuser' },
],
[
'returns distinct_id when person has only distinct_id',
{ distinct_id: 'user-123', properties: {} },
{ type: 'distinct_id', value: 'user-123' },
],
[
'returns distinct_id when properties is undefined',
{ distinct_id: 'user-123' },
{ type: 'distinct_id', value: 'user-123' },
],
[
'prioritizes email over username and distinct_id',
{ distinct_id: 'user-123', properties: { email: '[email protected]', username: 'testuser' } },
{ type: 'email', value: '[email protected]' },
],
[
'prioritizes username over distinct_id when no email',
{ distinct_id: 'user-123', properties: { username: 'testuser' } },
{ type: 'username', value: 'testuser' },
],
[
'ignores non-string email property',
{ distinct_id: 'user-123', properties: { email: 123 } },
{ type: 'distinct_id', value: 'user-123' },
],
[
'ignores non-string username property',
{ distinct_id: 'user-123', properties: { username: ['array'] } },
{ type: 'distinct_id', value: 'user-123' },
],
])('%s', (_description, person, expected) => {
expect(getFilterIdentifier(person)).toEqual(expected)
})
})
describe('createPersonFilter', () => {
it.each<[string, FilterIdentifier, ReturnType<typeof createPersonFilter>]>([
[
'creates HogQL filter for distinct_id type',
{ type: 'distinct_id', value: 'user-123' },
{ type: PropertyFilterType.HogQL, key: "distinct_id == 'user-123'" },
],
[
'creates Person property filter for email type',
{ type: 'email', value: '[email protected]' },
{
type: PropertyFilterType.Person,
key: 'email',
operator: PropertyOperator.Exact,
value: '[email protected]',
},
],
[
'creates Person property filter for username type',
{ type: 'username', value: 'testuser' },
{
type: PropertyFilterType.Person,
key: 'username',
operator: PropertyOperator.Exact,
value: 'testuser',
},
],
])('%s', (_description, filterIdentifier, expected) => {
expect(createPersonFilter(filterIdentifier)).toEqual(expected)
})
})
describe('getTracesUrlWithPersonFilter', () => {
it('generates URL with email filter', () => {
const url = getTracesUrlWithPersonFilter({ type: 'email', value: '[email protected]' })
expect(url).toContain('/ai-observability/traces')
expect(url).toContain('filters')
expect(url).toContain('email')
expect(url).toContain('test%40example.com')
})
it('generates URL with distinct_id filter', () => {
const url = getTracesUrlWithPersonFilter({ type: 'distinct_id', value: 'user-123' })
expect(url).toContain('/ai-observability/traces')
expect(url).toContain('filters')
expect(url).toContain("distinct_id%20%3D%3D%20'user-123'")
})
it('includes date range params when provided', () => {
const url = getTracesUrlWithPersonFilter(
{ type: 'email', value: '[email protected]' },
{ dateFrom: '2024-01-01', dateTo: '2024-01-31' }
)
expect(url).toContain('date_from=2024-01-01')
expect(url).toContain('date_to=2024-01-31')
})
it('omits date params when they are null', () => {
const url = getTracesUrlWithPersonFilter(
{ type: 'email', value: '[email protected]' },
{ dateFrom: null, dateTo: null }
)
expect(url).not.toContain('date_from')
expect(url).not.toContain('date_to')
})
it('handles partial date range', () => {
const url = getTracesUrlWithPersonFilter(
{ type: 'email', value: '[email protected]' },
{ dateFrom: '2024-01-01', dateTo: null }
)
expect(url).toContain('date_from=2024-01-01')
expect(url).not.toContain('date_to')
})
})
})