Skip to content

: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.

2.1 KB
Raw Blame
import { pick } from 'lodash'
import { DateTime } from 'luxon'

import { parseJSON } from '~/common/utils/json-parse'
import { serializeEvent } from '~/ingestion/common/steps/event-processing/emit-event-step'
import { ISOTimestamp, ProcessedEvent, ProjectId } from '~/types'

import { mapProcessedEventToFlagEvaluationRow } from './flag-evaluation-row'

describe('mapProcessedEventToFlagEvaluationRow', () => {
    const processedEvent: ProcessedEvent = {
        uuid: 'event-uuid-1',
        event: '$feature_flag_called',
        properties: { $feature_flag: 'my-flag', $feature_flag_response: true, $group_0: 'org-1' },
        timestamp: '2024-01-15T10:30:00.123Z' as ISOTimestamp,
        team_id: 42,
        project_id: 7 as ProjectId,
        distinct_id: 'distinct-1',
        elements_chain: '',
        created_at: DateTime.fromISO('2024-01-15T10:31:00.456Z'),
        captured_at: null,
        person_id: 'person-uuid-1',
        person_properties: { email: '[email protected]' },
        person_created_at: DateTime.fromISO('2023-01-01T00:00:00.000Z'),
        person_mode: 'full',
    }

    it('narrows the events wire format to the flag_evaluations columns, byte-identical on shared fields', () => {
        const row = mapProcessedEventToFlagEvaluationRow(processedEvent)
        const eventsRow = serializeEvent(processedEvent)

        // Both rows derive created_at and person_created_at from the event, so they agree.
        // Reading the wall clock per serialization instead would stamp two different values.
        expect(row).toStrictEqual(
            pick(eventsRow, [
                'uuid',
                'event',
                'properties',
                'timestamp',
                'team_id',
                'distinct_id',
                'created_at',
                'person_id',
                'person_properties',
                'person_created_at',
                'person_mode',
            ])
        )
        expect(row.timestamp).toBe('2024-01-15 10:30:00.123')
        expect(row.created_at).toBe('2024-01-15 10:31:00.456')
        expect(parseJSON(row.properties!).$feature_flag).toBe('my-flag')
    })
})