move to describe

This commit is contained in:
pythongosssss 2023-10-13 17:28:11 +01:00
parent a2fafda0e0
commit 8040581506

View File

@ -4,163 +4,165 @@
const { start } = require("../utils"); const { start } = require("../utils");
const lg = require("../utils/litegraph"); const lg = require("../utils/litegraph");
beforeEach(() => {
lg.setup(global);
});
afterEach(() => { describe("widget inputs", () => {
lg.teardown(global); beforeEach(() => {
jest.resetModules(); lg.setup(global);
});
[
{ name: "int", type: "INT", widget: "number", control: true },
{ name: "float", type: "FLOAT", widget: "number", control: true },
{ name: "text", type: "STRING" },
{
name: "customtext",
type: "STRING",
opt: { multiline: true },
},
{ name: "toggle", type: "BOOLEAN" },
{ name: "combo", type: ["a", "b", "c"], control: true },
].forEach((c) => {
test(`widget conversion + primitive works on ${c.name}`, async () => {
/**
* Test node with widgets of each type
* @type { import("../../web/types/comfy").ComfyObjectInfo } ComfyObjectInfo
*/
const WidgetTestNode = {
category: "test",
name: "WidgetTestNode",
output_name: [],
input: {
required: {
[c.name]: [c.type, c.opt ?? {}],
},
},
};
const { ez } = await start({
mockNodeDefs: {
WidgetTestNode,
},
});
// Create test node and convert to input
const n = ez.WidgetTestNode();
const w = n.widgets[c.name];
w.convertToInput();
expect(w.isConvertedToInput).toBeTruthy();
const input = w.getConvertedInput();
expect(input).toBeTruthy();
// Connect to primitive
const p1 = ez.PrimitiveNode();
// @ts-ignore : input is valid
p1.outputs[0].connectTo(input);
expect(p1.outputs[0].connectTo).toHaveLength(1);
// Ensure widget is correct type
const valueWidget = p1.widgets.value;
expect(valueWidget.widget.type).toBe(c.widget ?? c.name);
// Check if control_after_generate should be added
if (c.control) {
const controlWidget = p1.widgets.control_after_generate;
expect(controlWidget.widget.type).toBe("combo");
}
// Ensure we dont have other widgets
expect(p1.node.widgets).toHaveLength(1 + +!!c.control);
}); });
});
afterEach(() => {
lg.teardown(global);
jest.resetModules();
});
test("converted widget works after reload", async () => { [
const { graph, ez } = await start(); { name: "int", type: "INT", widget: "number", control: true },
let n = ez.CheckpointLoaderSimple(); { name: "float", type: "FLOAT", widget: "number", control: true },
{ name: "text", type: "STRING" },
{
name: "customtext",
type: "STRING",
opt: { multiline: true },
},
{ name: "toggle", type: "BOOLEAN" },
{ name: "combo", type: ["a", "b", "c"], control: true },
].forEach((c) => {
test(`widget conversion + primitive works on ${c.name}`, async () => {
/**
* Test node with widgets of each type
* @type { import("../../web/types/comfy").ComfyObjectInfo } ComfyObjectInfo
*/
const WidgetTestNode = {
category: "test",
name: "WidgetTestNode",
output_name: [],
input: {
required: {
[c.name]: [c.type, c.opt ?? {}],
},
},
};
const inputCount = n.inputs.length; const { ez } = await start({
mockNodeDefs: {
WidgetTestNode,
},
});
// Convert ckpt name to an input // Create test node and convert to input
n.widgets.ckpt_name.convertToInput(); const n = ez.WidgetTestNode();
expect(n.widgets.ckpt_name.isConvertedToInput).toBeTruthy(); const w = n.widgets[c.name];
expect(n.inputs.ckpt_name).toBeTruthy(); w.convertToInput();
expect(n.inputs.length).toEqual(inputCount + 1); expect(w.isConvertedToInput).toBeTruthy();
const input = w.getConvertedInput();
expect(input).toBeTruthy();
// Convert back to widget and ensure input is removed // Connect to primitive
n.widgets.ckpt_name.convertToWidget(); const p1 = ez.PrimitiveNode();
expect(n.widgets.ckpt_name.isConvertedToInput).toBeFalsy(); // @ts-ignore : input is valid
expect(n.inputs.ckpt_name).toBeFalsy(); p1.outputs[0].connectTo(input);
expect(n.inputs.length).toEqual(inputCount); expect(p1.outputs[0].connectTo).toHaveLength(1);
// Convert again and reload the graph to ensure it maintains state // Ensure widget is correct type
n.widgets.ckpt_name.convertToInput(); const valueWidget = p1.widgets.value;
expect(n.inputs.length).toEqual(inputCount + 1); expect(valueWidget.widget.type).toBe(c.widget ?? c.name);
let primitive = ez.PrimitiveNode(); // Check if control_after_generate should be added
primitive.outputs[0].connectTo(n.inputs.ckpt_name); if (c.control) {
const controlWidget = p1.widgets.control_after_generate;
expect(controlWidget.widget.type).toBe("combo");
}
await graph.reload(); // Ensure we dont have other widgets
expect(p1.node.widgets).toHaveLength(1 + +!!c.control);
});
});
// Find the reloaded nodes in the graph test("converted widget works after reload", async () => {
n = graph.find(n); const { graph, ez } = await start();
primitive = graph.find(primitive); let n = ez.CheckpointLoaderSimple();
// Ensure widget is converted const inputCount = n.inputs.length;
expect(n.widgets.ckpt_name.isConvertedToInput).toBeTruthy();
expect(n.inputs.ckpt_name).toBeTruthy();
expect(n.inputs.length).toEqual(inputCount + 1);
// Ensure primitive is connected // Convert ckpt name to an input
let { connections } = primitive.outputs[0]; n.widgets.ckpt_name.convertToInput();
expect(connections).toHaveLength(1); expect(n.widgets.ckpt_name.isConvertedToInput).toBeTruthy();
expect(connections[0].targetNode.id).toBe(n.node.id); expect(n.inputs.ckpt_name).toBeTruthy();
expect(n.inputs.length).toEqual(inputCount + 1);
// Disconnect & reconnect // Convert back to widget and ensure input is removed
connections[0].disconnect(); n.widgets.ckpt_name.convertToWidget();
({ connections } = primitive.outputs[0]); expect(n.widgets.ckpt_name.isConvertedToInput).toBeFalsy();
expect(connections).toHaveLength(0); expect(n.inputs.ckpt_name).toBeFalsy();
primitive.outputs[0].connectTo(n.inputs.ckpt_name); expect(n.inputs.length).toEqual(inputCount);
({ connections } = primitive.outputs[0]); // Convert again and reload the graph to ensure it maintains state
expect(connections).toHaveLength(1); n.widgets.ckpt_name.convertToInput();
expect(connections[0].targetNode.id).toBe(n.node.id); expect(n.inputs.length).toEqual(inputCount + 1);
// Convert back to widget and ensure input is removed let primitive = ez.PrimitiveNode();
n.widgets.ckpt_name.convertToWidget(); primitive.outputs[0].connectTo(n.inputs.ckpt_name);
expect(n.widgets.ckpt_name.isConvertedToInput).toBeFalsy();
expect(n.inputs.ckpt_name).toBeFalsy();
expect(n.inputs.length).toEqual(inputCount);
});
test("converted widget works on clone", async () => { await graph.reload();
const { graph, ez } = await start();
let n = ez.CheckpointLoaderSimple();
// Convert the widget to an input // Find the reloaded nodes in the graph
n.widgets.ckpt_name.convertToInput(); n = graph.find(n);
expect(n.widgets.ckpt_name.isConvertedToInput).toBeTruthy(); primitive = graph.find(primitive);
// Clone the node // Ensure widget is converted
n.menu["Clone"].call(); expect(n.widgets.ckpt_name.isConvertedToInput).toBeTruthy();
expect(graph.nodes).toHaveLength(2); expect(n.inputs.ckpt_name).toBeTruthy();
const clone = graph.nodes[1]; expect(n.inputs.length).toEqual(inputCount + 1);
expect(clone.id).not.toEqual(n.id);
// Ensure the clone has an input // Ensure primitive is connected
expect(clone.widgets.ckpt_name.isConvertedToInput).toBeTruthy(); let { connections } = primitive.outputs[0];
expect(clone.inputs.ckpt_name).toBeTruthy(); expect(connections).toHaveLength(1);
expect(connections[0].targetNode.id).toBe(n.node.id);
// Ensure primitive connects to both nodes // Disconnect & reconnect
let primitive = ez.PrimitiveNode(); connections[0].disconnect();
primitive.outputs[0].connectTo(n.inputs.ckpt_name); ({ connections } = primitive.outputs[0]);
primitive.outputs[0].connectTo(clone.inputs.ckpt_name); expect(connections).toHaveLength(0);
expect(primitive.outputs[0].connections).toHaveLength(2); primitive.outputs[0].connectTo(n.inputs.ckpt_name);
// Convert back to widget and ensure input is removed ({ connections } = primitive.outputs[0]);
clone.widgets.ckpt_name.convertToWidget(); expect(connections).toHaveLength(1);
expect(clone.widgets.ckpt_name.isConvertedToInput).toBeFalsy(); expect(connections[0].targetNode.id).toBe(n.node.id);
expect(clone.inputs.ckpt_name).toBeFalsy();
}); // Convert back to widget and ensure input is removed
n.widgets.ckpt_name.convertToWidget();
expect(n.widgets.ckpt_name.isConvertedToInput).toBeFalsy();
expect(n.inputs.ckpt_name).toBeFalsy();
expect(n.inputs.length).toEqual(inputCount);
});
test("converted widget works on clone", async () => {
const { graph, ez } = await start();
let n = ez.CheckpointLoaderSimple();
// Convert the widget to an input
n.widgets.ckpt_name.convertToInput();
expect(n.widgets.ckpt_name.isConvertedToInput).toBeTruthy();
// Clone the node
n.menu["Clone"].call();
expect(graph.nodes).toHaveLength(2);
const clone = graph.nodes[1];
expect(clone.id).not.toEqual(n.id);
// Ensure the clone has an input
expect(clone.widgets.ckpt_name.isConvertedToInput).toBeTruthy();
expect(clone.inputs.ckpt_name).toBeTruthy();
// Ensure primitive connects to both nodes
let primitive = ez.PrimitiveNode();
primitive.outputs[0].connectTo(n.inputs.ckpt_name);
primitive.outputs[0].connectTo(clone.inputs.ckpt_name);
expect(primitive.outputs[0].connections).toHaveLength(2);
// Convert back to widget and ensure input is removed
clone.widgets.ckpt_name.convertToWidget();
expect(clone.widgets.ckpt_name.isConvertedToInput).toBeFalsy();
expect(clone.inputs.ckpt_name).toBeFalsy();
});
});