Repository navigation
test(litegraph): drive the widget-form proof through the real serialize path #22017
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
93da152
feat(litegraph): prove a node's widget form rather than deriving it
claude fd6b67d
test(litegraph): drive the widget-form proof through the real seriali…
christian-byrne b8f219d
Merge origin/main into widget-form-proof-cases
christian-byrne f29bbd0
test: drop the code comments from the widget-form proof tests
christian-byrne 1f36a80
test: drop the subgraph cases that never reach the proof
christian-byrne 32b1e38
test: add the extra subgraph widget through addWidget instead of a cast
christian-byrne 5ac91ec
test: drop the projection case that duplicates the deleted-values case
christian-byrne 8a2f4d9
test: narrow the audio widget constructors with assert instead of a b…
christian-byrne 3adfd4b
test: share the serialization capture harness and type the fixtures
christian-byrne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| import { fromPartial } from '@total-typescript/shoehorn' | ||
| import { assert, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import { load3dWidgets } from '@/extensions/core/load3d/load3dWidgets' | ||
| import { | ||
| audioWidgetExtension, | ||
| recordAudioExtension | ||
| } from '@/extensions/core/uploadAudio' | ||
| import { LGraphNode } from '@/lib/litegraph/src/litegraph' | ||
| import type { LGraphNodeConstructor } from '@/lib/litegraph/src/litegraph' | ||
| import { | ||
| coreIsLastWriter, | ||
| serializeWithCapture | ||
| } from '@/lib/litegraph/src/widgets/__fixtures__/widgetFormProofHarness' | ||
| import { proveWidgetForm } from '@/lib/litegraph/src/widgets/widgetFormProof' | ||
| import type { WidgetFormProof } from '@/lib/litegraph/src/widgets/widgetFormProof' | ||
| import { useImageUploadWidget } from '@/renderer/extensions/vueNodes/widgets/composables/useImageUploadWidget' | ||
| import type { ComfyApp } from '@/scripts/app' | ||
| import '@/scripts/domWidget' | ||
|
|
||
| const noApp = () => fromPartial<ComfyApp>({}) | ||
|
|
||
| function proveSerialization(node: LGraphNode) { | ||
| const captured = serializeWithCapture(node) | ||
| return { | ||
| values: captured.final.widgets_values, | ||
| proof: proveWidgetForm( | ||
| captured.widgetsAtCoreSite, | ||
| captured.final, | ||
| captured.atCoreSite, | ||
| { | ||
| serializeWidgets: node.serialize_widgets, | ||
| replayed: false, | ||
| coreIsLastWriter: coreIsLastWriter(node) | ||
| } | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| function refusal(reason: NonNullable<WidgetFormProof['refusal']>) { | ||
| return { provable: false, refusal: reason, order: null } | ||
| } | ||
|
|
||
| function registeredNode(title: string): LGraphNode { | ||
| const node = new LGraphNode(title) | ||
| node.serialize_widgets = true | ||
| return node | ||
| } | ||
|
|
||
| describe('case 4 — DOM widgets', () => { | ||
| it('proves PREVIEW_3D, whose viewport widget occupies a slot', () => { | ||
| const node = registeredNode('Preview3D') | ||
| load3dWidgets.PREVIEW_3D(node, 'image', ['PREVIEW_3D', {}], noApp()) | ||
|
|
||
| expect(proveSerialization(node)).toEqual({ | ||
| values: [''], | ||
| proof: { provable: true, refusal: null, order: ['image'] } | ||
| }) | ||
| }) | ||
|
|
||
| it('proves LOAD_3D, counting its three unflagged buttons', () => { | ||
| const node = registeredNode('Load3D') | ||
| node.addWidget('combo', 'model_file', 'a.glb', () => {}, { | ||
| values: ['a.glb'] | ||
| }) | ||
| load3dWidgets.LOAD_3D(node, 'image', ['LOAD_3D', {}], noApp()) | ||
|
|
||
| expect(proveSerialization(node)).toEqual({ | ||
| values: ['a.glb', 'upload3dmodel', 'uploadExtraResources', 'clear', ''], | ||
| proof: { | ||
| provable: true, | ||
| refusal: null, | ||
| order: [ | ||
| 'model_file', | ||
| 'upload 3d model', | ||
| 'upload extra resources', | ||
| 'clear', | ||
| 'image' | ||
| ] | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| it('proves IMAGEUPLOAD at two slots, because the upload button is options-bag-flagged', () => { | ||
| const node = registeredNode('LoadImage') | ||
| node.addWidget('combo', 'image', 'x.png', () => {}, { values: ['x.png'] }) | ||
| vi.stubGlobal('requestAnimationFrame', vi.fn()) | ||
| useImageUploadWidget()( | ||
| node, | ||
| 'upload', | ||
| ['IMAGEUPLOAD', { imageInputName: 'image' }], | ||
| noApp() | ||
| ) | ||
|
|
||
| const uploadWidget = node.widgets?.at(-1) | ||
| expect(uploadWidget?.serialize).toBeUndefined() | ||
| expect(uploadWidget?.options.serialize).toBe(false) | ||
|
|
||
| expect(proveSerialization(node)).toEqual({ | ||
| values: ['x.png', 'image'], | ||
| proof: { provable: true, refusal: null, order: ['image', 'upload'] } | ||
| }) | ||
| }) | ||
|
|
||
| it('excludes the AUDIO_UI player, which is flagged on the widget itself', async () => { | ||
| const node = registeredNode('LoadAudio') | ||
| Object.defineProperty(node, 'constructor', { | ||
| value: fromPartial<LGraphNodeConstructor>({ | ||
| nodeData: { output_node: false } | ||
| }), | ||
| configurable: true | ||
| }) | ||
| node.addWidget('combo', 'audio', 'a.mp3', () => {}, { values: ['a.mp3'] }) | ||
| const audioWidgets = await audioWidgetExtension.getCustomWidgets?.(noApp()) | ||
| assert.exists(audioWidgets) | ||
| audioWidgets.AUDIO_UI(node, 'audioUI', ['AUDIO_UI', {}], noApp()) | ||
| node.addWidget('button', 'upload', '', () => {}, { serialize: false }) | ||
|
|
||
| expect( | ||
| node.widgets?.map((widget) => [ | ||
| widget.name, | ||
| widget.serialize, | ||
| widget.options.serialize | ||
| ]) | ||
| ).toEqual([ | ||
| ['audio', undefined, undefined], | ||
| ['audioUI', false, false], | ||
| ['upload', undefined, false] | ||
| ]) | ||
|
|
||
| expect(proveSerialization(node)).toEqual({ | ||
| values: ['a.mp3', ''], | ||
| proof: { provable: true, refusal: null, order: ['audio', 'upload'] } | ||
| }) | ||
| }) | ||
|
|
||
| it('refuses AUDIO_RECORD, whose player and button share one name', async () => { | ||
| const node = registeredNode('RecordAudio') | ||
| const recordWidgets = await recordAudioExtension.getCustomWidgets?.(noApp()) | ||
| assert.exists(recordWidgets) | ||
| recordWidgets.AUDIO_RECORD(node, 'audio', ['AUDIO_RECORD', {}], noApp()) | ||
|
|
||
| expect(node.widgets?.map((widget) => widget.name)).toEqual([ | ||
| 'audio', | ||
| 'audio' | ||
| ]) | ||
| expect(proveSerialization(node)).toEqual({ | ||
| values: ['', ''], | ||
| proof: refusal('duplicate_widget_name') | ||
| }) | ||
| }) | ||
| }) | ||
34 changes: 34 additions & 0 deletions
34
src/lib/litegraph/src/widgets/__fixtures__/widgetFormProofHarness.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import type { ISerialisedNode } from '@comfyorg/graph-model/serialisation' | ||
| import type { IBaseWidget } from '@comfyorg/graph-model/widgets' | ||
|
|
||
| import { LGraphNode } from '@/lib/litegraph/src/litegraph' | ||
|
|
||
| export interface CapturedSerialization { | ||
| final: ISerialisedNode | ||
| atCoreSite: unknown | ||
| widgetsAtCoreSite: readonly IBaseWidget[] | undefined | ||
| } | ||
|
|
||
| export function serializeWithCapture(node: LGraphNode): CapturedSerialization { | ||
| let atCoreSite: unknown | ||
| let widgetsAtCoreSite: readonly IBaseWidget[] | undefined | ||
| const previous = node.onSerialize | ||
| node.onSerialize = function (data: ISerialisedNode) { | ||
| atCoreSite = data.widgets_values | ||
| widgetsAtCoreSite = this.widgets | ||
| previous?.call(this, data) | ||
| } | ||
| try { | ||
| return { final: node.serialize(), atCoreSite, widgetsAtCoreSite } | ||
| } finally { | ||
| node.onSerialize = previous | ||
| } | ||
| } | ||
|
|
||
| export function coreIsLastWriter(node: LGraphNode): boolean { | ||
| return ( | ||
| node.serialize === LGraphNode.prototype.serialize && | ||
| node.serializeFromStoreState === | ||
| LGraphNode.prototype.serializeFromStoreState | ||
| ) | ||
| } |
181 changes: 181 additions & 0 deletions
181
src/lib/litegraph/src/widgets/widgetFormProof.subgraph.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| import { beforeEach, describe, expect, it } from 'vitest' | ||
|
|
||
| import { LegacyWidget, LGraphNode } from '@/lib/litegraph/src/litegraph' | ||
| import type { Subgraph, SubgraphNode } from '@/lib/litegraph/src/litegraph' | ||
| import { | ||
| createTestSubgraph, | ||
| createTestSubgraphNode, | ||
| resetSubgraphFixtureState | ||
| } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' | ||
| import type { CapturedSerialization } from '@/lib/litegraph/src/widgets/__fixtures__/widgetFormProofHarness' | ||
| import { | ||
| coreIsLastWriter, | ||
| serializeWithCapture | ||
| } from '@/lib/litegraph/src/widgets/__fixtures__/widgetFormProofHarness' | ||
| import { proveWidgetForm } from '@/lib/litegraph/src/widgets/widgetFormProof' | ||
| import type { WidgetFormProof } from '@/lib/litegraph/src/widgets/widgetFormProof' | ||
|
|
||
| function proveAgainstFinalPayload( | ||
| node: LGraphNode, | ||
| captured: CapturedSerialization, | ||
| claimedLastWriter = coreIsLastWriter(node) | ||
| ) { | ||
| return proveWidgetForm(node.widgets, captured.final, captured.atCoreSite, { | ||
| serializeWidgets: node.serialize_widgets, | ||
| replayed: false, | ||
| coreIsLastWriter: claimedLastWriter | ||
| }) | ||
| } | ||
|
|
||
| function proveAgainstCoreSitePayload( | ||
| node: LGraphNode, | ||
| captured: CapturedSerialization | ||
| ) { | ||
| return proveWidgetForm( | ||
| captured.widgetsAtCoreSite, | ||
| { widgets_values: captured.atCoreSite }, | ||
| captured.atCoreSite, | ||
| { | ||
| serializeWidgets: node.serialize_widgets, | ||
| replayed: false, | ||
| coreIsLastWriter: coreIsLastWriter(node) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| function interiorNode(widgetName: string, value: number): LGraphNode { | ||
| const node = new LGraphNode('Interior') | ||
| const input = node.addInput(widgetName, 'number') | ||
| node.addOutput('out', 'number') | ||
| const widget: LegacyWidget = new LegacyWidget( | ||
| { name: widgetName, type: 'number', value, y: 0, options: {}, node }, | ||
| node | ||
| ) | ||
| node.widgets = [widget] | ||
| input.widget = { name: widget.name } | ||
| return node | ||
| } | ||
|
|
||
| function subgraphHostWithPromotedInputs( | ||
| names: readonly string[] | ||
| ): SubgraphNode { | ||
| const subgraph: Subgraph = createTestSubgraph({ | ||
| inputs: names.map((name) => ({ name, type: 'number' as const })) | ||
| }) | ||
| for (const [index, name] of names.entries()) { | ||
| const interior = interiorNode(name, index + 1) | ||
| subgraph.add(interior) | ||
| subgraph.inputNode.slots[index].connect(interior.inputs[0], interior) | ||
| } | ||
| const host = createTestSubgraphNode(subgraph) | ||
| host.serialize_widgets = true | ||
| return host | ||
| } | ||
|
|
||
| function clearPromotedValues(node: SubgraphNode): void { | ||
| for (const widget of node.widgets) widget.value = undefined | ||
| } | ||
|
|
||
| function refusal(reason: NonNullable<WidgetFormProof['refusal']>) { | ||
| return { provable: false, refusal: reason, order: null } | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| resetSubgraphFixtureState() | ||
| }) | ||
|
|
||
| describe('the capture harness itself', () => { | ||
| it('observes the produced values without replacing them, so a plain node still proves', () => { | ||
| const node = new LGraphNode('Plain') | ||
| node.serialize_widgets = true | ||
| node.addWidget('number', 'seed', 1, () => {}) | ||
| node.addWidget('text', 'prompt', 'hi', () => {}) | ||
|
|
||
| const captured = serializeWithCapture(node) | ||
|
|
||
| expect(captured.final.widgets_values).toBe(captured.atCoreSite) | ||
| expect(proveAgainstFinalPayload(node, captured)).toEqual({ | ||
| provable: true, | ||
| refusal: null, | ||
| order: ['seed', 'prompt'] | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('case 1 — subgraph nodes', () => { | ||
| it('refuses a subgraph host, whose serializeFromStoreState rebuilds widgets_values after core', () => { | ||
| const host = subgraphHostWithPromotedInputs(['alpha', 'beta']) | ||
|
|
||
| const captured = serializeWithCapture(host) | ||
|
|
||
| expect(captured.final.widgets_values).toEqual([1, 2]) | ||
| expect(captured.final.widgets_values).not.toBe(captured.atCoreSite) | ||
| expect(proveAgainstFinalPayload(host, captured)).toEqual( | ||
| refusal('class_may_rewrite_payload') | ||
| ) | ||
| }) | ||
|
|
||
| it('still refuses by array identity when a caller wrongly claims core wrote the final payload', () => { | ||
| const host = subgraphHostWithPromotedInputs(['alpha', 'beta']) | ||
|
|
||
| const captured = serializeWithCapture(host) | ||
|
|
||
| expect(proveAgainstFinalPayload(host, captured, true)).toEqual( | ||
| refusal('values_replaced') | ||
| ) | ||
| }) | ||
|
|
||
| it('refuses when the subgraph deletes widgets_values because every promoted value is undefined', () => { | ||
| const host = subgraphHostWithPromotedInputs(['alpha']) | ||
| clearPromotedValues(host) | ||
|
|
||
| const captured = serializeWithCapture(host) | ||
|
|
||
| expect('widgets_values' in captured.final).toBe(false) | ||
| expect(proveAgainstFinalPayload(host, captured)).toEqual( | ||
| refusal('class_may_rewrite_payload') | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('case 2 — promoted widgets', () => { | ||
| it('counts every promoted input at core write site, because a projection carries no own serialize flag', () => { | ||
| const host = subgraphHostWithPromotedInputs(['alpha', 'beta']) | ||
|
|
||
| expect(host.widgets.map((widget) => widget.name)).toEqual(['alpha', 'beta']) | ||
| expect( | ||
| host.widgets.some((widget) => Object.hasOwn(widget, 'serialize')) | ||
| ).toBe(false) | ||
|
|
||
| const captured = serializeWithCapture(host) | ||
| expect(captured.atCoreSite).toEqual([1, 2]) | ||
| }) | ||
|
|
||
| it('refuses a host whose added widget core serializes but the subgraph rebuild then drops', () => { | ||
| const host = subgraphHostWithPromotedInputs(['alpha']) | ||
| host.addWidget('text', 'extra', 'hello', () => {}) | ||
|
|
||
| const captured = serializeWithCapture(host) | ||
|
|
||
| expect(captured.atCoreSite).toEqual([1, 'hello']) | ||
| expect(captured.final.widgets_values).toEqual([1]) | ||
| expect(proveAgainstFinalPayload(host, captured)).toEqual( | ||
| refusal('class_may_rewrite_payload') | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('the last writer', () => { | ||
| it('refuses a subgraph host at core own post-hook payload, before the subclass rewrites it', () => { | ||
| const host = subgraphHostWithPromotedInputs(['alpha']) | ||
| host.addWidget('text', 'extra', 'hello', () => {}) | ||
|
|
||
| const captured = serializeWithCapture(host) | ||
|
|
||
| expect(captured.atCoreSite).toEqual([1, 'hello']) | ||
| expect(captured.final.widgets_values).toEqual([1]) | ||
| expect(proveAgainstCoreSitePayload(host, captured)).toEqual( | ||
| refusal('class_may_rewrite_payload') | ||
| ) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.