Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions browser_tests/fixtures/helpers/WorkflowHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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<string[]> {
return this.comfyPage.page.evaluate(() =>
Expand Down
59 changes: 59 additions & 0 deletions browser_tests/tests/workflowPersistence.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
})

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
}) => {
Expand Down
22 changes: 19 additions & 3 deletions src/platform/workflow/management/stores/comfyWorkflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions src/platform/workflow/management/stores/workflowStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/platform/workflow/management/stores/workflowStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions src/scripts/changeTrackerAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
Loading