Files
comfyui_o1key/tests/test_o1key_image_generator_layout.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

201 lines
6.9 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs";
import vm from "node:vm";
import { webcrypto } from "node:crypto";
const sourcePath = new URL("../web/js/o1keyImageGenerator.js", import.meta.url);
const source = fs.readFileSync(sourcePath, "utf8").replace(/^import .*;\s*$/gm, "");
let nextNodeId = 1;
let nextLinkId = 1;
const graph = {
nodes: new Map(),
links: new Map(),
add(node) {
node.id = nextNodeId++;
node.graph = this;
this.nodes.set(node.id, node);
},
getNodeById(id) { return this.nodes.get(id); },
beforeChange() {},
afterChange() {},
setDirtyCanvas() {},
};
const LiteGraph = {
createNode(type) {
return {
type,
comfyClass: type,
pos: [0, 0],
size: [300, 260],
properties: {},
inputs: [{ link: null }],
widgets: [],
};
},
};
const app = {
graph,
ui: { settings: { getSettingValue: () => false } },
registerExtension() {},
};
const api = {
apiURL: (value) => value,
addEventListener() {},
fetchApi: async () => ({ ok: true, json: async () => ({}) }),
};
class Element {
constructor(tagName) {
this.tagName = String(tagName).toUpperCase();
this.children = [];
this.className = "";
this.style = {};
this.dataset = {};
this.listeners = new Map();
}
append(...items) { this.children.push(...items); }
prepend(...items) { this.children.unshift(...items); }
replaceChildren(...items) { this.children = [...items]; }
addEventListener(type, listener) { this.listeners.set(type, listener); }
setAttribute(name, value) { this[name] = value; }
classList = {
values: new Set(),
add(v) { this.values.add(v); },
remove(v) { this.values.delete(v); },
contains(v) { return this.values.has(v); },
toggle(v, force) {
const on = force ?? !this.values.has(v);
if (on) this.values.add(v); else this.values.delete(v);
return on;
},
};
}
const document = {
head: { append() {}, appendChild() {} },
body: { append() {} },
createElement(tag) { return new Element(tag); },
createElementNS(_ns, tag) { return new Element(tag); },
getElementById() { return null; },
querySelector() { return null; },
addEventListener() {},
removeEventListener() {},
};
const context = vm.createContext({
app,
api,
document,
window: { LiteGraph },
URL,
URLSearchParams,
FormData,
Event,
openReferenceImageEditor: async () => null,
crypto: webcrypto,
console,
clearTimeout,
setTimeout,
requestAnimationFrame: (cb) => cb(),
});
vm.runInContext(source, context, { filename: sourcePath.pathname });
const createSaveNodeForBatch = vm.runInContext("createSaveNodeForBatch", context);
const nextHorizontalSaveNodePos = vm.runInContext("nextHorizontalSaveNodePos", context);
const measureNodeExtent = vm.runInContext("measureNodeExtent", context);
const generatorPanelMinHeight = vm.runInContext("generatorPanelMinHeight", context);
const generatorMinimumHeight = vm.runInContext("generatorMinimumHeight", context);
const generatorChromeHeight = vm.runInContext("generatorChromeHeight", context);
const isVueNodesEnabled = vm.runInContext("isVueNodesEnabled", context);
const SAVE_NODE_LAYOUT_GAP_X = vm.runInContext("SAVE_NODE_LAYOUT_GAP_X", context);
const SAVE_NODE_LAYOUT_ORIGIN_GAP_X = vm.runInContext("SAVE_NODE_LAYOUT_ORIGIN_GAP_X", context);
const SAVE_NODE_LAYOUT_SIZE = vm.runInContext("SAVE_NODE_LAYOUT_SIZE", context);
assert.equal(isVueNodesEnabled(), false);
assert.equal(SAVE_NODE_LAYOUT_GAP_X, 50);
assert.equal(SAVE_NODE_LAYOUT_ORIGIN_GAP_X, 70);
{
const generator = { pos: [100, 200], size: [500, 1025] };
const firstPos = nextHorizontalSaveNodePos(generator, []);
assert.equal(firstPos[0], 100 + 500 + SAVE_NODE_LAYOUT_ORIGIN_GAP_X);
assert.equal(firstPos[1], 200 + 30);
const firstPeer = { pos: [700, 230], size: [300, 260] };
const secondPos = nextHorizontalSaveNodePos(generator, [firstPeer]);
assert.equal(secondPos[0], 700 + 300 + SAVE_NODE_LAYOUT_GAP_X);
assert.equal(secondPos[1], 230);
const secondPeer = { pos: [1050, 230], size: [407, 440] };
const thirdPos = nextHorizontalSaveNodePos(generator, [firstPeer, secondPeer]);
assert.equal(thirdPos[0], 1050 + 407 + SAVE_NODE_LAYOUT_GAP_X);
assert.equal(thirdPos[1], 230);
const blocking = {
pos: [700 + 300 + SAVE_NODE_LAYOUT_GAP_X, 230],
size: [...SAVE_NODE_LAYOUT_SIZE],
};
const blockedPos = nextHorizontalSaveNodePos(generator, [firstPeer, blocking]);
assert.equal(blockedPos[0], blocking.pos[0] + blocking.size[0] + SAVE_NODE_LAYOUT_GAP_X);
assert.equal(blockedPos[1], 230);
}
{
LiteGraph.vueNodesMode = true;
assert.equal(isVueNodesEnabled(), true);
assert.equal(generatorChromeHeight(), 16);
const generator = { pos: [10, 20], size: [560, 1035] };
const pos = nextHorizontalSaveNodePos(generator, []);
assert.equal(pos[0], 10 + 560 + SAVE_NODE_LAYOUT_ORIGIN_GAP_X);
assert.equal(pos[1], 20); // no classic +30 offset under Vue Nodes
LiteGraph.vueNodesMode = false;
}
{
assert.equal(generatorMinimumHeight({ _o1igModel: { value: "Nano Banana 2" } }), 1025);
assert.equal(
generatorMinimumHeight({
_o1igModel: { value: "gpt-image-2" },
_o1igBatchEnabled: true,
}),
1300,
);
assert.ok(generatorPanelMinHeight({ _o1igModel: { value: "Nano Banana 2" } }) >= 292);
assert.equal(measureNodeExtent({ pos: [1, 2], size: [0, 0] }).width, SAVE_NODE_LAYOUT_SIZE[0]);
assert.equal(measureNodeExtent({ pos: [1, 2], size: [410, 500] }).right, 411);
}
{
const generator = {
id: 100,
graph,
pos: [20, 30],
size: [410, 340],
outputs: [{ links: [] }],
connect(_originSlot, target) {
const linkId = nextLinkId++;
graph.links.set(linkId, { origin_id: this.id, target_id: target.id });
this.outputs[0].links.push(linkId);
target.inputs[0].link = linkId;
},
};
graph.nodes.set(generator.id, generator);
const first = createSaveNodeForBatch(generator, "11111111-1111-4111-8111-111111111111");
const second = createSaveNodeForBatch(generator, "22222222-2222-4222-8222-222222222222");
assert.equal(first.pos[0], generator.pos[0] + generator.size[0] + SAVE_NODE_LAYOUT_ORIGIN_GAP_X);
assert.equal(first.pos[1], generator.pos[1] + 30);
assert.equal(second.pos[0], first.pos[0] + first.size[0] + SAVE_NODE_LAYOUT_GAP_X);
assert.equal(second.pos[1], first.pos[1]);
assert.ok(second.pos[0] > first.pos[0]);
}
assert.doesNotMatch(source, /batchIndex % 3/);
assert.match(source, /function nextHorizontalSaveNodePos\(/);
assert.match(source, /getMinHeight: \(\) => generatorPanelMinHeight\(node\)/);
assert.match(source, /Comfy\.VueNodes\.Enabled/);
console.log("layout placement tests passed");