mirror of
https://github.com/hanzoai/insights-js.git
synced 2026-11-03 00:53:53 +00:00
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)
127 lines
5.1 KiB
TypeScript
127 lines
5.1 KiB
TypeScript
import { expect, test, WindowWithInsights } from '../utils/insights-playwright-test-base'
|
|
import { start } from '../utils/setup'
|
|
import { BrowserContext, Page } from '@playwright/test'
|
|
import { InsightsConfig } from '@/types'
|
|
import { assertThatRecordingStarted, pollUntilEventCaptured } from '../utils/event-capture-utils'
|
|
|
|
async function startWith(config: Partial<InsightsConfig>, page: Page, context: BrowserContext) {
|
|
// there will be a flags call
|
|
const flagsResponse = page.waitForResponse('**/flags/*')
|
|
|
|
await start(
|
|
{
|
|
options: config,
|
|
flagsResponseOverrides: {
|
|
sessionRecording: {
|
|
endpoint: '/ses/',
|
|
networkPayloadCapture: { recordBody: true, recordHeaders: true },
|
|
},
|
|
capturePerformance: true,
|
|
autocapture_opt_out: true,
|
|
},
|
|
url: './playground/cypress/index.html',
|
|
},
|
|
page,
|
|
context
|
|
)
|
|
|
|
// there will be a flags call
|
|
await flagsResponse
|
|
}
|
|
|
|
test.describe('Session Recording - opting out', () => {
|
|
test('does not capture events when config opts out by default', async ({ page, context }) => {
|
|
// but no recorder or snapshot call, because we're opting out
|
|
void expect(page.waitForResponse('**/*recorder.js*', { timeout: 250 })).rejects.toThrowError('Timeout')
|
|
void expect(page.waitForResponse('**/ses/*', { timeout: 250 })).rejects.toThrowError('Timeout')
|
|
await startWith({ opt_out_capturing_by_default: true }, page, context)
|
|
|
|
await page.locator('[data-cy-input]').type('hello insights!')
|
|
await page.waitForTimeout(250) // short delay since there's no snapshot to wait for
|
|
await page.expectCapturedEventsToBe([])
|
|
})
|
|
|
|
test('does not capture recordings when config disables session recording', async ({ page, context }) => {
|
|
// but no recorder or snapshot call, because we're opting out
|
|
void expect(page.waitForResponse('**/*recorder.js*', { timeout: 250 })).rejects.toThrowError('Timeout')
|
|
void expect(page.waitForResponse('**/ses/*', { timeout: 250 })).rejects.toThrowError('Timeout')
|
|
|
|
await startWith({ disable_session_recording: true }, page, context)
|
|
|
|
await page.locator('[data-cy-input]').type('hello insights!')
|
|
await page.waitForTimeout(250) // short delay since there's no snapshot to wait for
|
|
await page.expectCapturedEventsToBe(['$pageview'])
|
|
})
|
|
|
|
test('can start recording after starting opted out', async ({ page, context }) => {
|
|
await startWith({ opt_out_capturing_by_default: true }, page, context)
|
|
|
|
await page.waitingForNetworkCausedBy({
|
|
urlPatternsToWaitFor: ['**/*recorder.js*'],
|
|
action: async () => {
|
|
await page.evaluate(() => {
|
|
const ph = (window as WindowWithInsights).insights
|
|
ph?.opt_in_capturing()
|
|
ph?.startSessionRecording()
|
|
})
|
|
},
|
|
})
|
|
|
|
await page.expectCapturedEventsToBe(['$opt_in', '$pageview'])
|
|
|
|
await page.resetCapturedEvents()
|
|
|
|
await page.locator('[data-cy-input]').type('hello insights!')
|
|
await pollUntilEventCaptured(page, '$snapshot')
|
|
await assertThatRecordingStarted(page)
|
|
})
|
|
|
|
test('can start recording when starting disabled', async ({ page, context }) => {
|
|
await startWith({ disable_session_recording: true }, page, context)
|
|
|
|
await page.waitingForNetworkCausedBy({
|
|
urlPatternsToWaitFor: ['**/*recorder.js*'],
|
|
action: async () => {
|
|
await page.resetCapturedEvents()
|
|
await page.evaluate(() => {
|
|
const ph = (window as WindowWithInsights).insights
|
|
ph?.startSessionRecording()
|
|
})
|
|
},
|
|
})
|
|
|
|
await page.locator('[data-cy-input]').type('hello insights!')
|
|
await pollUntilEventCaptured(page, '$snapshot')
|
|
await assertThatRecordingStarted(page)
|
|
})
|
|
|
|
test('does not capture session recordings when flags is disabled', async ({ page, context }) => {
|
|
await start(
|
|
{ options: { advanced_disable_flags: true, autocapture: false }, waitForFlags: false },
|
|
page,
|
|
context
|
|
)
|
|
|
|
await page.locator('[data-cy-custom-event-button]').click()
|
|
|
|
const callsToSessionRecording = page.waitForResponse('**/ses/').catch(() => {
|
|
// when the test ends, waitForResponse will throw an error
|
|
// we're happy not to get a response here so we can swallow it
|
|
return null
|
|
})
|
|
|
|
await page.locator('[data-cy-input]').type('hello insights!')
|
|
|
|
void callsToSessionRecording.then((response) => {
|
|
if (response) {
|
|
throw new Error('Session recording call was made and should not have been')
|
|
}
|
|
})
|
|
await page.waitForTimeout(200)
|
|
|
|
const capturedEvents = await page.capturedEvents()
|
|
// no snapshot events sent
|
|
expect(capturedEvents.map((x) => x.event)).toEqual(['$pageview', 'custom-event'])
|
|
})
|
|
})
|