diff --git a/browser_tests/tests/dialogs/managerDialog.spec.ts b/browser_tests/tests/dialogs/managerDialog.spec.ts index 1a86bd3dfdd..dceda707634 100644 --- a/browser_tests/tests/dialogs/managerDialog.spec.ts +++ b/browser_tests/tests/dialogs/managerDialog.spec.ts @@ -722,6 +722,170 @@ test.describe('ManagerDialog', { tag: '@ui' }, () => { }) }) + test.describe('Failed next page', () => { + const PAGED_PACKS: RegistryNodePack[] = Array.from( + { length: 65 }, + (_, index) => ({ + id: `paged-pack-${index}`, + name: `Paged Pack ${index}`, + description: 'Pack used to fill two result pages', + downloads: 1000 - index, + status: 'NodeStatusActive', + publisher: { id: 'paged-publisher', name: 'Paged Publisher' }, + latest_version: { version: '1.0.0', status: 'NodeVersionStatusActive' }, + repository: `https://github.com/test/paged-pack-${index}` + }) + ) + const PAGE_SIZE = 64 + const LAST_FIRST_PAGE_PACK = `Paged Pack ${PAGE_SIZE - 1}` + const SECOND_PAGE_PACK = `Paged Pack ${PAGE_SIZE}` + + const packsOnPage = (pageNumber: number) => + PAGED_PACKS.slice(pageNumber * PAGE_SIZE, (pageNumber + 1) * PAGE_SIZE) + + let secondPageRequests = 0 + + test.beforeEach(async ({ comfyPage }) => { + secondPageRequests = 0 + const respondWithPage = async ( + route: Parameters[1]>[0], + pageNumber: number, + body: () => object + ) => { + if (pageNumber === 1 && secondPageRequests++ === 0) { + await route.fulfill({ status: 503, json: {} }) + return + } + await route.fulfill({ json: body() }) + } + for (const pattern of ['**/*.algolia.net/**', '**/*.algolianet.com/**']) { + await comfyPage.page.route(pattern, (route) => { + const { requests } = route.request().postDataJSON() as { + requests: { page?: number }[] + } + const pageNumber = requests[0]?.page ?? 0 + return respondWithPage(route, pageNumber, () => ({ + results: requests.map((_, index) => ({ + hits: + index === 0 + ? packsOnPage(pageNumber).map((pack) => ({ + objectID: pack.id, + id: pack.id, + name: pack.name, + description: pack.description, + total_install: pack.downloads, + status: pack.status, + publisher_id: 'paged-publisher', + latest_version: '1.0.0', + latest_version_status: 'NodeVersionStatusActive', + repository_url: pack.repository + })) + : [], + nbHits: PAGED_PACKS.length, + page: pageNumber, + nbPages: 2, + hitsPerPage: PAGE_SIZE + })) + })) + }) + } + await comfyPage.page.route( + (url) => + url.hostname === 'api.comfy.org' && + (url.pathname === '/nodes' || url.pathname === '/nodes/search'), + (route) => { + const registryPage = Number( + new URL(route.request().url()).searchParams.get('page') ?? '1' + ) + return respondWithPage(route, registryPage - 1, () => ({ + total: PAGED_PACKS.length, + nodes: packsOnPage(registryPage - 1), + page: registryPage, + limit: PAGE_SIZE, + totalPages: 2 + })) + } + ) + await comfyPage.page.route( + (url) => + url.hostname === 'api.comfy.org' && + url.pathname.startsWith('/nodes/paged-pack-'), + (route) => { + const { pathname } = new URL(route.request().url()) + if (pathname.endsWith('/comfy-nodes')) { + return route.fulfill({ + json: { + comfy_nodes: [], + totalNumberOfPages: 0 + } satisfies RegistryOperations['ListComfyNodes']['responses'][200]['content']['application/json'] + }) + } + const id = pathname.split('/')[2] + return route.fulfill({ + json: PAGED_PACKS.find((pack) => pack.id === id) + }) + } + ) + }) + + const scrollToEnd = (grid: Locator) => + grid.evaluate((element) => { + element.scrollTop = element.scrollHeight + }) + + test('keeps the loaded packs, scroll and selection and loads the page again on retry', async ({ + comfyPage + }) => { + await openManagerDialog(comfyPage) + const dialog = comfyPage.page.getByRole('dialog') + const grid = dialog.locator('#results-grid') + await expect( + dialog.getByText('Paged Pack 0', { exact: true }) + ).toBeVisible() + + await test.step('scroll to the end of the first page', async () => { + await scrollToEnd(grid) + await expect( + dialog.getByText(LAST_FIRST_PAGE_PACK, { exact: true }) + ).toBeVisible() + }) + + const failureNotice = dialog.getByRole('alert').filter({ + hasText: "Couldn't load more results." + }) + await expect(failureNotice).toBeVisible() + await expect( + dialog.getByText('Error connecting to the Comfy Node Registry.') + ).toBeHidden() + + await dialog.getByText(LAST_FIRST_PAGE_PACK, { exact: true }).click() + const infoPanel = dialog.getByRole('complementary') + await expect( + infoPanel.getByText(LAST_FIRST_PAGE_PACK, { exact: true }) + ).toBeVisible() + const scrollTopBeforeRetry = await grid.evaluate( + (element) => element.scrollTop + ) + expect(scrollTopBeforeRetry).toBeGreaterThan(0) + + await failureNotice.getByRole('button', { name: 'Retry' }).click() + + await expect(failureNotice).toBeHidden() + await expect( + infoPanel.getByText(LAST_FIRST_PAGE_PACK, { exact: true }) + ).toBeVisible() + expect( + await grid.evaluate((element) => element.scrollTop) + ).toBeGreaterThanOrEqual(scrollTopBeforeRetry) + await expect.poll(() => secondPageRequests).toBe(2) + + await scrollToEnd(grid) + await expect( + dialog.getByText(SECOND_PAGE_PACK, { exact: true }) + ).toBeVisible() + }) + }) + test('Search mode can be switched between packs and nodes', async ({ comfyPage }) => { diff --git a/src/locales/en/main.json b/src/locales/en/main.json index f0c6c895d27..878a4cd7972 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -551,6 +551,7 @@ "totalNodes": "Total Nodes", "discoverCommunityContent": "Discover community-made Node Packs, Extensions, and more...", "errorConnecting": "Error connecting to the Comfy Node Registry.", + "loadMoreFailed": "Couldn't load more results.", "noResultsFound": "No results found matching your search.", "tryDifferentSearch": "Please try a different search query.", "emptyState": { diff --git a/src/workbench/extensions/manager/components/manager/ManagerDialog.test.ts b/src/workbench/extensions/manager/components/manager/ManagerDialog.test.ts index 85539001337..38387bb7460 100644 --- a/src/workbench/extensions/manager/components/manager/ManagerDialog.test.ts +++ b/src/workbench/extensions/manager/components/manager/ManagerDialog.test.ts @@ -91,14 +91,22 @@ const BaseModalLayoutStub = defineComponent({ } }) +const LOAD_MORE = 'Scroll to the end' + const VirtualGridStub = defineComponent({ - props: { items: { type: Array as PropType, required: true } }, + props: { + items: { type: Array as PropType, required: true }, + onLoadMore: { type: Function as PropType<() => unknown>, required: true } + }, setup(props) { return () => - h( - 'ul', - props.items.map((pack) => h('li', pack.name)) - ) + h('div', [ + h( + 'ul', + props.items.map((pack) => h('li', pack.name)) + ), + h('button', { onClick: () => props.onLoadMore() }, LOAD_MORE) + ]) } }) @@ -607,6 +615,147 @@ describe('ManagerDialog failed search', () => { }) }) +describe('ManagerDialog failed next page', () => { + const FIRST_PACK: NodePack = { id: 'gguf-1', name: 'GGUF One' } + const SECOND_PACK: NodePack = { id: 'gguf-2', name: 'GGUF Two' } + + async function renderUntilSecondPageFails(tab?: ManagerTab) { + vi.mocked(useRegistrySearchGateway().searchPacks) + .mockResolvedValueOnce(succeeded([FIRST_PACK], false, true)) + .mockResolvedValueOnce(failed('search unavailable')) + const { user } = renderDialog(tab) + await settleSearch() + await user.click(screen.getByRole('button', { name: LOAD_MORE })) + await settleSearch() + return user + } + + it('keeps the loaded packs and offers a retry below them', async () => { + await renderUntilSecondPageFails() + + expect(screen.getByText('GGUF One')).toBeInTheDocument() + expect(screen.getByRole('alert')).toHaveTextContent( + enMessages.manager.loadMoreFailed + ) + expect( + screen.queryByText(enMessages.manager.errorConnecting) + ).not.toBeInTheDocument() + }) + + it('offers a retry instead of an empty state when the tab filters out every loaded pack', async () => { + vi.mocked( + useComfyManagerService().listInstalledPacks + ).mockResolvedValueOnce({ + 'gguf-1': { cnr_id: 'gguf-1', ver: '1.0.0', enabled: true } + }) + + await renderUntilSecondPageFails(ManagerTab.NotInstalled) + + expect(screen.queryByText('GGUF One')).not.toBeInTheDocument() + expect(screen.getByRole('alert')).toHaveTextContent( + enMessages.manager.loadMoreFailed + ) + expect( + screen.getByRole('button', { name: enMessages.g.retry }) + ).toBeInTheDocument() + }) + + it('keeps the grid instead of an empty state while a retry loads on a tab that filters out every loaded pack', async () => { + vi.mocked( + useComfyManagerService().listInstalledPacks + ).mockResolvedValueOnce({ + 'gguf-1': { cnr_id: 'gguf-1', ver: '1.0.0', enabled: true } + }) + const user = await renderUntilSecondPageFails(ManagerTab.NotInstalled) + vi.mocked(useRegistrySearchGateway().searchPacks).mockReturnValueOnce( + new Promise(() => {}) + ) + + await user.click(screen.getByRole('button', { name: enMessages.g.retry })) + await settleSearch() + + expect(screen.getByRole('button', { name: LOAD_MORE })).toBeInTheDocument() + }) + + it('holds the legacy-manager tip while the failed page may hold the match', async () => { + useManagerState().isNewManagerUI = computed(() => true) + vi.mocked( + useComfyManagerService().listInstalledPacks + ).mockResolvedValueOnce({ + 'gguf-1': { cnr_id: 'gguf-1', ver: '1.0.0', enabled: true } + }) + vi.mocked(useRegistrySearchGateway().searchPacks).mockImplementation( + async (_query, { pageNumber }) => + pageNumber === 0 + ? succeeded([FIRST_PACK], false, true) + : failed('search unavailable') + ) + const { user, searchBox } = renderDialog(ManagerTab.NotInstalled) + + await user.type(searchBox, 'comfyui-manager') + await settleSearch() + await user.click(screen.getByRole('button', { name: LOAD_MORE })) + await settleSearch() + + expect(screen.getByRole('alert')).toHaveTextContent( + enMessages.manager.loadMoreFailed + ) + expect(screen.queryByText(TIP, { exact: false })).not.toBeInTheDocument() + }) + + it('returns focus to Retry when the retried page fails again', async () => { + const user = await renderUntilSecondPageFails() + vi.mocked(useRegistrySearchGateway().searchPacks).mockResolvedValueOnce( + failed('search unavailable') + ) + + await user.click(screen.getByRole('button', { name: enMessages.g.retry })) + await settleSearch() + + expect( + screen.getByRole('button', { name: enMessages.g.retry }) + ).toHaveFocus() + }) + + it('leaves focus where the user moved it while the retry loaded', async () => { + const user = await renderUntilSecondPageFails() + let failRetry: () => void = () => {} + vi.mocked(useRegistrySearchGateway().searchPacks).mockReturnValueOnce( + new Promise((resolve) => { + failRetry = () => resolve(failed('search unavailable')) + }) + ) + + await user.click(screen.getByRole('button', { name: enMessages.g.retry })) + const searchBox = screen.getByRole('combobox') + searchBox.focus() + failRetry() + await settleSearch() + + expect(searchBox).toHaveFocus() + }) + + it('appends the failed page below the loaded packs when retried', async () => { + const user = await renderUntilSecondPageFails() + vi.mocked(useRegistrySearchGateway().searchPacks).mockResolvedValueOnce( + succeeded([SECOND_PACK], false) + ) + + await user.click(screen.getByRole('button', { name: enMessages.g.retry })) + await settleSearch() + + expect(screen.getAllByRole('listitem').map((li) => li.textContent)).toEqual( + ['GGUF One', 'GGUF Two'] + ) + expect(screen.queryByRole('alert')).not.toBeInTheDocument() + expect( + vi + .mocked(useRegistrySearchGateway().searchPacks) + .mock.calls.map(([, params]) => params.pageNumber) + ).toEqual([0, 1, 1]) + }) +}) + describe('ManagerDialog search analytics tags', () => { const searchedTags = () => vi diff --git a/src/workbench/extensions/manager/components/manager/ManagerDialog.vue b/src/workbench/extensions/manager/components/manager/ManagerDialog.vue index f66bc710d61..d6e2ec26645 100644 --- a/src/workbench/extensions/manager/components/manager/ManagerDialog.vue +++ b/src/workbench/extensions/manager/components/manager/ManagerDialog.vue @@ -114,31 +114,49 @@ :node-names="unresolvedNodeNames" /> -
- +
+ + + +
+ - -
+
+ {{ $t('manager.loadMoreFailed') }} + +
+
@@ -157,12 +175,14 @@ import { refDebounced, until, whenever } from '@vueuse/core' import { merge } from 'es-toolkit/compat' import { computed, + nextTick, onBeforeUnmount, onMounted, onUnmounted, provide, ref, toValue, + useTemplateRef, watch, watchEffect } from 'vue' @@ -414,6 +434,25 @@ const isSearchFailed = computed( isFirstPageForInput.value && packs.firstPage.value.status === 'failed' ) +const isNextPageFailed = computed( + () => + isSearchBacked.value && + isFirstPageForInput.value && + packs.isNextPageFailed.value +) +const retryButton = useTemplateRef>('retryButton') +async function retryNextPage() { + await packs.retryNextPage() + if (!isNextPageFailed.value) return + await nextTick() + if (document.activeElement && document.activeElement !== document.body) return + const button: HTMLElement | undefined = retryButton.value?.$el + button?.focus() +} +const hasPendingPages = computed( + () => + isSearchBacked.value && (hasMorePacks.value || packs.isNextPageFailed.value) +) const isFirstPagePending = computed( () => isSearchBacked.value && @@ -517,7 +556,7 @@ const isSettledWithNoPacks = computed( displayPacks.value.length === 0 && !isLoading.value && !isManagerErrorRelevant.value && - !hasMorePacks.value + !hasPendingPages.value ) const isUnmatchedLegacyManagerSearch = computed(() => { diff --git a/src/workbench/extensions/manager/composables/useRegistrySearch.test.ts b/src/workbench/extensions/manager/composables/useRegistrySearch.test.ts index 5e328e8f6f6..421c471722b 100644 --- a/src/workbench/extensions/manager/composables/useRegistrySearch.test.ts +++ b/src/workbench/extensions/manager/composables/useRegistrySearch.test.ts @@ -1,6 +1,7 @@ import { until } from '@vueuse/core' import { describe, expect, it, vi } from 'vitest' import { effectScope, nextTick, ref, toValue } from 'vue' +import type { MaybeRefOrGetter } from 'vue' import { DEFAULT_PAGE_SIZE } from '@/constants/searchConstants' import { reportError } from '@/platform/telemetry/reportError' @@ -676,26 +677,144 @@ describe('useRegistrySearch', () => { expect(reportError).not.toHaveBeenCalled() }) - it('keeps the first page when a later page fails', async () => { - vi.mocked(useRegistrySearchGateway().searchPacks) - .mockResolvedValueOnce( - succeeded({ - nodePacks: packs(DEFAULT_PAGE_SIZE), - querySuggestions: [], - hasMore: true, - rawResultsEmpty: false - }) + describe('a later page that fails', () => { + const firstPageResult: SearchPacksResult = { + nodePacks: [{ id: 'gguf-1' }], + querySuggestions: [], + hasMore: true, + rawResultsEmpty: false + } + const secondPageResult: SearchPacksResult = { + nodePacks: [{ id: 'gguf-2' }], + querySuggestions: [], + hasMore: false, + rawResultsEmpty: false + } + + async function searchUntilSecondPageFails( + searchMode: MaybeRefOrGetter = 'packs' + ) { + vi.mocked(useRegistrySearchGateway().searchPacks) + .mockResolvedValueOnce(succeeded(firstPageResult)) + .mockResolvedValueOnce(failed('search unavailable')) + const search = useRegistrySearch({ query: 'gguf', searchMode }) + await until(search.isLoading).toBe(false) + expect(await search.loadMore()).toBe(false) + return search + } + + const searchedPages = () => + vi + .mocked(useRegistrySearchGateway().searchPacks) + .mock.calls.map(([query, params]) => [ + query, + params.pageNumber, + params.restrictSearchableAttributes + ]) + + it('keeps the loaded packs and pauses pagination until it is retried', async () => { + const search = await searchUntilSecondPageFails() + + expect(toValue(search.items)).toEqual([{ id: 'gguf-1' }]) + expect(search.firstPage.value).toEqual({ + status: 'loaded', + query: 'gguf', + rawResultsEmpty: false + }) + expect(search.isNextPageFailed.value).toBe(true) + expect(toValue(search.hasMore)).toBe(false) + }) + + it('does not request the failed page again from a scroll load while paused', async () => { + const search = await searchUntilSecondPageFails() + + expect(await search.loadMore()).toBe(false) + + expect(searchedPages()).toHaveLength(2) + expect(search.isNextPageFailed.value).toBe(true) + }) + + it('ignores a retry when no page failed', async () => { + vi.mocked(useRegistrySearchGateway().searchPacks).mockResolvedValueOnce( + succeeded(firstPageResult) ) - .mockResolvedValueOnce(failed('search unavailable')) - const search = useRegistrySearch({ query: 'gguf', searchMode: 'packs' }) - await until(search.isLoading).toBe(false) + const search = useRegistrySearch({ query: 'gguf', searchMode: 'packs' }) + await until(search.isLoading).toBe(false) - await search.loadMore() + expect(await search.retryNextPage()).toBe(false) - expect(search.firstPage.value).toEqual({ - status: 'loaded', - query: 'gguf', - rawResultsEmpty: false + expect(useRegistrySearchGateway().resetCircuits).not.toHaveBeenCalled() + expect(searchedPages()).toHaveLength(1) + }) + + it('loads the same page for the same search when retried', async () => { + const search = await searchUntilSecondPageFails() + vi.mocked(useRegistrySearchGateway().searchPacks).mockResolvedValueOnce( + succeeded(secondPageResult) + ) + + expect(await search.retryNextPage()).toBe(true) + + expect(useRegistrySearchGateway().resetCircuits).toHaveBeenCalledOnce() + expect(toValue(search.items)).toEqual([ + { id: 'gguf-1' }, + { id: 'gguf-2' } + ]) + expect(search.isNextPageFailed.value).toBe(false) + expect(searchedPages()).toEqual([ + ['gguf', 0, ['name', 'description']], + ['gguf', 1, ['name', 'description']], + ['gguf', 1, ['name', 'description']] + ]) + }) + + it('requests the page once when retried again while the retry loads', async () => { + const search = await searchUntilSecondPageFails() + vi.mocked(useRegistrySearchGateway().searchPacks).mockResolvedValueOnce( + succeeded(secondPageResult) + ) + + await Promise.all([ + search.retryNextPage(), + search.retryNextPage(), + search.loadMore() + ]) + + expect(searchedPages()).toHaveLength(3) + expect(toValue(search.items)).toEqual([ + { id: 'gguf-1' }, + { id: 'gguf-2' } + ]) + }) + + it('drops a retried page that lands after the mode changed', async () => { + const searchMode = ref('packs') + const search = await searchUntilSecondPageFails(searchMode) + let resolveRetry: (result: SearchPacksResult) => void = () => {} + vi.mocked(useRegistrySearchGateway().searchPacks) + .mockReturnValueOnce( + new Promise((resolve) => { + resolveRetry = (result) => resolve(succeeded(result)) + }) + ) + .mockResolvedValueOnce( + succeeded({ + nodePacks: [{ id: 'nodes-pack' }], + querySuggestions: [], + hasMore: false, + rawResultsEmpty: false + }) + ) + const retrying = search.retryNextPage() + + searchMode.value = 'nodes' + await nextTick() + resolveRetry(secondPageResult) + + expect(await retrying).toBe(false) + await until(search.isLoading).toBe(false) + expect(toValue(search.items)).toEqual([{ id: 'nodes-pack' }]) + expect(search.isNextPageFailed.value).toBe(false) }) }) }) diff --git a/src/workbench/extensions/manager/composables/useRegistrySearch.ts b/src/workbench/extensions/manager/composables/useRegistrySearch.ts index f0affe9a96c..0771f245a25 100644 --- a/src/workbench/extensions/manager/composables/useRegistrySearch.ts +++ b/src/workbench/extensions/manager/composables/useRegistrySearch.ts @@ -50,6 +50,10 @@ export function useRegistrySearch(inputs: { * until its search settles, then the query it searched and its outcome. */ firstPage: Readonly> + /** Whether the page after the loaded ones failed and waits for a retry. */ + isNextPageFailed: Readonly> + /** Loads the failed next page again for the same search. */ + retryNextPage: () => Promise } { const { searchPacks, resetCircuits } = useRegistrySearchGateway() @@ -59,6 +63,7 @@ export function useRegistrySearch(inputs: { let isDisposed = false const items = ref([]) const morePages = ref(true) + const isNextPageFailed = ref(false) const suggestions = ref([]) const firstPage = ref(LOADING_FIRST_PAGE) const { enqueue, preempt, running: isLoading } = usePreemptableQueue() @@ -107,15 +112,17 @@ export function useRegistrySearch(inputs: { function applyFailure(query: string, cause: unknown) { reportFirstFailureSinceSuccess(cause) - morePages.value = false - if (pageNumber === 0) { - firstPage.value = { status: 'failed', query } - suggestions.value = [] + if (pageNumber > 0) { + isNextPageFailed.value = true + return } + morePages.value = false + firstPage.value = { status: 'failed', query } + suggestions.value = [] } async function doLoadMore(): Promise { - if (!morePages.value) return 'current' + if (!morePages.value || isNextPageFailed.value) return 'current' const requestGeneration = generation const query = searchedQuery() const outcome = await searchPacks(query, { @@ -143,6 +150,7 @@ export function useRegistrySearch(inputs: { generation++ pageNumber = 0 morePages.value = true + isNextPageFailed.value = false firstPage.value = LOADING_FIRST_PAGE items.value = [] return preempt(loadCurrentFirstPage) @@ -161,6 +169,13 @@ export function useRegistrySearch(inputs: { return reload() } + async function retryNextPage() { + if (!isNextPageFailed.value) return false + resetCircuits() + isNextPageFailed.value = false + return loadMore() + } + onScopeDispose(() => { isDisposed = true }, true) @@ -175,12 +190,14 @@ export function useRegistrySearch(inputs: { return { items, - hasMore: computed(() => morePages.value), + hasMore: computed(() => morePages.value && !isNextPageFailed.value), isLoading, loadMore, loadNew: retry, invalidate: reload, suggestions, - firstPage + firstPage, + isNextPageFailed, + retryNextPage } }