-
Notifications
You must be signed in to change notification settings - Fork 672
test: repair vacuous save/load round-trip tests for widget labels #15603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
baf13a1
bee1987
df8cc3b
cf6cebe
6e510a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ | |
| * Tests for SubgraphNode instances including construction, | ||
| * IO synchronization, and edge cases. | ||
| */ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' | ||
| import { fromPartial } from '@total-typescript/shoehorn' | ||
|
|
||
| import { | ||
|
|
@@ -398,7 +398,13 @@ describe('SubgraphNode Synchronization', () => { | |
| ) | ||
| }) | ||
|
|
||
| it('should preserve renamed label through serialize/configure round-trip', () => { | ||
| // Not a round trip. `SubgraphNode.configure` rebuilds `this.inputs` from the | ||
| // live `subgraph.inputNode.slots`, and `widgetValueStore` is keyed | ||
| // `graphId:nodeId:name` while `configure` re-adopts the payload's graph id — | ||
| // so reconfiguring the same node in place reads live state, not JSON. This | ||
| // test survives `serialize()` dropping every input label. The reload | ||
| // assertion lives in the sibling test below. | ||
|
DrJKL marked this conversation as resolved.
Outdated
|
||
| it('keeps the renamed label after an in-place reconfigure', () => { | ||
| const subgraph = createTestSubgraph({ | ||
| inputs: [{ name: 'seed', type: 'INT' }] | ||
| }) | ||
|
|
@@ -440,6 +446,73 @@ describe('SubgraphNode Synchronization', () => { | |
| 'My Seed' | ||
| ) | ||
| }) | ||
|
|
||
| it('preserves a renamed label across a real definition reload', () => { | ||
| const subgraph = createTestSubgraph({ | ||
| inputs: [{ name: 'seed', type: 'INT' }] | ||
| }) | ||
|
|
||
| // A registered type: `configure` will not re-create an unregistered node, | ||
| // so an anonymous interior node makes the reload silently empty. | ||
| class InteriorNode extends LGraphNode { | ||
| static override title = 'Interior' | ||
| constructor() { | ||
| super('Interior') | ||
| const slot = this.addInput('value', 'INT') | ||
| slot.widget = { name: 'value' } | ||
| this.addOutput('out', 'INT') | ||
| this.addWidget('number', 'value', 0, () => {}) | ||
| } | ||
| } | ||
| LiteGraph.registerNodeType('test/interior-label-reload', InteriorNode) | ||
| onTestFinished(() => { | ||
| delete LiteGraph.registered_node_types['test/interior-label-reload'] | ||
| }) | ||
|
|
||
| const interiorNode = LiteGraph.createNode('test/interior-label-reload')! | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do you feel about a |
||
| subgraph.add(interiorNode) | ||
| subgraph.inputNode.slots[0].connect(interiorNode.inputs[0], interiorNode) | ||
|
|
||
| const subgraphNode = createTestSubgraphNode(subgraph, { id: 101 }) | ||
| subgraph.inputs[0].label = 'My Seed' | ||
| subgraphNode.inputs[0].label = 'My Seed' | ||
| subgraph.events.dispatch('renaming-input', { | ||
| input: subgraph.inputs[0], | ||
| index: 0, | ||
| oldName: 'seed', | ||
| newName: 'My Seed' | ||
| }) | ||
|
|
||
| // The label must survive as JSON, not as a live object reference: the | ||
| // subgraph *definition* carries it, and the reloaded host reads it back | ||
| // through the reloaded definition. `SubgraphNode.configure` rebuilds | ||
| // `this.inputs` from the live subgraph slots and never reads | ||
| // `info.inputs`, so a same-object reconfigure proves nothing. | ||
|
DrJKL marked this conversation as resolved.
Outdated
|
||
| const definition = JSON.parse( | ||
| JSON.stringify(subgraph.asSerialisable()) | ||
| ) as ReturnType<typeof subgraph.asSerialisable> | ||
| const instance = JSON.parse( | ||
| JSON.stringify(subgraphNode.serialize()) | ||
| ) as ExportedSubgraphInstance | ||
|
|
||
| // Drop the live widget state so the reloaded host cannot re-adopt it: | ||
| // `configure` re-adopts the payload's graph id and `widgetValueStore` is | ||
| // keyed `graphId:nodeId:name`. | ||
|
DrJKL marked this conversation as resolved.
Outdated
|
||
| useWidgetValueStore().clearGraph(subgraphNode.rootGraph.id) | ||
|
|
||
| const reloadedSubgraph = createTestSubgraph({ | ||
| rootGraph: subgraphNode.rootGraph | ||
| }) | ||
| reloadedSubgraph.configure(definition) | ||
|
|
||
| const reloadedHost = createTestSubgraphNode(reloadedSubgraph, { id: 101 }) | ||
| reloadedHost.configure(instance) | ||
|
|
||
| expect(reloadedHost.widgets).toMatchObject([ | ||
| { name: 'seed', label: 'My Seed' } | ||
| ]) | ||
| expect(reloadedHost.inputs[0].label).toBe('My Seed') | ||
| }) | ||
| }) | ||
|
|
||
| describe('SubgraphNode widget name collision on rename', () => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,9 @@ 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 type { UUID } from '@/utils/uuid' | ||
| import { zeroUuid } from '@/utils/uuid' | ||
|
|
||
| /** Creates a node shell state with minimal required fields. */ | ||
|
|
@@ -395,3 +398,37 @@ export function createMockLinks(links: LLink[]): LGraph['links'] { | |
| } | ||
| return Object.assign(map, record) as LGraph['links'] | ||
| } | ||
|
|
||
| /** | ||
| * Reloads a serialized graph the way a fresh page would, and returns the new | ||
| * graph. | ||
| * | ||
| * Save/load assertions written as `configure(...serialize())` are vacuous by | ||
| * default. `LGraph.clear()` drops the widget store under the graph's | ||
| * *pre-configure* id, but `LGraph._configureBase` then re-adopts the *payload's* | ||
| * graph id — and `widgetValueStore` is keyed `graphId:nodeId:name`. The source | ||
| * graph's entries are therefore still live under exactly the key the reloaded | ||
| * node looks up, so `registerWidget` returns the existing state and the | ||
| * assertion passes without the serialized payload ever being read. | ||
| * | ||
| * This helper removes both escape hatches: the payload is forced through | ||
| * `JSON.parse(JSON.stringify(...))` (so live-object aliasing cannot leak) and | ||
| * the widget/preview stores are dropped for the payload's graph id before | ||
| * `configure()` runs. | ||
| * | ||
| * Verify with a mutation: a save/load test that still passes when the producer | ||
| * stops writing the field under test never read the payload. | ||
| */ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. THE COMMENTS...
DrJKL marked this conversation as resolved.
Outdated
|
||
| export function reloadSerializedGraph<T extends { id?: string }>( | ||
| serialized: T, | ||
| graphFactory: () => LGraph | ||
| ): LGraph { | ||
| const payload = JSON.parse(JSON.stringify(serialized)) as T | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So many casts |
||
| if (payload.id) { | ||
| useWidgetValueStore().clearGraph(payload.id as UUID) | ||
| usePreviewExposureStore().clearGraph(payload.id as UUID) | ||
| } | ||
| const reloaded = graphFactory() | ||
| reloaded.configure(payload as never) | ||
| return reloaded | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.