import assert from "node:assert/strict"; import fs from "node:fs"; import vm from "node:vm"; const sourcePath = new URL("../web/js/promptMultiFunctionDynamic.js", import.meta.url); const source = fs.readFileSync(sourcePath, "utf8").replace(/^import .*;\s*$/gm, ""); let extension; const context = { app: { registerExtension(value) { extension = value; }, }, requestAnimationFrame(callback) { callback(); }, Set, }; vm.runInNewContext(source, context, { filename: sourcePath.pathname }); function createNode(modeValue = "全部使用") { let modeCallbackCount = 0; const countComputeSize = () => [220, 20]; const indicesComputeSize = () => [220, 20]; const mode = { name: "功能", value: modeValue, callback() { modeCallbackCount += 1; }, }; const count = { name: "抽取数量", value: 5, options: {}, computeSize: countComputeSize, }; const indices = { name: "指定序号", value: "1,3,5", options: {}, computeSize: indicesComputeSize, }; const node = { comfyClass: "O1keyPromptMultiFunction", widgets: [mode, count, indices], dirtyCalls: 0, setDirtyCanvas() { this.dirtyCalls += 1; }, }; return { node, mode, count, indices, countComputeSize, indicesComputeSize, getModeCallbackCount: () => modeCallbackCount, }; } const state = createNode(); extension.nodeCreated(state.node); assert.equal(state.count.hidden, true, "全部使用时应隐藏抽取数量"); assert.equal(state.indices.hidden, true, "全部使用时应隐藏指定序号"); assert.deepEqual(Array.from(state.count.computeSize()), [0, -4]); assert.deepEqual(Array.from(state.indices.computeSize()), [0, -4]); state.mode.value = "随机抽取n套"; state.mode.callback(state.mode.value); assert.equal(state.count.hidden, false, "随机模式应显示抽取数量"); assert.equal(state.indices.hidden, true, "随机模式应隐藏指定序号"); assert.equal(state.count.computeSize, state.countComputeSize); assert.equal(state.count.value, 5, "隐藏切换不得清空抽取数量"); state.mode.value = "指定序号"; state.mode.callback(state.mode.value); assert.equal(state.count.hidden, true, "指定序号模式应隐藏抽取数量"); assert.equal(state.indices.hidden, false, "指定序号模式应显示序号输入"); assert.equal(state.indices.computeSize, state.indicesComputeSize); assert.equal(state.indices.value, "1,3,5", "隐藏切换不得清空指定序号"); extension.nodeCreated(state.node); state.mode.callback(state.mode.value); assert.equal(state.getModeCallbackCount(), 3, "重复初始化不得叠加控件回调"); const loaded = createNode("指定序号"); loaded.node.comfyClass = undefined; loaded.node.type = "O1keyPromptMultiFunction"; extension.loadedGraphNode(loaded.node); assert.equal(loaded.count.hidden, true); assert.equal(loaded.indices.hidden, false); console.log("prompt multi-function dynamic visibility tests passed");