File size: 8,160 Bytes
583c1c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type {
  LiteGraph as TLiteGraph,
  LGraphCanvas as TLGraphCanvas,
  LGraph as TLGraph,
  LGraphNode as TLGraphNode,
  Vector2,
  LGraphNode,
} from "typings/litegraph.js";
import {rgthree} from "../rgthree.js";
import {NodeTypesString} from "../constants.js";
import {wait} from "rgthree/common/shared_utils.js";
import {describe, should, beforeEach, expect, describeRun} from "../testing/runner.js";
import {ComfyUITestEnvironment} from "../testing/comfyui_env.js";

declare const LiteGraph: typeof TLiteGraph;

const env = new ComfyUITestEnvironment();

function verifyInputAndOutputName(

  node: LGraphNode,

  index: number,

  inputName: string | null,

  isLinked?: boolean,

) {
  if (inputName != null) {
    expect(node.inputs[index]!.name).toBe(`input ${index} name`, inputName);
  }
  if (isLinked) {
    expect(node.inputs[index]!.link).toBeANumber(`input ${index} connection`);
  } else if (isLinked === false) {
    expect(node.inputs[index]!.link).toBeNullOrUndefined(`input ${index} connection`);
  }
  if (inputName != null) {
    if (inputName === "+") {
      expect(node.outputs[index]).toBeUndefined(`output ${index}`);
    } else {
      let outputName =
        inputName === "base_ctx" ? "CONTEXT" : inputName.replace(/^\+\s/, "").toUpperCase();
      expect(node.outputs[index]!.name).toBe(`output ${index} name`, outputName);
    }
  }
}

function vertifyInputsStructure(node: LGraphNode, expectedLength: number) {
  expect(node.inputs.length).toBe("inputs length", expectedLength);
  expect(node.outputs.length).toBe("outputs length", expectedLength - 1);
  verifyInputAndOutputName(node, expectedLength - 1, "+", false);
}

