|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { encodeBaggage } from "./baggage.js"; |
| 3 | + |
| 4 | +describe("encodeBaggage", () => { |
| 5 | + it("returns empty string for an empty map", () => { |
| 6 | + expect(encodeBaggage({})).toBe(""); |
| 7 | + }); |
| 8 | + |
| 9 | + it("encodes a single entry as k=v", () => { |
| 10 | + expect(encodeBaggage({ run_id: "run-1" })).toBe("run_id=run-1"); |
| 11 | + }); |
| 12 | + |
| 13 | + it("sorts keys for stable output across hops", () => { |
| 14 | + expect(encodeBaggage({ b: "2", a: "1", c: "3" })).toBe("a=1,b=2,c=3"); |
| 15 | + }); |
| 16 | + |
| 17 | + it("skips empty keys and empty values", () => { |
| 18 | + expect(encodeBaggage({ "": "v", k: "", real: "x" })).toBe("real=x"); |
| 19 | + }); |
| 20 | + |
| 21 | + it("truncates values longer than the cap", () => { |
| 22 | + const long = "x".repeat(1024); |
| 23 | + const got = encodeBaggage({ k: long }); |
| 24 | + const value = got.slice("k=".length); |
| 25 | + expect(value.length).toBe(256); |
| 26 | + }); |
| 27 | + |
| 28 | + it("caps the number of entries", () => { |
| 29 | + const meta: Record<string, string> = {}; |
| 30 | + for (let i = 0; i < 50; i++) { |
| 31 | + // Sortable two-digit keys so we know which 32 survive. |
| 32 | + meta[`k${String(i).padStart(2, "0")}`] = "v"; |
| 33 | + } |
| 34 | + const got = encodeBaggage(meta); |
| 35 | + expect(got.split(",").length).toBe(32); |
| 36 | + }); |
| 37 | +}); |
0 commit comments