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.

1.6 KB
Raw Blame
import { promotePosthogCustomMetadata } from './custom-metadata'

describe('promotePosthogCustomMetadata', () => {
    it.each([
        ['posthog_tags', 'tags', ['beta', 'internal']],
        ['posthog_environment', 'environment', 'prod'],
        ['posthog_constructor', 'constructor', 'ctor-value'],
    ])('promotes <namespace>%s to %s with the prefix stripped', (suffix, name, value) => {
        const props: Record<string, unknown> = { [`ns.${suffix}`]: value }
        promotePosthogCustomMetadata(props, 'ns.')
        expect(props[name]).toEqual(value)
    })

    it.each([
        ['a non-posthog key', 'ns.org_id', 'org_id'],
        ['a reserved $-prefixed name', 'ns.posthog_$ai_model', '$ai_model'],
        ['the reserved distinct_id name', 'ns.posthog_distinct_id', 'distinct_id'],
    ])('does not promote %s', (_label, key, resultName) => {
        const props: Record<string, unknown> = { [key]: 'value' }
        promotePosthogCustomMetadata(props, 'ns.')
        expect(props[resultName]).toBeUndefined()
    })

    it('does not overwrite an existing property', () => {
        const props: Record<string, unknown> = { tags: ['keep'], 'ns.posthog_tags': ['drop'] }
        promotePosthogCustomMetadata(props, 'ns.')
        expect(props['tags']).toEqual(['keep'])
    })

    it('skips __proto__ instead of polluting the prototype', () => {
        const props: Record<string, unknown> = { 'ns.posthog___proto__': { polluted: true } }
        promotePosthogCustomMetadata(props, 'ns.')
        expect(Object.getPrototypeOf(props)).toBe(Object.prototype)
    })
})