diff --git a/browser_tests/fixtures/helpers/WorkflowHelper.ts b/browser_tests/fixtures/helpers/WorkflowHelper.ts index 4657e723bb1..7ca3c40d017 100644 --- a/browser_tests/fixtures/helpers/WorkflowHelper.ts +++ b/browser_tests/fixtures/helpers/WorkflowHelper.ts @@ -288,6 +288,16 @@ export class WorkflowHelper { await this.waitForWorkflowIdle() } + /** A saved workflow's tab, opened and confirmed unmodified with no `*` title. */ + async expectTabOpensAsSaved(tabName: string): Promise { + await this.switchToTab(tabName) + await expect + .poll(() => this.comfyPage.menu.topbar.getActiveTabName()) + .toBe(tabName) + await expect.poll(() => this.isCurrentWorkflowModified()).toBe(false) + await expect.poll(() => this.comfyPage.page.title()).not.toMatch(/^\*/) + } + /** Node ids on the live graph, read from LiteGraph rather than the DOM. */ getGraphNodeIds(): Promise { return this.comfyPage.page.evaluate(() => diff --git a/browser_tests/tests/workflowPersistence.spec.ts b/browser_tests/tests/workflowPersistence.spec.ts index 978a7cc8b20..97e1fb3cfe3 100644 --- a/browser_tests/tests/workflowPersistence.spec.ts +++ b/browser_tests/tests/workflowPersistence.spec.ts @@ -1014,6 +1014,65 @@ test.describe('Workflow Persistence', () => { await expect(comfyPage.toast.toastErrors).toHaveCount(0) }) + test('Unedited saved workflows reopen as saved after a reload', async ({ + comfyPage + }) => { + test.info().annotations.push({ + type: 'regression', + description: + 'Switching tabs leaves a draft equal to the saved file; after a reload that draft marked the workflow unsaved' + }) + test.setTimeout(45_000) + + await comfyPage.settings.setSetting('Comfy.UseNewMenu', 'Top') + await comfyPage.settings.setSetting('Comfy.Workflow.Persist', true) + await comfyPage.settings.setSetting( + 'Comfy.Workflow.WorkflowTabsPosition', + 'Topbar' + ) + + const [first, second, third] = [ + generateUniqueFilename(), + generateUniqueFilename(), + generateUniqueFilename() + ] + await comfyPage.menu.topbar.saveWorkflow(first) + await comfyPage.menu.topbar.triggerTopbarCommand(['New']) + await comfyPage.menu.topbar.saveWorkflow(second) + await comfyPage.menu.topbar.triggerTopbarCommand(['New']) + await comfyPage.menu.topbar.saveWorkflow(third) + + await test.step('visit every tab without editing, leaving drafts behind', async () => { + await comfyPage.workflow.expectTabOpensAsSaved(first) + await comfyPage.workflow.expectTabOpensAsSaved(second) + await comfyPage.workflow.expectTabOpensAsSaved(third) + await waitForWorkflowTabState(comfyPage, 3) + await expect + .poll(async () => (await comfyPage.workflow.getDraftWorkflows()).length) + .toBeGreaterThanOrEqual(2) + }) + + await comfyPage.workflow.reloadAndWaitForApp() + + await test.step('the tab active at reload is restored as saved', async () => { + await expect + .poll(() => comfyPage.menu.topbar.getActiveTabName()) + .toBe(third) + await expect + .poll(() => comfyPage.workflow.isCurrentWorkflowModified()) + .toBe(false) + await expect.poll(() => comfyPage.page.title()).not.toMatch(/^\*/) + }) + + await test.step('the first restored tab reopens as saved', async () => { + await comfyPage.workflow.expectTabOpensAsSaved(first) + }) + + await test.step('the second restored tab reopens as saved', async () => { + await comfyPage.workflow.expectTabOpensAsSaved(second) + }) + }) + test('Flushes a pending draft edit before an immediate reload', async ({ comfyPage }) => { diff --git a/src/platform/workflow/management/stores/comfyWorkflow.ts b/src/platform/workflow/management/stores/comfyWorkflow.ts index 4b25bc33ab3..b06a610fec7 100644 --- a/src/platform/workflow/management/stores/comfyWorkflow.ts +++ b/src/platform/workflow/management/stores/comfyWorkflow.ts @@ -82,6 +82,8 @@ export interface WorkflowRuntime { workflow: ComfyWorkflow, initialState: ComfyWorkflowJSON ): WorkflowChangeTracker + /** `ChangeTracker.graphEqual`: the comparison that decides unsaved changes. */ + graphEqual(a: ComfyWorkflowJSON, b: ComfyWorkflowJSON): boolean getDraft(path: string): WorkflowDraft | null removeDraft(path: string): void markDraftUsed(path: string): void @@ -219,6 +221,18 @@ function readUsableDraft( } } +function draftMatchesSavedFile( + runtime: WorkflowRuntime, + saved: ComfyWorkflowJSON, + draft: ComfyWorkflowJSON +): boolean { + return ( + Array.isArray(saved.nodes) && + Array.isArray(draft.nodes) && + runtime.graphEqual(saved, draft) + ) +} + /** * The workflow object parsed from the stored text. Anything else (nothing * stored, empty text, JSON `null`, a number, an array) cannot back a change @@ -365,9 +379,11 @@ export class ComfyWorkflow extends UserFile { if (draft?.state && draft.content) { this.changeTracker.activeState = draft.state this.content = draft.content - this._isModified = true - // Saved-workflow draft overlay path; direct persisted-draft restores - // are touched in workflowDraftStoreV2.loadDraft(). + this._isModified = !draftMatchesSavedFile( + runtime, + initialState, + draft.state + ) runtime.markDraftUsed(this.path) } return this as this & LoadedComfyWorkflow diff --git a/src/platform/workflow/management/stores/workflowStore.test.ts b/src/platform/workflow/management/stores/workflowStore.test.ts index 11e33c44ef8..8eccba45f99 100644 --- a/src/platform/workflow/management/stores/workflowStore.test.ts +++ b/src/platform/workflow/management/stores/workflowStore.test.ts @@ -503,6 +503,73 @@ describe('useWorkflowStore', () => { expect(workflow.isModified).toBe(true) }) + describe('restoring a saved workflow over its draft', () => { + const loadWithDraft = async ( + draftData: string, + fileData = defaultGraphJSON + ) => { + enableWorkflowPersistence() + await syncRemoteWorkflowsWithMeta([ + { path: 'a.json', modified: 100, size: 1 } + ]) + const workflow = store.getWorkflowByPath('workflows/a.json')! + saveV2Draft(workflow.path, { data: draftData, name: 'a.json' }) + vi.mocked(api.getUserData).mockResolvedValue( + new Response(fileData, { status: 200 }) + ) + await workflow.load() + return workflow + } + + const draftWithExtra = (extra: ComfyWorkflowJSON['extra']) => { + const draftGraph: ComfyWorkflowJSON = structuredClone(defaultGraph) + draftGraph.extra = { ...draftGraph.extra, ...extra } + return JSON.stringify(draftGraph) + } + + it('does not mark it modified when the draft a tab switch left matches the file', async () => { + const workflow = await loadWithDraft(defaultGraphJSON) + + expect(workflow.activeState).toEqual(defaultGraph) + expect(workflow.isModified).toBe(false) + }) + + it('keeps the draft viewport without marking it modified', async () => { + const workflow = await loadWithDraft( + draftWithExtra({ ds: { scale: 2.5, offset: [40, -80] } }) + ) + + expect(workflow.activeState?.extra?.ds).toEqual({ + scale: 2.5, + offset: [40, -80] + }) + expect(workflow.isModified).toBe(false) + }) + + it('marks it modified when the draft only changes app mode', async () => { + const workflow = await loadWithDraft( + draftWithExtra({ linearMode: true }) + ) + + expect(workflow.activeState?.extra?.linearMode).toBe(true) + expect(workflow.isModified).toBe(true) + }) + + it('marks it modified when the draft has no node list to compare', async () => { + const workflow = await loadWithDraft('{"links":[]}') + + expect(workflow.activeState).toEqual({ links: [] }) + expect(workflow.isModified).toBe(true) + }) + + it('marks it modified when the saved file has no node list to compare', async () => { + const workflow = await loadWithDraft(defaultGraphJSON, '{"links":[]}') + + expect(workflow.activeState).toEqual(defaultGraph) + expect(workflow.isModified).toBe(true) + }) + }) + it('should discard a stale V2 draft when the remote workflow is newer', async () => { enableWorkflowPersistence() const remoteModifiedAt = Date.now() + 60_000 diff --git a/src/platform/workflow/management/stores/workflowStore.ts b/src/platform/workflow/management/stores/workflowStore.ts index 13b2c62b345..a1d636ed939 100644 --- a/src/platform/workflow/management/stores/workflowStore.ts +++ b/src/platform/workflow/management/stores/workflowStore.ts @@ -55,6 +55,7 @@ export { ComfyWorkflow, type LoadedComfyWorkflow } registerWorkflowRuntime({ createChangeTracker: (workflow, initialState) => new ChangeTrackerAccess(workflow, initialState), + graphEqual: (a, b) => ChangeTrackerAccess.graphEqual(a, b), getDraft: (path) => useWorkflowDraftStoreV2().getDraft(path), removeDraft: (path) => useWorkflowDraftStoreV2().removeDraft(path), markDraftUsed: (path) => useWorkflowDraftStoreV2().markDraftUsed(path), diff --git a/src/scripts/changeTrackerAccess.ts b/src/scripts/changeTrackerAccess.ts index 4f7e2119df2..a9802a41c57 100644 --- a/src/scripts/changeTrackerAccess.ts +++ b/src/scripts/changeTrackerAccess.ts @@ -10,6 +10,10 @@ interface ChangeTrackerClass { initialState: ComfyWorkflowJSON ): WorkflowChangeTracker readonly isLoadingGraph: boolean + graphEqual( + a: ComfyWorkflowJSON | null | undefined, + b: ComfyWorkflowJSON | null | undefined + ): boolean } /** `ChangeTracker` for the stores it imports, which cannot import it back. */