-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathGraph.test.ts
More file actions
716 lines (682 loc) · 22.7 KB
/
Graph.test.ts
File metadata and controls
716 lines (682 loc) · 22.7 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import { deepClone } from 'common/util/deepClone';
import { Graph } from 'features/nodes/util/graph/generation/Graph';
import type { AnyInvocation, Invocation } from 'services/api/types';
import { assert, AssertionError, is } from 'tsafe';
import { describe, expect, it } from 'vitest';
import { z } from 'zod';
describe('Graph', () => {
describe('constructor', () => {
it('should create a new graph with the correct id', () => {
const g = new Graph('test-id');
expect(g._graph.id).toBe('test-id');
expect(g.id).toBe('test-id');
});
it('should create an id if none is provided', () => {
const g = new Graph();
expect(g._graph.id).not.toBeUndefined();
expect(g.id).not.toBeUndefined();
});
});
describe('addNode', () => {
const testNode = {
id: 'test-node',
type: 'add',
} as const;
it('should add a node to the graph', () => {
const g = new Graph();
g.addNode(testNode);
expect(g._graph.nodes['test-node']).not.toBeUndefined();
expect(g._graph.nodes['test-node']?.type).toBe('add');
});
it('should set is_intermediate to true if not provided', () => {
const g = new Graph();
g.addNode(testNode);
expect(g._graph.nodes['test-node']?.is_intermediate).toBe(true);
});
it('should not overwrite is_intermediate if provided', () => {
const g = new Graph();
g.addNode({
...testNode,
is_intermediate: false,
});
expect(g._graph.nodes['test-node']?.is_intermediate).toBe(false);
});
it('should set use_cache to true if not provided', () => {
const g = new Graph();
g.addNode(testNode);
expect(g._graph.nodes['test-node']?.use_cache).toBe(true);
});
it('should not overwrite use_cache if provided', () => {
const g = new Graph();
g.addNode({
...testNode,
use_cache: false,
});
expect(g._graph.nodes['test-node']?.use_cache).toBe(false);
});
it('should error if the node id is already in the graph', () => {
const g = new Graph();
g.addNode(testNode);
expect(() => g.addNode(testNode)).toThrowError(AssertionError);
});
it('should infer the types if provided', () => {
const g = new Graph();
const node = g.addNode(testNode);
assert(is<Invocation<'add'>>(node));
const g2 = new Graph();
// @ts-expect-error The node object is an `add` type, but the generic is a `sub` type
g2.addNode<'sub'>(testNode);
});
});
describe('updateNode', () => {
const initialNode: Invocation<'add'> = {
id: 'old-id',
type: 'add',
a: 1,
};
it('should update node properties correctly', () => {
const g = new Graph();
const n = g.addNode(deepClone(initialNode));
const updates = { is_intermediate: true, use_cache: true };
const updatedNode = g.updateNode(n, updates);
expect(updatedNode.is_intermediate).toBe(true);
expect(updatedNode.use_cache).toBe(true);
});
it('should allow updating the node id and update related edges', () => {
const g = new Graph();
const n = g.addNode(deepClone(initialNode));
const n2 = g.addNode({ id: 'node-2', type: 'add' });
const n3 = g.addNode({ id: 'node-4', type: 'add' });
const oldId = n.id;
const newId = 'new-id';
const e1 = g.addEdge(n, 'value', n2, 'a');
const e2 = g.addEdge(n3, 'value', n, 'a');
g.updateNode(n, { id: newId });
expect(g.hasNode(newId)).toBe(true);
expect(g.hasNode(oldId)).toBe(false);
expect(e1.source.node_id).toBe(newId);
expect(e2.destination.node_id).toBe(newId);
});
it('should throw an error if updated id already exists', () => {
const g = new Graph();
const n = g.addNode(deepClone(initialNode));
const n2 = g.addNode({
id: 'other-id',
type: 'add',
});
expect(() => g.updateNode(n, { id: n2.id })).toThrowError(AssertionError);
});
it('should preserve other fields not specified in updates', () => {
const g = new Graph();
const n = g.addNode(deepClone(initialNode));
const updatedNode = g.updateNode(n, { b: 3 });
expect(updatedNode.b).toBe(3);
expect(updatedNode.a).toBe(initialNode.a);
});
it('should allow changing multiple properties at once', () => {
const g = new Graph();
const n = g.addNode(deepClone(initialNode));
const updatedNode = g.updateNode(n, { a: 2, b: 3 });
expect(updatedNode.a).toBe(2);
expect(updatedNode.b).toBe(3);
});
it('should handle updates with no changes gracefully', () => {
const g = new Graph();
const n = g.addNode(deepClone(initialNode));
const updates = {};
const updatedNode = g.updateNode(n, updates);
expect(updatedNode).toEqual(n);
});
});
describe('getNodes', () => {
it('should return all nodes in the graph', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'add',
});
const n2 = g.addNode({
id: 'n2',
type: 'sub',
});
expect(g.getNodes()).toEqual([n1, n2]);
});
});
describe('addEdge', () => {
const add: Invocation<'add'> = {
id: 'from-node',
type: 'add',
};
const sub: Invocation<'sub'> = {
id: 'to-node',
type: 'sub',
};
it('should add an edge to the graph with the provided values', () => {
const g = new Graph();
g.addNode(add);
g.addNode(sub);
g.addEdge(add, 'value', sub, 'b');
expect(g._graph.edges.length).toBe(1);
expect(g._graph.edges[0]).toEqual({
source: { node_id: 'from-node', field: 'value' },
destination: { node_id: 'to-node', field: 'b' },
});
});
it('should throw an error if the edge already exists', () => {
const g = new Graph();
g.addEdge(add, 'value', sub, 'b');
expect(() => g.addEdge(add, 'value', sub, 'b')).toThrowError(AssertionError);
});
it('should infer field names', () => {
const g = new Graph();
// @ts-expect-error The first field must be a valid output field of the first type arg
g.addEdge(add, 'not-a-valid-field', add, 'a');
// @ts-expect-error The second field must be a valid input field of the second type arg
g.addEdge(add, 'value', sub, 'not-a-valid-field');
// @ts-expect-error The first field must be any valid output field
g.addEdge(add, 'not-a-valid-field', sub, 'a');
// @ts-expect-error The second field must be any valid input field
g.addEdge(add, 'clip', sub, 'not-a-valid-field');
});
});
describe('addEdgeFromObj', () => {
it('should add an edge to the graph with the provided values', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'add',
});
const n2 = g.addNode({
id: 'n2',
type: 'sub',
});
g.addEdgeFromObj({
source: { node_id: n1.id, field: 'value' },
destination: { node_id: n2.id, field: 'b' },
});
expect(g._graph.edges.length).toBe(1);
expect(g._graph.edges[0]).toEqual({
source: { node_id: n1.id, field: 'value' },
destination: { node_id: n2.id, field: 'b' },
});
});
});
describe('getNode', () => {
const g = new Graph();
const node = g.addNode({
id: 'test-node',
type: 'add',
});
it('should return the node with the provided id', () => {
const n = g.getNode('test-node');
expect(n).toBe(node);
});
it('should throw an error if the node is not found', () => {
expect(() => g.getNode('not-found')).toThrowError(AssertionError);
});
});
describe('deleteNode', () => {
it('should delete the node with the provided id', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'add',
});
const n2 = g.addNode({
id: 'n2',
type: 'add',
});
const n3 = g.addNode({
id: 'n3',
type: 'add',
});
g.addEdge(n1, 'value', n2, 'a');
g.addEdge(n2, 'value', n3, 'a');
// This edge should not be deleted bc it doesn't touch n2
g.addEdge(n1, 'value', n3, 'a');
g.deleteNode(n2.id);
expect(g.hasNode(n1.id)).toBe(true);
expect(g.hasNode(n2.id)).toBe(false);
expect(g.hasNode(n3.id)).toBe(true);
// Should delete edges to and from the node
expect(g.getEdges().length).toBe(1);
});
});
describe('hasNode', () => {
const g = new Graph();
g.addNode({
id: 'test-node',
type: 'add',
});
it('should return true if the node is in the graph', () => {
expect(g.hasNode('test-node')).toBe(true);
});
it('should return false if the node is not in the graph', () => {
expect(g.hasNode('not-found')).toBe(false);
});
});
describe('getEdge', () => {
const g = new Graph();
const add: Invocation<'add'> = {
id: 'from-node',
type: 'add',
};
const sub: Invocation<'sub'> = {
id: 'to-node',
type: 'sub',
};
g.addEdge(add, 'value', sub, 'b');
it('should return the edge with the provided values', () => {
expect(g.getEdge(add, 'value', sub, 'b')).toEqual({
source: { node_id: 'from-node', field: 'value' },
destination: { node_id: 'to-node', field: 'b' },
});
});
it('should throw an error if the edge is not found', () => {
expect(() => g.getEdge(add, 'value', sub, 'a')).toThrowError(AssertionError);
});
});
describe('getEdges', () => {
it('should get all edges in the graph', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'add',
});
const n2 = g.addNode({
id: 'n2',
type: 'add',
});
const n3 = g.addNode({
id: 'n3',
type: 'add',
});
const e1 = g.addEdge(n1, 'value', n2, 'a');
const e2 = g.addEdge(n2, 'value', n3, 'a');
expect(g.getEdges()).toEqual([e1, e2]);
});
});
describe('hasEdge', () => {
const g = new Graph();
const add: Invocation<'add'> = {
id: 'from-node',
type: 'add',
};
const sub: Invocation<'sub'> = {
id: 'to-node',
type: 'sub',
};
g.addEdge(add, 'value', sub, 'b');
it('should return true if the edge is in the graph', () => {
expect(g.hasEdge(add, 'value', sub, 'b')).toBe(true);
});
it('should return false if the edge is not in the graph', () => {
expect(g.hasEdge(add, 'value', sub, 'a')).toBe(false);
});
});
describe('getGraph', () => {
it('should return the graph', () => {
const g = new Graph();
expect(g.getGraph()).toBe(g._graph);
});
it('should raise an error if the graph is invalid', () => {
const g = new Graph();
const add: Invocation<'add'> = {
id: 'from-node',
type: 'add',
};
const sub: Invocation<'sub'> = {
id: 'to-node',
type: 'sub',
};
g.addEdge(add, 'value', sub, 'b');
expect(() => g.getGraph()).toThrowError(AssertionError);
});
});
describe('getGraphSafe', () => {
it('should return the graph even if it is invalid', () => {
const g = new Graph();
const add: Invocation<'add'> = {
id: 'from-node',
type: 'add',
};
const sub: Invocation<'sub'> = {
id: 'to-node',
type: 'sub',
};
g.addEdge(add, 'value', sub, 'b');
expect(g.getGraphSafe()).toBe(g._graph);
});
});
describe('validate', () => {
it('should not throw an error if the graph is valid', () => {
const g = new Graph();
expect(() => g.validate()).not.toThrow();
});
it("should throw an error if the graph contains an edge without a source or destination node that doesn't exist", () => {
const g = new Graph();
// These nodes do not get added to the graph, only used to add the edge
const add: Invocation<'add'> = {
id: 'from-node',
type: 'add',
};
const sub: Invocation<'sub'> = {
id: 'to-node',
type: 'sub',
};
// edge from nowhere to nowhere
g.addEdge(add, 'value', sub, 'b');
expect(() => g.validate()).toThrowError(AssertionError);
});
it('should throw an error if a destination node is not a collect node and has multiple edges to it', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'add',
});
const n2 = g.addNode({
id: 'n2',
type: 'add',
});
const n3 = g.addNode({
id: 'n3',
type: 'add',
});
g.addEdge(n1, 'value', n3, 'a');
g.addEdge(n2, 'value', n3, 'a');
expect(() => g.validate()).toThrowError(AssertionError);
});
it('should not throw an error if a destination node is a collect node and has multiple edges to it', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'add',
});
const n2 = g.addNode({
id: 'n2',
type: 'add',
});
const n3 = g.addNode({
id: 'n3',
type: 'collect',
});
g.addEdge(n1, 'value', n3, 'item');
g.addEdge(n2, 'value', n3, 'item');
expect(() => g.validate()).not.toThrow();
});
});
describe('traversal', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'add',
});
const n2 = g.addNode({
id: 'n2',
type: 'alpha_mask_to_tensor',
});
const n3 = g.addNode({
id: 'n3',
type: 'add',
});
const n4 = g.addNode({
id: 'n4',
type: 'add',
});
const n5 = g.addNode({
id: 'n5',
type: 'add',
});
const e1 = g.addEdge(n1, 'value', n3, 'a');
const e2 = g.addEdge(n2, 'height', n3, 'b');
const e3 = g.addEdge(n3, 'value', n4, 'a');
const e4 = g.addEdge(n3, 'value', n5, 'b');
describe('getEdgesFrom', () => {
it('should return the edges that start at the provided node', () => {
expect(g.getEdgesFrom(n3)).toEqual([e3, e4]);
});
it('should return the edges that start at the provided node and have the provided field', () => {
expect(g.getEdgesFrom(n3, ['value'])).toEqual([e3, e4]);
});
});
describe('getEdgesTo', () => {
it('should return the edges that end at the provided node', () => {
expect(g.getEdgesTo(n3)).toEqual([e1, e2]);
});
it('should return the edges that end at the provided node and have the provided field', () => {
expect(g.getEdgesTo(n3, ['b', 'a'])).toEqual([e1, e2]);
});
});
describe('getIncomers', () => {
it('should return the nodes that have an edge to the provided node', () => {
expect(g.getIncomers(n3)).toEqual([n1, n2]);
});
});
describe('getOutgoers', () => {
it('should return the nodes that the provided node has an edge to', () => {
expect(g.getOutgoers(n3)).toEqual([n4, n5]);
});
});
});
describe('deleteEdgesFrom', () => {
it('should delete edges from the provided node', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'img_resize',
});
const n2 = g.addNode({
id: 'n2',
type: 'add',
});
const _e1 = g.addEdge(n1, 'height', n2, 'a');
const _e2 = g.addEdge(n1, 'width', n2, 'b');
g.deleteEdgesFrom(n1);
expect(g.getEdgesFrom(n1)).toEqual([]);
});
it('should delete edges from the provided node, with the provided field', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'img_resize',
});
const n2 = g.addNode({
id: 'n2',
type: 'add',
});
const n3 = g.addNode({
id: 'n3',
type: 'add',
});
const _e1 = g.addEdge(n1, 'height', n2, 'a');
const e2 = g.addEdge(n1, 'width', n2, 'b');
const e3 = g.addEdge(n1, 'width', n3, 'b');
g.deleteEdgesFrom(n1, ['height']);
expect(g.getEdgesFrom(n1)).toEqual([e2, e3]);
});
});
describe('deleteEdgesTo', () => {
it('should delete edges to the provided node', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'img_resize',
});
const n2 = g.addNode({
id: 'n2',
type: 'add',
});
const _e1 = g.addEdge(n1, 'height', n2, 'a');
const _e2 = g.addEdge(n1, 'width', n2, 'b');
g.deleteEdgesTo(n2);
expect(g.getEdgesTo(n2)).toEqual([]);
});
it('should delete edges to the provided node, with the provided field', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'img_resize',
});
const n2 = g.addNode({
id: 'n2',
type: 'img_resize',
});
const n3 = g.addNode({
id: 'n3',
type: 'add',
});
const _e1 = g.addEdge(n1, 'height', n3, 'a');
const e2 = g.addEdge(n1, 'width', n3, 'b');
const _e3 = g.addEdge(n2, 'width', n3, 'a');
g.deleteEdgesTo(n3, ['a']);
expect(g.getEdgesTo(n3)).toEqual([e2]);
});
});
describe('metadata utils', () => {
describe('getMetadataNode', () => {
it("should get the metadata node, creating it if it doesn't exist", () => {
const g = new Graph();
const metadata = g.getMetadataNode();
expect(metadata.id).toBe(g._metadataNodeId);
expect(metadata.type).toBe('core_metadata');
g.upsertMetadata({ test: 'test' });
const metadata2 = g.getMetadataNode();
expect(metadata2).toHaveProperty('test');
});
});
describe('upsertMetadata', () => {
it('should add metadata to the metadata node', () => {
const g = new Graph();
g.upsertMetadata({ test: 'test' });
const metadata = g.getMetadataNode();
expect(metadata).toHaveProperty('test');
});
it("should overwrite metadata on the metadata node if the strategy is 'replace'", () => {
const g = new Graph();
g.upsertMetadata({ test: { foo: 'test' } }, 'replace');
g.upsertMetadata({ test: { bar: 'test2' } }, 'replace');
const metadata = g.getMetadataNode();
expect(metadata.test).toEqual({ bar: 'test2' });
});
it("should merge keys if the strategy is 'merge'", () => {
const g = new Graph();
g.upsertMetadata({ test: { foo: 'test' }, arr: [1] }, 'merge');
g.upsertMetadata({ test: { bar: 'test2' }, arr: [2] }, 'merge');
const metadata = g.getMetadataNode();
expect(metadata.test).toEqual({ foo: 'test', bar: 'test2' });
expect(metadata.arr).toEqual([1, 2]);
});
});
describe('removeMetadata', () => {
it('should remove metadata from the metadata node', () => {
const g = new Graph();
g.upsertMetadata({ test: 'test', test2: 'test2' });
g.removeMetadata(['test']);
const metadata = g.getMetadataNode();
expect(metadata).not.toHaveProperty('test');
});
it('should remove multiple metadata from the metadata node', () => {
const g = new Graph();
g.upsertMetadata({ test: 'test', test2: 'test2' });
g.removeMetadata(['test', 'test2']);
const metadata = g.getMetadataNode();
expect(metadata).not.toHaveProperty('test');
expect(metadata).not.toHaveProperty('test2');
});
});
describe('addEdgeToMetadata', () => {
it('should add an edge to the metadata node', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'img_resize',
});
g.upsertMetadata({ test: 'test' });
g.addEdgeToMetadata(n1, 'width', 'width');
const metadata = g.getMetadataNode();
expect(g.getEdgesFrom(n1).length).toBe(1);
expect(g.getEdgesTo(metadata as unknown as AnyInvocation).length).toBe(1);
});
});
describe('setMetadataReceivingNode', () => {
it('should set the metadata receiving node', () => {
const g = new Graph();
const n1 = g.addNode({
id: 'n1',
type: 'img_resize',
});
g.upsertMetadata({ test: 'test' });
g.setMetadataReceivingNode(n1);
const metadata = g.getMetadataNode();
expect(g.getEdgesFrom(metadata as unknown as AnyInvocation).length).toBe(1);
expect(g.getEdgesTo(n1).length).toBe(1);
});
});
describe('getModelMetadataField', () => {
it('should return a ModelIdentifierField', () => {
const field = Graph.getModelMetadataField({
key: 'b00ee8df-523d-40d2-9578-597283b07cb2',
hash: 'random:9adf270422f525715297afa1649c4ff007a55f09937f57ca628278305624d194',
path: 'sdxl/main/stable-diffusion-xl-1.0-inpainting-0.1',
file_size: 6123456789,
name: 'stable-diffusion-xl-1.0-inpainting-0.1',
base: 'sdxl',
description: 'sdxl main model stable-diffusion-xl-1.0-inpainting-0.1',
source: '/home/bat/invokeai-4.0.0/models/sdxl/main/stable-diffusion-xl-1.0-inpainting-0.1',
source_type: 'path',
source_api_response: null,
source_url: null,
cover_image: null,
type: 'main',
trigger_phrases: null,
prediction_type: 'epsilon',
default_settings: {
vae: null,
vae_precision: null,
scheduler: null,
steps: null,
cfg_scale: null,
cfg_rescale_multiplier: null,
width: 1024,
height: 1024,
},
variant: 'inpaint',
format: 'diffusers',
repo_variant: 'fp16',
});
expect(field).toEqual({
key: 'b00ee8df-523d-40d2-9578-597283b07cb2',
hash: 'random:9adf270422f525715297afa1649c4ff007a55f09937f57ca628278305624d194',
name: 'stable-diffusion-xl-1.0-inpainting-0.1',
base: 'sdxl',
type: 'main',
});
});
});
});
describe('other utils', () => {
describe('edgeToString', () => {
it('should return a string representation of the edge given the edge fields', () => {
expect(Graph.edgeToString('from-node', 'value', 'to-node', 'b')).toBe('from-node.value -> to-node.b');
});
it('should return a string representation of the edge given an edge object', () => {
expect(
Graph.edgeToString({
source: { node_id: 'from-node', field: 'value' },
destination: { node_id: 'to-node', field: 'b' },
})
).toBe('from-node.value -> to-node.b');
});
});
describe('getId', () => {
it('should create a new uuid v4 id', () => {
const id = Graph.getId();
expect(() => z.string().uuid().parse(id)).not.toThrow();
});
it('should prepend the prefix if provided', () => {
const id = Graph.getId('prefix');
expect(id.startsWith('prefix_')).toBe(true);
});
});
});
});