Files
zeekay e43ab1afe7 chore: rename all PostHog references to Insights/Hanzo
Bulk rename across 989 files:
- Package names: posthog-js → @hanzo/insights, posthog-node → @hanzo/insights-node, etc.
- Repository URLs: posthog/posthog-js → hanzoai/insights-js
- Brand names: PostHog → Insights
- File renames: 259 reference JSON files, source files, workflow files
- Directory rename: eslint-plugin-posthog-js → eslint-plugin-insights

Remaining: 8 refs to @posthog/rrweb-* (real npm deps, need fork+publish)
2026-03-06 01:41:12 -08:00

410 lines
16 KiB
TypeScript

import '../src/__tests__/helpers/mock-logger'
import { createInsightsInstance } from '../src/__tests__/helpers/insights-instance'
import { waitFor } from '@testing-library/dom'
import { getRequests, resetRequests } from './mock-server'
import { uuidv7 } from '../src/uuidv7'
async function shortWait() {
// no need to worry about ie11 compat in tests
// eslint-disable-next-line compat/compat
await new Promise<void>((resolve: () => void) => setTimeout(resolve, 500))
}
describe('FunctionalTests / Feature Flags', () => {
let token: string
beforeEach(() => {
token = uuidv7()
})
test('person properties set in identify() with new distinct_id are sent to /flags', async () => {
const insights = await createInsightsInstance(token, { advanced_disable_flags: false, before_send: (cr) => cr })
const anonymousId = insights.get_distinct_id()
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
// This is the initial call to the flags endpoint on Insights init.
{
$device_id: anonymousId,
distinct_id: anonymousId,
person_properties: expect.any(Object),
groups: {},
timezone: expect.any(String),
token,
},
])
})
resetRequests(token)
// wait for flags callback
await shortWait()
// Person properties set here should also be sent to the flags endpoint.
insights.identify('test-id', {
email: '[email protected]',
})
await shortWait()
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
// Then we have another flags call triggered by the call to
// `identify()`.
{
$anon_distinct_id: anonymousId,
$device_id: anonymousId,
distinct_id: 'test-id',
person_properties: {
$initial__kx: null,
$initial_current_url: 'http://localhost/',
$initial_dclid: null,
$initial_epik: null,
$initial_fbclid: null,
$initial_gad_source: null,
$initial_gbraid: null,
$initial_gclid: null,
$initial_gclsrc: null,
$initial_host: 'localhost',
$initial_igshid: null,
$initial_irclid: null,
$initial_li_fat_id: null,
$initial_mc_cid: null,
$initial_msclkid: null,
$initial_pathname: '/',
$initial_qclid: null,
$initial_rdt_cid: null,
$initial_referrer: '$direct',
$initial_referring_domain: '$direct',
$initial_sccid: null,
$initial_ttclid: null,
$initial_twclid: null,
$initial_utm_campaign: null,
$initial_utm_content: null,
$initial_utm_medium: null,
$initial_utm_source: null,
$initial_utm_term: null,
$initial_wbraid: null,
email: '[email protected]',
},
groups: {},
timezone: expect.any(String),
token,
},
])
})
})
test('person properties set in identify() with the same distinct_id are sent to flags', async () => {
const insights = await createInsightsInstance(token, { advanced_disable_flags: false, before_send: (cr) => cr })
const anonymousId = insights.get_distinct_id()
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
// This is the initial call to the flags endpoint on Insights init.
{
$device_id: anonymousId,
distinct_id: anonymousId,
person_properties: expect.any(Object),
groups: {},
timezone: expect.any(String),
token,
},
])
})
resetRequests(token)
// wait for flags callback
await shortWait()
// First we identify with a new distinct_id but with no properties set
insights.identify('test-id')
// By this point we should have already called `/flags/` twice.
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
// Then we have another flags call triggered by the first call to
// `identify()`.
{
$anon_distinct_id: anonymousId,
$device_id: anonymousId,
distinct_id: 'test-id',
groups: {},
person_properties: {
$initial__kx: null,
$initial_current_url: 'http://localhost/',
$initial_dclid: null,
$initial_epik: null,
$initial_fbclid: null,
$initial_gad_source: null,
$initial_gbraid: null,
$initial_gclid: null,
$initial_gclsrc: null,
$initial_host: 'localhost',
$initial_igshid: null,
$initial_irclid: null,
$initial_li_fat_id: null,
$initial_mc_cid: null,
$initial_msclkid: null,
$initial_pathname: '/',
$initial_qclid: null,
$initial_rdt_cid: null,
$initial_referrer: '$direct',
$initial_referring_domain: '$direct',
$initial_sccid: null,
$initial_ttclid: null,
$initial_twclid: null,
$initial_utm_campaign: null,
$initial_utm_content: null,
$initial_utm_medium: null,
$initial_utm_source: null,
$initial_utm_term: null,
$initial_wbraid: null,
},
timezone: expect.any(String),
token,
},
])
})
resetRequests(token)
// Then we identify again, but with the same distinct_id and with some
// properties set.
insights.identify('test-id', { email: '[email protected]' })
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
{
$device_id: anonymousId,
distinct_id: 'test-id',
groups: {},
person_properties: {
$initial__kx: null,
$initial_current_url: 'http://localhost/',
$initial_dclid: null,
$initial_epik: null,
$initial_fbclid: null,
$initial_gad_source: null,
$initial_gbraid: null,
$initial_gclid: null,
$initial_gclsrc: null,
$initial_host: 'localhost',
$initial_igshid: null,
$initial_irclid: null,
$initial_li_fat_id: null,
$initial_mc_cid: null,
$initial_msclkid: null,
$initial_pathname: '/',
$initial_qclid: null,
$initial_rdt_cid: null,
$initial_referrer: '$direct',
$initial_referring_domain: '$direct',
$initial_sccid: null,
$initial_ttclid: null,
$initial_twclid: null,
$initial_utm_campaign: null,
$initial_utm_content: null,
$initial_utm_medium: null,
$initial_utm_source: null,
$initial_utm_term: null,
$initial_wbraid: null,
email: '[email protected]',
},
timezone: expect.any(String),
token,
},
])
})
})
test('identify() triggers new request in queue after first request', async () => {
const insights = await createInsightsInstance(token, { advanced_disable_flags: false, before_send: (cr) => cr })
const anonymousId = insights.get_distinct_id()
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
// This is the initial call to the flags endpoint on Insights init.
{
$device_id: anonymousId,
distinct_id: anonymousId,
person_properties: expect.any(Object),
groups: {},
timezone: expect.any(String),
token,
},
])
})
resetRequests(token)
// don't wait for flags callback
insights.identify('test-id', {
email: '[email protected]',
})
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([])
})
// wait for flags callback
await shortWait()
// now second call should've fired
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
{
$anon_distinct_id: anonymousId,
$device_id: anonymousId,
distinct_id: 'test-id',
groups: {},
person_properties: {
$initial__kx: null,
$initial_current_url: 'http://localhost/',
$initial_dclid: null,
$initial_epik: null,
$initial_fbclid: null,
$initial_gad_source: null,
$initial_gbraid: null,
$initial_gclid: null,
$initial_gclsrc: null,
$initial_host: 'localhost',
$initial_igshid: null,
$initial_irclid: null,
$initial_li_fat_id: null,
$initial_mc_cid: null,
$initial_msclkid: null,
$initial_pathname: '/',
$initial_qclid: null,
$initial_rdt_cid: null,
$initial_referrer: '$direct',
$initial_referring_domain: '$direct',
$initial_sccid: null,
$initial_ttclid: null,
$initial_twclid: null,
$initial_utm_campaign: null,
$initial_utm_content: null,
$initial_utm_medium: null,
$initial_utm_source: null,
$initial_utm_term: null,
$initial_wbraid: null,
email: '[email protected]',
},
timezone: expect.any(String),
token,
},
])
})
})
test('identify() does not trigger new request in queue after first request for loaded callback', async () => {
await createInsightsInstance(token, {
advanced_disable_flags: false,
bootstrap: { distinctID: 'anon-id' },
before_send: (cr) => cr,
loaded: (ph) => {
ph.identify('test-id', { email: '[email protected]' })
ph.group('playlist', 'id:77', { length: 8 })
},
})
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
// This is the initial call to the flags endpoint on Insights init, with all info added from `loaded`.
{
$anon_distinct_id: 'anon-id',
$device_id: 'anon-id',
distinct_id: 'test-id',
groups: { playlist: 'id:77' },
person_properties: {
$initial__kx: null,
$initial_current_url: 'http://localhost/',
$initial_dclid: null,
$initial_epik: null,
$initial_fbclid: null,
$initial_gad_source: null,
$initial_gbraid: null,
$initial_gclid: null,
$initial_gclsrc: null,
$initial_host: 'localhost',
$initial_igshid: null,
$initial_irclid: null,
$initial_li_fat_id: null,
$initial_mc_cid: null,
$initial_msclkid: null,
$initial_pathname: '/',
$initial_qclid: null,
$initial_rdt_cid: null,
$initial_referrer: '$direct',
$initial_referring_domain: '$direct',
$initial_sccid: null,
$initial_ttclid: null,
$initial_twclid: null,
$initial_utm_campaign: null,
$initial_utm_content: null,
$initial_utm_medium: null,
$initial_utm_source: null,
$initial_utm_term: null,
$initial_wbraid: null,
email: '[email protected]',
},
group_properties: {
playlist: {
length: 8,
},
},
timezone: expect.any(String),
token,
},
])
})
})
})
describe('feature flags v2', () => {
let token: string
beforeEach(() => {
token = uuidv7()
})
it('should call flags endpoint when eligible', async () => {
const insights = await createInsightsInstance(token, {
__preview_flags_v2: true,
advanced_disable_flags: false,
before_send: (cr) => cr,
})
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
expect.objectContaining({
token,
distinct_id: insights.get_distinct_id(),
}),
])
})
})
it('should call flags endpoint when not eligible', async () => {
const insights = await createInsightsInstance(token, {
__preview_flags_v2: false,
advanced_disable_flags: false,
before_send: (cr) => cr,
})
await waitFor(() => {
expect(getRequests(token)['/flags/']).toEqual([
expect.objectContaining({
token,
distinct_id: insights.get_distinct_id(),
}),
])
})
})
})