-
Notifications
You must be signed in to change notification settings - Fork 672
fix: keep node preview images across workflow tab switches #15360
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 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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,164 @@ | ||
| import { fromAny } from '@total-typescript/shoehorn' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| import type { LGraphNode } from '@/lib/litegraph/src/litegraph' | ||
| import { app } from '@/scripts/app' | ||
| import { useNodeOutputStore } from '@/stores/nodeOutputStore' | ||
| import { toNodeId } from '@/types/nodeId' | ||
|
|
||
| const { WORKFLOW_A, WORKFLOW_B } = vi.hoisted(() => ({ | ||
| WORKFLOW_A: 'workflows/a.json', | ||
| WORKFLOW_B: 'workflows/b.json' | ||
| })) | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| workflowStore: null as unknown as { | ||
| activeWorkflow: { path: string } | null | ||
| openWorkflows: { path: string }[] | ||
| nodeIdToNodeLocatorId: (id: string | number) => string | ||
| nodeToNodeLocatorId: (node: { id: string | number }) => string | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('@/utils/litegraphUtil', () => ({ | ||
| isAnimatedOutput: vi.fn(() => false), | ||
| isVideoNode: vi.fn(() => false), | ||
| resolveNode: vi.fn() | ||
| })) | ||
|
|
||
| vi.mock('@/utils/graphTraversalUtil', () => ({ | ||
| executionIdToNodeLocatorId: vi.fn((_rootGraph: unknown, id: string) => id) | ||
| })) | ||
|
|
||
| vi.mock('@/scripts/app', () => ({ | ||
| app: { | ||
| getPreviewFormatParam: vi.fn(() => ''), | ||
| getRandParam: vi.fn(() => ''), | ||
| rootGraph: { getNodeById: vi.fn() }, | ||
| nodeOutputs: {} as Record<string, unknown>, | ||
| nodePreviewImages: {} as Record<string, string[]> | ||
| } | ||
| })) | ||
|
|
||
| vi.mock('@/platform/workflow/management/stores/workflowStore', async () => { | ||
| const { reactive } = await import('vue') | ||
| mocks.workflowStore = reactive({ | ||
| activeWorkflow: { path: WORKFLOW_A } as { path: string } | null, | ||
| openWorkflows: [{ path: WORKFLOW_A }, { path: WORKFLOW_B }], | ||
| nodeIdToNodeLocatorId: (id: string | number) => String(id), | ||
| nodeToNodeLocatorId: (node: { id: string | number }) => String(node.id) | ||
| }) | ||
| return { useWorkflowStore: () => mocks.workflowStore } | ||
| }) | ||
|
|
||
| const createMockNode = (id: number): LGraphNode => | ||
| fromAny<LGraphNode, unknown>({ id: toNodeId(id), type: 'KSampler' }) | ||
|
|
||
| /** | ||
| * Mirrors `workflowService.beforeLoadNewGraph()` -> `app.clean()` -> | ||
| * `workflowService.afterLoadNewGraph()`, the sequence every workflow load and | ||
| * tab switch runs. | ||
| */ | ||
| function switchToWorkflow(path: string) { | ||
| const store = useNodeOutputStore() | ||
| const leaving = mocks.workflowStore.activeWorkflow | ||
| if (leaving) store.stashPreviewsForWorkflow(leaving.path) | ||
| store.resetAllOutputsAndPreviews() | ||
| mocks.workflowStore.activeWorkflow = { path } | ||
| store.restorePreviewsForWorkflow(path) | ||
| } | ||
|
|
||
| // Object URL retain counts live in a module-level map in objectUrlUtil, so | ||
| // every test needs its own URLs or the counts leak between them. | ||
| let urlCounter = 0 | ||
|
|
||
| describe('nodeOutputStore preview lifecycle across workflow tab switches', () => { | ||
| let revokeObjectURL: ReturnType<typeof vi.spyOn> | ||
|
|
||
| const createBlobUrl = () => URL.createObjectURL(new Blob(['x'])) | ||
|
|
||
| beforeEach(() => { | ||
| vi.spyOn(URL, 'createObjectURL').mockImplementation( | ||
| () => `blob:mock/${++urlCounter}` | ||
| ) | ||
| revokeObjectURL = vi | ||
| .spyOn(URL, 'revokeObjectURL') | ||
| .mockImplementation(() => {}) | ||
| app.nodeOutputs = {} | ||
| app.nodePreviewImages = {} | ||
| mocks.workflowStore.activeWorkflow = { path: WORKFLOW_A } | ||
| mocks.workflowStore.openWorkflows = [ | ||
| { path: WORKFLOW_A }, | ||
| { path: WORKFLOW_B } | ||
| ] | ||
| }) | ||
|
|
||
| it('keeps a finished run preview when the user switches tabs and comes back', () => { | ||
| const store = useNodeOutputStore() | ||
| const node = createMockNode(5) | ||
| const previewUrl = createBlobUrl() | ||
|
|
||
| // A run finished on workflow A and left its last preview frame on node 5. | ||
| // Nothing revokes it at execution end, so the node keeps displaying it. | ||
| store.setNodePreviewsByNodeId(node.id, [previewUrl]) | ||
| expect(store.getNodePreviews(node)).toEqual([previewUrl]) | ||
|
|
||
| // User switches to workflow B, then back to workflow A. | ||
| switchToWorkflow(WORKFLOW_B) | ||
| switchToWorkflow(WORKFLOW_A) | ||
|
|
||
| expect(store.getNodePreviews(node)).toEqual([previewUrl]) | ||
| expect(store.nodePreviewImages['5']).toEqual([previewUrl]) | ||
| // The blob must still be alive; a b_preview frame cannot be re-fetched. | ||
| expect(revokeObjectURL).not.toHaveBeenCalledWith(previewUrl) | ||
| }) | ||
|
|
||
| it('does not revoke the preview blob when leaving the workflow', () => { | ||
| const store = useNodeOutputStore() | ||
| const previewUrl = createBlobUrl() | ||
|
|
||
| store.setNodePreviewsByNodeId(createMockNode(5).id, [previewUrl]) | ||
| switchToWorkflow(WORKFLOW_B) | ||
|
|
||
| // Revoking makes the loss permanent: there is no source to re-derive a | ||
| // websocket preview frame from once the object URL is gone. | ||
| expect(revokeObjectURL).not.toHaveBeenCalledWith(previewUrl) | ||
| }) | ||
|
|
||
| it('does not show one workflow preview on another workflow same-id node', () => { | ||
| const store = useNodeOutputStore() | ||
| const node = createMockNode(5) | ||
|
|
||
| store.setNodePreviewsByNodeId(node.id, [createBlobUrl()]) | ||
| switchToWorkflow(WORKFLOW_B) | ||
|
|
||
| expect(store.getNodePreviews(node)).toBeUndefined() | ||
| expect(store.nodePreviewImages).toEqual({}) | ||
| }) | ||
|
|
||
| it('releases stashed previews once the workflow is closed', () => { | ||
| const store = useNodeOutputStore() | ||
| const previewUrl = createBlobUrl() | ||
|
|
||
| store.setNodePreviewsByNodeId(createMockNode(5).id, [previewUrl]) | ||
| switchToWorkflow(WORKFLOW_B) | ||
|
|
||
| mocks.workflowStore.openWorkflows = [{ path: WORKFLOW_B }] | ||
| switchToWorkflow(WORKFLOW_B) | ||
|
|
||
| expect(revokeObjectURL).toHaveBeenCalledWith(previewUrl) | ||
| }) | ||
|
|
||
| it('still revokes previews when the workflow is cleared in place', () => { | ||
| const store = useNodeOutputStore() | ||
| const node = createMockNode(5) | ||
| const previewUrl = createBlobUrl() | ||
|
|
||
| store.setNodePreviewsByNodeId(node.id, [previewUrl]) | ||
| // "Clear Workflow" calls app.clean() without loading another graph. | ||
| store.resetAllOutputsAndPreviews() | ||
|
|
||
| expect(store.getNodePreviews(node)).toBeUndefined() | ||
| expect(revokeObjectURL).toHaveBeenCalledWith(previewUrl) | ||
| }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| }) | ||
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.