mirror of
https://github.com/hanzoai/insights-js.git
synced 2026-10-30 18:34:19 +00:00
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.
134 lines
4.5 KiB
TypeScript
134 lines
4.5 KiB
TypeScript
import { describe, it, expect, vi } from 'vitest';
|
|
import { compare } from 'compare-versions';
|
|
import { RRDocument } from '../src/document-nodejs';
|
|
import {
|
|
polyfillPerformance,
|
|
polyfillRAF,
|
|
polyfillEvent,
|
|
polyfillNode,
|
|
polyfillDocument,
|
|
} from '../src/polyfill';
|
|
import { performance as nativePerformance } from 'perf_hooks';
|
|
import { BaseRRNode } from '@hanzo/rrdom';
|
|
|
|
describe('polyfill for nodejs', () => {
|
|
it('should polyfill performance api', () => {
|
|
if (compare(process.version, 'v16.0.0', '<'))
|
|
expect(global.performance).toBeUndefined();
|
|
polyfillPerformance();
|
|
expect(global.performance).toBeDefined();
|
|
expect(performance).toBeDefined();
|
|
expect(performance.now).toBeDefined();
|
|
expect(performance.now()).toBeCloseTo(nativePerformance.now(), 1e-10);
|
|
});
|
|
|
|
it('should not polyfill performance if it already exists', () => {
|
|
if (compare(process.version, 'v16.0.0', '>=')) {
|
|
const originalPerformance = global.performance;
|
|
polyfillPerformance();
|
|
expect(global.performance).toBe(originalPerformance);
|
|
}
|
|
const fakePerformance = vi.fn() as unknown as Performance;
|
|
global.performance = fakePerformance;
|
|
polyfillPerformance();
|
|
expect(global.performance).toEqual(fakePerformance);
|
|
});
|
|
|
|
it('should polyfill requestAnimationFrame', () => {
|
|
expect(global.requestAnimationFrame).toBeUndefined();
|
|
expect(global.cancelAnimationFrame).toBeUndefined();
|
|
polyfillRAF();
|
|
expect(global.requestAnimationFrame).toBeDefined();
|
|
expect(global.cancelAnimationFrame).toBeDefined();
|
|
expect(requestAnimationFrame).toBeDefined();
|
|
expect(cancelAnimationFrame).toBeDefined();
|
|
|
|
vi.useFakeTimers();
|
|
const AnimationTime = 1_000; // target animation time(unit: ms)
|
|
const startTime = Date.now();
|
|
let frameCount = 0;
|
|
const rafCallback1 = () => {
|
|
const currentTime = Date.now();
|
|
frameCount++;
|
|
if (currentTime - startTime < AnimationTime) {
|
|
requestAnimationFrame(rafCallback1);
|
|
} else {
|
|
expect(frameCount).toBeGreaterThanOrEqual(55);
|
|
expect(frameCount).toBeLessThanOrEqual(65);
|
|
}
|
|
};
|
|
requestAnimationFrame(rafCallback1);
|
|
// Fast-forward until all timers have been executed
|
|
vi.runAllTimers();
|
|
|
|
let rafHandle;
|
|
const rafCallback2 = () => {
|
|
rafHandle = requestAnimationFrame(rafCallback2);
|
|
};
|
|
rafHandle = requestAnimationFrame(rafCallback2);
|
|
|
|
// If this function doesn't work, recursive function will never end.
|
|
cancelAnimationFrame(rafHandle);
|
|
vi.runAllTimers();
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
it('should not polyfill requestAnimationFrame if it already exists', () => {
|
|
const fakeRequestAnimationFrame =
|
|
vi.fn() as unknown as typeof global.requestAnimationFrame;
|
|
global.requestAnimationFrame = fakeRequestAnimationFrame;
|
|
const fakeCancelAnimationFrame =
|
|
vi.fn() as unknown as typeof global.cancelAnimationFrame;
|
|
global.cancelAnimationFrame = fakeCancelAnimationFrame;
|
|
polyfillRAF();
|
|
expect(global.requestAnimationFrame).toBe(fakeRequestAnimationFrame);
|
|
expect(global.cancelAnimationFrame).toBe(fakeCancelAnimationFrame);
|
|
});
|
|
|
|
it('should polyfill Event type', () => {
|
|
// if the second version is greater
|
|
if (compare(process.version, 'v15.0.0', '<'))
|
|
expect(global.Event).toBeUndefined();
|
|
polyfillEvent();
|
|
expect(global.Event).toBeDefined();
|
|
expect(Event).toBeDefined();
|
|
});
|
|
|
|
it('should not polyfill Event type if it already exists', () => {
|
|
const fakeEvent = vi.fn() as unknown as typeof global.Event;
|
|
global.Event = fakeEvent;
|
|
polyfillEvent();
|
|
expect(global.Event).toBe(fakeEvent);
|
|
});
|
|
|
|
it('should polyfill Node type', () => {
|
|
expect(global.Node).toBeUndefined();
|
|
polyfillNode();
|
|
expect(global.Node).toBeDefined();
|
|
expect(Node).toBeDefined();
|
|
expect(Node).toEqual(BaseRRNode);
|
|
});
|
|
|
|
it('should not polyfill Node type if it already exists', () => {
|
|
const fakeNode = vi.fn() as unknown as typeof global.Node;
|
|
global.Node = fakeNode;
|
|
polyfillNode();
|
|
expect(global.Node).toBe(fakeNode);
|
|
});
|
|
|
|
it('should polyfill document object', () => {
|
|
expect(global.document).toBeUndefined();
|
|
polyfillDocument();
|
|
expect(global.document).toBeDefined();
|
|
expect(document).toBeDefined();
|
|
expect(document).toBeInstanceOf(RRDocument);
|
|
});
|
|
|
|
it('should not polyfill document object if it already exists', () => {
|
|
const fakeDocument = vi.fn() as unknown as typeof global.document;
|
|
global.document = fakeDocument;
|
|
polyfillDocument();
|
|
expect(global.document).toBe(fakeDocument);
|
|
});
|
|
});
|