|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { emit, EmitMessage } from "./emit.js"; |
| 3 | +import { newState } from "./new.js"; |
| 4 | + |
| 5 | +function captureEmit(state: Parameters<typeof emit>[0]): Record<string, unknown> { |
| 6 | + const captured: string[] = []; |
| 7 | + const origWrite = process.stdout.write; |
| 8 | + process.stdout.write = ((chunk: unknown) => { |
| 9 | + captured.push(String(chunk)); |
| 10 | + return true; |
| 11 | + }) as typeof process.stdout.write; |
| 12 | + try { |
| 13 | + emit(state); |
| 14 | + } finally { |
| 15 | + process.stdout.write = origWrite; |
| 16 | + } |
| 17 | + expect(captured).toHaveLength(1); |
| 18 | + const line = captured[0]; |
| 19 | + if (!line) throw new Error("no captured line"); |
| 20 | + return JSON.parse(line) as Record<string, unknown>; |
| 21 | +} |
| 22 | + |
| 23 | +describe("emit", () => { |
| 24 | + it("emits a single line with the stable message + request_id", () => { |
| 25 | + const s = newState({ service: "supervisor", env: {} }); |
| 26 | + s.statusCode = 200; |
| 27 | + s.ok = true; |
| 28 | + s.durationMs = 5; |
| 29 | + const out = captureEmit(s); |
| 30 | + expect(out.msg).toBe(EmitMessage); |
| 31 | + expect(out.request_id).toBe(s.requestId); |
| 32 | + expect(out.service).toBe("supervisor"); |
| 33 | + expect(out.ok).toBe(true); |
| 34 | + expect(out.status).toBe(200); |
| 35 | + expect(out.duration_ms).toBe(5); |
| 36 | + }); |
| 37 | + |
| 38 | + it("omits empty optional fields", () => { |
| 39 | + const s = newState({ service: "supervisor", env: {} }); |
| 40 | + s.statusCode = 200; |
| 41 | + s.ok = true; |
| 42 | + const out = captureEmit(s); |
| 43 | + expect(out).not.toHaveProperty("trace_id"); |
| 44 | + expect(out).not.toHaveProperty("version"); |
| 45 | + expect(out).not.toHaveProperty("commit_sha"); |
| 46 | + expect(out).not.toHaveProperty("instance_id"); |
| 47 | + expect(out).not.toHaveProperty("error.code"); |
| 48 | + }); |
| 49 | + |
| 50 | + it("flattens meta keys as meta.<key>", () => { |
| 51 | + const s = newState({ service: "supervisor", env: {} }); |
| 52 | + s.statusCode = 200; |
| 53 | + s.ok = true; |
| 54 | + s.meta.run_id = "run_abc"; |
| 55 | + s.meta.deployment_id = "dep_xyz"; |
| 56 | + const out = captureEmit(s); |
| 57 | + expect(out["meta.run_id"]).toBe("run_abc"); |
| 58 | + expect(out["meta.deployment_id"]).toBe("dep_xyz"); |
| 59 | + expect(out).not.toHaveProperty("meta"); |
| 60 | + }); |
| 61 | + |
| 62 | + it("flattens phases as phase.<name>.<field>", () => { |
| 63 | + const s = newState({ service: "supervisor", env: {} }); |
| 64 | + s.statusCode = 200; |
| 65 | + s.ok = true; |
| 66 | + s.phases.push({ name: "warm_start", durationMs: 12, ok: true, attempts: 1 }); |
| 67 | + s.phases.push({ |
| 68 | + name: "workload_create", |
| 69 | + durationMs: 3, |
| 70 | + ok: false, |
| 71 | + attempts: 2, |
| 72 | + errorCode: "Error", |
| 73 | + errorMsg: "boom", |
| 74 | + sub: { create_ms: 1 }, |
| 75 | + }); |
| 76 | + const out = captureEmit(s); |
| 77 | + expect(out["phase.warm_start.duration_ms"]).toBe(12); |
| 78 | + expect(out["phase.warm_start.ok"]).toBe(true); |
| 79 | + expect(out["phase.warm_start.attempts"]).toBe(1); |
| 80 | + expect(out["phase.workload_create.duration_ms"]).toBe(3); |
| 81 | + expect(out["phase.workload_create.ok"]).toBe(false); |
| 82 | + expect(out["phase.workload_create.attempts"]).toBe(2); |
| 83 | + expect(out["phase.workload_create.error_code"]).toBe("Error"); |
| 84 | + expect(out["phase.workload_create.error_message"]).toBe("boom"); |
| 85 | + expect(out["phase.workload_create.create_ms"]).toBe(1); |
| 86 | + }); |
| 87 | + |
| 88 | + it("includes error.code/message/kind when state.error is set", () => { |
| 89 | + const s = newState({ service: "supervisor", env: {} }); |
| 90 | + s.statusCode = 500; |
| 91 | + s.error = { code: "InternalError", message: "kaboom", kind: "internal" }; |
| 92 | + const out = captureEmit(s); |
| 93 | + expect(out["error.code"]).toBe("InternalError"); |
| 94 | + expect(out["error.message"]).toBe("kaboom"); |
| 95 | + expect(out["error.kind"]).toBe("internal"); |
| 96 | + }); |
| 97 | + |
| 98 | + it("truncates very long error messages", () => { |
| 99 | + const s = newState({ service: "supervisor", env: {} }); |
| 100 | + s.error = { code: "Big", message: "x".repeat(2000), kind: "internal" }; |
| 101 | + const out = captureEmit(s); |
| 102 | + expect((out["error.message"] as string).length).toBe(512); |
| 103 | + }); |
| 104 | + |
| 105 | + it("flattens extras at the top level", () => { |
| 106 | + const s = newState({ service: "supervisor", env: {} }); |
| 107 | + s.statusCode = 200; |
| 108 | + s.ok = true; |
| 109 | + s.extras.route = "/health"; |
| 110 | + s.extras["dispatch.result"] = "hit"; |
| 111 | + const out = captureEmit(s); |
| 112 | + expect(out.route).toBe("/health"); |
| 113 | + expect(out["dispatch.result"]).toBe("hit"); |
| 114 | + }); |
| 115 | +}); |
0 commit comments