-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathlitegraphTestUtils.ts
More file actions
412 lines (391 loc) · 10.6 KB
/
Copy pathlitegraphTestUtils.ts
File metadata and controls
412 lines (391 loc) · 10.6 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import type {
INodeInputSlot,
INodeOutputSlot,
Positionable
} from '@/lib/litegraph/src/interfaces'
import { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget'
import type { LGraphEventMap } from '@/lib/litegraph/src/infrastructure/LGraphEventMap'
import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle'
import type {
CanvasPointerEvent,
ISerialisedGraph,
LGraph,
LGraphCanvas,
LGraphGroup,
LinkNetwork,
LLink,
SerialisableGraph
} from '@/lib/litegraph/src/litegraph'
import { LGraphEventMode, LGraphNode } from '@/lib/litegraph/src/litegraph'
import { fromPartial } from '@total-typescript/shoehorn'
import { vi } from 'vitest'
import type { LoadedComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow'
import type { ChangeTracker } from '@/scripts/changeTracker'
import type { LinkId } from '@/types/linkId'
import { toGroupId } from '@/types/groupId'
import { toLinkId } from '@/types/linkId'
import type { NodeId } from '@/types/nodeId'
import { toNodeId } from '@/types/nodeId'
import type { NodeState } from '@/types/nodeState'
import { usePreviewExposureStore } from '@/stores/previewExposureStore'
import { useWidgetValueStore } from '@/stores/widgetValueStore'
import { zeroUuid } from '@/utils/uuid'
/** Creates a node shell state with minimal required fields. */
export function createNodeState(overrides: Partial<NodeState> = {}): NodeState {
return {
flags: {},
graphId: zeroUuid,
id: toNodeId(1),
inputs: [],
mode: LGraphEventMode.ALWAYS,
outputs: [],
title: 'Test Node',
type: 'TestNode',
...overrides
}
}
/**
* Creates a mock LGraphNode with minimal required properties
*/
export function createMockLGraphNode(
overrides: Partial<LGraphNode> | Record<string, unknown> = {}
): LGraphNode {
const nodeOverrides = overrides as Partial<LGraphNode>
const size = nodeOverrides.size ?? [100, 100]
return fromPartial<LGraphNode>({
id: toNodeId(1),
pos: [0, 0],
size,
renderingSize: size,
title: 'Test Node',
mode: LGraphEventMode.ALWAYS,
...nodeOverrides
})
}
/**
* Creates a mock Positionable object
*/
export function createMockPositionable(
overrides: Partial<Positionable> = {}
): Positionable {
const partial: Partial<Positionable> = {
id: toGroupId(1),
pos: [0, 0],
...overrides
}
return partial as Partial<Positionable> as Positionable
}
/**
* Creates a mock LGraphGroup with minimal required properties
*/
export function createMockLGraphGroup(
overrides: Partial<LGraphGroup> = {}
): LGraphGroup {
const partial: Partial<LGraphGroup> = {
id: toGroupId(1),
pos: [0, 0],
boundingRect: new Rectangle(0, 0, 100, 100),
...overrides
}
return partial as Partial<LGraphGroup> as LGraphGroup
}
/**
* Creates a mock SubgraphNode with sub-nodes
*/
export function createMockSubgraphNode(
subNodes: LGraphNode[],
overrides: Partial<LGraphNode> | Record<string, unknown> = {}
): LGraphNode {
const baseNode = createMockLGraphNode(overrides)
return Object.assign(baseNode, {
isSubgraphNode: () => true,
subgraph: {
nodes: subNodes
}
})
}
/**
* Creates a mock LGraphCanvas with minimal required properties for testing
*/
export function createMockCanvas(
overrides: Partial<LGraphCanvas> | Record<string, unknown> = {}
): LGraphCanvas {
return {
setDirty: vi.fn(),
state: {
selectionChanged: false
},
...(overrides as Partial<LGraphCanvas>)
} as LGraphCanvas
}
/**
* Creates a mock LGraph with trigger function
*/
export function createMockLGraph(overrides: Partial<LGraph> = {}): LGraph {
const nodes = overrides._nodes ?? []
const byId = new Map(nodes.map((node) => [String(node.id), node]))
const graph = fromPartial<LGraph>({
trigger: vi.fn(),
// A real dispatcher: node lifecycle subscribers listen on `graph.events`.
events: new CustomEventTarget<LGraphEventMap>(),
_nodes: nodes,
_groups: [],
links: createMockLinks([]),
getNodeById: (id: NodeId) => byId.get(String(id)) ?? null,
...overrides
})
return Object.assign(graph, { rootGraph: overrides.rootGraph ?? graph })
}
/**
* Creates a mock CanvasPointerEvent
*/
export function createMockCanvasPointerEvent(
canvasX: number,
canvasY: number,
overrides: Partial<CanvasPointerEvent> = {}
): CanvasPointerEvent {
return {
canvasX,
canvasY,
...overrides
} as CanvasPointerEvent
}
/**
* Creates a mock CanvasRenderingContext2D
*/
export function createMockCanvasRenderingContext2D(
overrides: Partial<CanvasRenderingContext2D> = {}
): CanvasRenderingContext2D {
const partial: Partial<CanvasRenderingContext2D> = {
save: vi.fn(),
restore: vi.fn(),
translate: vi.fn(),
scale: vi.fn(),
fillRect: vi.fn(),
strokeRect: vi.fn(),
fillText: vi.fn(),
measureText: vi.fn(() => ({ width: 10 }) as TextMetrics),
beginPath: vi.fn(),
moveTo: vi.fn(),
lineTo: vi.fn(),
stroke: vi.fn(),
fill: vi.fn(),
closePath: vi.fn(),
arc: vi.fn(),
rect: vi.fn(),
clip: vi.fn(),
clearRect: vi.fn(),
setTransform: vi.fn(),
roundRect: vi.fn(),
getTransform: vi.fn(
() => ({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 }) as DOMMatrix
),
font: '',
fillStyle: '',
strokeStyle: '',
lineWidth: 1,
globalAlpha: 1,
textAlign: 'left' as CanvasTextAlign,
textBaseline: 'alphabetic' as CanvasTextBaseline,
...overrides
}
return partial as CanvasRenderingContext2D
}
/**
* Creates a mock LinkNetwork
*/
export function createMockLinkNetwork(
overrides: Partial<LinkNetwork> = {}
): LinkNetwork {
return {
...overrides
} as LinkNetwork
}
/**
* Creates a mock INodeInputSlot
*/
export function createMockNodeInputSlot(
overrides: Partial<INodeInputSlot> = {}
): INodeInputSlot {
return {
name: 'input',
type: '*',
boundingRect: [0, 0, 0, 0],
link: null,
...overrides
}
}
/**
* Creates a mock INodeOutputSlot
*/
export function createMockNodeOutputSlot(
overrides: Partial<INodeOutputSlot> = {}
): INodeOutputSlot {
return {
name: 'output',
type: '*',
boundingRect: [0, 0, 0, 0],
links: null,
...overrides
}
}
/**
* Creates a real LGraphNode instance (not a lightweight mock) with its boundingRect
* property represented as a Float64Array for testing position methods.
*
* Use createMockLGraphNodeWithArrayBoundingRect when:
* - Tests rely on Float64Array boundingRect behavior
* - Tests call position-related methods like updateArea()
* - Tests need actual LGraphNode implementation details
*
* Use createMockLGraphNode when:
* - Tests only need simple/mock-only behavior
* - Tests don't depend on boundingRect being a Float64Array
* - A lightweight mock with minimal properties is sufficient
*
* @param name - The node name/type to pass to the LGraphNode constructor
* @returns A fully constructed LGraphNode instance with Float64Array boundingRect
*/
export function createMockLGraphNodeWithArrayBoundingRect(
name: string
): LGraphNode {
const node = new LGraphNode(name)
// The actual node has a Float64Array boundingRect, we just need to type it correctly
return node
}
/**
* Creates a mock FileList from an array of files
*/
export function createMockFileList(files: File[]): FileList {
const fileList = Object.assign(
{
length: files.length,
item: (index: number) => files[index] ?? null,
[Symbol.iterator]: function* () {
yield* files
}
},
files
)
return fileList as FileList
}
/**
* Creates a mock ChangeTracker for workflow testing
* The ChangeTracker requires a proper ComfyWorkflowJSON structure
*/
export function createMockChangeTracker(
overrides: Partial<ChangeTracker> = {}
): ChangeTracker {
const partial = {
activeState: {
last_node_id: 0,
last_link_id: 0,
nodes: [],
links: [],
groups: [],
config: {},
version: 0.4
},
undoQueue: [],
redoQueue: [],
changeCount: 0,
captureCanvasState: vi.fn(),
checkState: vi.fn(),
deactivate: vi.fn(),
prepareForSave: vi.fn(),
reset: vi.fn(),
restore: vi.fn(),
store: vi.fn(),
...overrides
}
return partial as Partial<ChangeTracker> as ChangeTracker
}
/**
* Creates a mock LoadedComfyWorkflow with sensible defaults
*/
export function createMockLoadedWorkflow(
overrides: Partial<LoadedComfyWorkflow> | Record<string, unknown> = {}
): LoadedComfyWorkflow {
return fromPartial<LoadedComfyWorkflow>({
changeTracker: createMockChangeTracker(),
...overrides
})
}
/**
* Creates a mock MinimapCanvas for minimap testing
*/
export function createMockMinimapCanvas(
overrides: Partial<HTMLCanvasElement> = {}
): HTMLCanvasElement {
const mockGetContext = vi.fn()
mockGetContext.mockImplementation((contextId: string) =>
contextId === '2d' ? createMockCanvas2DContext() : null
)
const partial: Partial<HTMLCanvasElement> = {
width: 200,
height: 200,
clientWidth: 200,
clientHeight: 200,
getContext: mockGetContext as HTMLCanvasElement['getContext'],
...overrides
}
return partial as HTMLCanvasElement
}
/**
* Creates a mock CanvasRenderingContext2D for canvas testing
*/
export function createMockCanvas2DContext(
overrides: Partial<CanvasRenderingContext2D> = {}
): CanvasRenderingContext2D {
const partial: Partial<CanvasRenderingContext2D> = {
clearRect: vi.fn(),
fillRect: vi.fn(),
strokeRect: vi.fn(),
beginPath: vi.fn(),
moveTo: vi.fn(),
lineTo: vi.fn(),
stroke: vi.fn(),
arc: vi.fn(),
fill: vi.fn(),
fillStyle: '',
strokeStyle: '',
lineWidth: 1,
save: vi.fn(),
restore: vi.fn(),
...overrides
}
return partial as CanvasRenderingContext2D
}
export function createMockLLink(overrides: Partial<LLink> = {}): LLink {
const partial: Partial<LLink> = {
id: toLinkId(1),
type: '*',
origin_id: toNodeId(1),
origin_slot: 0,
target_id: toNodeId(2),
target_slot: 0,
_pos: [0, 0],
...overrides
}
return partial as LLink
}
export function createMockLinks(links: LLink[]): LGraph['links'] {
const map = new Map<LinkId, LLink>()
const record: Record<number, LLink> = {}
for (const link of links) {
map.set(link.id, link)
record[link.id] = link
}
return Object.assign(map, record) as LGraph['links']
}
export function reloadSerializedGraph(
serialized: ISerialisedGraph | SerialisableGraph,
graphFactory: () => LGraph
): LGraph {
const payload = JSON.parse(JSON.stringify(serialized)) as typeof serialized
useWidgetValueStore().clearGraph(payload.id)
usePreviewExposureStore().clearGraph(payload.id)
const reloaded = graphFactory()
reloaded.configure(payload)
return reloaded
}