From d97bd369eb9f8b071b344c62f8fc4b039e5282be Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 26 Aug 2026 02:55:09 -0700 Subject: [PATCH 1/8] fix: make asset refresh pagination bounded --- .../assets/composables/useAssetsQuery.test.ts | 159 ++++++++++++++++++ .../assets/composables/useAssetsQuery.ts | 30 +++- 2 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 src/platform/assets/composables/useAssetsQuery.test.ts diff --git a/src/platform/assets/composables/useAssetsQuery.test.ts b/src/platform/assets/composables/useAssetsQuery.test.ts new file mode 100644 index 00000000000..99e062f795a --- /dev/null +++ b/src/platform/assets/composables/useAssetsQuery.test.ts @@ -0,0 +1,159 @@ +import { effectScope, toValue } from 'vue' +import { describe, expect, it, onTestFinished, vi } from 'vitest' + +import { useAssetsQuery } from '@/platform/assets/composables/useAssetsQuery' +import type { + AssetItem, + AssetResponse +} from '@/platform/assets/schemas/assetSchema' +import { api } from '@/scripts/api' + +vi.mock('@/scripts/api', () => ({ + api: { fetchApi: vi.fn() } +})) + +const fetchApiMock = vi.mocked(api.fetchApi) + +function asset(id: string): AssetItem { + return { + id, + name: `${id}.png`, + loader_path: `${id}.png`, + tags: ['output'], + created_at: '2026-08-26T00:00:00Z', + updated_at: '2026-08-26T00:00:00Z' + } +} + +function response( + ids: string[], + { + hasMore = false, + nextCursor + }: { hasMore?: boolean; nextCursor?: string } = {} +): Response { + const body: AssetResponse = { + assets: ids.map(asset), + total: ids.length, + has_more: hasMore, + ...(nextCursor === undefined ? {} : { next_cursor: nextCursor }) + } + return { + ok: true, + json: vi.fn().mockResolvedValue(body) + } as unknown as Response +} + +async function createList(key: string, initialIds: string[]) { + fetchApiMock.mockResolvedValueOnce(response(initialIds)) + const scope = effectScope() + const list = scope.run(() => useAssetsQuery({ name_contains: key }))! + onTestFinished(() => scope.stop()) + await vi.waitFor(() => expect(toValue(list.isLoading)).toBe(false)) + return list +} + +function requestedAfterCursors() { + return fetchApiMock.mock.calls.slice(1).map(([url]) => { + const requestUrl = new URL(String(url), 'http://localhost') + return requestUrl.searchParams.get('after') + }) +} + +describe('useAssetsQuery loadNew pagination', () => { + it('prepends multiple pages in newest-to-oldest order', async () => { + const list = await createList('order', ['known']) + fetchApiMock + .mockResolvedValueOnce( + response(['newest', 'newer'], { hasMore: true, nextCursor: 'page-2' }) + ) + .mockResolvedValueOnce( + response(['new'], { hasMore: true, nextCursor: 'page-3' }) + ) + .mockResolvedValueOnce(response(['newish'], { hasMore: false })) + + await list.loadNew() + + expect(toValue(list.items).map(({ id }) => id)).toEqual([ + 'newest', + 'newer', + 'new', + 'newish', + 'known' + ]) + expect(requestedAfterCursors()).toEqual([null, 'page-2', 'page-3']) + }) + + it('stops at a known id without inserting duplicates from overlapping pages', async () => { + const list = await createList('known-id', ['known', 'old']) + fetchApiMock + .mockResolvedValueOnce( + response(['new'], { hasMore: true, nextCursor: 'page-2' }) + ) + .mockResolvedValueOnce( + response(['new', 'known', 'older-unseen'], { + hasMore: true, + nextCursor: 'page-3' + }) + ) + .mockRejectedValue(new Error('unexpected page after known id')) + + await list.loadNew() + + expect(fetchApiMock).toHaveBeenCalledTimes(3) + expect(toValue(list.items).map(({ id }) => id)).toEqual([ + 'new', + 'known', + 'old' + ]) + expect(requestedAfterCursors()).toEqual([null, 'page-2']) + }) + + it('terminates when the cursor does not advance', async () => { + const list = await createList('stuck-cursor', ['known']) + fetchApiMock + .mockResolvedValueOnce( + response(['newest'], { hasMore: true, nextCursor: 'stuck' }) + ) + .mockResolvedValueOnce( + response(['newer'], { hasMore: true, nextCursor: 'stuck' }) + ) + .mockRejectedValue(new Error('unexpected extra page')) + + await list.loadNew() + + expect(fetchApiMock).toHaveBeenCalledTimes(3) + expect(toValue(list.items).map(({ id }) => id)).toEqual([ + 'newest', + 'newer', + 'known' + ]) + expect(requestedAfterCursors()).toEqual([null, 'stuck']) + }) + + it('terminates when cursors cycle', async () => { + const list = await createList('cycling-cursor', ['known']) + fetchApiMock + .mockResolvedValueOnce( + response(['newest'], { hasMore: true, nextCursor: 'A' }) + ) + .mockResolvedValueOnce( + response(['newer'], { hasMore: true, nextCursor: 'B' }) + ) + .mockResolvedValueOnce( + response(['new'], { hasMore: true, nextCursor: 'A' }) + ) + .mockRejectedValue(new Error('unexpected page after cursor cycle')) + + await list.loadNew() + + expect(fetchApiMock).toHaveBeenCalledTimes(4) + expect(toValue(list.items).map(({ id }) => id)).toEqual([ + 'newest', + 'newer', + 'new', + 'known' + ]) + expect(requestedAfterCursors()).toEqual([null, 'A', 'B']) + }) +}) diff --git a/src/platform/assets/composables/useAssetsQuery.ts b/src/platform/assets/composables/useAssetsQuery.ts index b7ce7bd12ef..bcfd2b94051 100644 --- a/src/platform/assets/composables/useAssetsQuery.ts +++ b/src/platform/assets/composables/useAssetsQuery.ts @@ -57,21 +57,37 @@ function assetsQueryInternal( function loadNew() { return enqueue('loadNew', async function (signal: AbortSignal) { const knownIds = new Set(items.value.map((item) => item.id)) + const seenIds = new Set(knownIds) + const seenHeadCursors = new Set() const newItems: AssetItem[] = [] let headCursor: string | undefined - const seenHeadCursors = new Set() while (true) { - if (seenHeadCursors.has(headCursor)) break - seenHeadCursors.add(headCursor) - const assetResponse = await doQuery({ after: headCursor }, signal) if (!assetResponse) return const { assets, has_more, next_cursor } = assetResponse + let reachedKnownId = false + for (const asset of assets) { + if (knownIds.has(asset.id)) { + reachedKnownId = true + break + } + if (seenIds.has(asset.id)) { + continue + } + seenIds.add(asset.id) + newItems.push(asset) + } + if ( + reachedKnownId || + !has_more || + next_cursor === undefined || + seenHeadCursors.has(next_cursor) + ) { + break + } + seenHeadCursors.add(next_cursor) headCursor = next_cursor - const newFromPage = assets.filter(({ id }) => !knownIds.has(id)) - newItems.push(...newFromPage) - if (newFromPage.length !== assets.length || !has_more) break } items.value.splice(0, 0, ...newItems) }) From 6bbeb7cee4f31395a59fb93234efcffa092caff8 Mon Sep 17 00:00:00 2001 From: christian-byrne Date: Wed, 26 Aug 2026 03:21:56 -0700 Subject: [PATCH 2/8] fix: load queued outputs across asset pages --- .../queue/QueueProgressOverlay.test.ts | 103 +++++++++++++++++- src/components/queue/QueueProgressOverlay.vue | 26 ++++- .../assets/composables/useAssetsQuery.test.ts | 78 ++++++++++++- .../assets/composables/useAssetsQuery.ts | 54 ++++++--- src/utils/pagedList.ts | 2 + 5 files changed, 245 insertions(+), 18 deletions(-) diff --git a/src/components/queue/QueueProgressOverlay.test.ts b/src/components/queue/QueueProgressOverlay.test.ts index 067c8084d9b..cffb1615afb 100644 --- a/src/components/queue/QueueProgressOverlay.test.ts +++ b/src/components/queue/QueueProgressOverlay.test.ts @@ -1,11 +1,13 @@ import { createTestingPinia } from '@pinia/testing' -import { render, screen } from '@testing-library/vue' +import { render, screen, waitFor } from '@testing-library/vue' import userEvent from '@testing-library/user-event' import { beforeEach, describe, expect, it, vi } from 'vitest' import { defineComponent } from 'vue' import QueueProgressOverlay from '@/components/queue/QueueProgressOverlay.vue' +import type { JobListItem as JobListViewItem } from '@/composables/queue/useJobList' import { i18n } from '@/i18n' +import { useAssetSelectionStore } from '@/platform/assets/composables/useAssetSelectionStore' import type { JobStatus } from '@/platform/remote/comfyui/jobs/jobTypes' import { TaskItemImpl, useQueueStore } from '@/stores/queueStore' import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore' @@ -14,6 +16,21 @@ vi.mock('@/platform/distribution/types', () => ({ isCloud: false })) +const { outputAssetsMock } = vi.hoisted(() => ({ + outputAssetsMock: { + hasMore: false, + items: [] as Array<{ id: string }>, + loadMore: vi.fn<() => Promise>(), + loadMoreWithProgress: vi.fn<() => Promise>() + } +})) + +vi.mock('@/stores/assetsStore', () => ({ + useAssetsStore: () => ({ outputAssets: outputAssetsMock }) +})) + +let itemToView: JobListViewItem | undefined + const QueueOverlayExpandedStub = defineComponent({ name: 'QueueOverlayExpanded', props: { @@ -22,10 +39,17 @@ const QueueOverlayExpandedStub = defineComponent({ required: true } }, + emits: ['viewItem'], + setup(_, { emit }) { + return { + viewItem: () => itemToView && emit('viewItem', itemToView) + } + }, template: `
{{ headerTitle }}
` }) @@ -39,6 +63,30 @@ function createTask(id: string, status: JobStatus): TaskItemImpl { }) } +function createCompletedJobView(id: string): JobListViewItem { + const task = new TaskItemImpl({ + id, + status: 'completed', + create_time: 0, + priority: 0, + preview_output: { + filename: `${id}.png`, + mediaType: 'images', + nodeId: '1', + subfolder: '', + type: 'output' + } + }) + return { + id, + meta: '', + showClear: false, + state: 'completed', + taskRef: task, + title: id + } as JobListViewItem +} + function renderComponent( runningTasks: TaskItemImpl[], pendingTasks: TaskItemImpl[] @@ -48,6 +96,7 @@ function renderComponent( stubActions: false }) const queueStore = useQueueStore(pinia) + const assetSelectionStore = useAssetSelectionStore(pinia) const sidebarTabStore = useSidebarTabStore(pinia) queueStore.runningTasks = runningTasks queueStore.pendingTasks = pendingTasks @@ -71,12 +120,17 @@ function renderComponent( } }) - return { sidebarTabStore, user } + return { assetSelectionStore, sidebarTabStore, user } } describe('QueueProgressOverlay', () => { beforeEach(() => { i18n.global.locale.value = 'en' + itemToView = undefined + outputAssetsMock.hasMore = false + outputAssetsMock.items = [] + outputAssetsMock.loadMore.mockReset() + outputAssetsMock.loadMoreWithProgress.mockReset() }) it('shows expanded header with running and queued labels', () => { @@ -116,4 +170,49 @@ describe('QueueProgressOverlay', () => { await user.click(screen.getByTestId('show-assets-button')) expect(sidebarTabStore.activeSidebarTabId).toBe(null) }) + + it('loads older pages before selecting a job asset', async () => { + itemToView = createCompletedJobView('target-job') + outputAssetsMock.hasMore = true + outputAssetsMock.loadMoreWithProgress + .mockImplementationOnce(async () => { + outputAssetsMock.items.push({ id: 'other-job' }) + return true + }) + .mockImplementationOnce(async () => { + outputAssetsMock.items.push({ id: 'target-job' }) + outputAssetsMock.hasMore = false + return true + }) + const { assetSelectionStore, sidebarTabStore, user } = renderComponent( + [], + [] + ) + + await user.click(screen.getByTestId('view-item-button')) + + await waitFor(() => { + expect(outputAssetsMock.loadMoreWithProgress).toHaveBeenCalledTimes(2) + expect(assetSelectionStore.selectedIdsArray).toEqual(['target-job']) + }) + expect(sidebarTabStore.activeSidebarTabId).toBe('assets') + }) + + it('does not select a missing job asset when pagination cannot advance', async () => { + itemToView = createCompletedJobView('missing-job') + outputAssetsMock.hasMore = true + outputAssetsMock.loadMoreWithProgress.mockResolvedValue(false) + const { assetSelectionStore, sidebarTabStore, user } = renderComponent( + [], + [] + ) + + await user.click(screen.getByTestId('view-item-button')) + + await waitFor(() => + expect(outputAssetsMock.loadMoreWithProgress).toHaveBeenCalledTimes(1) + ) + expect(assetSelectionStore.selectedIdsArray).toEqual([]) + expect(sidebarTabStore.activeSidebarTabId).toBe('assets') + }) }) diff --git a/src/components/queue/QueueProgressOverlay.vue b/src/components/queue/QueueProgressOverlay.vue index 1167f27af3e..afe9800f03d 100644 --- a/src/components/queue/QueueProgressOverlay.vue +++ b/src/components/queue/QueueProgressOverlay.vue @@ -53,7 +53,7 @@