(window as any).rgthree_tests = (window as any).rgthree_tests || {};
(window as any).rgthree_tests.test_dynamic_context = describe("ContextDynamicTest", async () => {
  let nodeConfig!: TLGraphNode;
  let nodeCtx!: TLGraphNode;

  let lastNode: LGraphNode | null = null;

  await beforeEach(async () => {
    await env.clear();
    lastNode = nodeConfig = await env.addNode(NodeTypesString.KSAMPLER_CONFIG);
    lastNode = nodeCtx = await env.addNode(NodeTypesString.DYNAMIC_CONTEXT);
    nodeConfig.connect(0, nodeCtx, 1); // steps
    nodeConfig.connect(2, nodeCtx, 2); // cfg
    nodeConfig.connect(4, nodeCtx, 3); // scheduler
    nodeConfig.connect(0, nodeCtx, 4); // This is the step.1
    nodeConfig.connect(0, nodeCtx, 5); // This is the step.2
    nodeCtx.disconnectInput(2);
    nodeCtx.disconnectInput(5);
    nodeConfig.connect(0, nodeCtx, 6); // This is the step.3
    nodeCtx.disconnectInput(6);
    await wait();
  });

  await should("add correct inputs", async () => {
    vertifyInputsStructure(nodeCtx, 8);
    let i = 0;
    verifyInputAndOutputName(nodeCtx, i++, "base_ctx", false);
    verifyInputAndOutputName(nodeCtx, i++, "+ steps", true);
    verifyInputAndOutputName(nodeCtx, i++, "+ cfg", false);
    verifyInputAndOutputName(nodeCtx, i++, "+ scheduler", true);
    verifyInputAndOutputName(nodeCtx, i++, "+ steps.1", true);
    verifyInputAndOutputName(nodeCtx, i++, "+ steps.2", false);
    verifyInputAndOutputName(nodeCtx, i++, "+ steps.3", false);
  });

  await should("add evaluate correct outputs", async () => {
    const displayAny1 = await env.addNode(NodeTypesString.DISPLAY_ANY, {placement: "right"});
    const displayAny2 = await env.addNode(NodeTypesString.DISPLAY_ANY, {placement: "under"});
    const displayAny3 = await env.addNode(NodeTypesString.DISPLAY_ANY, {placement: "under"});
    const displayAny4 = await env.addNode(NodeTypesString.DISPLAY_ANY, {placement: "under"});

    nodeCtx.connect(1, displayAny1, 0); // steps
    nodeCtx.connect(3, displayAny2, 0); // scheduler
    nodeCtx.connect(4, displayAny3, 0); // steps.1
    nodeCtx.connect(6, displayAny4, 0); // steps.3 (unlinked)

    await env.queuePrompt();

    expect(displayAny1.widgets![0]!.value).toBe("output 1", 30);
    expect(displayAny2.widgets![0]!.value).toBe("output 3", '"normal"');
    expect(displayAny3.widgets![0]!.value).toBe("output 4", 30);
    expect(displayAny4.widgets![0]!.value).toBe("output 6", "None");
  });

  await describeRun("Nested", async () => {
    let nodeConfig2!: TLGraphNode;
    let nodeCtx2!: TLGraphNode;

    await beforeEach(async () => {
      nodeConfig2 = await env.addNode(NodeTypesString.KSAMPLER_CONFIG, {placement: "start"});
      nodeConfig2.widgets[0]!.value = 111;
      nodeConfig2.widgets[2]!.value = 11.1;
      nodeCtx2 = await env.addNode(NodeTypesString.DYNAMIC_CONTEXT, {placement: "right"});
      nodeConfig2.connect(0, nodeCtx2, 1); // steps
      nodeConfig2.connect(2, nodeCtx2, 2); // cfg
      nodeConfig2.connect(3, nodeCtx2, 3); // sampler
      nodeConfig2.connect(2, nodeCtx2, 4); // This is the cfg.1
      nodeConfig2.connect(0, nodeCtx2, 5); // This is the steps.1
      nodeCtx2.disconnectInput(2);
      nodeCtx2.disconnectInput(5);
      nodeConfig2.connect(2, nodeCtx2, 6); // This is the cfg.2
      nodeCtx2.disconnectInput(6);

      await wait();
    });

    await should("disallow context node to be connected to non-first spot.", async () => {
      // Connect to first node.
      let expectedInputs = 8;

      nodeCtx2.connect(0, nodeCtx, expectedInputs - 1);
      console.log(nodeCtx.inputs);

      vertifyInputsStructure(nodeCtx, expectedInputs);
      verifyInputAndOutputName(nodeCtx, 0, "base_ctx", false);
      verifyInputAndOutputName(nodeCtx, nodeCtx.inputs.length - 1, null, false);

      nodeCtx2.connect(0, nodeCtx, 0);
      expectedInputs = 14;
      vertifyInputsStructure(nodeCtx, expectedInputs);
      verifyInputAndOutputName(nodeCtx, 0, "base_ctx", true);
      verifyInputAndOutputName(nodeCtx, expectedInputs - 1, null, false);
    });

    await should("add inputs from connected above owned.", async () => {
      // Connect to first node.
      nodeCtx2.connect(0, nodeCtx, 0);

      let expectedInputs = 14;
      vertifyInputsStructure(nodeCtx, expectedInputs);
      let i = 0;
      verifyInputAndOutputName(nodeCtx, i++, "base_ctx", true);
      verifyInputAndOutputName(nodeCtx, i++, "steps", false);
      verifyInputAndOutputName(nodeCtx, i++, "cfg", false);
      verifyInputAndOutputName(nodeCtx, i++, "sampler", false);
      verifyInputAndOutputName(nodeCtx, i++, "cfg.1", false);
      verifyInputAndOutputName(nodeCtx, i++, "steps.1", false);
      verifyInputAndOutputName(nodeCtx, i++, "cfg.2", false);
      verifyInputAndOutputName(nodeCtx, i++, "+ steps.2", true);
      verifyInputAndOutputName(nodeCtx, i++, "+ cfg.3", false);
      verifyInputAndOutputName(nodeCtx, i++, "+ scheduler", true);
      verifyInputAndOutputName(nodeCtx, i++, "+ steps.3", true);
      verifyInputAndOutputName(nodeCtx, i++, "+ steps.4", false);
      verifyInputAndOutputName(nodeCtx, i++, "+ steps.5", false);
      verifyInputAndOutputName(nodeCtx, i++, "+", false);
    });

    await should("add then remove inputs when disconnected.", async () => {
      // Connect to first node.
      nodeCtx2.connect(0, nodeCtx, 0);

      let expectedInputs = 14;
      expect(nodeCtx.inputs.length).toBe("inputs length", expectedInputs);
      expect(nodeCtx.outputs.length).toBe("outputs length", expectedInputs - 1);

      nodeCtx.disconnectInput(0);

      expectedInputs = 8;
      expect(nodeCtx.inputs.length).toBe("inputs length", expectedInputs);
      expect(nodeCtx.outputs.length).toBe("outputs length", expectedInputs - 1);
      let i = 0;
      verifyInputAndOutputName(nodeCtx, i++, "base_ctx", false);
      verifyInputAndOutputName(nodeCtx, i++, "+ steps", true);
      verifyInputAndOutputName(nodeCtx, i++, "+ cfg", false);
      verifyInputAndOutputName(nodeCtx, i++, "+ scheduler", true);
      verifyInputAndOutputName(nodeCtx, i++, "+ steps.1", true);
      verifyInputAndOutputName(nodeCtx, i++, "+ steps.2", false);
      verifyInputAndOutputName(nodeCtx, i++, "+ steps.3", false);
      verifyInputAndOutputName(nodeCtx, i++, "+", false);
    });
  });
});