diff --git a/src/mint.ts b/src/mint.ts index 1c1b537..f5bb4fb 100644 --- a/src/mint.ts +++ b/src/mint.ts @@ -228,18 +228,18 @@ export function importedLinkState(raw: unknown, workflow: WorkflowJSON): Importe } /** - * The `links` map key for one interior link of a definition: the tuple's - * `[0]` for the litegraph array form, `id` for the frontend's object form, and - * the link's mint position for anything else (so an id-less entry still - * occupies its own key rather than colliding on `"undefined"`). + * The explicit `links` map key for one interior link of a definition: the + * tuple's `[0]` for the litegraph array form or `id` for the frontend's object + * form. ID-less links receive a collision-free positional key in + * `mintDefinition` after all explicit keys have been reserved. */ -function definitionLinkKey(ln: unknown, index: number): string { +function definitionLinkKey(ln: unknown): string | undefined { if (Array.isArray(ln) && ln[0] !== undefined) return String(ln[0]); if (typeof ln === "object" && ln !== null && !Array.isArray(ln)) { const id = (ln as { id?: unknown }).id; if (id !== undefined && id !== null) return String(id); } - return `#${String(index)}`; + return undefined; } export function mintDefinition(sg: SubgraphDef, catalog: WidgetCatalog): Y.Map { @@ -263,6 +263,13 @@ export function mintDefinition(sg: SubgraphDef, catalog: WidgetCatalog): Y.Map(); const order: string[] = []; + const usedKeys = new Set(); + for (const link of v) { + const key = definitionLinkKey(link); + if (key === undefined) continue; + if (usedKeys.has(key)) throw new TypeError(`mint: duplicate definition link id '${key}'`); + usedKeys.add(key); + } v.forEach((ln, i) => { // A definition's interior links are serialized by the frontend as // OBJECTS (`{id, origin_id, origin_slot, target_id, target_slot, @@ -270,7 +277,13 @@ export function mintDefinition(sg: SubgraphDef, catalog: WidgetCatalog): Y.Map (wf["definitions"] as { subgraphs: { links: unknown[] }[] }).subgraphs[0]!.links; +function withInteriorLinks(links: unknown[]): WorkflowJSON { + const wf = zImageTemplate(); + (wf["definitions"] as { subgraphs: { links: unknown[] }[] }).subgraphs[0]!.links = links; + return wf; +} + +function interiorLinkStorage(doc: Y.Doc): { keys: string[]; order: string[] } { + const definition = doc.getMap>("definitions").get(DEF)!; + const links = definition.get("links") as Y.Map; + return { + keys: [...links.keys()], + order: definition.get("link_order") as string[], + }; +} + describe("mint → project keeps a definition's interior links distinct (frontend object form)", () => { it("round-trips all 18 links of the z-image turbo definition with exact ids, origins, targets and slots, in authored order", () => { const out = project(mint(zImageTemplate(), catalog), catalog); @@ -122,4 +138,60 @@ describe("mint → project keeps a definition's interior links distinct (fronten const out = project(mint(wf, catalog), catalog); expect(interiorLinks(out)).toEqual(sg.links); }); + + it("reserves a later explicit '#0' before allocating the id-less link at position zero", () => { + const idless = { origin_id: 101, origin_slot: 1, target_id: 102, target_slot: 2, type: "IDLESS" }; + const explicit = { id: "#0", origin_id: 201, origin_slot: 3, target_id: 202, target_slot: 4, type: "EXPLICIT" }; + const doc = mint(withInteriorLinks([idless, explicit]), catalog); + + expect(interiorLinks(project(doc, catalog))).toEqual([idless, explicit]); + expect(interiorLinkStorage(doc)).toEqual({ keys: ["#0~1", "#0"], order: ["#0~1", "#0"] }); + }); + + it("preserves reverse-order noncollision keys and exact asymmetric projected values", () => { + const explicit = { id: "#0", origin_id: 301, origin_slot: 5, target_id: 302, target_slot: 6, type: "EXPLICIT-FIRST" }; + const idless = { origin_id: 401, origin_slot: 7, target_id: 402, target_slot: 8, type: "IDLESS-SECOND" }; + const doc = mint(withInteriorLinks([explicit, idless]), catalog); + + expect(interiorLinks(project(doc, catalog))).toEqual([explicit, idless]); + expect(interiorLinkStorage(doc)).toEqual({ keys: ["#0", "#1"], order: ["#0", "#1"] }); + }); + + it("loops past explicit suffix adversaries while keeping every internal key and order entry unique", () => { + const idless = { origin_id: 501, origin_slot: 9, target_id: 502, target_slot: 10, type: "IDLESS" }; + const explicitBase = { id: "#0", origin_id: 601, origin_slot: 11, target_id: 602, target_slot: 12, type: "BASE" }; + const explicitSuffix = { id: "#0~1", origin_id: 701, origin_slot: 13, target_id: 702, target_slot: 14, type: "SUFFIX" }; + const doc = mint(withInteriorLinks([idless, explicitBase, explicitSuffix]), catalog); + const storage = interiorLinkStorage(doc); + + expect(interiorLinks(project(doc, catalog))).toEqual([idless, explicitBase, explicitSuffix]); + expect(storage).toEqual({ keys: ["#0~2", "#0", "#0~1"], order: ["#0~2", "#0", "#0~1"] }); + expect(new Set(storage.keys).size).toBe(3); + expect(new Set(storage.order).size).toBe(3); + }); + + it("reserves normalized tuple IDs before allocating synthetic keys", () => { + const idless = { origin_id: 801, origin_slot: 15, target_id: 802, target_slot: 16, type: "IDLESS" }; + const tuple = ["#0", 901, 17, 902, 18, "TUPLE"]; + const doc = mint(withInteriorLinks([idless, tuple]), catalog); + + expect(interiorLinks(project(doc, catalog))).toEqual([idless, tuple]); + expect(interiorLinkStorage(doc)).toEqual({ keys: ["#0~1", "#0"], order: ["#0~1", "#0"] }); + }); + + it.each([ + ["tuple then object", [[1, 1001, 19, 1002, 20, "TUPLE-FIRST"], { id: "1", origin_id: 1101, origin_slot: 21, target_id: 1102, target_slot: 22, type: "OBJECT-SECOND" }]], + ["object then tuple", [{ id: "1", origin_id: 1201, origin_slot: 23, target_id: 1202, target_slot: 24, type: "OBJECT-FIRST" }, [1, 1301, 25, 1302, 26, "TUPLE-SECOND"]]], + ])("refuses duplicate explicit normalized link IDs in %s order", (_name, links) => { + expect(() => mint(withInteriorLinks(links), catalog)).toThrow(new TypeError("mint: duplicate definition link id '1'")); + }); + + it("accepts distinct normalized tuple and object IDs with exact asymmetric payloads", () => { + const tuple = [1, 1401, 27, 1402, 28, "TUPLE"]; + const object = { id: "2", origin_id: 1501, origin_slot: 29, target_id: 1502, target_slot: 30, type: "OBJECT" }; + const doc = mint(withInteriorLinks([tuple, object]), catalog); + + expect(interiorLinks(project(doc, catalog))).toEqual([tuple, object]); + expect(interiorLinkStorage(doc)).toEqual({ keys: ["1", "2"], order: ["1", "2"] }); + }); });