mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-24 14:45:36 +00:00
## Summary When a socket disconnects messages can be missed and lead to a stale UI state, this updates the state on reconnect and clears the active job if it is no longer running ## Changes - **What**: - add call to update queue on reconnect - clear active job if job not in queue response - tests ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-12067-fix-clear-active-job-on-reconnect-if-no-longer-in-queue-3596d73d365081f79d42d73966420c50) by [Unito](https://www.unito.io)
89 lines
2.8 KiB
TypeScript
89 lines
2.8 KiB
TypeScript
import { createTestingPinia } from '@pinia/testing'
|
|
import { setActivePinia } from 'pinia'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { useReconnectQueueRefresh } from '@/composables/useReconnectQueueRefresh'
|
|
import type { JobListItem } from '@/platform/remote/comfyui/jobs/jobTypes'
|
|
import { api } from '@/scripts/api'
|
|
import { useExecutionStore } from '@/stores/executionStore'
|
|
|
|
function makeJob(id: string, status: JobListItem['status']): JobListItem {
|
|
return {
|
|
id,
|
|
status,
|
|
create_time: 0,
|
|
update_time: 0,
|
|
last_state_update: 0,
|
|
priority: 0
|
|
}
|
|
}
|
|
|
|
vi.mock('@/scripts/api', () => ({
|
|
api: {
|
|
getQueue: vi.fn(),
|
|
getHistory: vi.fn(),
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn(),
|
|
apiURL: vi.fn((p: string) => `/api${p}`)
|
|
}
|
|
}))
|
|
|
|
describe('useReconnectQueueRefresh', () => {
|
|
beforeEach(() => {
|
|
setActivePinia(createTestingPinia({ stubActions: false }))
|
|
vi.restoreAllMocks()
|
|
vi.mocked(api.getQueue).mockResolvedValue({ Running: [], Pending: [] })
|
|
vi.mocked(api.getHistory).mockResolvedValue([])
|
|
})
|
|
|
|
it('forwards running+pending job ids to clearActiveJobIfStale', async () => {
|
|
vi.mocked(api.getQueue).mockResolvedValue({
|
|
Running: [makeJob('run-1', 'in_progress')],
|
|
Pending: [makeJob('pend-1', 'pending'), makeJob('pend-2', 'pending')]
|
|
})
|
|
const executionStore = useExecutionStore()
|
|
const clearSpy = vi
|
|
.spyOn(executionStore, 'clearActiveJobIfStale')
|
|
.mockImplementation(() => {})
|
|
|
|
const refresh = useReconnectQueueRefresh()
|
|
await refresh()
|
|
|
|
expect(clearSpy).toHaveBeenCalledTimes(1)
|
|
expect(clearSpy).toHaveBeenCalledWith(
|
|
new Set(['run-1', 'pend-1', 'pend-2'])
|
|
)
|
|
})
|
|
|
|
it('passes an empty set when the queue is genuinely empty', async () => {
|
|
const executionStore = useExecutionStore()
|
|
const clearSpy = vi
|
|
.spyOn(executionStore, 'clearActiveJobIfStale')
|
|
.mockImplementation(() => {})
|
|
|
|
const refresh = useReconnectQueueRefresh()
|
|
await refresh()
|
|
|
|
expect(clearSpy).toHaveBeenCalledWith(new Set())
|
|
})
|
|
|
|
it('reuses the prior queue snapshot when the fetch fails, so a still-running job is not falsely cleared', async () => {
|
|
vi.mocked(api.getQueue)
|
|
.mockResolvedValueOnce({
|
|
Running: [makeJob('run-1', 'in_progress')],
|
|
Pending: []
|
|
})
|
|
.mockRejectedValueOnce(new Error('network down'))
|
|
const executionStore = useExecutionStore()
|
|
const clearSpy = vi
|
|
.spyOn(executionStore, 'clearActiveJobIfStale')
|
|
.mockImplementation(() => {})
|
|
|
|
const refresh = useReconnectQueueRefresh()
|
|
await refresh() // primes the store with run-1
|
|
await refresh() // network failure here — store must not go empty
|
|
|
|
expect(clearSpy).toHaveBeenLastCalledWith(new Set(['run-1']))
|
|
})
|
|
})
|