Files
comfyui_o1key/tests/test_gpt_image_quality_frontend.mjs
T
Jony a181cc1768 Add MJ image node, GIF save, and Kling/image generator updates
Ship o1key MJ图片生成 with content/style refs and prompt flags, save-GIF with preview, Kling 4.0 helpers, plus GPT/unified image node polish and docs.
2026-11-09 09:11:12 +08:00

166 lines
6.5 KiB
JavaScript

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"]);
for (const nodeType of ["O1keyGPTImage", "O1keyGPTImageBatch"]) {
const node = makeNode("gpt-image-2.6-sunburst", "最高", nodeType);
const size = () => [150, 20];
const complexity = { name: "复杂度", value: "中", options: {}, computeSize: size };
node.widgets.push(complexity);
extension.nodeCreated(node);
const callback = node.modelWidget.callback;
extension.nodeCreated(node);
assert.equal(node.modelWidget.callback, callback, "Extension binding remains idempotent");
assert.equal(node.qualityWidget.value, "最高");
assert.equal(complexity.hidden, false);
assert.equal(complexity.computeSize, size);
node.modelWidget.value = "gpt-image-2.5-flare";
node.modelWidget.callback();
assert.equal(complexity.hidden, true);
assert.equal(complexity.options.hidden, true);
assert.equal(complexity.value, "中");
assert.deepEqual(Array.from(complexity.computeSize()), [0, -4]);
node.modelWidget.value = "gpt-image-2.6-flare";
node.onConfigure({});
extension.loadedGraphNode(node);
assert.equal(complexity.hidden, false);
assert.equal(complexity.computeSize, size);
assert.equal(complexity.value, "中");
assert.equal(node.qualityWidget.value, "最高");
}
for (const nodeType of ["O1keyGPTImage", "O1keyGPTImageBatch"]) {
const node = makeNode("gpt-image-2.6-sunburst", "最高", nodeType);
const size = () => [150, 20];
const complexity = { name: "复杂度", value: "中", options: {}, computeSize: size };
const formatName = nodeType === "O1keyGPTImageBatch" ? "图片输出格式" : "输出格式";
const format = {
name: formatName,
value: nodeType === "O1keyGPTImageBatch" ? "PNG" : "png",
options: { values: [] },
callback() {},
};
const frameCount = { name: "帧数", value: 9, options: {}, computeSize: size };
const frameDuration = { name: "帧时长ms", value: 200, options: {}, computeSize: size };
node.widgets.push(complexity, format, frameCount, frameDuration);
extension.nodeCreated(node);
assert.equal(complexity.hidden, false);
assert.ok(format.options.values.includes(nodeType === "O1keyGPTImageBatch" ? "GIF" : "gif"));
assert.equal(frameCount.hidden, true);
format.value = nodeType === "O1keyGPTImageBatch" ? "GIF" : "gif";
format.callback();
assert.equal(frameCount.hidden, false);
assert.equal(frameDuration.hidden, false);
node.modelWidget.value = "gpt-image-2";
node.modelWidget.callback();
assert.equal(format.options.values.includes(nodeType === "O1keyGPTImageBatch" ? "GIF" : "gif"), false);
assert.equal(frameCount.hidden, true);
}