diff --git a/src/mint.ts b/src/mint.ts index 3433822..1c1b537 100644 --- a/src/mint.ts +++ b/src/mint.ts @@ -132,6 +132,7 @@ export function mint(workflow: WorkflowJSON, catalog: WidgetCatalog, catalogVers meta.set("__definitions_extra", extra); const defsRoot = definitionsMap(doc); for (const sg of Array.isArray(subgraphs) ? (subgraphs as SubgraphDef[]) : []) { + if (sg.id === undefined || sg.id === null) throw new TypeError("mint: definition is missing id"); defsRoot.set(String(sg.id), mintDefinition(sg, catalog)); } } diff --git a/test/mint-definition-id.regression.test.ts b/test/mint-definition-id.regression.test.ts new file mode 100644 index 0000000..fe118ee --- /dev/null +++ b/test/mint-definition-id.regression.test.ts @@ -0,0 +1,41 @@ +import { describe, expect, it } from "vitest"; +import { mint, project } from "../src/index.js"; +import type { WorkflowJSON } from "../src/index.js"; +import { loadCatalog } from "./helpers.js"; + +const catalog = loadCatalog(); + +// Preserved review: https://github.com/Comfy-Org/ComfyUI_frontend/pull/16644#discussion_r3926236102 +describe("top-level definition identity", () => { + it.each([ + ["absent", {}], + ["undefined", { id: undefined }], + ["null", { id: null }], + ])("regression: rejects %s definition IDs instead of silently overwriting subgraphs", (_label, identity) => { + const workflow: WorkflowJSON = { + nodes: [], + links: [], + definitions: { + subgraphs: [ + { ...identity, name: "first", nodes: [], links: [] }, + { ...identity, name: "second", nodes: [], links: [] }, + ], + }, + }; + + expect(() => mint(workflow, catalog)).toThrow("mint: definition is missing id"); + }); + + it("preserves zero and literal null/undefined string IDs with their distinct payloads", () => { + const definitions = { + subgraphs: [ + { id: 0, name: "zero", nodes: [], links: [] }, + { id: "null", name: "literal-null", nodes: [], links: [] }, + { id: "undefined", name: "literal-undefined", nodes: [], links: [] }, + ], + }; + const workflow: WorkflowJSON = { nodes: [], links: [], definitions }; + + expect(project(mint(workflow, catalog), catalog).definitions).toEqual(definitions); + }); +});