Skip to content

Commit a23285a

Browse files
authored
Fix/add test for execution (#665)
* Add changeset * Add some additional tests and fix import issue
1 parent 2993b05 commit a23285a

File tree

6 files changed

+240
-0
lines changed

6 files changed

+240
-0
lines changed

.changeset/large-dogs-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tokens-studio/graph-editor": patch
3+
---
4+
5+
Fixes an issue with the editor losing the reference to the graph frame when moving between frames

packages/graph-engine/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
".": {
1010
"import": "./dist/index.js",
1111
"types": "./dist/index.d.ts"
12+
},
13+
"./nodes/*":{
14+
"import": "./dist/nodes/*",
15+
"types": "./dist/nodes/*"
1216
}
1317
},
1418
"repository": {

packages/graph-engine/tests/suites/graph/execution.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Graph } from '../../../src/graph/graph.js';
22
import { Node, NumberSchema } from '../../../src/index.js';
33
import { describe, expect, test } from 'vitest';
4+
import Add from '../../../src/nodes/math/add.js';
5+
import Output from '../../../src/nodes/generic/output.js';
46

57
class ExternalNode extends Node {
68
constructor(props) {
@@ -45,4 +47,31 @@ describe('Graph/execution', () => {
4547

4648
expect(graph.nodes.a.outputs.foo.value).toEqual(5);
4749
});
50+
51+
test('works without an input node', async () => {
52+
const graph = new Graph();
53+
54+
const a = new Add({ id: 'a', graph });
55+
const b = new Add({ id: 'b', graph });
56+
57+
a.inputs.a.setValue(2);
58+
a.inputs.b.setValue(3);
59+
60+
a.outputs.value.connect(b.inputs.a);
61+
b.inputs.b.setValue(5);
62+
63+
const output = new Output({ id: 'c', graph });
64+
65+
output.addInput('value', { type: NumberSchema });
66+
67+
b.outputs.value.connect(output.inputs.value);
68+
69+
graph.addNode(a);
70+
graph.addNode(b);
71+
graph.addNode(output);
72+
73+
const result = await graph.execute();
74+
75+
expect(result.output!.value.value).toEqual(10);
76+
});
4877
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Graph } from '../../../../src/graph/graph.js';
2+
import { describe, expect, test } from 'vitest';
3+
import Node from '../../../../src/nodes/logic/and.js';
4+
5+
describe('logic/and', () => {
6+
test('returns true when all inputs are true', async () => {
7+
const graph = new Graph();
8+
const node = new Node({ graph });
9+
10+
node.inputs.inputs.setValue([true, true, true]);
11+
12+
await node.execute();
13+
14+
const result = node.outputs.value.value;
15+
expect(result).toBe(true);
16+
});
17+
18+
test('returns false when any input is false', async () => {
19+
const graph = new Graph();
20+
const node = new Node({ graph });
21+
22+
node.inputs.inputs.setValue([true, false, true]);
23+
24+
await node.execute();
25+
26+
const result = node.outputs.value.value;
27+
expect(result).toBe(false);
28+
});
29+
30+
test('returns false when all inputs are false', async () => {
31+
const graph = new Graph();
32+
const node = new Node({ graph });
33+
34+
node.inputs.inputs.setValue([false, false, false]);
35+
36+
await node.execute();
37+
38+
const result = node.outputs.value.value;
39+
expect(result).toBe(false);
40+
});
41+
42+
test('returns true when inputs array is empty', async () => {
43+
const graph = new Graph();
44+
const node = new Node({ graph });
45+
46+
node.inputs.inputs.setValue([]);
47+
48+
await node.execute();
49+
50+
const result = node.outputs.value.value;
51+
expect(result).toBe(true);
52+
});
53+
});
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { Graph } from '../../../../src/graph/graph.js';
2+
import { describe, expect, test } from 'vitest';
3+
import Node from '../../../../src/nodes/logic/or.js';
4+
5+
describe('logic/or', () => {
6+
test('returns true when any input is true', async () => {
7+
const graph = new Graph();
8+
const node = new Node({ graph });
9+
10+
node.inputs.inputs.setValue([false, true, false]);
11+
12+
await node.execute();
13+
14+
const result = node.outputs.value.value;
15+
expect(result).toBe(true);
16+
});
17+
18+
test('returns false when all inputs are false', async () => {
19+
const graph = new Graph();
20+
const node = new Node({ graph });
21+
22+
node.inputs.inputs.setValue([false, false, false]);
23+
24+
await node.execute();
25+
26+
const result = node.outputs.value.value;
27+
expect(result).toBe(false);
28+
});
29+
30+
test('returns true when all inputs are true', async () => {
31+
const graph = new Graph();
32+
const node = new Node({ graph });
33+
34+
node.inputs.inputs.setValue([true, true, true]);
35+
36+
await node.execute();
37+
38+
const result = node.outputs.value.value;
39+
expect(result).toBe(true);
40+
});
41+
42+
test('returns false when inputs array is empty', async () => {
43+
const graph = new Graph();
44+
const node = new Node({ graph });
45+
46+
node.inputs.inputs.setValue([]);
47+
48+
await node.execute();
49+
50+
const result = node.outputs.value.value;
51+
expect(result).toBe(false);
52+
});
53+
});
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { AnySchema, ToInput } from '../../../../src/index.js';
2+
import { Graph } from '../../../../src/graph/graph.js';
3+
import { describe, expect, test } from 'vitest';
4+
import Node from '../../../../src/nodes/logic/switch.js';
5+
6+
describe('logic/switch', () => {
7+
test('outputs the value of the matching input', async () => {
8+
const graph = new Graph();
9+
const node = new Node({ graph });
10+
11+
node.addInput('foo', { type: AnySchema });
12+
node.addInput('bar', { type: AnySchema });
13+
node.addInput('default', { type: AnySchema });
14+
15+
const inputs = node.inputs as ToInput<{
16+
foo: any;
17+
bar: any;
18+
default: any;
19+
condition: string;
20+
}>;
21+
22+
inputs.foo.setValue('fooValue');
23+
inputs.bar.setValue('barValue');
24+
inputs.default.setValue('defaultValue');
25+
inputs.condition.setValue('foo');
26+
27+
await node.execute();
28+
29+
const result = node.outputs.value.value;
30+
expect(result).toBe('fooValue');
31+
});
32+
33+
test('outputs the default value when no condition matches', async () => {
34+
const graph = new Graph();
35+
const node = new Node({ graph });
36+
37+
node.addInput('foo', { type: AnySchema });
38+
node.addInput('bar', { type: AnySchema });
39+
node.addInput('default', { type: AnySchema });
40+
41+
const inputs = node.inputs as ToInput<{
42+
foo: any;
43+
bar: any;
44+
default: any;
45+
condition: string;
46+
}>;
47+
48+
inputs.foo.setValue('fooValue');
49+
inputs.bar.setValue('barValue');
50+
inputs.default.setValue('defaultValue');
51+
inputs.condition.setValue('baz');
52+
53+
await node.execute();
54+
55+
const result = node.outputs.value.value;
56+
expect(result).toBe('defaultValue');
57+
});
58+
59+
test('outputs undefined when no inputs and no default value', async () => {
60+
const graph = new Graph();
61+
const node = new Node({ graph });
62+
63+
node.inputs.condition.setValue('foo');
64+
65+
await node.execute();
66+
67+
const result = node.outputs.value.value;
68+
expect(result).toBeUndefined();
69+
});
70+
71+
test('outputs the value of the matching input with dynamic inputs', async () => {
72+
const graph = new Graph();
73+
const node = new Node({ graph });
74+
75+
node.addInput('dynamic1', { type: AnySchema });
76+
node.addInput('dynamic2', { type: AnySchema });
77+
node.addInput('default', { type: AnySchema });
78+
79+
const inputs = node.inputs as ToInput<{
80+
dynamic1: any;
81+
dynamic2: any;
82+
default: any;
83+
condition: string;
84+
}>;
85+
86+
inputs.dynamic1.setValue('dynamic1Value');
87+
inputs.dynamic2.setValue('dynamic2Value');
88+
inputs.default.setValue('defaultValue');
89+
inputs.condition.setValue('dynamic2');
90+
91+
await node.execute();
92+
93+
const result = node.outputs.value.value;
94+
expect(result).toBe('dynamic2Value');
95+
});
96+
});

0 commit comments

Comments
 (0)