diff --git a/scripts/registry-census/matrix_runner.ts b/scripts/registry-census/matrix_runner.ts index a96ba105b3f..1c138f72e20 100644 --- a/scripts/registry-census/matrix_runner.ts +++ b/scripts/registry-census/matrix_runner.ts @@ -20,6 +20,7 @@ import type { PointTuple as Point } from '@comfyorg/shared-frontend-utils/geomet import type { CanvasPointerEvent } from '@comfyorg/graph-model/events' import type { IBaseWidget } from '@comfyorg/graph-model/widgets' import type { ComfyNodeDef, InputSpec } from '@comfyorg/object-info-parser' +import { setRootGraph } from '@/scripts/__tests__/appTestUtils' import type { useWidgetValueStore } from '@/stores/widgetValueStore' import type * as WidgetValuesForm from '@/workbench/extensions/agent/crdt/widgetValuesForm' @@ -459,8 +460,8 @@ export async function runPack( const graph = new LGraph() // ComfyApp.setup() non-null-asserts five DOM elements and needs a 2D // context, so it cannot boot here. DOM widgets read app.rootGraph while - // graph.configure runs, so the private field has to be seeded directly. - Reflect.set(app, 'rootGraphInternal', graph) + // graph.configure runs, so the root graph has to be seeded directly. + setRootGraph(app, graph) const byType = (t: string) => graph._nodes.find((n) => n.type === t) let wvStore: WidgetValueStore | undefined diff --git a/src/extensions/core/customWidgets.clone.test.ts b/src/extensions/core/customWidgets.clone.test.ts index a03d55c63ea..d7bd1f4ba4a 100644 --- a/src/extensions/core/customWidgets.clone.test.ts +++ b/src/extensions/core/customWidgets.clone.test.ts @@ -3,6 +3,7 @@ import { beforeAll, beforeEach, describe, expect, it } from 'vitest' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import type { ComfyNodeDef } from '@comfyorg/object-info-parser' import { app } from '@/scripts/app' +import { getRootGraph, setRootGraph } from '@/scripts/__tests__/appTestUtils' import { customWidgetsExtension } from './customWidgets' @@ -40,10 +41,8 @@ describe('CustomCombo copy/paste', () => { it('preserves combo options and selected value through clone and paste', () => { const graph = new LGraph() - type AppWithRootGraph = { rootGraphInternal?: LGraph } - const appWithRootGraph = app as unknown as AppWithRootGraph - const previousRootGraph = appWithRootGraph.rootGraphInternal - appWithRootGraph.rootGraphInternal = graph + const previousRootGraph = getRootGraph(app) + setRootGraph(app, graph) try { const original = LiteGraph.createNode(TEST_CUSTOM_COMBO_TYPE)! @@ -72,7 +71,7 @@ describe('CustomCombo copy/paste', () => { 'gamma' ]) } finally { - appWithRootGraph.rootGraphInternal = previousRootGraph + setRootGraph(app, previousRootGraph) } }) }) diff --git a/src/extensions/core/customWidgets.subgraphPromotion.test.ts b/src/extensions/core/customWidgets.subgraphPromotion.test.ts index ac7881d50b3..441ff1fb2d5 100644 --- a/src/extensions/core/customWidgets.subgraphPromotion.test.ts +++ b/src/extensions/core/customWidgets.subgraphPromotion.test.ts @@ -9,6 +9,7 @@ import { } from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' import type { ComfyNodeDef } from '@comfyorg/object-info-parser' import { app } from '@/scripts/app' +import { getRootGraph, setRootGraph } from '@/scripts/__tests__/appTestUtils' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { graphToPrompt } from '@/utils/executionUtil' @@ -89,10 +90,8 @@ describe('CustomCombo index widget after subgraph promotion', () => { it('resolves INDEX from the promoted host choice, not the frozen interior value', async () => { const rootGraph = new LGraph() - type AppWithRootGraph = { rootGraphInternal?: LGraph } - const appWithRootGraph = app as unknown as AppWithRootGraph - const previousRootGraph = appWithRootGraph.rootGraphInternal - appWithRootGraph.rootGraphInternal = rootGraph + const previousRootGraph = getRootGraph(app) + setRootGraph(app, rootGraph) try { const subgraph = createTestSubgraph({ rootGraph }) @@ -134,16 +133,14 @@ describe('CustomCombo index widget after subgraph promotion', () => { // "four" is index 3 of ["one", "two", "three", "four"]. expect(promptInputs.index).toBe(3) } finally { - appWithRootGraph.rootGraphInternal = previousRootGraph + setRootGraph(app, previousRootGraph) } }) it('resolves INDEX from the interior widget when choice was never promoted', async () => { const rootGraph = new LGraph() - type AppWithRootGraph = { rootGraphInternal?: LGraph } - const appWithRootGraph = app as unknown as AppWithRootGraph - const previousRootGraph = appWithRootGraph.rootGraphInternal - appWithRootGraph.rootGraphInternal = rootGraph + const previousRootGraph = getRootGraph(app) + setRootGraph(app, rootGraph) try { const comboNode = LiteGraph.createNode( @@ -162,7 +159,7 @@ describe('CustomCombo index widget after subgraph promotion', () => { // "two" is index 1 of ["one", "two", "three"]. expect(promptInputs.index).toBe(1) } finally { - appWithRootGraph.rootGraphInternal = previousRootGraph + setRootGraph(app, previousRootGraph) } }) }) diff --git a/src/scripts/__tests__/appTestUtils.ts b/src/scripts/__tests__/appTestUtils.ts new file mode 100644 index 00000000000..a1516a104d9 --- /dev/null +++ b/src/scripts/__tests__/appTestUtils.ts @@ -0,0 +1,15 @@ +import type { LGraph } from '@/lib/litegraph/src/litegraph' +import type { ComfyApp } from '@/scripts/app' + +/** + * `ComfyApp.setup` is the only production writer of the root graph, and it needs + * a real canvas. Tests that just need a graph in place reach the same storage + * through this seam instead. + */ +export function setRootGraph(app: ComfyApp, graph: LGraph | undefined) { + app['rootGraphRef'].value = graph +} + +export function getRootGraph(app: ComfyApp) { + return app['rootGraphRef'].value +} diff --git a/src/scripts/app.graphReadiness.test.ts b/src/scripts/app.graphReadiness.test.ts index b538d64042e..150b22eba13 100644 --- a/src/scripts/app.graphReadiness.test.ts +++ b/src/scripts/app.graphReadiness.test.ts @@ -2,15 +2,10 @@ import { beforeEach, describe, expect, it } from 'vitest' import { effectScope, nextTick, watch } from 'vue' import { LGraph } from '@/lib/litegraph/src/litegraph' +import { setRootGraph } from '@/scripts/__tests__/appTestUtils' import { app } from '@/scripts/app' -const setGraph = (graph: LGraph | undefined) => { - ;( - app as unknown as { - rootGraphInternal: LGraph | undefined - } - ).rootGraphInternal = graph -} +const setGraph = (graph: LGraph | undefined) => setRootGraph(app, graph) describe('ComfyApp graph-readiness reactivity', () => { beforeEach(() => setGraph(undefined)) diff --git a/src/scripts/app.test.ts b/src/scripts/app.test.ts index 4241658d044..cf541eee300 100644 --- a/src/scripts/app.test.ts +++ b/src/scripts/app.test.ts @@ -1,6 +1,7 @@ import { fromAny, fromPartial } from '@total-typescript/shoehorn' import { attachLegacyUI } from '@/core/legacyUI/attachLegacyUI' import { ComfyApp, app as singletonApp } from './app' +import { getRootGraph, setRootGraph } from './__tests__/appTestUtils' import { useToast } from '@/components/ui/toast/toastStore' import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore' import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore' @@ -371,7 +372,7 @@ describe('ComfyApp', () => { describe('loadGraphData', () => { function prepareResourceReload() { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) const store = useExecutionErrorStore() const graphId = '11111111-1111-4111-8111-111111111111' store.setActiveGraph(graphId) @@ -497,7 +498,7 @@ describe('ComfyApp', () => { it('offers to migrate legacy reroutes and dismisses the offer once migrated', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) const legacyReroute = { ...createWorkflowGraphData(), nodes: [ @@ -536,7 +537,7 @@ describe('ComfyApp', () => { it('forwards clean and navigation intent to workflow navigation', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) await app.loadGraphData(createWorkflowGraphData(), false, true, null, { workflowNavigationId: 42 @@ -550,7 +551,7 @@ describe('ComfyApp', () => { }) it('suppresses the workflow reset for a default clean load', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) await app.loadGraphData(createWorkflowGraphData()) @@ -563,7 +564,7 @@ describe('ComfyApp', () => { it('reports the load outcome explicitly: true on success', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) await expect(app.loadGraphData(createWorkflowGraphData())).resolves.toBe( true @@ -573,7 +574,7 @@ describe('ComfyApp', () => { it('resolves false, not rejects, when graph configure fails', async () => { app.canvasElRef.value = document.createElement('canvas') const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) + setRootGraph(app, graph) vi.spyOn(graph, 'configure').mockImplementation(() => { throw new Error('bad workflow json') }) @@ -604,7 +605,7 @@ describe('ComfyApp', () => { // promise instead and leaking any suppression/loading-state a // `beforeLoadGraph` listener had opened for this same load. app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) const showDialog = vi.spyOn(useDialogStore(), 'showDialog') const error = new Error('bad extension') let releaseErrorHandling!: () => void @@ -658,7 +659,7 @@ describe('ComfyApp', () => { it('resolves false and notifies onGraphLoadError when the outgoing preflight throws', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) const showDialog = vi.spyOn(useDialogStore(), 'showDialog') mockWorkflowService.beforeLoadNewGraph.mockImplementationOnce(() => { throw new Error('outgoing serializer blew up') @@ -690,7 +691,7 @@ describe('ComfyApp', () => { // suppression/loading-state a `beforeLoadGraph` listener had opened // for this same load. app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) useSettingStore().settingValues['Comfy.Validation.Workflows'] = true const showDialog = vi.spyOn(useDialogStore(), 'showDialog') mockValidateWorkflow.mockRejectedValueOnce( @@ -732,7 +733,7 @@ describe('ComfyApp', () => { it('applies load-time widget fixups as load provenance, not local edits', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) class KSampler extends LGraphNode { constructor(title = 'KSampler') { super(title) @@ -790,7 +791,7 @@ describe('ComfyApp', () => { it('applies synchronous afterConfigureGraph widget writes as load provenance', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) class KSampler extends LGraphNode { constructor(title = 'KSampler') { super(title) @@ -855,7 +856,7 @@ describe('ComfyApp', () => { it('notifies extensions once on each side of a graph load, in order', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) await app.loadGraphData(createWorkflowGraphData(), false) @@ -880,7 +881,7 @@ describe('ComfyApp', () => { it('closes beforeLoadGraph with an abort when the graph is cleared during activation', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) mockWorkflowService.afterLoadNewGraph.mockImplementation(async () => { app.clean() }) @@ -904,7 +905,7 @@ describe('ComfyApp', () => { it('does not assign a newer workflow graph to an Undo load waiting after configuration', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) const firstWorkflow = new ComfyWorkflow({ path: 'workflows/undo.json', modified: 0, @@ -974,11 +975,11 @@ describe('ComfyApp', () => { const previousGraph = singletonApp.rootGraphOrUndefined const previousCanvas = singletonApp.canvas onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) singletonApp.canvas = previousCanvas }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) singletonApp.canvas = mockCanvas Reflect.set(mockCanvas, 'ds', { scale: 1, offset: [0, 0] }) await useRealWorkflowService() @@ -1095,10 +1096,10 @@ describe('ComfyApp', () => { graph.add(new LGraphNode('outgoing')) const previousGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const outgoing = new ComfyWorkflow({ path: 'workflows/outgoing.json', modified: 0, @@ -1125,7 +1126,7 @@ describe('ComfyApp', () => { it('allows capture while the loaded workflow resource scans are pending', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) let releaseModels!: () => void const modelsPending = new Promise((resolve) => { releaseModels = resolve @@ -1149,7 +1150,7 @@ describe('ComfyApp', () => { it('keeps a configured load when the next queued configure fails without clearing it', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) const graph = createWorkflowGraphData() graph.extra = { marker: 'configured' } let releaseFirst!: () => void @@ -1187,7 +1188,7 @@ describe('ComfyApp', () => { it('lets an older valid load commit when its queued replacement fails', async () => { app.canvasElRef.value = document.createElement('canvas') - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) let releaseOlderLoad!: () => void const olderLoadBlocked = new Promise((resolve) => { releaseOlderLoad = resolve @@ -1223,7 +1224,7 @@ describe('ComfyApp', () => { canvasElement = createTestCanvasElement({ visible: false }) document.body.append(canvasElement) app.canvasElRef.value = canvasElement - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) Reflect.set(mockCanvas, 'ds', createTestDragAndScale()) Reflect.set(mockCanvas, 'bgcanvas', createTestCanvasElement()) useCanvasScheduler().cancel('graph-load-camera') @@ -1269,10 +1270,10 @@ describe('ComfyApp', () => { const graph = new LGraph() const previousGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) await app.loadApiJson({}, 'empty.json') @@ -1319,10 +1320,10 @@ describe('ComfyApp', () => { const graph = new LGraph() const previousGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) class KSampler extends LGraphNode { constructor(title = 'KSampler') { super(title) @@ -1372,10 +1373,10 @@ describe('ComfyApp', () => { const graph = new LGraph() const previousGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) vi.spyOn(useNodeReplacementStore(), 'load').mockRejectedValueOnce( new Error('replacement lookup failed') ) @@ -1442,10 +1443,10 @@ describe('ComfyApp', () => { graph.add(new LGraphNode('outgoing')) const previousGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const outgoing = markLoaded( new ComfyWorkflow({ path: 'workflows/outgoing-api.json', @@ -1501,11 +1502,11 @@ describe('ComfyApp', () => { const previousGraph = singletonApp.rootGraphOrUndefined const previousCanvas = singletonApp.canvas onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) singletonApp.canvas = previousCanvas }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) singletonApp.canvas = mockCanvas Reflect.set(mockCanvas, 'ds', { scale: 1, offset: [0, 0] }) onTestFinished(attachLegacyUI(singletonApp)) @@ -1672,8 +1673,8 @@ describe('ComfyApp', () => { size: 0 }) const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) onTestFinished(attachLegacyUI(app)) useWorkflowStore().activeWorkflow = markLoaded(workflow) vi.spyOn(app, 'graphToPrompt').mockResolvedValue({ @@ -2063,8 +2064,8 @@ describe('ComfyApp', () => { ] } } - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) onTestFinished(attachLegacyUI(app)) useWorkflowStore().activeWorkflow = markLoaded(workflow) vi.spyOn(app, 'graphToPrompt').mockResolvedValue({ @@ -2742,10 +2743,10 @@ describe('ComfyApp', () => { const graph = new LGraph() const previousSingletonGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousSingletonGraph) + setRootGraph(singletonApp, previousSingletonGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const nodeType = 'test/ApiDynamicGroup' const group = { min: 0, @@ -2880,8 +2881,8 @@ describe('ComfyApp', () => { it('clears missing node packs before loading API JSON without missing nodes', async () => { const graph = new LGraph() const activeSubgraph = createTestSubgraph({ rootGraph: graph }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) Reflect.set(mockCanvas, 'graph', activeSubgraph) Reflect.set(mockCanvas, 'subgraph', activeSubgraph) vi.mocked(mockCanvas.setGraph).mockImplementation((nextGraph) => { @@ -2930,8 +2931,8 @@ describe('ComfyApp', () => { }) const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const nodeReplacementStore = useNodeReplacementStore() vi.spyOn(nodeReplacementStore, 'load').mockResolvedValue() @@ -3011,8 +3012,8 @@ describe('ComfyApp', () => { 'validates API placeholder pack metadata: $name', async ({ metadata, expectedProperties, expectedCnrId }) => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const cleanupErrorHooks = installErrorClearingHooks(graph) const missingNodesStore = useMissingNodesErrorStore() const nodeReplacementStore = useNodeReplacementStore() @@ -3056,9 +3057,9 @@ describe('ComfyApp', () => { 'preserves replacement defaults for API text substitutions $name', async ({ metadata }) => { const graph = new LGraph() - const previousGraph = Reflect.get(singletonApp, 'rootGraphInternal') - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + const previousGraph = getRootGraph(singletonApp) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const nodeType = 'test/ReplacementText' class ReplacementText extends LGraphNode { constructor() { @@ -3071,7 +3072,7 @@ describe('ComfyApp', () => { const cleanupErrorHooks = installErrorClearingHooks(graph) onTestFinished(() => { cleanupErrorHooks() - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) useSettingStore().settingValues['Comfy.NodeReplacement.Enabled'] = true const replacementStore = useNodeReplacementStore() @@ -3111,13 +3112,10 @@ describe('ComfyApp', () => { it('restores late autogrow widgets and links without repeating callbacks', async () => { const graph = new LGraph() - const previousAppGraph = Reflect.get(app, 'rootGraphInternal') - const previousSingletonGraph = Reflect.get( - singletonApp, - 'rootGraphInternal' - ) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + const previousAppGraph = getRootGraph(app) + const previousSingletonGraph = getRootGraph(singletonApp) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const sourceType = 'test/ApiSourceNode' const targetType = 'test/ApiTargetNode' const targetConnectionChanges = vi.fn() @@ -3178,8 +3176,8 @@ describe('ComfyApp', () => { ?.widgets?.find((widget) => widget.name === '0.weight2')?.value ).toBe(0.5) } finally { - Reflect.set(app, 'rootGraphInternal', previousAppGraph) - Reflect.set(singletonApp, 'rootGraphInternal', previousSingletonGraph) + setRootGraph(app, previousAppGraph) + setRootGraph(singletonApp, previousSingletonGraph) LiteGraph.unregisterNodeType(sourceType) LiteGraph.unregisterNodeType(targetType) } @@ -3187,13 +3185,10 @@ describe('ComfyApp', () => { it('saves links connected on retry in missing-node snapshots', async () => { const graph = new LGraph() - const previousAppGraph = Reflect.get(app, 'rootGraphInternal') - const previousSingletonGraph = Reflect.get( - singletonApp, - 'rootGraphInternal' - ) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + const previousAppGraph = getRootGraph(app) + const previousSingletonGraph = getRootGraph(singletonApp) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) vi.spyOn(useNodeReplacementStore(), 'load').mockResolvedValue() const sourceType = 'test/ApiSnapshotSource' const lateType = 'test/ApiLateOutput' @@ -3248,8 +3243,8 @@ describe('ComfyApp', () => { ) expect(reloaded.links.size).toBe(2) } finally { - Reflect.set(app, 'rootGraphInternal', previousAppGraph) - Reflect.set(singletonApp, 'rootGraphInternal', previousSingletonGraph) + setRootGraph(app, previousAppGraph) + setRootGraph(singletonApp, previousSingletonGraph) LiteGraph.unregisterNodeType(sourceType) LiteGraph.unregisterNodeType(lateType) } @@ -3257,13 +3252,10 @@ describe('ComfyApp', () => { it('does not retry a connection vetoed by an extension callback', async () => { const graph = new LGraph() - const previousAppGraph = Reflect.get(app, 'rootGraphInternal') - const previousSingletonGraph = Reflect.get( - singletonApp, - 'rootGraphInternal' - ) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + const previousAppGraph = getRootGraph(app) + const previousSingletonGraph = getRootGraph(singletonApp) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const sourceType = 'test/ApiVetoSourceNode' const targetType = 'test/ApiVetoTargetNode' const connectionAttempts = vi.fn() @@ -3306,8 +3298,8 @@ describe('ComfyApp', () => { expect(connectionAttempts).toHaveBeenCalledOnce() expect(graph.links.size).toBe(0) } finally { - Reflect.set(app, 'rootGraphInternal', previousAppGraph) - Reflect.set(singletonApp, 'rootGraphInternal', previousSingletonGraph) + setRootGraph(app, previousAppGraph) + setRootGraph(singletonApp, previousSingletonGraph) LiteGraph.unregisterNodeType(sourceType) LiteGraph.unregisterNodeType(targetType) } @@ -3315,8 +3307,8 @@ describe('ComfyApp', () => { it('remaps flattened subgraph ids to colon-free local ids', async () => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const cleanupErrorHooks = installErrorClearingHooks(graph) const missingNodesStore = useMissingNodesErrorStore() const nodeReplacementStore = useNodeReplacementStore() @@ -3388,8 +3380,8 @@ describe('ComfyApp', () => { const missingNodesStore = useMissingNodesErrorStore() const previousMissingNodeTypes = missingNodesStore.missingNodesError?.nodeTypes ?? [] - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const widgetNodeType = 'test/ApiCurveNode' const curveCallback = vi.fn() class ApiCurveNode extends LGraphNode { @@ -3473,8 +3465,8 @@ describe('ComfyApp', () => { expect(passthroughNode?.widgets?.[1].value).toEqual(passthrough) } finally { missingNodesStore.setMissingNodeTypes(previousMissingNodeTypes) - Reflect.set(app, 'rootGraphInternal', previousAppGraph) - Reflect.set(singletonApp, 'rootGraphInternal', previousSingletonGraph) + setRootGraph(app, previousAppGraph) + setRootGraph(singletonApp, previousSingletonGraph) } }) @@ -3482,8 +3474,8 @@ describe('ComfyApp', () => { const graph = new LGraph() const previousAppGraph = app.rootGraph const previousSingletonGraph = singletonApp.rootGraph - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const videoCallback = (value: string) => value.lastIndexOf('.') class VhsLoadVideoNode extends LGraphNode { constructor() { @@ -3531,15 +3523,15 @@ describe('ComfyApp', () => { }) } finally { LiteGraph.unregisterNodeType('VHS_LoadVideo') - Reflect.set(app, 'rootGraphInternal', previousAppGraph) - Reflect.set(singletonApp, 'rootGraphInternal', previousSingletonGraph) + setRootGraph(app, previousAppGraph) + setRootGraph(singletonApp, previousSingletonGraph) } }) it('creates a removable placeholder for an API JSON missing node', async () => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const cleanupErrorHooks = installErrorClearingHooks(graph) const missingNodesStore = useMissingNodesErrorStore() const missingNodeType = 'Uninstalled<&Node>' @@ -3604,8 +3596,8 @@ describe('ComfyApp', () => { it('preserves API JSON inputs on a missing node across reload', async () => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const sourceNodeType = 'test/ApiJsonSourceNode' const missingNodeType = 'UninstalledInputNode' class ApiJsonSourceNode extends LGraphNode { @@ -3706,8 +3698,8 @@ describe('ComfyApp', () => { it('defers API JSON missing node warnings until they are flushed', async () => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const nodeReplacementStore = useNodeReplacementStore() vi.spyOn(nodeReplacementStore, 'load').mockResolvedValue() vi.spyOn(nodeReplacementStore, 'getReplacementFor').mockReturnValue(null) @@ -3745,7 +3737,7 @@ describe('ComfyApp', () => { describe('A1111 import', () => { it('clears missing node packs, which its graph swap skips clean() for', async () => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) + setRootGraph(app, graph) const missingNodesStore = useMissingNodesErrorStore() missingNodesStore.setMissingNodeTypes(['OutgoingMissingNode']) vi.mocked(getWorkflowDataFromFile).mockResolvedValue({ @@ -3767,7 +3759,7 @@ describe('ComfyApp', () => { 'keeps missing node packs when the import fails with %s', async (outcome) => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) + setRootGraph(app, graph) const missingNodesStore = useMissingNodesErrorStore() missingNodesStore.setMissingNodeTypes(['OutgoingMissingNode']) vi.mocked(getWorkflowDataFromFile).mockResolvedValue({ @@ -3787,10 +3779,10 @@ describe('ComfyApp', () => { const graph = new LGraph() const previousGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) vi.mocked(getWorkflowDataFromFile).mockResolvedValue({ parameters: 'positive\nNegative prompt: negative\nSteps: 20' }) @@ -3840,10 +3832,10 @@ describe('ComfyApp', () => { const graph = new LGraph() const previousGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) }) it('runs the next workflow after a load rejects', async () => { @@ -4097,7 +4089,7 @@ describe('ComfyApp', () => { describe('clean', () => { it('clears missing node packs when the graph is discarded', () => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) + setRootGraph(app, graph) const missingNodesStore = useMissingNodesErrorStore() const executionErrorStore = useExecutionErrorStore() missingNodesStore.setMissingNodeTypes(['MissingGroupNode']) @@ -4120,7 +4112,7 @@ describe('ComfyApp', () => { it('records run errors after clearing the current workflow', () => { const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) + setRootGraph(app, graph) const executionErrorStore = useExecutionErrorStore() app.clean() @@ -4239,7 +4231,7 @@ describe('ComfyApp', () => { }) it('keeps outputs and previews of a background run off the visible workflow', () => { - Reflect.set(app, 'rootGraphInternal', new LGraph()) + setRootGraph(app, new LGraph()) const workflowA = markLoaded( new ComfyWorkflow({ path: 'workflows/a.json', modified: 0, size: 0 }) ) @@ -4313,8 +4305,8 @@ describe('ComfyApp', () => { it('restores the failed run state when returning to a workflow tab', async () => { const workflowService = await useRealWorkflowService() const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const workflowA = markLoaded( new ComfyWorkflow({ path: 'workflows/a.json', modified: 0, size: 0 }) @@ -4340,8 +4332,8 @@ describe('ComfyApp', () => { const workflowService = await useRealWorkflowService() const workflowStore = useWorkflowStore() const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) singletonApp.canvas = mockCanvas Reflect.set(mockCanvas, 'ds', { scale: 1, offset: [0, 0] }) vi.mocked(workflowStore.createNewTemporary).mockRestore() @@ -4375,8 +4367,8 @@ describe('ComfyApp', () => { it('reuses the active workflow when importing the same file again', async () => { await useRealWorkflowService() const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const importedWorkflow = markLoaded( new ComfyWorkflow({ path: 'workflows/repeat.json', @@ -4530,7 +4522,7 @@ describe('ComfyApp', () => { experimental: false } } - Reflect.set(app, 'rootGraphInternal', rootGraph) + setRootGraph(app, rootGraph) vi.spyOn(app, 'getNodeDefs').mockResolvedValue(defs) vi.spyOn(app, 'registerNodeDef').mockResolvedValue(undefined) @@ -4572,7 +4564,7 @@ describe('ComfyApp', () => { const defs: Record = { Custom: fromPartial({ name: 'Custom' }) } - Reflect.set(app, 'rootGraphInternal', createTestRootGraph()) + setRootGraph(app, createTestRootGraph()) const getNodeDefs = vi.spyOn(app, 'getNodeDefs') const registerNodeDef = vi .spyOn(app, 'registerNodeDef') @@ -4586,7 +4578,7 @@ describe('ComfyApp', () => { it('gives the next fetched definitions to addCustomNodeDefs once when late extensions are owed them, and registers what they add', async () => { const custom = fromPartial({ name: 'Custom' }) - Reflect.set(app, 'rootGraphInternal', createTestRootGraph()) + setRootGraph(app, createTestRootGraph()) vi.spyOn(api, 'fetchApi').mockImplementation(async () => Response.json({ Server: { name: 'Server' } }) ) @@ -4612,7 +4604,7 @@ describe('ComfyApp', () => { }) it('keeps the definitions owed to addCustomNodeDefs through a refused refresh and gives them the next successful ones', async () => { - Reflect.set(app, 'rootGraphInternal', createTestRootGraph()) + setRootGraph(app, createTestRootGraph()) vi.spyOn(app, 'registerNodeDef').mockResolvedValue(undefined) vi.spyOn(api, 'fetchApi') .mockResolvedValueOnce( @@ -4638,7 +4630,7 @@ describe('ComfyApp', () => { }) it('refuses each refused refresh while definitions are owed, reports it, keeps them owed, and registers nothing', async () => { - Reflect.set(app, 'rootGraphInternal', createTestRootGraph()) + setRootGraph(app, createTestRootGraph()) const registerNodeDef = vi .spyOn(app, 'registerNodeDef') .mockResolvedValue(undefined) @@ -4683,15 +4675,15 @@ describe('ComfyApp', () => { describe('refreshMissingModels', () => { it('delegates to the app-independent missing model refresh pipeline', async () => { - const graph = { + const graph = fromPartial({ nodes: [], serialize: vi.fn(() => createWorkflowGraphData()) - } + }) const result = { missingModels: [], confirmedCandidates: [] } - Reflect.set(app, 'rootGraphInternal', graph) + setRootGraph(app, graph) vi.spyOn(app, 'reloadNodeDefs').mockResolvedValue(true) mockRefreshMissingModelPipeline.mockResolvedValue(result) @@ -4711,11 +4703,11 @@ describe('ComfyApp', () => { }) it('omits the node definition reload when reloadDefs is false', async () => { - const graph = { + const graph = fromPartial({ nodes: [], serialize: vi.fn(() => createWorkflowGraphData()) - } - Reflect.set(app, 'rootGraphInternal', graph) + }) + setRootGraph(app, graph) vi.spyOn(app, 'reloadNodeDefs').mockResolvedValue(true) mockRefreshMissingModelPipeline.mockResolvedValue({ missingModels: [], @@ -5139,7 +5131,7 @@ describe('ComfyApp', () => { ])('maps $outcome to its message', async (testCase) => { const graph = new LGraph() const parameters = 'positive\nNegative prompt: negative\nSteps: 20' - Reflect.set(app, 'rootGraphInternal', graph) + setRootGraph(app, graph) vi.mocked(getWorkflowDataFromFile).mockResolvedValue({ parameters }) mockImportA1111.mockResolvedValue(testCase.outcome) @@ -5165,7 +5157,7 @@ describe('ComfyApp', () => { it('awaits persistence and orders its clear callback before setGraph', async () => { const graph = new LGraph() const parameters = 'positive\nNegative prompt: negative\nSteps: 20' - Reflect.set(app, 'rootGraphInternal', graph) + setRootGraph(app, graph) vi.mocked(getWorkflowDataFromFile).mockResolvedValue({ parameters }) mockImportA1111.mockImplementation( async (_graph, _parameters, beforeGraphClear) => { @@ -5233,8 +5225,8 @@ describe('ComfyApp', () => { }) const graph = new LGraph() - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) const outgoingWorkflow = new ComfyWorkflow({ path: 'workflows/outgoing.json', modified: 0, @@ -5399,10 +5391,10 @@ describe('ComfyApp', () => { const graph = new LGraph() const previousGraph = singletonApp.rootGraphOrUndefined onTestFinished(() => { - Reflect.set(singletonApp, 'rootGraphInternal', previousGraph) + setRootGraph(singletonApp, previousGraph) }) - Reflect.set(app, 'rootGraphInternal', graph) - Reflect.set(singletonApp, 'rootGraphInternal', graph) + setRootGraph(app, graph) + setRootGraph(singletonApp, graph) vi.mocked(extractFilesFromDragEvent).mockResolvedValue([ createTestFile('a1111.png', 'image/png') ]) diff --git a/src/scripts/app.ts b/src/scripts/app.ts index e9c832ebf0e..19830de7fd9 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -1,6 +1,6 @@ import { useEventListener, useResizeObserver } from '@vueuse/core' import _ from 'es-toolkit/compat' -import { reactive, unref, shallowRef } from 'vue' +import { reactive, shallowRef, unref } from 'vue' import { useToast } from '@/components/ui/toast/toastStore' import { partnerRunGateBlocksAutoQueue } from '@/platform/billing/composables/usePartnerNodesRunGate' @@ -391,31 +391,24 @@ export class ComfyApp implements IComfyApp { ) nodePreviewImages: Partial> + /** Shallow: the graph is observed for readiness, never deep-proxied. */ private readonly rootGraphRef = shallowRef(undefined) - private get rootGraphInternal(): LGraph | undefined { - return this.rootGraphRef.value - } - - private set rootGraphInternal(graph: LGraph | undefined) { - this.rootGraphRef.value = graph - } - // TODO: Migrate internal usage to the /** @deprecated Use {@link rootGraph} instead */ get graph() { - return this.rootGraphInternal! + return this.rootGraphRef.value! } get rootGraph(): LGraph { - if (!this.rootGraphInternal) { + if (!this.rootGraphRef.value) { console.error('ComfyApp graph accessed before initialization') } - return this.rootGraphInternal! + return this.rootGraphRef.value! } get rootGraphOrUndefined(): LGraph | undefined { - return this.rootGraphInternal + return this.rootGraphRef.value } /** @@ -423,7 +416,7 @@ export class ComfyApp implements IComfyApp { * triggering error logs, and reactive, so it can be watched. */ get isGraphReady(): boolean { - return !!this.rootGraphInternal + return !!this.rootGraphRef.value } private canvasInternal: LGraphCanvas | undefined @@ -912,7 +905,7 @@ export class ComfyApp implements IComfyApp { this.addAfterConfigureHandler(graph) - this.rootGraphInternal = graph + this.rootGraphRef.value = graph installNodeAddedTelemetry(graph) const interactionMode = createCanvasInteractionMode() this.canvas = new LGraphCanvas(canvasEl, graph, { interactionMode }) diff --git a/src/scripts/rootGraphReadiness.test.ts b/src/scripts/rootGraphReadiness.test.ts new file mode 100644 index 00000000000..299651cb4d8 --- /dev/null +++ b/src/scripts/rootGraphReadiness.test.ts @@ -0,0 +1,78 @@ +import { useEventListener } from '@vueuse/core' +import { effectScope, nextTick, watchEffect } from 'vue' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import { LGraph } from '@/lib/litegraph/src/litegraph' +import { getRootGraph, setRootGraph } from '@/scripts/__tests__/appTestUtils' +import { app } from '@/scripts/app' + +describe('ComfyApp root graph readiness', () => { + let scope: ReturnType + let previousRootGraph: LGraph | undefined + + beforeEach(() => { + previousRootGraph = getRootGraph(app) + setRootGraph(app, undefined) + scope = effectScope() + }) + + afterEach(() => { + scope.stop() + setRootGraph(app, previousRootGraph) + }) + + it('re-runs an effect reading isGraphReady when the graph is assigned', async () => { + const readiness: boolean[] = [] + scope.run(() => { + watchEffect(() => readiness.push(app.isGraphReady)) + }) + + expect(readiness).toEqual([false]) + + setRootGraph(app, new LGraph()) + await nextTick() + + expect(readiness).toEqual([false, true]) + }) + + it('binds a rootGraph.events listener registered before the graph exists', async () => { + const onConfigured = vi.fn() + scope.run(() => { + useEventListener( + () => app.rootGraphOrUndefined?.events, + 'configured', + onConfigured + ) + }) + + const graph = new LGraph() + setRootGraph(app, graph) + await nextTick() + graph.events.dispatch('configured') + + expect(onConfigured).toHaveBeenCalledOnce() + }) + + it('rebinds a rootGraph.events listener when the graph is replaced', async () => { + const onConfigured = vi.fn() + const firstGraph = new LGraph() + setRootGraph(app, firstGraph) + scope.run(() => { + useEventListener( + () => app.rootGraphOrUndefined?.events, + 'configured', + onConfigured + ) + }) + + const secondGraph = new LGraph() + setRootGraph(app, secondGraph) + await nextTick() + + firstGraph.events.dispatch('configured') + expect(onConfigured).not.toHaveBeenCalled() + + secondGraph.events.dispatch('configured') + expect(onConfigured).toHaveBeenCalledOnce() + }) +})