|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { RunId, WaitpointId, SnapshotId, setKsuidMintEnabled } from "./friendlyId.js"; |
| 3 | +import { |
| 4 | + ownerEngine, |
| 5 | + classifyResidency, |
| 6 | + classifyKind, |
| 7 | + isClassifiable, |
| 8 | + UnclassifiableRunId, |
| 9 | +} from "./runOpsResidency.js"; |
| 10 | + |
| 11 | +afterEach(() => setKsuidMintEnabled(false)); // never leak W0-FND-01 flag state |
| 12 | + |
| 13 | +const SAMPLES = 50_000; // property-scale; CI-fast. (Bump locally toward "millions" per DoD.) |
| 14 | + |
| 15 | +describe("ownerEngine — residency classifier", () => { |
| 16 | + it("cuid-length ids (mint flag OFF) classify LEGACY, friendly + internal", () => { |
| 17 | + setKsuidMintEnabled(false); |
| 18 | + for (const util of [RunId, WaitpointId]) { |
| 19 | + const { id, friendlyId } = util.generate(); |
| 20 | + expect(ownerEngine(id)).toBe("LEGACY"); |
| 21 | + expect(ownerEngine(friendlyId)).toBe("LEGACY"); // strips run_/waitpoint_ prefix |
| 22 | + expect(classifyResidency(id)).toBe("LEGACY"); // alias agrees |
| 23 | + expect(classifyKind(id)).toBe("cuid"); |
| 24 | + expect(isClassifiable(id)).toBe(true); |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + it("ksuid-length ids (mint flag ON) classify NEW, friendly + internal", () => { |
| 29 | + setKsuidMintEnabled(true); |
| 30 | + for (const util of [RunId, WaitpointId]) { |
| 31 | + const { id, friendlyId } = util.generate(); |
| 32 | + expect(ownerEngine(id)).toBe("NEW"); |
| 33 | + expect(ownerEngine(friendlyId)).toBe("NEW"); |
| 34 | + expect(classifyResidency(id)).toBe("NEW"); |
| 35 | + expect(classifyKind(id)).toBe("ksuid"); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + it("disjointness: no cuid sample is ever NEW, no ksuid sample is ever LEGACY", () => { |
| 40 | + for (let i = 0; i < SAMPLES; i++) { |
| 41 | + setKsuidMintEnabled(false); |
| 42 | + expect(ownerEngine(RunId.generate().id)).toBe("LEGACY"); |
| 43 | + setKsuidMintEnabled(true); |
| 44 | + expect(ownerEngine(RunId.generate().id)).toBe("NEW"); |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + it("throws UnclassifiableRunId on malformed lengths (24, 26, 28, empty)", () => { |
| 49 | + for (const bad of ["", "x".repeat(24), "x".repeat(26), "x".repeat(28), "x".repeat(40)]) { |
| 50 | + expect(() => ownerEngine(bad)).toThrow(UnclassifiableRunId); |
| 51 | + } |
| 52 | + }); |
| 53 | + |
| 54 | + it("error carries the offending value + length for diagnostics", () => { |
| 55 | + try { |
| 56 | + ownerEngine("x".repeat(26)); |
| 57 | + throw new Error("should have thrown"); |
| 58 | + } catch (e) { |
| 59 | + expect(e).toBeInstanceOf(UnclassifiableRunId); |
| 60 | + expect((e as UnclassifiableRunId).message).toContain("26"); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + it("SnapshotId (always cuid, even with flag ON) classifies LEGACY — proves snapshot needs no residency key", () => { |
| 65 | + setKsuidMintEnabled(true); |
| 66 | + expect(ownerEngine(SnapshotId.generate().id)).toBe("LEGACY"); |
| 67 | + }); |
| 68 | +}); |
0 commit comments