| 1 | import { readFileSync } from "node:fs"; |
| 2 | |
| 3 | import { describe, expect, it } from "vitest"; |
| 4 | |
| 5 | import worker from "../src/index"; |
| 6 | import { INGEST_PATH } from "../src/route"; |
| 7 | import { BLOB_COLUMNS, DOUBLE_COLUMNS } from "../src/datapoint"; |
| 8 | import { MAX_BODY_BYTES, SURFACES, validateBatch } from "../src/schema"; |
| 9 | import { goldenBatch, harness, post, postJson } from "./support"; |
| 10 | |
| 11 | describe("method and route", () => { |
| 12 | it("answers 405 to GET, with no body", async () => { |
| 13 | const { env, written } = harness(); |
| 14 | const response = await worker.fetch( |
| 15 | new Request(`https://telemetry.invalid${INGEST_PATH}`), |
| 16 | env, |
| 17 | ); |
| 18 | expect(response.status).toBe(405); |
| 19 | expect(response.headers.get("allow")).toBe("POST"); |
| 20 | expect(await response.text()).toBe(""); |
| 21 | expect(written).toHaveLength(0); |
| 22 | }); |
| 23 | |
| 24 | it.each(["HEAD", "PUT", "PATCH", "DELETE", "OPTIONS"])( |
| 25 | "answers 405 to %s", |
| 26 | async (method) => { |
| 27 | const { env } = harness(); |
| 28 | const response = await worker.fetch( |
| 29 | new Request(`https://telemetry.invalid${INGEST_PATH}`, { method }), |
| 30 | env, |
| 31 | ); |
| 32 | expect(response.status).toBe(405); |
| 33 | }, |
| 34 | ); |
| 35 | |
| 36 | it("has no readable surface: GET on any other path is also 405", async () => { |
| 37 | const { env } = harness(); |
| 38 | for (const path of ["/", "/v1", "/v1/telemetry/query", "/health"]) { |
| 39 | const response = await worker.fetch( |
| 40 | new Request(`https://telemetry.invalid${path}`), |
| 41 | env, |
| 42 | ); |
| 43 | expect(response.status).toBe(405); |
| 44 | expect(await response.text()).toBe(""); |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | it("answers 404 to a POST at another path", async () => { |
| 49 | const { env } = harness(); |
| 50 | const response = await worker.fetch( |
| 51 | post(JSON.stringify(goldenBatch()), { path: "/v1/other" }), |
| 52 | env, |
| 53 | ); |
| 54 | expect(response.status).toBe(404); |
| 55 | }); |
| 56 | |
| 57 | it("answers 415 without a JSON content type", async () => { |
| 58 | const { env } = harness(); |
| 59 | const response = await worker.fetch( |
| 60 | post(JSON.stringify(goldenBatch()), { contentType: "text/plain" }), |
| 61 | env, |
| 62 | ); |
| 63 | expect(response.status).toBe(415); |
| 64 | }); |
| 65 | }); |
| 66 | |
| 67 | describe("a valid batch", () => { |
| 68 | it("accepts the client's own golden v1 batch with 204 and no body", async () => { |
| 69 | const { env, written } = harness(); |
| 70 | const response = await worker.fetch(postJson(goldenBatch()), env); |
| 71 | |
| 72 | expect(response.status).toBe(204); |
| 73 | expect(await response.text()).toBe(""); |
| 74 | expect(response.headers.get("content-type")).toBeNull(); |
| 75 | expect(written).toHaveLength(4); |
| 76 | }); |
| 77 | |
| 78 | it("writes one data point per event, indexed by install_id only", async () => { |
| 79 | const { env, written } = harness(); |
| 80 | await worker.fetch(postJson(goldenBatch()), env); |
| 81 | |
| 82 | for (const point of written) { |
| 83 | expect(point.indexes).toEqual(["3f2a9c1e-0000-4000-8000-000000000001"]); |
| 84 | expect(point.blobs).toHaveLength(BLOB_COLUMNS.length); |
| 85 | expect(point.doubles).toHaveLength(DOUBLE_COLUMNS.length); |
| 86 | } |
| 87 | expect(written.map((point) => point.blobs[0])).toEqual([ |
| 88 | "install_or_upgrade", |
| 89 | "session_start", |
| 90 | "session_end", |
| 91 | "panic", |
| 92 | ]); |
| 93 | }); |
| 94 | |
| 95 | it("puts the counters, errors and turn_wall in the documented columns", async () => { |
| 96 | const { env, written } = harness(); |
| 97 | await worker.fetch(postJson(goldenBatch()), env); |
| 98 | |
| 99 | const sessionEnd = written[2]; |
| 100 | const column = (name: string) => |
| 101 | sessionEnd.doubles[DOUBLE_COLUMNS.indexOf(name as never)]; |
| 102 | expect(column("turns")).toBe(14); |
| 103 | expect(column("tool_calls")).toBe(61); |
| 104 | expect(column("subagent_spawn")).toBe(2); |
| 105 | expect(column("provider_http_5xx")).toBe(1); |
| 106 | expect(column("lt_5s")).toBe(9); |
| 107 | expect(column("gte_120s")).toBe(0); |
| 108 | |
| 109 | const blob = (name: string) => |
| 110 | sessionEnd.blobs[BLOB_COLUMNS.indexOf(name as never)]; |
| 111 | expect(blob("providers")).toBe("custom,deepseek"); |
| 112 | expect(blob("exit_class")).toBe("panic"); |
| 113 | expect(blob("cold_start_bucket")).toBe("250_1000"); |
| 114 | |
| 115 | expect(written[3].blobs[BLOB_COLUMNS.indexOf("panic_site")]).toBe( |
| 116 | "crates/tui/src/tui/ui.rs:8801:17", |
| 117 | ); |
| 118 | }); |
| 119 | |
| 120 | it("stores nothing that is not in the batch", async () => { |
| 121 | const { env, written } = harness(); |
| 122 | await worker.fetch(postJson(goldenBatch()), env); |
| 123 | |
| 124 | const stored = written.flatMap((point) => [ |
| 125 | ...point.indexes, |
| 126 | ...point.blobs, |
| 127 | ]); |
| 128 | const batchText = JSON.stringify(goldenBatch()); |
| 129 | for (const value of stored) { |
| 130 | if (value === "" || value === "true" || value === "false") continue; |
| 131 | // Every stored string is a substring of the batch the client sent, or a |
| 132 | // comma-join of values from it. Nothing is derived from the request. |
| 133 | for (const part of value.split(",")) { |
| 134 | expect(batchText).toContain(part); |
| 135 | } |
| 136 | } |
| 137 | }); |
| 138 | }); |
| 139 | |
| 140 | describe("the closed field set", () => { |
| 141 | it("rejects an unknown key on the envelope", async () => { |
| 142 | const { env, written } = harness(); |
| 143 | const batch = goldenBatch(); |
| 144 | batch.cwd = "/Users/someone/work/secret-repo"; |
| 145 | const response = await worker.fetch(postJson(batch), env); |
| 146 | |
| 147 | expect(response.status).toBe(400); |
| 148 | expect(await response.text()).toBe(""); |
| 149 | expect(written).toHaveLength(0); |
| 150 | }); |
| 151 | |
| 152 | it("rejects an unknown key nested inside an event", async () => { |
| 153 | const { env, written } = harness(); |
| 154 | const batch = goldenBatch(); |
| 155 | const events = batch.events as Record<string, unknown>[]; |
| 156 | events[2].prompt = "please refactor crates/tui/src/main.rs"; |
| 157 | const response = await worker.fetch(postJson(batch), env); |
| 158 | |
| 159 | expect(response.status).toBe(400); |
| 160 | expect(written).toHaveLength(0); |
| 161 | }); |
| 162 | |
| 163 | it("rejects an unknown key nested inside counters", async () => { |
| 164 | const { env } = harness(); |
| 165 | const batch = goldenBatch(); |
| 166 | const events = batch.events as Record<string, Record<string, unknown>>[]; |
| 167 | events[2].counters.git_branch_switches = 1; |
| 168 | expect((await worker.fetch(postJson(batch), env)).status).toBe(400); |
| 169 | }); |
| 170 | |
| 171 | it("rejects an unknown event discriminant", async () => { |
| 172 | const { env } = harness(); |
| 173 | const batch = goldenBatch(); |
| 174 | (batch.events as unknown[]).push({ event: "keystroke", site: "<dep>" }); |
| 175 | expect((await worker.fetch(postJson(batch), env)).status).toBe(400); |
| 176 | }); |
| 177 | |
| 178 | it("rejects a batch with any envelope key removed", async () => { |
| 179 | const original = goldenBatch(); |
| 180 | for (const key of Object.keys(original)) { |
| 181 | const { env } = harness(); |
| 182 | const batch = goldenBatch(); |
| 183 | delete batch[key]; |
| 184 | const response = await worker.fetch(postJson(batch), env); |
| 185 | expect(response.status, `deleting ${key} must reject`).toBe(400); |
| 186 | } |
| 187 | }); |
| 188 | |
| 189 | it("rejects a batch with any event key removed", async () => { |
| 190 | const events = goldenBatch().events as Record<string, unknown>[]; |
| 191 | for (let index = 0; index < events.length; index += 1) { |
| 192 | for (const key of Object.keys(events[index])) { |
| 193 | const { env } = harness(); |
| 194 | const batch = goldenBatch(); |
| 195 | delete (batch.events as Record<string, unknown>[])[index][key]; |
| 196 | const response = await worker.fetch(postJson(batch), env); |
| 197 | expect(response.status, `deleting events[${index}].${key}`).toBe(400); |
| 198 | } |
| 199 | } |
| 200 | }); |
| 201 | }); |
| 202 | |
| 203 | describe("value rules from the published schema", () => { |
| 204 | const reject = async (mutate: (batch: Record<string, unknown>) => void) => { |
| 205 | const { env, written } = harness(); |
| 206 | const batch = goldenBatch(); |
| 207 | mutate(batch); |
| 208 | const response = await worker.fetch(postJson(batch), env); |
| 209 | expect(response.status).toBe(400); |
| 210 | expect(written).toHaveLength(0); |
| 211 | }; |
| 212 | |
| 213 | it("rejects a wrong schema_version", () => |
| 214 | reject((batch) => { |
| 215 | batch.schema_version = 2; |
| 216 | })); |
| 217 | |
| 218 | it("rejects a non-UTC or sub-second sent_at", () => |
| 219 | reject((batch) => { |
| 220 | batch.sent_at = "2026-08-03T18:04:11.234Z"; |
| 221 | })); |
| 222 | |
| 223 | it("rejects an install_id that is not a v4 uuid", () => |
| 224 | reject((batch) => { |
| 225 | batch.install_id = "hostname-derived-id"; |
| 226 | })); |
| 227 | |
| 228 | it("rejects an app_version that is not a release version", () => |
| 229 | reject((batch) => { |
| 230 | batch.app_version = "0.9.4 (/Users/someone/src/codewhale)"; |
| 231 | })); |
| 232 | |
| 233 | it("rejects a git_sha that is not 12 hex chars", () => |
| 234 | reject((batch) => { |
| 235 | batch.git_sha = "refs/heads/feature-acme-migration"; |
| 236 | })); |
| 237 | |
| 238 | it("accepts a null git_sha — every locally built binary sends one", async () => { |
| 239 | const { env } = harness(); |
| 240 | const batch = goldenBatch(); |
| 241 | batch.git_sha = null; |
| 242 | expect((await worker.fetch(postJson(batch), env)).status).toBe(204); |
| 243 | }); |
| 244 | |
| 245 | it("accepts a null cold_start_bucket — non-TUI surfaces send one", async () => { |
| 246 | const { env } = harness(); |
| 247 | const batch = goldenBatch(); |
| 248 | (batch.events as Record<string, unknown>[])[2].cold_start_bucket = null; |
| 249 | expect((await worker.fetch(postJson(batch), env)).status).toBe(204); |
| 250 | }); |
| 251 | |
| 252 | it("rejects a surface outside the closed enum", () => |
| 253 | reject((batch) => { |
| 254 | batch.surface = "desktop"; |
| 255 | })); |
| 256 | |
| 257 | it("rejects a provider list that is not sorted and deduplicated", () => |
| 258 | reject((batch) => { |
| 259 | (batch.events as Record<string, unknown>[])[2].providers = [ |
| 260 | "deepseek", |
| 261 | "custom", |
| 262 | ]; |
| 263 | })); |
| 264 | |
| 265 | it("rejects a provider entry shaped like a config table key", () => |
| 266 | reject((batch) => { |
| 267 | (batch.events as Record<string, unknown>[])[2].providers = [ |
| 268 | "acme_internal_gateway", |
| 269 | ]; |
| 270 | })); |
| 271 | |
| 272 | it("rejects a panic site outside the crates/ allowlist", () => |
| 273 | reject((batch) => { |
| 274 | (batch.events as Record<string, unknown>[])[3].site = |
| 275 | "/Users/builder/.cargo/registry/src/index.crates.io/ratatui-0.29.0/src/lib.rs:1:1"; |
| 276 | })); |
| 277 | |
| 278 | it("accepts the reduced <dep> panic site", async () => { |
| 279 | const { env } = harness(); |
| 280 | const batch = goldenBatch(); |
| 281 | (batch.events as Record<string, unknown>[])[3].site = "<dep>"; |
| 282 | expect((await worker.fetch(postJson(batch), env)).status).toBe(204); |
| 283 | }); |
| 284 | |
| 285 | it("rejects a counter that is not a non-negative integer", () => |
| 286 | reject((batch) => { |
| 287 | const events = batch.events as Record<string, Record<string, unknown>>[]; |
| 288 | events[2].counters.turns = -1; |
| 289 | })); |
| 290 | |
| 291 | it("rejects more events than the client can put in one batch", () => |
| 292 | reject((batch) => { |
| 293 | batch.events = Array.from({ length: 201 }, () => ({ |
| 294 | event: "session_start", |
| 295 | source: "interactive", |
| 296 | })); |
| 297 | })); |
| 298 | |
| 299 | it("rejects a body that is not JSON", async () => { |
| 300 | const { env } = harness(); |
| 301 | const response = await worker.fetch(post("not json at all"), env); |
| 302 | expect(response.status).toBe(400); |
| 303 | }); |
| 304 | |
| 305 | it("rejects a JSON array at the top level", async () => { |
| 306 | const { env } = harness(); |
| 307 | expect((await worker.fetch(postJson([goldenBatch()]), env)).status).toBe( |
| 308 | 400, |
| 309 | ); |
| 310 | }); |
| 311 | }); |
| 312 | |
| 313 | describe("size caps", () => { |
| 314 | it("rejects an oversized body declared by content-length", async () => { |
| 315 | const { env, written } = harness(); |
| 316 | const batch = goldenBatch(); |
| 317 | batch.events = [ |
| 318 | { |
| 319 | event: "panic", |
| 320 | site: `crates/tui/src/${"a".repeat(MAX_BODY_BYTES)}.rs:1:1`, |
| 321 | }, |
| 322 | ]; |
| 323 | const body = JSON.stringify(batch); |
| 324 | expect(body.length).toBeGreaterThan(MAX_BODY_BYTES); |
| 325 | |
| 326 | const response = await worker.fetch(post(body), env); |
| 327 | expect(response.status).toBe(413); |
| 328 | expect(await response.text()).toBe(""); |
| 329 | expect(written).toHaveLength(0); |
| 330 | }); |
| 331 | |
| 332 | it("rejects an oversized body that declares no content-length", async () => { |
| 333 | const { env, written } = harness(); |
| 334 | const chunk = new TextEncoder().encode("x".repeat(8 * 1024)); |
| 335 | let remaining = MAX_BODY_BYTES + 8 * 1024; |
| 336 | const stream = new ReadableStream<Uint8Array>({ |
| 337 | pull(controller) { |
| 338 | if (remaining <= 0) { |
| 339 | controller.close(); |
| 340 | return; |
| 341 | } |
| 342 | remaining -= chunk.byteLength; |
| 343 | controller.enqueue(chunk); |
| 344 | }, |
| 345 | }); |
| 346 | |
| 347 | const request = new Request(`https://telemetry.invalid${INGEST_PATH}`, { |
| 348 | method: "POST", |
| 349 | headers: { "content-type": "application/json" }, |
| 350 | body: stream, |
| 351 | // Node requires this for a streaming request body; Workers does not. |
| 352 | duplex: "half", |
| 353 | } as RequestInit); |
| 354 | |
| 355 | expect(request.headers.get("content-length")).toBeNull(); |
| 356 | const response = await worker.fetch(request, env); |
| 357 | expect(response.status).toBe(413); |
| 358 | expect(written).toHaveLength(0); |
| 359 | }); |
| 360 | |
| 361 | it("accepts a batch at the documented ceiling of 200 events", async () => { |
| 362 | const { env, written } = harness(); |
| 363 | const batch = goldenBatch(); |
| 364 | batch.events = Array.from({ length: 200 }, () => ({ |
| 365 | event: "session_start", |
| 366 | source: "interactive", |
| 367 | })); |
| 368 | const response = await worker.fetch(postJson(batch), env); |
| 369 | expect(response.status).toBe(204); |
| 370 | expect(written).toHaveLength(200); |
| 371 | }); |
| 372 | }); |
| 373 | |
| 374 | describe("rate limiting", () => { |
| 375 | it("keys the limiter on install_id and nothing else", async () => { |
| 376 | const { env, limited } = harness({ rateLimit: true }); |
| 377 | const response = await worker.fetch(postJson(goldenBatch()), env); |
| 378 | expect(response.status).toBe(204); |
| 379 | expect(limited).toEqual(["3f2a9c1e-0000-4000-8000-000000000001"]); |
| 380 | }); |
| 381 | |
| 382 | it("answers 429 with no body when the limiter refuses", async () => { |
| 383 | const { env, written } = harness({ rateLimit: false }); |
| 384 | const response = await worker.fetch(postJson(goldenBatch()), env); |
| 385 | expect(response.status).toBe(429); |
| 386 | expect(await response.text()).toBe(""); |
| 387 | expect(written).toHaveLength(0); |
| 388 | }); |
| 389 | |
| 390 | it("caps rotating install_ids via the server-keyed ingest limiter", async () => { |
| 391 | const { env, written, ingestLimited } = harness({ |
| 392 | rateLimit: true, |
| 393 | ingestRateLimit: 3, |
| 394 | }); |
| 395 | const ids = [ |
| 396 | "11111111-1111-4111-8111-111111111111", |
| 397 | "22222222-2222-4222-8222-222222222222", |
| 398 | "33333333-3333-4333-8333-333333333333", |
| 399 | "44444444-4444-4444-8444-444444444444", |
| 400 | ]; |
| 401 | const statuses: number[] = []; |
| 402 | for (const installId of ids) { |
| 403 | const batch = goldenBatch(); |
| 404 | batch.install_id = installId; |
| 405 | const response = await worker.fetch(postJson(batch), env); |
| 406 | statuses.push(response.status); |
| 407 | expect(await response.text()).toBe(""); |
| 408 | } |
| 409 | expect(statuses).toEqual([204, 204, 204, 429]); |
| 410 | expect(ingestLimited).toEqual([ |
| 411 | INGEST_PATH, |
| 412 | INGEST_PATH, |
| 413 | INGEST_PATH, |
| 414 | INGEST_PATH, |
| 415 | ]); |
| 416 | expect(written).toHaveLength(12); |
| 417 | }); |
| 418 | }); |
| 419 | |
| 420 | /** |
| 421 | * The surface whitelist is the field that tells one client of this schema from |
| 422 | * another, so a value added to it has to be proved *reachable* — not merely |
| 423 | * present in a constant that `schema-doc.test.ts` compares against the doc. |
| 424 | */ |
| 425 | describe("the v3 surface whitelist", () => { |
| 426 | /** |
| 427 | * `crates/telemetry/tests/golden/v2.json` is a real client batch, and v3 |
| 428 | * changed no field: its v3 form is that envelope with the explicit-consent |
| 429 | * field swapped for the policy field. Kept local rather than added to |
| 430 | * `support.ts`, because v1 is the only golden the endpoint is contractually |
| 431 | * welded to. |
| 432 | */ |
| 433 | const v3Batch = (surface: string): Record<string, unknown> => { |
| 434 | const batch = JSON.parse( |
| 435 | readFileSync( |
| 436 | new URL("../../crates/telemetry/tests/golden/v2.json", import.meta.url), |
| 437 | "utf8", |
| 438 | ), |
| 439 | ) as Record<string, unknown>; |
| 440 | delete batch.consent_version; |
| 441 | batch.schema_version = 3; |
| 442 | batch.notice_version = 5; |
| 443 | batch.surface = surface; |
| 444 | // `operations_summary` is accepted only on `control-plane`; the other |
| 445 | // values in the whitelist must not be made to fail on an unrelated rule. |
| 446 | batch.events = (batch.events as Array<Record<string, unknown>>).filter( |
| 447 | (event) => event.event !== "operations_summary" || surface === "control-plane", |
| 448 | ); |
| 449 | return batch; |
| 450 | }; |
| 451 | |
| 452 | it("accepts every value in the whitelist and stores it in blob2", async () => { |
| 453 | for (const surface of SURFACES) { |
| 454 | const { env, written } = harness(); |
| 455 | const response = await worker.fetch(postJson(v3Batch(surface)), env); |
| 456 | expect(response.status, surface).toBe(204); |
| 457 | expect(written[0]?.blobs[BLOB_COLUMNS.indexOf("surface")]).toBe(surface); |
| 458 | } |
| 459 | }); |
| 460 | |
| 461 | it("rejects a value that is not in the whitelist", async () => { |
| 462 | const { env, written } = harness(); |
| 463 | const response = await worker.fetch(postJson(v3Batch("vscode")), env); |
| 464 | expect(response.status).toBe(400); |
| 465 | expect(written).toHaveLength(0); |
| 466 | }); |
| 467 | |
| 468 | it("the v1 contract still refuses the extension surface", async () => { |
| 469 | const { env, written } = harness(); |
| 470 | const batch = goldenBatch(); |
| 471 | batch.surface = "vscode-extension"; |
| 472 | expect((await worker.fetch(postJson(batch), env)).status).toBe(400); |
| 473 | expect(written).toHaveLength(0); |
| 474 | }); |
| 475 | }); |
| 476 | |
| 477 | describe("failing closed and quiet", () => { |
| 478 | it("answers 500 with no body when a binding throws", async () => { |
| 479 | const env = { |
| 480 | TELEMETRY: { |
| 481 | writeDataPoint() { |
| 482 | throw new Error("dataset unavailable"); |
| 483 | }, |
| 484 | }, |
| 485 | }; |
| 486 | const response = await worker.fetch(postJson(goldenBatch()), env); |
| 487 | expect(response.status).toBe(500); |
| 488 | expect(await response.text()).toBe(""); |
| 489 | }); |
| 490 | |
| 491 | it("never returns a body on any path", async () => { |
| 492 | const { env } = harness(); |
| 493 | const requests = [ |
| 494 | new Request(`https://telemetry.invalid${INGEST_PATH}`), |
| 495 | post("{", {}), |
| 496 | post(JSON.stringify(goldenBatch()), { path: "/nope" }), |
| 497 | postJson(goldenBatch()), |
| 498 | ]; |
| 499 | for (const request of requests) { |
| 500 | const response = await worker.fetch(request, env); |
| 501 | expect(await response.text()).toBe(""); |
| 502 | } |
| 503 | }); |
| 504 | }); |
| 505 | |
| 506 | describe("schema 6", () => { |
| 507 | const golden = (version: number): Record<string, any> => |
| 508 | JSON.parse( |
| 509 | readFileSync( |
| 510 | new URL(`../../crates/telemetry/tests/golden/v${version}.json`, import.meta.url), |
| 511 | "utf8", |
| 512 | ), |
| 513 | ); |
| 514 | const event = (batch: Record<string, any>, name: string) => |
| 515 | batch.events.find((item: Record<string, any>) => item.event === name); |
| 516 | |
| 517 | it("accepts every historical golden and the v6 golden", () => { |
| 518 | for (const version of [1, 2, 3, 4, 6]) { |
| 519 | expect(validateBatch(golden(version)).ok, `v${version}`).toBe(true); |
| 520 | } |
| 521 | }); |
| 522 | |
| 523 | it("answers 204 to the v4 and v6 goldens", async () => { |
| 524 | for (const version of [4, 6]) { |
| 525 | const { env, written } = harness(); |
| 526 | expect((await worker.fetch(postJson(golden(version)), env)).status).toBe(204); |
| 527 | expect(written).toHaveLength(golden(version).events.length); |
| 528 | } |
| 529 | }); |
| 530 | |
| 531 | it("requires notice 8 on v6 and refuses the v4 notice", () => { |
| 532 | const batch = golden(6); |
| 533 | batch.notice_version = 6; |
| 534 | expect(validateBatch(batch).ok).toBe(false); |
| 535 | expect(validateBatch({ ...golden(4), notice_version: 8 }).ok).toBe(false); |
| 536 | }); |
| 537 | |
| 538 | it("refuses a v4 batch carrying any v6 key or value", () => { |
| 539 | const mutations: Array<(batch: Record<string, any>) => void> = [ |
| 540 | (batch) => { event(batch, "model_usage").context = event(golden(6), "model_usage").context; }, |
| 541 | (batch) => { event(batch, "model_usage").counters.requests = 1; }, |
| 542 | (batch) => { event(batch, "model_usage").counters.reasoning_tokens = 1; }, |
| 543 | (batch) => { event(batch, "model_usage").counters.generation_secs = 1; }, |
| 544 | (batch) => { event(batch, "session_start").install_age = "older"; }, |
| 545 | (batch) => { event(batch, "session_start").source = "fleet_worker"; }, |
| 546 | (batch) => { event(batch, "session_start").source = "agent"; }, |
| 547 | (batch) => { event(batch, "session_start").source = "scripted"; }, |
| 548 | (batch) => { event(batch, "session_end").exit_class = "unclean"; }, |
| 549 | ]; |
| 550 | for (const [index, mutate] of mutations.entries()) { |
| 551 | const batch = golden(4); |
| 552 | mutate(batch); |
| 553 | expect(validateBatch(batch).ok, `mutation ${index}`).toBe(false); |
| 554 | } |
| 555 | }); |
| 556 | |
| 557 | it("refuses a v6 batch missing or adding a key", () => { |
| 558 | const mutations: Array<(batch: Record<string, any>) => void> = [ |
| 559 | (batch) => { delete event(batch, "model_usage").context; }, |
| 560 | (batch) => { delete event(batch, "model_usage").context["32k_128k"]; }, |
| 561 | (batch) => { event(batch, "model_usage").context.gte_512k = 0; }, |
| 562 | (batch) => { delete event(batch, "model_usage").counters.requests; }, |
| 563 | (batch) => { delete event(batch, "model_usage").counters.reasoning_tokens; }, |
| 564 | (batch) => { delete event(batch, "model_usage").counters.generation_secs; }, |
| 565 | (batch) => { event(batch, "model_usage").counters.generation_ms = 1; }, |
| 566 | (batch) => { event(batch, "model_usage").model_family = "x"; }, |
| 567 | (batch) => { delete event(batch, "session_start").install_age; }, |
| 568 | (batch) => { event(batch, "session_start").extra = "x"; }, |
| 569 | (batch) => { event(batch, "session_end").counters.requests = 1; }, |
| 570 | ]; |
| 571 | for (const [index, mutate] of mutations.entries()) { |
| 572 | const batch = golden(6); |
| 573 | mutate(batch); |
| 574 | expect(validateBatch(batch).ok, `mutation ${index}`).toBe(false); |
| 575 | } |
| 576 | }); |
| 577 | |
| 578 | it("refuses open values where v6 has a closed set or a count", () => { |
| 579 | const mutations: Array<(batch: Record<string, any>) => void> = [ |
| 580 | (batch) => { event(batch, "session_start").install_age = "2026-11-01"; }, |
| 581 | (batch) => { event(batch, "session_start").install_age = "OLDER"; }, |
| 582 | (batch) => { event(batch, "session_start").source = "github-actions"; }, |
| 583 | (batch) => { event(batch, "session_end").exit_class = "killed"; }, |
| 584 | (batch) => { event(batch, "model_usage").counters.requests = 4294967296; }, |
| 585 | (batch) => { event(batch, "model_usage").counters.requests = 1.5; }, |
| 586 | (batch) => { event(batch, "model_usage").counters.generation_secs = -1; }, |
| 587 | (batch) => { event(batch, "model_usage").counters.generation_secs = 4294967296; }, |
| 588 | (batch) => { event(batch, "model_usage").counters.generation_secs = 1794528251123; }, |
| 589 | (batch) => { event(batch, "model_usage").counters.reasoning_tokens = "7"; }, |
| 590 | (batch) => { event(batch, "model_usage").context.lt_32k = 4294967296; }, |
| 591 | (batch) => { event(batch, "model_usage").context.lt_32k = "1"; }, |
| 592 | (batch) => { event(batch, "model_usage").model = "/Users/someone/model"; }, |
| 593 | ]; |
| 594 | for (const [index, mutate] of mutations.entries()) { |
| 595 | const batch = golden(6); |
| 596 | mutate(batch); |
| 597 | expect(validateBatch(batch).ok, `mutation ${index}`).toBe(false); |
| 598 | } |
| 599 | }); |
| 600 | |
| 601 | it("accepts a null install_age and every v6 source and exit class", () => { |
| 602 | for (const source of ["interactive", "resume", "fork", "api", "unknown", "ci", "fleet_worker", "agent", "scripted"]) { |
| 603 | for (const age of ["new_today", "under_week", "older", null]) { |
| 604 | const batch = golden(6); |
| 605 | event(batch, "session_start").source = source; |
| 606 | event(batch, "session_start").install_age = age; |
| 607 | expect(validateBatch(batch).ok, `${source}/${age}`).toBe(true); |
| 608 | } |
| 609 | } |
| 610 | for (const exit of ["clean", "signal", "panic", "error", "unclean"]) { |
| 611 | const batch = golden(6); |
| 612 | event(batch, "session_end").exit_class = exit; |
| 613 | expect(validateBatch(batch).ok, exit).toBe(true); |
| 614 | } |
| 615 | }); |
| 616 | |
| 617 | it("writes v6 model_usage after the unchanged eleven doubles, and leaves v4 rows zero there", async () => { |
| 618 | const v6 = harness(); |
| 619 | await worker.fetch(postJson(golden(6)), v6.env); |
| 620 | const row = v6.written.find((point) => point.blobs[0] === "model_usage")!; |
| 621 | expect(row.doubles).toEqual([ |
| 622 | 12, 1, 0, 418230, 9120, 380001, 0, |
| 623 | 3, 8, 2, 0, |
| 624 | 27, 2310, 412, |
| 625 | 9, 11, 5, 2, |
| 626 | 0, 0, |
| 627 | ]); |
| 628 | expect(row.blobs[BLOB_COLUMNS.indexOf("schema_version")]).toBe("6"); |
| 629 | |
| 630 | const v4 = harness(); |
| 631 | await worker.fetch(postJson(golden(4)), v4.env); |
| 632 | const old = v4.written.find((point) => point.blobs[0] === "model_usage")!; |
| 633 | expect(old.doubles).toEqual([ |
| 634 | 12, 1, 0, 418230, 9120, 380001, 0, |
| 635 | 3, 8, 2, 0, |
| 636 | 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 637 | ]); |
| 638 | }); |
| 639 | |
| 640 | it("writes install_age to blob12 of session_start rows only", async () => { |
| 641 | const { env, written } = harness(); |
| 642 | await worker.fetch(postJson(golden(6)), env); |
| 643 | const start = written.find((point) => point.blobs[0] === "session_start")!; |
| 644 | expect(start.blobs[BLOB_COLUMNS.indexOf("session_source")]).toBe("fleet_worker"); |
| 645 | expect(start.blobs[BLOB_COLUMNS.indexOf("duration_bucket")]).toBe("under_week"); |
| 646 | const end = written.find((point) => point.blobs[0] === "session_end")!; |
| 647 | expect(end.blobs[BLOB_COLUMNS.indexOf("exit_class")]).toBe("unclean"); |
| 648 | expect(end.blobs[BLOB_COLUMNS.indexOf("duration_bucket")]).toBe("1m_10m"); |
| 649 | }); |
| 650 | }); |
| 651 |