File size: 2,031 Bytes
4450790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { app } from "scripts/app.js";
import { NodeTypesString } from "../constants.js";
import { wait } from "rgthree/common/shared_utils.js";
import type { LGraphNode } from "typings/litegraph.js";

type addNodeOptions = {
  placement?: string;
};

/**

 * A testing environment to make setting up, clearing, and queuing more predictable in an

 * integration test environment.

 */
export class ComfyUITestEnvironment {
  private lastNode: LGraphNode | null = null;
  private maxY = 0;

  constructor() {}

  async addNode(nodeString: string, options: addNodeOptions = {}) {
    const [canvas, graph] = [app.canvas, app.graph];
    const node = LiteGraph.createNode(nodeString);
    let x = 0;
    let y = 30;
    if (this.lastNode) {
      const placement = options.placement || "right";
      if (placement === "under") {
        x = this.lastNode.pos[0];
        y = this.lastNode.pos[1] + this.lastNode.size[1] + 30;
      } else if (placement === "right") {
        x = this.lastNode.pos[0] + this.lastNode.size[0] + 100;
        y = this.lastNode.pos[1];
      } else if (placement === "start") {
        x = 0;
        y = this.maxY + 50;
      }
    }
    canvas.graph.add(node);
    node.pos = [x, y];
    canvas.selectNode(node);
    app.graph.setDirtyCanvas(true, true);
    await wait();
    this.lastNode = node;
    this.maxY = Math.max(this.maxY, y + this.lastNode.size[1]);
    return (this.lastNode = node);
  }

  async clear() {
    app.clean();
    app.graph.clear();
    const nodeConfig = await this.addNode(NodeTypesString.KSAMPLER_CONFIG);
    const displayAny = await this.addNode(NodeTypesString.DISPLAY_ANY);
    nodeConfig.widgets[0]!.value = Math.round(Math.random() * 100);
    nodeConfig.connect(0, displayAny, 0);
    await this.queuePrompt();
    app.clean();
    app.graph.clear();
    this.lastNode = null;
    this.maxY = 0;
    await wait();
  }

  async queuePrompt() {
    await app.queuePrompt();
    await wait(150);
  }
}