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

124 lines
4.7 KiB
JavaScript

/**
* Regression: GPT Image widgets_values must survive reload after 2.6 fields /
* 复杂度 reorder (above 分辨率) and Chinese labels.
* Legacy migrate rules look at the array tail; seed's control_after_generate
* made "补齐缩放和背景" mis-fire and insert 智能缩放/auto before 复杂度.
*/
import assert from "node:assert/strict";
import test from "node:test";
const CONTROL = new Set(["fixed", "increment", "decrement", "randomize"]);
const RESIZE = new Set(["不缩放", "智能缩放"]);
const BG = new Set(["auto", "transparent", "opaque"]);
const MOD = new Set(["自动", "低"]);
const CX = new Set(["low", "medium", "high", "低", "中", "高"]);
const CX_TO_LABEL = Object.freeze({
low: "低", medium: "中", high: "高", 低: "低", 中: "中", 高: "高",
});
function normalizeComplexity(value) {
if (value == null) return "高";
return CX_TO_LABEL[value] ?? CX_TO_LABEL[String(value).toLowerCase()] ?? "高";
}
function placeComplexity(wv, complexity) {
if (CX.has(wv[3])) {
wv[3] = normalizeComplexity(wv[3]);
return;
}
wv.splice(3, 0, normalizeComplexity(complexity === undefined ? "高" : complexity));
}
/** Mirrors peel/repair/restore + O1keyGPTImage tail rules in migrateWorkflow.js */
function migrateGptImageWidgets(values) {
const wv = [...values];
let frameDuration;
let frameCount;
let complexity;
let controlAfterGenerate;
const maybeDuration = wv.at(-1);
const maybeCount = wv.at(-2);
const maybeComplexity = wv.at(-3);
if (
Number.isInteger(maybeDuration)
&& Number.isInteger(maybeCount)
&& CX.has(maybeComplexity)
) {
frameDuration = wv.pop();
frameCount = wv.pop();
complexity = wv.pop();
} else if (Number.isInteger(maybeDuration) && Number.isInteger(maybeCount)) {
frameDuration = wv.pop();
frameCount = wv.pop();
} else if (["low", "medium", "high"].includes(String(wv.at(-1)).toLowerCase())) {
complexity = wv.pop();
}
if (CONTROL.has(wv.at(-3)) && RESIZE.has(wv.at(-2)) && BG.has(wv.at(-1))) {
wv.splice(wv.length - 2, 2);
}
if (CONTROL.has(wv.at(-1))) {
controlAfterGenerate = wv.pop();
}
const alreadyHasTail = (
(Number.isInteger(wv.at(-3)) && RESIZE.has(wv.at(-2)) && BG.has(wv.at(-1)))
|| (RESIZE.has(wv.at(-3)) && BG.has(wv.at(-2)) && MOD.has(wv.at(-1)))
|| (BG.has(wv.at(-3)) && RESIZE.has(wv.at(-2)) && Number.isInteger(wv.at(-1)))
);
if (!alreadyHasTail) wv.push("智能缩放", "auto", "自动");
if (RESIZE.has(wv.at(-3)) && BG.has(wv.at(-2)) && MOD.has(wv.at(-1))) wv.pop();
if (Number.isInteger(wv.at(-3)) && RESIZE.has(wv.at(-2)) && BG.has(wv.at(-1))) {
const start = wv.length - 3;
const [seed, resize, background] = wv.slice(start);
wv.splice(start, 3, background, resize, seed);
}
if (controlAfterGenerate !== undefined) wv.push(controlAfterGenerate);
placeComplexity(wv, complexity);
if (frameCount !== undefined) wv.push(frameCount);
if (frameDuration !== undefined) wv.push(frameDuration);
return wv;
}
const healthy26 = [
"一个在跳舞的美女", "gpt-image-2.6-sunburst", "专线", "高", "智能", 1, "自动", "gif",
"auto", "智能缩放", 123, "randomize", 9, 200,
];
const old25 = [
"prompt", "gpt-image-2.5-sunburst", "畅速", "高", "智能", 1, "自动", "png",
"auto", "智能缩放", 123, "randomize",
];
const old26TailComplexity = [
"一个在跳舞的美女", "gpt-image-2.6-sunburst", "专线", "智能", 1, "自动", "gif",
"auto", "智能缩放", 123, "randomize", "high", 9, 200,
];
const scrambled = [
"一个在跳舞的美女", "gpt-image-2.6-sunburst", "专线", "智能", 1, "自动", "gif",
"auto", "智能缩放", 123, "randomize", "智能缩放", "auto", "high", 9, 200,
];
test("keeps healthy 2.6 gif widgets_values with mid-list 复杂度", () => {
assert.deepEqual(migrateGptImageWidgets(healthy26), healthy26);
});
test("inserts default 复杂度 for pre-2.6 widgets_values", () => {
const input = [
"prompt", "gpt-image-2.5-sunburst", "畅速", "智能", 1, "自动", "png",
"auto", "智能缩放", 123, "randomize",
];
assert.deepEqual(migrateGptImageWidgets(input), old25);
});
test("moves old tail 复杂度 above 分辨率 and localizes label", () => {
assert.deepEqual(migrateGptImageWidgets(old26TailComplexity), healthy26);
});
test("repairs scrambled 复杂度/帧数/帧时长ms shift", () => {
assert.deepEqual(migrateGptImageWidgets(scrambled), healthy26);
});
test("repair is idempotent", () => {
assert.deepEqual(migrateGptImageWidgets(migrateGptImageWidgets(scrambled)), healthy26);
});