Skip to content

Commit 699cec6

Browse files
AustinMrozDrJKL
authored andcommitted
Add tests
1 parent 5a28c64 commit 699cec6

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { createPinia, setActivePinia } from 'pinia'
2+
import { describe, expect, test } from 'vitest'
3+
import { LGraphNode } from '@/lib/litegraph/src/litegraph'
4+
import { transformInputSpecV1ToV2 } from '@/schemas/nodeDef/migration'
5+
import type { InputSpec } from '@/schemas/nodeDefSchema'
6+
import { useLitegraphService } from '@/services/litegraphService'
7+
8+
setActivePinia(createPinia())
9+
type DynamicInputs = ('INT' | 'STRING' | 'IMAGE' | DynamicInputs)[][]
10+
11+
const { addNodeInput } = useLitegraphService()
12+
13+
function addDynamicCombo(node: LGraphNode, inputs: DynamicInputs) {
14+
const namePrefix = `${node.widgets?.length ?? 0}`
15+
function getSpec(
16+
inputs: DynamicInputs,
17+
depth: number = 0
18+
): { key: string; inputs: object }[] {
19+
return inputs.map((group, groupIndex) => {
20+
const inputs = group.map((input, inputIndex) => [
21+
`${namePrefix}.${depth}.${inputIndex}`,
22+
Array.isArray(input)
23+
? ['COMFY_DYNAMICCOMBO_V3', { options: getSpec(input, depth + 1) }]
24+
: [input, {}]
25+
])
26+
return {
27+
key: `${groupIndex}`,
28+
inputs: { required: Object.fromEntries(inputs) }
29+
}
30+
})
31+
}
32+
const inputSpec: Required<InputSpec> = [
33+
'COMFY_DYNAMICCOMBO_V3',
34+
{ options: getSpec(inputs) }
35+
]
36+
addNodeInput(
37+
node,
38+
transformInputSpecV1ToV2(inputSpec, { name: namePrefix, isOptional: false })
39+
)
40+
}
41+
function testNode() {
42+
const node: LGraphNode & {
43+
_initialMinSize?: { width: number; height: number }
44+
} = new LGraphNode('test')
45+
node.widgets = []
46+
node._initialMinSize = { width: 1, height: 1 }
47+
node.constructor.nodeData = {
48+
name: 'testnode'
49+
} as typeof node.constructor.nodeData
50+
return node as LGraphNode & Required<Pick<LGraphNode, 'widgets'>>
51+
}
52+
53+
describe('Dynamic Combos', () => {
54+
test('Can add widget on selection', () => {
55+
const node = testNode()
56+
addDynamicCombo(node, [['INT'], ['INT', 'STRING']])
57+
expect(node.widgets.length).toBe(2)
58+
node.widgets[0].value = '1'
59+
expect(node.widgets.length).toBe(3)
60+
})
61+
test('Can add nested widgets', () => {
62+
const node = testNode()
63+
addDynamicCombo(node, [['INT'], [[[], ['STRING']]]])
64+
expect(node.widgets.length).toBe(2)
65+
node.widgets[0].value = '1'
66+
expect(node.widgets.length).toBe(2)
67+
node.widgets[1].value = '1'
68+
expect(node.widgets.length).toBe(3)
69+
})
70+
test('Can add input', () => {
71+
const node = testNode()
72+
addDynamicCombo(node, [['INT'], ['IMAGE']])
73+
expect(node.widgets.length).toBe(2)
74+
node.widgets[0].value = '1'
75+
expect(node.widgets.length).toBe(1)
76+
expect(node.inputs.length).toBe(2)
77+
expect(node.inputs[1].type).toBe('IMAGE')
78+
})
79+
test('Dynamically added inputs are well ordered', () => {
80+
const node = testNode()
81+
addDynamicCombo(node, [['INT'], ['IMAGE']])
82+
addDynamicCombo(node, [['INT'], ['IMAGE']])
83+
node.widgets[2].value = '1'
84+
node.widgets[0].value = '1'
85+
expect(node.widgets.length).toBe(2)
86+
expect(node.inputs.length).toBe(4)
87+
expect(node.inputs[1].name).toBe('0.0.0')
88+
expect(node.inputs[3].name).toBe('2.0.0')
89+
})
90+
})

0 commit comments

Comments
 (0)