Files
insights-js/packages/rrweb/all/test/cross-origin-iframe-packer.test.ts
zooqueenandzeekay 4712ee9d98 chore: complete posthog->insights rebrand (zero @posthog/* identity)
Decomplect the half-finished fork so the dependency graph has ONE source
with no posthog package identity, living under github.com/hanzoinsights.

LAYER 1 (package identity): rename straggler package.json names —
@posthog/next->@hanzo/insights-next (Next.js SDK; distinct from the existing
@hanzo/insights-nextjs config helper), @posthog/mcp->@hanzo/insights-mcp,
@posthog/plugin-utils->@hanzo/insights-plugin-utils,
@posthog/react-slim->@hanzo/react-slim, @posthog-tooling/release->
@hanzo/insights-tooling-release, and the @posthog/rrweb* family->@hanzo/rrweb*
(matching the already-published @hanzo/rrweb*@0.0.47 + minimumReleaseAgeExclude).
Repoint every repository.url/homepage/bugs to github.com/hanzoinsights/insights-js
and rebrand the PostHog author/description identity to Hanzo AI.

LAYER 2 (cross-package imports): rewrite every internal @posthog/* import to its
@hanzo/insights*|@hanzo/rrweb* equivalent and the bare module specifiers
posthog-js->@hanzo/insights, posthog-node->@hanzo/insights-node, mirrored in
package.json deps + the pnpm-lock importer keys. Result: 0 @posthog/* specifiers
in packages/tooling/examples/playground source.

LAYER 4 (wire protocol) PRESERVED: window.posthog global, ph_* keys,
$-prefixed event names, distinct_id, /decide, /e/, /flags, and the
__PosthogExtensions__ hook are left unchanged — the Insights server expects them.

Also repair pre-existing fork merge damage that blocked compilation (unrelated to
naming; identical to origin/main): restore the @hanzo/insights-core ./process
subpath (spawn-local/utils, re-matching the published artifact), the lost
constructor/var declarations in core + node clients, the mangled
external-scripts-loader, the missing TreeShakeable/isPlainError imports, the
DeviceId enum member, and stale ../posthog-core-stateless module paths. 26/34
turbo build tasks now pass; remaining failures (browser/react-native and their
dependents) are pre-existing fork damage, not rebrand-caused.
2026-07-16 09:09:17 -07:00

160 lines
3.9 KiB
TypeScript

import {
describe,
it,
vi,
beforeAll,
beforeEach,
afterEach,
afterAll,
} from 'vitest';
import type {
eventWithTime,
listenerHandler,
mutationData,
} from '@hanzo/rrweb-types';
import { unpack } from '@hanzo/rrweb-packer';
import * as fs from 'fs';
import * as path from 'path';
import type * as puppeteer from 'puppeteer';
import type { recordOptions } from '@hanzo/rrweb';
import type {} from '@hanzo/rrweb-types';
import { EventType } from '@hanzo/rrweb-types';
import {
assertSnapshot,
getServerURL,
launchPuppeteer,
startServer,
waitForRAF,
waitForIFrameLoad,
} from './utils';
import type * as http from 'http';
interface ISuite {
code: string;
browser: puppeteer.Browser;
page: puppeteer.Page;
events: eventWithTime[];
server: http.Server;
serverURL: string;
}
interface IWindow extends Window {
rrweb: {
record: (
options: recordOptions<eventWithTime>,
) => listenerHandler | undefined;
addCustomEvent<T>(tag: string, payload: T): void;
pack: (e: eventWithTime) => string;
};
emit: (e: eventWithTime) => undefined;
snapshots: eventWithTime[];
}
type ExtraOptions = {
usePackFn?: boolean;
};
async function injectRecordScript(
frame: puppeteer.Frame,
options?: ExtraOptions,
) {
await frame.addScriptTag({
path: path.resolve(__dirname, '../dist/rrweb-all.umd.cjs'),
});
options = options || {};
await frame.evaluate((options) => {
(window as unknown as IWindow).snapshots = [];
const { record, pack } = (window as unknown as IWindow).rrweb;
const config: recordOptions<eventWithTime> = {
recordCrossOriginIframes: true,
recordCanvas: true,
emit(event) {
(window as unknown as IWindow).snapshots.push(event);
(window as unknown as IWindow).emit(event);
},
};
if (options.usePackFn) {
config.packFn = pack;
}
record(config);
}, options);
for (const child of frame.childFrames()) {
await injectRecordScript(child, options);
}
}
const setup = function (
this: ISuite,
content: string,
options?: ExtraOptions,
): ISuite {
const ctx = {} as ISuite;
beforeAll(async () => {
ctx.browser = await launchPuppeteer();
ctx.server = await startServer();
ctx.serverURL = getServerURL(ctx.server);
});
beforeEach(async () => {
ctx.page = await ctx.browser.newPage();
await ctx.page.goto('about:blank');
await ctx.page.setContent(
content.replace(/\{SERVER_URL\}/g, ctx.serverURL),
);
ctx.events = [];
await ctx.page.exposeFunction('emit', (e: eventWithTime) => {
if (e.type === EventType.DomContentLoaded || e.type === EventType.Load) {
return;
}
ctx.events.push(e);
});
ctx.page.on('console', (msg) => console.log('PAGE LOG:', msg.text()));
await waitForIFrameLoad(ctx.page, 'iframe');
await injectRecordScript(ctx.page.mainFrame(), options);
});
afterEach(async () => {
await ctx.page.close();
});
afterAll(async () => {
await ctx.browser?.close();
ctx.server?.close();
});
return ctx;
};
describe('cross origin iframes & packer', function (this: ISuite) {
vi.setConfig({ testTimeout: 100_000 });
describe('blank.html', function (this: ISuite) {
const content = `
<!DOCTYPE html>
<html>
<body>
<iframe src="{SERVER_URL}/html/blank.html"></iframe>
</body>
</html>
`;
const ctx = setup.call(this, content, { usePackFn: true });
describe('should support packFn option in record()', () => {
it('', async () => {
const frame = ctx.page.mainFrame().childFrames()[0];
await waitForRAF(frame);
await ctx.page.waitForTimeout(100);
const packedSnapshots = (await ctx.page.evaluate(
'window.snapshots',
)) as string[];
const unpackedSnapshots = packedSnapshots.map((packed) =>
unpack(packed),
) as eventWithTime[];
await assertSnapshot(unpackedSnapshots);
});
});
});
});