import { describe, expect, it } from "vitest"; import { createToolcraftPerformancePathId, decodeToolcraftPerformancePathId, } from "./performance-path-codec.mjs"; const canonicalPath = { interaction: "control-change", invalidates: ["composite", "source"], profile: "interactive-discrete", runsOn: ["main", "worker"], workloadDimensions: ["pixel-count"], } as const; function encodeSignature(signature: readonly unknown[]): string { return `performance-path:${encodeURIComponent(JSON.stringify(signature))}`; } describe("Toolcraft performance path codec", () => { it("round trips one canonical runtime signature", () => { const pathId = createToolcraftPerformancePathId(canonicalPath); expect(decodeToolcraftPerformancePathId(pathId)).toEqual(canonicalPath); }); it.each([ [ "unknown profile", ["unknown", "control-change", ["composite"], ["main"], []], ], [ "unknown interaction", ["interactive-discrete", "unknown", ["composite"], ["main"], []], ], [ "mismatched profile", ["interactive-continuous", "control-change", ["composite"], ["main"], []], ], [ "duplicate pass", [ "interactive-discrete", "control-change", ["composite", "composite"], ["main"], [], ], ], [ "unsorted pass", [ "interactive-discrete", "control-change", ["source", "composite"], ["main"], [], ], ], [ "unknown run location", [ "interactive-discrete", "control-change", ["composite"], ["satellite"], [], ], ], [ "duplicate dimension", [ "interactive-discrete", "control-change", ["composite"], ["main"], ["pixels", "pixels"], ], ], ])("rejects %s", (_case, signature) => { expect(decodeToolcraftPerformancePathId(encodeSignature(signature))).toBeUndefined(); }); it("rejects non-array and non-canonically encoded JSON", () => { expect( decodeToolcraftPerformancePathId( `performance-path:${encodeURIComponent(JSON.stringify({ ...canonicalPath }))}`, ), ).toBeUndefined(); expect( decodeToolcraftPerformancePathId( `${createToolcraftPerformancePathId(canonicalPath)}%20`, ), ).toBeUndefined(); }); });