110 lines · 5.2 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { mock, test } from "node:test"; |
| 3 | import { captureAccountEvent } from "../analytics.ts"; |
| 4 | |
| 5 | const config = { |
| 6 | POSTHOG_ENABLED: "true", |
| 7 | POSTHOG_PROJECT_TOKEN: "project-token", |
| 8 | POSTHOG_HOST: "https://us.i.posthog.com", |
| 9 | }; |
| 10 | |
| 11 | function context() { |
| 12 | const pending: Promise<unknown>[] = []; |
| 13 | return { pending, waitUntil: (promise: Promise<unknown>) => pending.push(promise) }; |
| 14 | } |
| 15 | |
| 16 | test("delivery uses stable identity and an explicit property allowlist", async (t) => { |
| 17 | let request: { url: string; init: RequestInit } | undefined; |
| 18 | t.mock.method(globalThis, "fetch", async (url: string, init: RequestInit) => { |
| 19 | request = { url, init }; |
| 20 | return new Response("ok"); |
| 21 | }); |
| 22 | const ctx = context(); |
| 23 | const event = { name: "account.signed_up", userId: "user-id", entryPath: "open", email: "private@example.test", password: "secret" } as const; |
| 24 | captureAccountEvent(config, ctx, event); |
| 25 | assert.equal(ctx.pending.length, 1); |
| 26 | await Promise.all(ctx.pending); |
| 27 | assert.equal(request?.url, "https://us.i.posthog.com/i/v0/e/"); |
| 28 | assert.equal(request.init.method, "POST"); |
| 29 | assert.equal(request.init.redirect, "manual"); |
| 30 | assert.ok(request.init.signal instanceof AbortSignal); |
| 31 | const payload = JSON.parse(String(request.init.body)); |
| 32 | assert.equal(payload.api_key, "project-token"); |
| 33 | assert.equal(payload.distinct_id, "human:user-id"); |
| 34 | assert.equal(payload.event, "account.signed_up"); |
| 35 | assert.match(payload.uuid, /^[0-9a-f-]{36}$/); |
| 36 | assert.ok(Number.isFinite(Date.parse(payload.timestamp))); |
| 37 | assert.deepEqual(payload.properties, { schema_version: 1, subject_kind: "human", is_user_activity: true, $geoip_disable: true, entry_path: "open" }); |
| 38 | assert.doesNotMatch(String(request.init.body), /private@example|secret|password|email/); |
| 39 | }); |
| 40 | |
| 41 | test("approval is not attributed as activity by the account holder", async (t) => { |
| 42 | const payloads: { properties: object }[] = []; |
| 43 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 44 | payloads.push(JSON.parse(String(init.body))); |
| 45 | return new Response(null, { status: 204 }); |
| 46 | }); |
| 47 | const ctx = context(); |
| 48 | captureAccountEvent(config, ctx, { name: "account.approved", userId: "user-id", approvalPath: "open" }); |
| 49 | await Promise.all(ctx.pending); |
| 50 | assert.deepEqual(payloads[0].properties, { schema_version: 1, subject_kind: "human", is_user_activity: false, $geoip_disable: true, approval_path: "open" }); |
| 51 | }); |
| 52 | |
| 53 | test("disabled, incomplete, or untrusted configuration sends nothing", (t) => { |
| 54 | const fetch = t.mock.method(globalThis, "fetch", mock.fn()); |
| 55 | for (const settings of [ |
| 56 | {}, |
| 57 | { ...config, POSTHOG_ENABLED: "false" }, |
| 58 | { ...config, POSTHOG_PROJECT_TOKEN: "" }, |
| 59 | { ...config, POSTHOG_HOST: "" }, |
| 60 | { ...config, POSTHOG_HOST: "https://untrusted.test" }, |
| 61 | ]) { |
| 62 | const ctx = context(); |
| 63 | captureAccountEvent(settings, ctx, { name: "account.signed_in", userId: "user-id" }); |
| 64 | assert.equal(ctx.pending.length, 0); |
| 65 | } |
| 66 | captureAccountEvent(config, context(), { name: "account.signed_in", userId: "" }); |
| 67 | assert.equal(fetch.mock.callCount(), 0); |
| 68 | }); |
| 69 | |
| 70 | test("delivery does not wait for PostHog before returning", async (t) => { |
| 71 | let finish: ((response: Response) => void) | undefined; |
| 72 | t.mock.method(globalThis, "fetch", () => new Promise<Response>((resolve) => { finish = resolve; })); |
| 73 | const ctx = context(); |
| 74 | assert.equal(captureAccountEvent(config, ctx, { name: "account.signed_in", userId: "user-id" }), undefined); |
| 75 | assert.equal(ctx.pending.length, 1); |
| 76 | assert.ok(finish); |
| 77 | finish(new Response(null, { status: 204 })); |
| 78 | await Promise.all(ctx.pending); |
| 79 | }); |
| 80 | |
| 81 | test("network failures are contained and do not log private details", async (t) => { |
| 82 | t.mock.method(globalThis, "fetch", async () => { throw new Error("private token and URL"); }); |
| 83 | const warning = t.mock.method(console, "warn", mock.fn()); |
| 84 | const ctx = context(); |
| 85 | captureAccountEvent(config, ctx, { name: "account.signed_in", userId: "user-id" }); |
| 86 | await assert.doesNotReject(Promise.all(ctx.pending)); |
| 87 | assert.deepEqual(warning.mock.calls[0].arguments, ["posthog_delivery_failed", { reason: "transport" }]); |
| 88 | }); |
| 89 | |
| 90 | test("HTTP failures are contained without logging response bodies", async (t) => { |
| 91 | t.mock.method(globalThis, "fetch", async () => new Response("private response", { status: 503 })); |
| 92 | const warning = t.mock.method(console, "warn", mock.fn()); |
| 93 | const ctx = context(); |
| 94 | captureAccountEvent(config, ctx, { name: "account.signed_in", userId: "user-id" }); |
| 95 | await assert.doesNotReject(Promise.all(ctx.pending)); |
| 96 | assert.deepEqual(warning.mock.calls[0].arguments, ["posthog_delivery_failed", { status: 503 }]); |
| 97 | }); |
| 98 | |
| 99 | test("EU delivery is supported and separate captures have separate identifiers", async (t) => { |
| 100 | const identifiers: string[] = []; |
| 101 | t.mock.method(globalThis, "fetch", async (url: string, init: RequestInit) => { |
| 102 | assert.equal(url, "https://eu.i.posthog.com/i/v0/e/"); |
| 103 | identifiers.push(JSON.parse(String(init.body)).uuid); |
| 104 | return new Response(null, { status: 204 }); |
| 105 | }); |
| 106 | const ctx = context(); |
| 107 | for (let index = 0; index < 2; index++) captureAccountEvent({ ...config, POSTHOG_HOST: "https://eu.i.posthog.com" }, ctx, { name: "account.signed_in", userId: "user-id" }); |
| 108 | await Promise.all(ctx.pending); |
| 109 | assert.equal(new Set(identifiers).size, 2); |
| 110 | }); |