返回 CodeWhale
funnel.test.ts
根目录 / telemetry-ingest / test / funnel.test.ts
1 import { readFileSync } from "node:fs";
2 import { afterEach, describe, expect, it, vi } from "vitest";
3 import worker from "../src/index";
4 import {
5 ENVELOPE_FIELDS, FUNNEL_COUNTER_FIELDS, FUNNEL_NOTICE_VERSION, FUNNEL_SCHEMA_VERSION,
6 PRODUCT_COUNTER_FIELDS, WEBSITE_FUNNEL_SCHEMA, validateBatch,
7 } from "../src/schema";
8 import { BLOB_COLUMNS, DOUBLE_COLUMNS } from "../src/datapoint";
9 import { harness, postJson } from "./support";
10
11 const fixture = () => JSON.parse(readFileSync(new URL("./golden/browser-v5.json", import.meta.url), "utf8"));
12 const counters = (batch: ReturnType<typeof fixture>) => batch.events.find((event: { event: string }) => event.event === "funnel_usage").counters;
13
14 afterEach(() => vi.unstubAllGlobals());
15
16 describe("additive website funnel contract", () => {
17 it("accepts the local v5 fixture through the existing pipeline with unchanged column positions", async () => {
18 const batch = fixture();
19 expect(Object.keys(batch)).toEqual([...ENVELOPE_FIELDS]);
20 expect(validateBatch(batch).ok).toBe(true);
21 expect(Object.keys(counters(batch))).toEqual([...FUNNEL_COUNTER_FIELDS]);
22 expect(Object.keys(batch.events[0].counters)).toEqual([...PRODUCT_COUNTER_FIELDS]);
23 const fetch = vi.fn(); vi.stubGlobal("fetch", fetch);
24 const { env, written } = harness();
25 const response = await worker.fetch(postJson(batch), env);
26 expect(response.status).toBe(204);
27 expect(await response.text()).toBe("");
28 expect(written).toHaveLength(2);
29 const row = written[1];
30 expect(row.indexes).toEqual([batch.install_id]);
31 expect(row.blobs).toHaveLength(20);
32 expect(row.doubles).toHaveLength(20);
33 expect(row.blobs[BLOB_COLUMNS.indexOf("event")]).toBe("funnel_usage");
34 expect(JSON.parse(row.blobs[BLOB_COLUMNS.indexOf("aggregate_counters")])).toEqual(counters(batch));
35 expect(row.blobs.slice(18)).toEqual(["5", "7"]);
36 expect(DOUBLE_COLUMNS).toHaveLength(20);
37 expect(fetch).not.toHaveBeenCalled();
38 });
39
40 it("exports the exact browser contract without changing the v3 application artifact", () => {
41 const schema = JSON.parse(readFileSync(new URL("../schema/website-funnel-v5.schema.json", import.meta.url), "utf8"));
42 expect(schema).toEqual(WEBSITE_FUNNEL_SCHEMA);
43 const doc = readFileSync(new URL("../../docs/TELEMETRY.md", import.meta.url), "utf8");
44 const extension = doc.split("## Website funnel aggregates in the source candidate")[1];
45 const documented = JSON.parse(extension.match(/```json\n([\s\S]*?)```/)![1]);
46 expect(documented.event).toBe("funnel_usage");
47 expect(Object.keys(documented.counters)).toEqual([...FUNNEL_COUNTER_FIELDS]);
48 expect(schema.properties.schema_version.const).toBe(FUNNEL_SCHEMA_VERSION);
49 expect(schema.properties.notice_version.const).toBe(FUNNEL_NOTICE_VERSION);
50 expect(schema.properties.events.items.oneOf[1].properties.counters.required).toEqual([...FUNNEL_COUNTER_FIELDS]);
51 const legacy = JSON.parse(readFileSync(new URL("../schema/cwc-product-v3.schema.json", import.meta.url), "utf8"));
52 expect(legacy.properties.schema_version.const).toBe(3);
53 expect(legacy.properties.events.items.properties.counters.required).toEqual([...PRODUCT_COUNTER_FIELDS]);
54 });
55
56 it.each(FUNNEL_COUNTER_FIELDS)("requires bounded aggregate %s", (field) => {
57 for (const invalid of [undefined, -1, 0.5, 4294967296, NaN, Infinity, "1", true]) {
58 const batch = fixture(); counters(batch)[field] = invalid;
59 expect(validateBatch(batch).ok, `${field}: ${String(invalid)}`).toBe(false);
60 }
61 const batch = fixture(); counters(batch)[field] = 4294967295;
62 expect(validateBatch(batch).ok).toBe(true);
63 });
64
65 it.each([1, 2, 3, 4])("refuses funnel events in historical schema %i", (version) => {
66 const batch = fixture(); batch.schema_version = version;
67 if (version === 1) delete batch.notice_version;
68 if (version === 2) { delete batch.notice_version; batch.consent_version = 4; }
69 if (version === 3) batch.notice_version = 5;
70 if (version === 4) batch.notice_version = 6;
71 expect(validateBatch(batch).ok).toBe(false);
72 });
73
74 it.each(["tui", "exec", "cli", "web-app", "desktop", "control-plane"])("refuses v5 on %s", (surface) => {
75 const batch = fixture(); batch.surface = surface;
76 expect(validateBatch(batch).ok).toBe(false);
77 });
78
79 it("rejects content, fingerprinting, empty events and incorrect notice before any write", async () => {
80 const mutations = [
81 (batch: ReturnType<typeof fixture>) => { batch.url = "/private"; },
82 (batch: ReturnType<typeof fixture>) => { counters(batch).search_query = "private"; },
83 (batch: ReturnType<typeof fixture>) => { batch.git_sha = "abcdef012345"; },
84 (batch: ReturnType<typeof fixture>) => { batch.os = "macos"; },
85 (batch: ReturnType<typeof fixture>) => { batch.tty = true; },
86 (batch: ReturnType<typeof fixture>) => { batch.notice_version = 6; },
87 (batch: ReturnType<typeof fixture>) => { batch.events = []; },
88 (batch: ReturnType<typeof fixture>) => { batch.events = [{ event: "session_start", source: "interactive" }]; },
89 ];
90 for (const mutate of mutations) {
91 const batch = fixture(); mutate(batch);
92 const { env, written } = harness();
93 expect((await worker.fetch(postJson(batch), env)).status).toBe(400);
94 expect(written).toHaveLength(0);
95 }
96 });
97
98 it("keeps the historical goldens and v6 accepted, with runtime v4 and v6 first-party only", async () => {
99 const fetch = vi.fn(); vi.stubGlobal("fetch", fetch);
100 for (const version of [1, 2, 3, 4, 6]) {
101 const batch = JSON.parse(readFileSync(new URL(`../../crates/telemetry/tests/golden/v${version}.json`, import.meta.url), "utf8"));
102 expect(validateBatch(batch).ok, `v${version}`).toBe(true);
103 if (version !== 4 && version !== 6) continue;
104 const { env, written } = harness();
105 expect((await worker.fetch(postJson(batch), env)).status).toBe(204);
106 expect(written).toHaveLength(batch.events.length);
107 expect(fetch).not.toHaveBeenCalled();
108 }
109 });
110 });
111
111 lines TYPESCRIPT