返回 CodeWhale
support.ts
根目录 / telemetry-ingest / test / support.ts
1 import { readFileSync } from "node:fs";
2 import { fileURLToPath } from "node:url";
3
4 import type { DataPoint } from "../src/datapoint";
5 import type { Env } from "../src/index";
6 import { INGEST_PATH } from "../src/route";
7
8 /** `docs/TELEMETRY.md` — the published schema, and the thing we must match. */
9 export const DOC = readFileSync(
10 new URL("../../docs/TELEMETRY.md", import.meta.url),
11 "utf8",
12 );
13
14 /**
15 * `crates/telemetry/tests/golden/v1.json` — the client's own pinned v1 wire
16 * form. Reading the real file rather than a hand-written fixture is the point:
17 * the Rust suite already welds this file to the doc and to the serializer, so
18 * an endpoint that accepts it byte for byte is welded to both by transitivity.
19 */
20 export const GOLDEN_PATH = fileURLToPath(
21 new URL("../../crates/telemetry/tests/golden/v1.json", import.meta.url),
22 );
23
24 export const GOLDEN_TEXT = readFileSync(GOLDEN_PATH, "utf8");
25
26 /** A fresh deep copy of the golden batch. */
27 export function goldenBatch(): Record<string, unknown> {
28 return JSON.parse(GOLDEN_TEXT) as Record<string, unknown>;
29 }
30
31 export interface Harness {
32 env: Env;
33 written: DataPoint[];
34 limited: string[];
35 ingestLimited: string[];
36 }
37
38 /** An `Env` whose bindings record instead of calling Cloudflare. */
39 export function harness(
40 options: { rateLimit?: boolean; ingestRateLimit?: boolean | number } = {},
41 ): Harness {
42 const written: DataPoint[] = [];
43 const limited: string[] = [];
44 const ingestLimited: string[] = [];
45 const env: Env = {
46 TELEMETRY: {
47 writeDataPoint(point) {
48 written.push({
49 indexes: point.indexes ?? [],
50 blobs: point.blobs ?? [],
51 doubles: point.doubles ?? [],
52 });
53 },
54 },
55 };
56 if (options.rateLimit !== undefined) {
57 env.RATE_LIMITER = {
58 async limit({ key }) {
59 limited.push(key);
60 return { success: options.rateLimit === true };
61 },
62 };
63 }
64 if (options.ingestRateLimit !== undefined) {
65 const ingestRateLimit = options.ingestRateLimit;
66 const counts = new Map<string, number>();
67 env.INGEST_RATE_LIMITER = {
68 async limit({ key }) {
69 ingestLimited.push(key);
70 if (typeof ingestRateLimit === "boolean") {
71 return { success: ingestRateLimit };
72 }
73 const next = (counts.get(key) ?? 0) + 1;
74 counts.set(key, next);
75 return { success: next <= ingestRateLimit };
76 },
77 };
78 }
79 return { env, written, limited, ingestLimited };
80 }
81
82 const ORIGIN = "https://telemetry.invalid";
83
84 /** A POST shaped the way `crates/telemetry/src/client.rs` shapes it. */
85 export function post(
86 body: BodyInit,
87 init: { path?: string; contentType?: string | null } = {},
88 ): Request {
89 const headers = new Headers();
90 if (init.contentType !== null) {
91 headers.set("content-type", init.contentType ?? "application/json");
92 }
93 return new Request(`${ORIGIN}${init.path ?? INGEST_PATH}`, {
94 method: "POST",
95 headers,
96 body,
97 });
98 }
99
100 /** A POST of a JSON value. */
101 export function postJson(value: unknown): Request {
102 return post(JSON.stringify(value));
103 }
104
104 lines TYPESCRIPT