|
| 1 | +// @ts-check |
| 2 | +/// <reference path="../node_modules/@types/jest/index.d.ts" /> |
| 3 | +const { start } = require("../utils"); |
| 4 | +const lg = require("../utils/litegraph"); |
| 5 | + |
| 6 | +describe("extensions", () => { |
| 7 | + beforeEach(() => { |
| 8 | + lg.setup(global); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + lg.teardown(global); |
| 13 | + }); |
| 14 | + |
| 15 | + it("calls each extension hook", async () => { |
| 16 | + const mockExtension = { |
| 17 | + name: "TestExtension", |
| 18 | + init: jest.fn(), |
| 19 | + setup: jest.fn(), |
| 20 | + addCustomNodeDefs: jest.fn(), |
| 21 | + getCustomWidgets: jest.fn(), |
| 22 | + beforeRegisterNodeDef: jest.fn(), |
| 23 | + registerCustomNodes: jest.fn(), |
| 24 | + loadedGraphNode: jest.fn(), |
| 25 | + nodeCreated: jest.fn(), |
| 26 | + beforeConfigureGraph: jest.fn(), |
| 27 | + afterConfigureGraph: jest.fn(), |
| 28 | + }; |
| 29 | + |
| 30 | + const { app, ez, graph } = await start({ |
| 31 | + async preSetup(app) { |
| 32 | + app.registerExtension(mockExtension); |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + // Basic initialisation hooks should be called once, with app |
| 37 | + expect(mockExtension.init).toHaveBeenCalledTimes(1); |
| 38 | + expect(mockExtension.init).toHaveBeenCalledWith(app); |
| 39 | + |
| 40 | + // Adding custom node defs should be passed the full list of nodes |
| 41 | + expect(mockExtension.addCustomNodeDefs).toHaveBeenCalledTimes(1); |
| 42 | + expect(mockExtension.addCustomNodeDefs.mock.calls[0][1]).toStrictEqual(app); |
| 43 | + const defs = mockExtension.addCustomNodeDefs.mock.calls[0][0]; |
| 44 | + expect(defs).toHaveProperty("KSampler"); |
| 45 | + expect(defs).toHaveProperty("LoadImage"); |
| 46 | + |
| 47 | + // Get custom widgets is called once and should return new widget types |
| 48 | + expect(mockExtension.getCustomWidgets).toHaveBeenCalledTimes(1); |
| 49 | + expect(mockExtension.getCustomWidgets).toHaveBeenCalledWith(app); |
| 50 | + |
| 51 | + // Before register node def will be called once per node type |
| 52 | + const nodeNames = Object.keys(defs); |
| 53 | + const nodeCount = nodeNames.length; |
| 54 | + expect(mockExtension.beforeRegisterNodeDef).toHaveBeenCalledTimes(nodeCount); |
| 55 | + for (let i = 0; i < nodeCount; i++) { |
| 56 | + // It should be send the JS class and the original JSON definition |
| 57 | + const nodeClass = mockExtension.beforeRegisterNodeDef.mock.calls[i][0]; |
| 58 | + const nodeDef = mockExtension.beforeRegisterNodeDef.mock.calls[i][1]; |
| 59 | + |
| 60 | + expect(nodeClass.name).toBe("ComfyNode"); |
| 61 | + expect(nodeClass.comfyClass).toBe(nodeNames[i]); |
| 62 | + expect(nodeDef.name).toBe(nodeNames[i]); |
| 63 | + expect(nodeDef).toHaveProperty("input"); |
| 64 | + expect(nodeDef).toHaveProperty("output"); |
| 65 | + } |
| 66 | + |
| 67 | + // Register custom nodes is called once after registerNode defs to allow adding other frontend nodes |
| 68 | + expect(mockExtension.registerCustomNodes).toHaveBeenCalledTimes(1); |
| 69 | + |
| 70 | + // Before configure graph will be called here as the default graph is being loaded |
| 71 | + expect(mockExtension.beforeConfigureGraph).toHaveBeenCalledTimes(1); |
| 72 | + // it gets sent the graph data that is going to be loaded |
| 73 | + const graphData = mockExtension.beforeConfigureGraph.mock.calls[0][0]; |
| 74 | + |
| 75 | + // A node created is fired for each node constructor that is called |
| 76 | + expect(mockExtension.nodeCreated).toHaveBeenCalledTimes(graphData.nodes.length); |
| 77 | + for (let i = 0; i < graphData.nodes.length; i++) { |
| 78 | + expect(mockExtension.nodeCreated.mock.calls[i][0].type).toBe(graphData.nodes[i].type); |
| 79 | + } |
| 80 | + |
| 81 | + // Each node then calls loadedGraphNode to allow them to be updated |
| 82 | + expect(mockExtension.loadedGraphNode).toHaveBeenCalledTimes(graphData.nodes.length); |
| 83 | + for (let i = 0; i < graphData.nodes.length; i++) { |
| 84 | + expect(mockExtension.loadedGraphNode.mock.calls[i][0].type).toBe(graphData.nodes[i].type); |
| 85 | + } |
| 86 | + |
| 87 | + // After configure is then called once all the setup is done |
| 88 | + expect(mockExtension.afterConfigureGraph).toHaveBeenCalledTimes(1); |
| 89 | + |
| 90 | + expect(mockExtension.setup).toHaveBeenCalledTimes(1); |
| 91 | + expect(mockExtension.setup).toHaveBeenCalledWith(app); |
| 92 | + |
| 93 | + // Ensure hooks are called in the correct order |
| 94 | + const callOrder = [ |
| 95 | + "init", |
| 96 | + "addCustomNodeDefs", |
| 97 | + "getCustomWidgets", |
| 98 | + "beforeRegisterNodeDef", |
| 99 | + "registerCustomNodes", |
| 100 | + "beforeConfigureGraph", |
| 101 | + "nodeCreated", |
| 102 | + "loadedGraphNode", |
| 103 | + "afterConfigureGraph", |
| 104 | + "setup", |
| 105 | + ]; |
| 106 | + for (let i = 1; i < callOrder.length; i++) { |
| 107 | + const fn1 = mockExtension[callOrder[i - 1]]; |
| 108 | + const fn2 = mockExtension[callOrder[i]]; |
| 109 | + expect(fn1.mock.invocationCallOrder[0]).toBeLessThan(fn2.mock.invocationCallOrder[0]); |
| 110 | + } |
| 111 | + |
| 112 | + graph.clear(); |
| 113 | + |
| 114 | + // Ensure adding a new node calls the correct callback |
| 115 | + ez.LoadImage(); |
| 116 | + expect(mockExtension.loadedGraphNode).toHaveBeenCalledTimes(graphData.nodes.length); |
| 117 | + expect(mockExtension.nodeCreated).toHaveBeenCalledTimes(graphData.nodes.length + 1); |
| 118 | + expect(mockExtension.nodeCreated.mock.lastCall[0].type).toBe("LoadImage"); |
| 119 | + |
| 120 | + // Reload the graph to ensure correct hooks are fired |
| 121 | + await graph.reload(); |
| 122 | + |
| 123 | + // These hooks should not be fired again |
| 124 | + expect(mockExtension.init).toHaveBeenCalledTimes(1); |
| 125 | + expect(mockExtension.addCustomNodeDefs).toHaveBeenCalledTimes(1); |
| 126 | + expect(mockExtension.getCustomWidgets).toHaveBeenCalledTimes(1); |
| 127 | + expect(mockExtension.registerCustomNodes).toHaveBeenCalledTimes(1); |
| 128 | + expect(mockExtension.beforeRegisterNodeDef).toHaveBeenCalledTimes(nodeCount); |
| 129 | + expect(mockExtension.setup).toHaveBeenCalledTimes(1); |
| 130 | + |
| 131 | + // These should be called again |
| 132 | + expect(mockExtension.beforeConfigureGraph).toHaveBeenCalledTimes(2); |
| 133 | + expect(mockExtension.nodeCreated).toHaveBeenCalledTimes(graphData.nodes.length + 2); |
| 134 | + expect(mockExtension.loadedGraphNode).toHaveBeenCalledTimes(graphData.nodes.length + 1); |
| 135 | + expect(mockExtension.afterConfigureGraph).toHaveBeenCalledTimes(2); |
| 136 | + }); |
| 137 | + |
| 138 | + it("allows custom nodeDefs and widgets to be registered", async () => { |
| 139 | + const widgetMock = jest.fn((node, inputName, inputData, app) => { |
| 140 | + expect(node.constructor.comfyClass).toBe("TestNode"); |
| 141 | + expect(inputName).toBe("test_input"); |
| 142 | + expect(inputData[0]).toBe("CUSTOMWIDGET"); |
| 143 | + expect(inputData[1]?.hello).toBe("world"); |
| 144 | + expect(app).toStrictEqual(app); |
| 145 | + |
| 146 | + return { |
| 147 | + widget: node.addWidget("button", inputName, "hello", () => {}), |
| 148 | + }; |
| 149 | + }); |
| 150 | + |
| 151 | + // Register our extension that adds a custom node + widget type |
| 152 | + const mockExtension = { |
| 153 | + name: "TestExtension", |
| 154 | + addCustomNodeDefs: (nodeDefs) => { |
| 155 | + nodeDefs["TestNode"] = { |
| 156 | + output: [], |
| 157 | + output_name: [], |
| 158 | + output_is_list: [], |
| 159 | + name: "TestNode", |
| 160 | + display_name: "TestNode", |
| 161 | + category: "Test", |
| 162 | + input: { |
| 163 | + required: { |
| 164 | + test_input: ["CUSTOMWIDGET", { hello: "world" }], |
| 165 | + }, |
| 166 | + }, |
| 167 | + }; |
| 168 | + }, |
| 169 | + getCustomWidgets: jest.fn(() => { |
| 170 | + return { |
| 171 | + CUSTOMWIDGET: widgetMock, |
| 172 | + }; |
| 173 | + }), |
| 174 | + }; |
| 175 | + |
| 176 | + const { graph, ez } = await start({ |
| 177 | + async preSetup(app) { |
| 178 | + app.registerExtension(mockExtension); |
| 179 | + }, |
| 180 | + }); |
| 181 | + |
| 182 | + expect(mockExtension.getCustomWidgets).toBeCalledTimes(1); |
| 183 | + |
| 184 | + graph.clear(); |
| 185 | + expect(widgetMock).toBeCalledTimes(0); |
| 186 | + const node = ez.TestNode(); |
| 187 | + expect(widgetMock).toBeCalledTimes(1); |
| 188 | + |
| 189 | + // Ensure our custom widget is created |
| 190 | + expect(node.inputs.length).toBe(0); |
| 191 | + expect(node.widgets.length).toBe(1); |
| 192 | + const w = node.widgets[0].widget; |
| 193 | + expect(w.name).toBe("test_input"); |
| 194 | + expect(w.type).toBe("button"); |
| 195 | + }); |
| 196 | +}); |
0 commit comments