Skip to content

Commit 4651215

Browse files
fix: type the serialized-link helper against the branded id types
1 parent 522f746 commit 4651215

1 file changed

Lines changed: 47 additions & 19 deletions

File tree

src/lib/litegraph/src/linkDeduplication.conflictingOrigins.test.ts

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@ import { setActivePinia } from 'pinia'
33
import { beforeEach, describe, expect, it, vi } from 'vitest'
44

55
import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
6+
import type {
7+
SerialisableLLink,
8+
SerialisedLLinkArray
9+
} from '@/lib/litegraph/src/LLink'
610
import { useLinkStore } from '@/stores/linkStore'
711
import { graphScopeOf } from '@/types/graphScopeId'
12+
import { toLinkId } from '@/types/linkId'
813
import { toNodeId } from '@/types/nodeId'
14+
import type { NodeId } from '@/types/nodeId'
915

1016
import { conflictingOriginLinksRoot } from './__fixtures__/duplicateLinks'
1117

@@ -23,23 +29,42 @@ function configureConflictingOrigins() {
2329
return graph
2430
}
2531

32+
interface SerializedLinkFields {
33+
origin_id: NodeId
34+
target_id: NodeId
35+
target_slot: number
36+
}
37+
38+
function linkFieldsOf(
39+
link: SerialisedLLinkArray | SerialisableLLink
40+
): SerializedLinkFields {
41+
if (Array.isArray(link)) {
42+
const [, origin_id, , target_id, target_slot] = link
43+
return {
44+
origin_id: toNodeId(origin_id),
45+
target_id: toNodeId(target_id),
46+
target_slot
47+
}
48+
}
49+
return {
50+
origin_id: toNodeId(link.origin_id),
51+
target_id: toNodeId(link.target_id),
52+
target_slot: link.target_slot
53+
}
54+
}
55+
2656
function linksIntoTargetSlot(
2757
links: ReturnType<LGraph['serialize']>['links'],
28-
targetId: number,
58+
targetId: NodeId,
2959
targetSlot: number
30-
) {
31-
return (links ?? []).filter((link) => {
32-
const [, , , target_id, target_slot] = Array.isArray(link)
33-
? link
34-
: [
35-
link.id,
36-
link.origin_id,
37-
link.origin_slot,
38-
link.target_id,
39-
link.target_slot
40-
]
41-
return target_id === targetId && target_slot === targetSlot
42-
})
60+
): SerializedLinkFields[] {
61+
const fields = (links ?? []).map((link) =>
62+
linkFieldsOf(link as SerialisedLLinkArray | SerialisableLLink)
63+
)
64+
return fields.filter(
65+
({ target_id, target_slot }) =>
66+
target_id === targetId && target_slot === targetSlot
67+
)
4368
}
4469

4570
describe('normalizeConfiguredTopology with conflicting origins (#15577)', () => {
@@ -78,10 +103,13 @@ describe('normalizeConfiguredTopology with conflicting origins (#15577)', () =>
78103
it.fails('re-saves the workflow without changing the upstream node', () => {
79104
const graph = configureConflictingOrigins()
80105

81-
const [survivor] = linksIntoTargetSlot(graph.serialize().links, 3, 0)
82-
const origin = Array.isArray(survivor) ? survivor[1] : survivor.origin_id
106+
const [survivor] = linksIntoTargetSlot(
107+
graph.serialize().links,
108+
toNodeId(3),
109+
0
110+
)
83111

84-
expect(origin).toBe(toNodeId(2))
112+
expect(survivor?.origin_id).toBe(toNodeId(2))
85113
})
86114
})
87115

@@ -101,13 +129,13 @@ describe('legacy mirror link creation (#15577 reachability)', () => {
101129
graph.add(target)
102130
sourceA.connect(0, target, 0)
103131

104-
const mirroredId = ++graph.state.lastLinkId
132+
const mirroredId = toLinkId(++graph.state.lastLinkId)
105133
const output = sourceB.outputs[0]
106134
output.links = [...(output.links ?? []), mirroredId]
107135
target.inputs[0].link = mirroredId
108136

109137
expect(
110-
linksIntoTargetSlot(graph.serialize().links, Number(target.id), 0)
138+
linksIntoTargetSlot(graph.serialize().links, target.id, 0)
111139
).toHaveLength(1)
112140
expect(target.getInputLink(0)?.origin_id).toBe(sourceA.id)
113141
})

0 commit comments

Comments
 (0)