diff --git a/src/components/queue/QueueProgressOverlay.test.ts b/src/components/queue/QueueProgressOverlay.test.ts index 067c8084d9b..dfe5a390264 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,18 @@ vi.mock('@/platform/distribution/types', () => ({ isCloud: false })) +const { outputAssetsMock } = vi.hoisted(() => ({ + outputAssetsMock: { + loadOutputAsset: vi.fn<(id: string) => Promise>() + } +})) + +vi.mock('@/stores/assetsStore', () => ({ + useAssetsStore: () => outputAssetsMock +})) + +let itemToView: JobListViewItem | undefined + const QueueOverlayExpandedStub = defineComponent({ name: 'QueueOverlayExpanded', props: { @@ -22,10 +36,17 @@ const QueueOverlayExpandedStub = defineComponent({ required: true } }, + emits: ['viewItem'], + setup(_, { emit }) { + return { + viewItem: () => itemToView && emit('viewItem', itemToView) + } + }, template: `
{{ headerTitle }}
` }) @@ -39,6 +60,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 + } +} + function renderComponent( runningTasks: TaskItemImpl[], pendingTasks: TaskItemImpl[] @@ -48,6 +93,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 +117,14 @@ function renderComponent( } }) - return { sidebarTabStore, user } + return { assetSelectionStore, sidebarTabStore, user } } describe('QueueProgressOverlay', () => { beforeEach(() => { i18n.global.locale.value = 'en' + itemToView = undefined + outputAssetsMock.loadOutputAsset.mockReset() }) it('shows expanded header with running and queued labels', () => { @@ -116,4 +164,66 @@ 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.loadOutputAsset.mockResolvedValue(true) + const { assetSelectionStore, sidebarTabStore, user } = renderComponent( + [], + [] + ) + + await user.click(screen.getByTestId('view-item-button')) + + await waitFor(() => { + expect(outputAssetsMock.loadOutputAsset).toHaveBeenCalledWith( + 'target-job' + ) + 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.loadOutputAsset.mockResolvedValue(false) + const { assetSelectionStore, sidebarTabStore, user } = renderComponent( + [], + [] + ) + + await user.click(screen.getByTestId('view-item-button')) + + await waitFor(() => + expect(outputAssetsMock.loadOutputAsset).toHaveBeenCalledWith( + 'missing-job' + ) + ) + expect(assetSelectionStore.selectedIdsArray).toEqual([]) + expect(sidebarTabStore.activeSidebarTabId).toBe('assets') + }) + + it('only selects the latest asset when focus requests overlap', async () => { + let resolveFirst!: (found: boolean) => void + outputAssetsMock.loadOutputAsset + .mockImplementationOnce( + () => new Promise((resolve) => (resolveFirst = resolve)) + ) + .mockResolvedValueOnce(true) + const { assetSelectionStore, user } = renderComponent([], []) + + itemToView = createCompletedJobView('older-request') + await user.click(screen.getByTestId('view-item-button')) + itemToView = createCompletedJobView('latest-request') + await user.click(screen.getByTestId('view-item-button')) + + await waitFor(() => + expect(assetSelectionStore.selectedIdsArray).toEqual(['latest-request']) + ) + resolveFirst(true) + await waitFor(() => + expect(outputAssetsMock.loadOutputAsset).toHaveBeenCalledTimes(2) + ) + expect(assetSelectionStore.selectedIdsArray).toEqual(['latest-request']) + }) }) diff --git a/src/components/queue/QueueProgressOverlay.vue b/src/components/queue/QueueProgressOverlay.vue index 1167f27af3e..b6779b74531 100644 --- a/src/components/queue/QueueProgressOverlay.vue +++ b/src/components/queue/QueueProgressOverlay.vue @@ -71,6 +71,7 @@ import { api } from '@/scripts/api' import { useCommandStore } from '@/stores/commandStore' import { useExecutionStore } from '@/stores/executionStore' import { useQueueStore } from '@/stores/queueStore' +import { useAssetsStore } from '@/stores/assetsStore' import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore' type OverlayState = 'hidden' | 'active' | 'expanded' @@ -88,6 +89,7 @@ const { t, n } = useI18n() const queueStore = useQueueStore() const commandStore = useCommandStore() const executionStore = useExecutionStore() +const assetsStore = useAssetsStore() const sidebarTabStore = useSidebarTabStore() const assetSelectionStore = useAssetSelectionStore() const { showQueueClearHistoryDialog } = useQueueClearHistoryDialog() @@ -234,7 +236,9 @@ const openAssetsSidebar = () => { sidebarTabStore.activeSidebarTabId = 'assets' } +let focusRequest = 0 const focusAssetInSidebar = async (item: JobListItem) => { + const request = ++focusRequest const task = item.taskRef const jobId = task?.jobId const preview = task?.previewOutput @@ -243,6 +247,10 @@ const focusAssetInSidebar = async (item: JobListItem) => { const assetId = String(jobId) openAssetsSidebar() await nextTick() + + if (!(await assetsStore.loadOutputAsset(assetId)) || request !== focusRequest) + return + assetSelectionStore.setSelection([assetId]) assetSelectionStore.setLastSelectedAssetId(assetId) } diff --git a/src/platform/assets/composables/useAssetsQuery.test.ts b/src/platform/assets/composables/useAssetsQuery.test.ts new file mode 100644 index 00000000000..6ed3c16b9c0 --- /dev/null +++ b/src/platform/assets/composables/useAssetsQuery.test.ts @@ -0,0 +1,267 @@ +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 new Response(JSON.stringify(body), { + status: 200, + headers: { 'Content-Type': 'application/json' } + }) +} + +function deferred() { + let resolve!: (value: T) => void + const promise = new Promise((res) => { + resolve = res + }) + return { promise, resolve } +} + +async function createList( + key: string, + initialIds: string[], + options: { hasMore?: boolean; nextCursor?: string } = {} +) { + fetchApiMock.mockResolvedValueOnce(response(initialIds, options)) + 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']) + }) +}) + +describe('useAssetsQuery loadMore pagination', () => { + it('deduplicates overlapping pages and reports successful progress', async () => { + const list = await createList('load-more', ['newest', 'overlap'], { + hasMore: true, + nextCursor: 'page-2' + }) + fetchApiMock.mockResolvedValueOnce( + response(['overlap', 'older'], { hasMore: false }) + ) + + await list.loadMore() + + expect(toValue(list.items).map(({ id }) => id)).toEqual([ + 'newest', + 'overlap', + 'older' + ]) + }) + + it('stops pagination when a cursor does not advance', async () => { + const list = await createList('stuck-load-more', ['newest'], { + hasMore: true, + nextCursor: 'stuck' + }) + fetchApiMock.mockResolvedValueOnce( + response(['older'], { hasMore: true, nextCursor: 'stuck' }) + ) + + await list.loadMore() + + expect(toValue(list.hasMore)).toBe(false) + await list.loadMore() + expect(fetchApiMock).toHaveBeenCalledTimes(2) + }) + + it('stops pagination when cursors cycle across calls', async () => { + const list = await createList('cycling-load-more', ['newest'], { + hasMore: true, + nextCursor: 'A' + }) + fetchApiMock + .mockResolvedValueOnce( + response(['older'], { hasMore: true, nextCursor: 'B' }) + ) + .mockResolvedValueOnce( + response(['oldest'], { hasMore: true, nextCursor: 'A' }) + ) + + await list.loadMore() + await list.loadMore() + + expect(toValue(list.hasMore)).toBe(false) + await list.loadMore() + expect(fetchApiMock).toHaveBeenCalledTimes(3) + }) + + it('reports a request failure without consuming pagination state', async () => { + const list = await createList('failed-load-more', ['newest'], { + hasMore: true, + nextCursor: 'page-2' + }) + fetchApiMock.mockRejectedValueOnce(new Error('network failed')) + + await list.loadMore() + + expect(toValue(list.hasMore)).toBe(true) + expect(toValue(list.items).map(({ id }) => id)).toEqual(['newest']) + }) + + it('discards a delayed page when invalidation resets the list', async () => { + const list = await createList('invalidate-race', ['old-head'], { + hasMore: true, + nextCursor: 'old-page-2' + }) + const delayedPage = deferred() + fetchApiMock + .mockReturnValueOnce(delayedPage.promise) + .mockResolvedValueOnce( + response(['reset-head'], { hasMore: true, nextCursor: 'reset-page-2' }) + ) + + const loadMorePromise = list.loadMore() + await vi.waitFor(() => expect(fetchApiMock).toHaveBeenCalledTimes(2)) + const delayedSignal = fetchApiMock.mock.calls[1][1]?.signal + + const invalidatePromise = list.invalidate() + expect(delayedSignal?.aborted).toBe(true) + delayedPage.resolve(response(['stale-tail'], { hasMore: false })) + await Promise.all([loadMorePromise, invalidatePromise]) + + expect(toValue(list.items).map(({ id }) => id)).toEqual(['reset-head']) + expect(toValue(list.hasMore)).toBe(true) + expect(requestedAfterCursors()).toEqual(['old-page-2', null]) + }) +}) diff --git a/src/platform/assets/composables/useAssetsQuery.ts b/src/platform/assets/composables/useAssetsQuery.ts index fe786704b81..3da4b05a5c5 100644 --- a/src/platform/assets/composables/useAssetsQuery.ts +++ b/src/platform/assets/composables/useAssetsQuery.ts @@ -25,18 +25,14 @@ function assetsQueryInternal( const onError = options.onError ?? console.error let nextCursor: string | undefined - const seenCursors = new Set() + const seenCursors = new Set(params.after === undefined ? [] : [params.after]) const hasMore = ref(true) const items = ref([]) const { enqueue, preempt, running: isLoading } = usePreemptableQueue() async function doLoadMore(signal?: AbortSignal) { if (!hasMore.value) return - if (seenCursors.has(nextCursor)) { - hasMore.value = false - return - } - + const previousCursor = nextCursor const assetResponse = await doQuery( { after: nextCursor ?? params.after @@ -44,34 +40,63 @@ function assetsQueryInternal( signal ) if (!assetResponse) return - seenCursors.add(nextCursor) + + const knownIds = new Set(items.value.map(({ id }) => id)) + const newItems = assetResponse.assets.filter(({ id }) => { + if (knownIds.has(id)) return false + knownIds.add(id) + return true + }) nextCursor = assetResponse.next_cursor - hasMore.value = assetResponse.has_more - items.value.push(...assetResponse.assets) + hasMore.value = + assetResponse.has_more && + nextCursor !== undefined && + nextCursor !== previousCursor && + !seenCursors.has(nextCursor) + if (nextCursor !== undefined) seenCursors.add(nextCursor) + items.value.push(...newItems) } - function loadMore() { - return enqueue('loadMore', doLoadMore) + async function loadMore() { + await enqueue('loadMore', async (signal) => { + await doLoadMore(signal) + }) } 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) }) @@ -87,6 +112,7 @@ function assetsQueryInternal( hasMore.value = true nextCursor = undefined seenCursors.clear() + if (params.after !== undefined) seenCursors.add(params.after) items.value = [] await doLoadMore() }) @@ -122,7 +148,14 @@ function assetsQueryInternal( } void loadMore() - return { hasMore, invalidate, isLoading, items, loadMore, loadNew } + return { + hasMore, + invalidate, + isLoading, + items, + loadMore, + loadNew + } } const sharedState: SharedPagedListState = { diff --git a/src/stores/assetsStore.outputLookup.test.ts b/src/stores/assetsStore.outputLookup.test.ts new file mode 100644 index 00000000000..3cdc7e6dbec --- /dev/null +++ b/src/stores/assetsStore.outputLookup.test.ts @@ -0,0 +1,91 @@ +import { createPinia, setActivePinia } from 'pinia' +import { beforeEach, describe, expect, it, vi } from 'vitest' +import { ref } from 'vue' + +import type { AssetItem } from '@/platform/assets/schemas/assetSchema' +import type { PagedList } from '@/utils/pagedList' + +const query = vi.hoisted(() => ({ + current: undefined as PagedList | undefined +})) + +vi.mock('@/composables/useFeatureFlags', () => ({ + useFeatureFlags: () => ({ flags: { assetsEnabled: true } }) +})) + +vi.mock('@/platform/assets/composables/useAssetsQuery', () => ({ + invalidateAll: vi.fn(), + useAssetsQuery: vi.fn(() => query.current) +})) + +vi.mock('@/stores/assetDownloadStore', () => ({ + useAssetDownloadStore: () => ({}) +})) + +vi.mock('@/stores/modelToNodeStore', () => ({ + useModelToNodeStore: () => ({}) +})) + +import { useAssetsStore } from '@/stores/assetsStore' + +function asset(id: string): AssetItem { + return { + id, + name: `${id}.png`, + size: 1, + created_at: '2026-08-29T00:00:00Z', + updated_at: '2026-08-29T00:00:00Z', + tags: ['output'] + } +} + +function pagedList( + items: AssetItem[], + loadMore: () => Promise, + hasMore = ref(true) +): PagedList { + return { + hasMore, + invalidate: vi.fn(), + isLoading: ref(false), + items: ref(items), + loadMore, + loadNew: vi.fn() + } +} + +describe('assetsStore output lookup', () => { + beforeEach(() => { + setActivePinia(createPinia()) + }) + + it('loads pages until the requested output is found', async () => { + const items = ref([asset('first-page')]) + const loadMore = vi.fn(async () => { + items.value.push( + loadMore.mock.calls.length === 2 + ? asset('requested-output') + : asset('second-page') + ) + }) + query.current = { + ...pagedList([], loadMore), + items + } + + const found = await useAssetsStore().loadOutputAsset('requested-output') + + expect(found).toBe(true) + expect(loadMore).toHaveBeenCalledTimes(2) + }) + + it('stops after 1,000 pages when the requested output is absent', async () => { + const loadMore = vi.fn(async () => {}) + query.current = pagedList([], loadMore) + + const found = await useAssetsStore().loadOutputAsset('missing-output') + + expect(found).toBe(false) + expect(loadMore).toHaveBeenCalledTimes(1_000) + }) +}) diff --git a/src/stores/assetsStore.ts b/src/stores/assetsStore.ts index 1331c5fd0fb..55f2428885e 100644 --- a/src/stores/assetsStore.ts +++ b/src/stores/assetsStore.ts @@ -99,6 +99,7 @@ function mapHistoryToAssets(historyItems: JobListItem[]): AssetItem[] { const BATCH_SIZE = 200 const MAX_HISTORY_ITEMS = 1000 // Maximum items to keep in memory +const MAX_OUTPUT_ASSET_PAGES = 1000 export const useAssetsStore = defineStore('assets', () => { const assetDownloadStore = useAssetDownloadStore() @@ -306,6 +307,22 @@ export const useAssetsStore = defineStore('assets', () => { { immediate: true } ) + async function loadOutputAsset(id: string): Promise { + const assets = toValue(outputAssets) + const hasAsset = () => + toValue(assets.items).some((asset) => asset.id === id) + let pagesLoaded = 0 + while ( + !hasAsset() && + toValue(assets.hasMore) && + pagesLoaded < MAX_OUTPUT_ASSET_PAGES + ) { + await assets.loadMore() + pagesLoaded++ + } + return hasAsset() + } + /** * Map of asset hash filename to asset item for O(1) lookup * Cloud assets use hash for the hash-based filename @@ -914,6 +931,9 @@ export const useAssetsStore = defineStore('assets', () => { outputAssets, invalidateAll, + // Actions + loadOutputAsset, + // Deletion tracking deletingAssetIds, setAssetDeleting,