import { describe, expect, it } from "vitest"; import { currentLogBindings, getTraceId, newTraceId, withLogContext, } from "@/lib/log-context"; describe("log context / traceId", () => { it("newTraceId is uuid-shaped", () => { const id = newTraceId(); expect(id).toMatch( /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i, ); }); it("withLogContext nests and restores", async () => { expect(getTraceId()).toBeUndefined(); await withLogContext({ traceId: "t-pull", stage: "pull" }, async () => { expect(getTraceId()).toBe("t-pull"); expect(currentLogBindings().stage).toBe("pull"); await withLogContext({ stage: "parse", mailId: "9" }, async () => { expect(getTraceId()).toBe("t-pull"); expect(currentLogBindings()).toMatchObject({ traceId: "t-pull", stage: "parse", mailId: "9", }); }); expect(currentLogBindings().stage).toBe("pull"); expect(currentLogBindings().mailId).toBeUndefined(); }); expect(getTraceId()).toBeUndefined(); }); });