import { describe, it, expect } from "vitest"; import { flattenMetadata } from "../metadata"; describe("flattenMetadata", () => { it("flattens nested objects to dot-notation entries", () => { const entries = flattenMetadata({ kind: "action.completed", envelope: { messageId: "m1", correlationId: "c1" }, }); const map = Object.fromEntries(entries.map((e) => [e.path, e.value])); expect(map.kind).toBe("action.completed"); expect(map["envelope.messageId"]).toBe("m1"); expect(map["envelope.correlationId"]).toBe("c1"); }); it("indexes arrays by position", () => { const entries = flattenMetadata({ items: [{ id: "a" }, { id: "b" }] }); const paths = entries.map((e) => e.path); expect(paths).toContain("items.0.id"); expect(paths).toContain("items.1.id"); }); it("tags each entry with its value type", () => { const entries = flattenMetadata({ s: "x", n: 3, b: true, z: null }); const byPath = Object.fromEntries(entries.map((e) => [e.path, e.type])); expect(byPath.s).toBe("string"); expect(byPath.n).toBe("number"); expect(byPath.b).toBe("boolean"); expect(byPath.z).toBe("null"); }); it("drops undefined and functions", () => { const entries = flattenMetadata({ keep: 1, gone: undefined, fn: () => 0 }); expect(entries.map((e) => e.path)).toEqual(["keep"]); }); it("guards against cycles", () => { const a: Record = { name: "a" }; a.self = a; const entries = flattenMetadata(a); expect(entries.some((e) => e.value === "[circular]")).toBe(true); }); it("wraps a bare primitive under `value`", () => { expect(flattenMetadata("hi")).toEqual([{ path: "value", value: "hi", type: "string" }]); }); });