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