import assert from "node:assert/strict"; import fs from "node:fs"; import vm from "node:vm"; const sourcePath = new URL("../web/js/gptImageQuality.js", import.meta.url); const source = fs.readFileSync(sourcePath, "utf8").replace(/^import .*;\s*$/gm, ""); let extension; vm.runInNewContext(source, { app: { registerExtension(value) { extension = value; } }, }); const backgroundLabelsPath = new URL("../web/js/gptImageBackgroundLabels.js", import.meta.url); const backgroundLabelsSource = fs.readFileSync(backgroundLabelsPath, "utf8").replace(/^import .*;\s*$/gm, ""); let backgroundLabelsExtension; vm.runInNewContext(backgroundLabelsSource, { app: { registerExtension(value) { backgroundLabelsExtension = value; } }, }); function makeNode(model, quality, nodeType = "O1keyGPTImage") { const calls = []; const modelWidget = { name: "模型", value: model, callback() { calls.push("model"); }, }; const qualityWidget = { name: "质量", value: quality, options: { values: ["高", "中", "低", "自动", "超高", "最高"] }, callback(value) { calls.push(value); }, }; return { comfyClass: nodeType, widgets: [modelWidget, qualityWidget], dirty: 0, setDirtyCanvas() { this.dirty += 1; }, modelWidget, qualityWidget, calls, }; } const node = makeNode("gpt-image-2", "自动"); extension.nodeCreated(node); assert.deepEqual(Array.from(node.qualityWidget.options.values), ["高", "中", "低", "自动"]); const newNode = makeNode("gpt-image-2.5-sunburst", "自动"); extension.nodeCreated(newNode); assert.deepEqual( Array.from(newNode.qualityWidget.options.values), ["高", "中", "低", "自动", "超高", "最高"], ); node.modelWidget.value = "gpt-image-2.5-flare"; node.modelWidget.callback(); assert.deepEqual( Array.from(node.qualityWidget.options.values), ["高", "中", "低", "自动", "超高", "最高"], ); node.qualityWidget.value = "最高"; node.modelWidget.value = "gpt-image-2"; node.modelWidget.callback(); assert.equal(node.qualityWidget.value, "自动"); assert.deepEqual(Array.from(node.qualityWidget.options.values), ["高", "中", "低", "自动"]); assert.equal(node.calls.at(-1), "自动"); const restoredNode = makeNode("gpt-image-2.5-sunburst", "最高"); extension.loadedGraphNode(restoredNode); assert.equal(restoredNode.qualityWidget.value, "最高"); assert.deepEqual( Array.from(restoredNode.qualityWidget.options.values), ["高", "中", "低", "自动", "超高", "最高"], ); const batchNode = makeNode("gpt-image-2.5-flare", "超高", "O1keyGPTImageBatch"); extension.nodeCreated(batchNode); assert.deepEqual( Array.from(batchNode.qualityWidget.options.values), ["高", "中", "低", "自动", "超高", "最高"], ); const batchBackgroundWidget = { name: "背景", value: "opaque", options: { values: ["auto", "transparent", "opaque"] }, }; backgroundLabelsExtension.nodeCreated({ comfyClass: "O1keyGPTImageBatch", widgets: [batchBackgroundWidget], }); assert.equal(batchBackgroundWidget.options.getOptionLabel("auto"), "自动"); assert.equal(batchBackgroundWidget.options.getOptionLabel("transparent"), "透明"); assert.equal(batchBackgroundWidget.options.getOptionLabel("opaque"), "不透明"); const backgroundWidget = { name: "背景", value: "transparent", options: { values: ["auto", "transparent", "opaque"] }, }; const backgroundNode = { comfyClass: "O1keyGPTImage", widgets: [backgroundWidget], }; backgroundLabelsExtension.nodeCreated(backgroundNode); assert.equal(backgroundWidget.options.getOptionLabel("auto"), "自动"); assert.equal(backgroundWidget.options.getOptionLabel("transparent"), "透明"); assert.equal(backgroundWidget.options.getOptionLabel("opaque"), "不透明"); assert.equal(backgroundWidget.value, "transparent"); assert.deepEqual(Array.from(backgroundWidget.options.values), ["auto", "transparent", "opaque"]);