import assert from "node:assert/strict"; import { mock, test } from "node:test"; import { captureAccountEvent } from "../analytics.ts"; const config = { POSTHOG_ENABLED: "true", POSTHOG_PROJECT_TOKEN: "project-token", POSTHOG_HOST: "https://us.i.posthog.com", }; function context() { const pending: Promise[] = []; return { pending, waitUntil: (promise: Promise) => pending.push(promise) }; } test("delivery uses stable identity and an explicit property allowlist", async (t) => { let request: { url: string; init: RequestInit } | undefined; t.mock.method(globalThis, "fetch", async (url: string, init: RequestInit) => { request = { url, init }; return new Response("ok"); }); const ctx = context(); const event = { name: "account.signed_up", userId: "user-id", entryPath: "waitlist", email: "private@example.test", password: "secret" } as const; captureAccountEvent(config, ctx, event); assert.equal(ctx.pending.length, 1); await Promise.all(ctx.pending); assert.equal(request?.url, "https://us.i.posthog.com/i/v0/e/"); assert.equal(request.init.method, "POST"); assert.equal(request.init.redirect, "manual"); assert.ok(request.init.signal instanceof AbortSignal); const payload = JSON.parse(String(request.init.body)); assert.equal(payload.api_key, "project-token"); assert.equal(payload.distinct_id, "human:user-id"); assert.equal(payload.event, "account.signed_up"); assert.match(payload.uuid, /^[0-9a-f-]{36}$/); assert.ok(Number.isFinite(Date.parse(payload.timestamp))); assert.deepEqual(payload.properties, { schema_version: 1, subject_kind: "human", is_user_activity: true, $geoip_disable: true, entry_path: "waitlist" }); assert.doesNotMatch(String(request.init.body), /private@example|secret|password|email/); }); test("approval is not attributed as activity by the account holder", async (t) => { const payloads: { properties: object }[] = []; t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { payloads.push(JSON.parse(String(init.body))); return new Response(null, { status: 204 }); }); const ctx = context(); captureAccountEvent(config, ctx, { name: "account.approved", userId: "user-id", approvalPath: "waitlist" }); await Promise.all(ctx.pending); assert.deepEqual(payloads[0].properties, { schema_version: 1, subject_kind: "human", is_user_activity: false, $geoip_disable: true, approval_path: "waitlist" }); }); test("disabled, incomplete, or untrusted configuration sends nothing", (t) => { const fetch = t.mock.method(globalThis, "fetch", mock.fn()); for (const settings of [ {}, { ...config, POSTHOG_ENABLED: "false" }, { ...config, POSTHOG_PROJECT_TOKEN: "" }, { ...config, POSTHOG_HOST: "" }, { ...config, POSTHOG_HOST: "https://untrusted.test" }, ]) { const ctx = context(); captureAccountEvent(settings, ctx, { name: "account.signed_in", userId: "user-id" }); assert.equal(ctx.pending.length, 0); } captureAccountEvent(config, context(), { name: "account.signed_in", userId: "" }); assert.equal(fetch.mock.callCount(), 0); }); test("delivery does not wait for PostHog before returning", async (t) => { let finish: ((response: Response) => void) | undefined; t.mock.method(globalThis, "fetch", () => new Promise((resolve) => { finish = resolve; })); const ctx = context(); assert.equal(captureAccountEvent(config, ctx, { name: "account.signed_in", userId: "user-id" }), undefined); assert.equal(ctx.pending.length, 1); assert.ok(finish); finish(new Response(null, { status: 204 })); await Promise.all(ctx.pending); }); test("network failures are contained and do not log private details", async (t) => { t.mock.method(globalThis, "fetch", async () => { throw new Error("private token and URL"); }); const warning = t.mock.method(console, "warn", mock.fn()); const ctx = context(); captureAccountEvent(config, ctx, { name: "account.signed_in", userId: "user-id" }); await assert.doesNotReject(Promise.all(ctx.pending)); assert.deepEqual(warning.mock.calls[0].arguments, ["posthog_delivery_failed", { reason: "transport" }]); }); test("HTTP failures are contained without logging response bodies", async (t) => { t.mock.method(globalThis, "fetch", async () => new Response("private response", { status: 503 })); const warning = t.mock.method(console, "warn", mock.fn()); const ctx = context(); captureAccountEvent(config, ctx, { name: "account.signed_in", userId: "user-id" }); await assert.doesNotReject(Promise.all(ctx.pending)); assert.deepEqual(warning.mock.calls[0].arguments, ["posthog_delivery_failed", { status: 503 }]); }); test("EU delivery is supported and separate captures have separate identifiers", async (t) => { const identifiers: string[] = []; t.mock.method(globalThis, "fetch", async (url: string, init: RequestInit) => { assert.equal(url, "https://eu.i.posthog.com/i/v0/e/"); identifiers.push(JSON.parse(String(init.body)).uuid); return new Response(null, { status: 204 }); }); const ctx = context(); for (let index = 0; index < 2; index++) captureAccountEvent({ ...config, POSTHOG_HOST: "https://eu.i.posthog.com" }, ctx, { name: "account.signed_in", userId: "user-id" }); await Promise.all(ctx.pending); assert.equal(new Set(identifiers).size, 2); });