Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
/* oxlint-disable react-perf/jsx-no-new-object-as-prop */
import type { GalleryItem, GalleryItemsPage } from '@features/gallery/core/items';
import type { GalleryBoard, GeneratedImageContract } from '@features/gallery/core/types';
import type { QueueProgressSession } from '@features/queue/contracts';
import { Box, ChakraProvider } from '@chakra-ui/react';
import { DndContext } from '@dnd-kit/core';
import { GalleryItemActionsProvider, GalleryUiProvider, type GalleryUiAdapter } from '@features/gallery/react';
import { resolvedValues } from '@platform/core/resolvedValues.testing';
import { accountLifecycle } from '@platform/state/accountLifecycle';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { system } from '@theme/system';
import { createInstance } from 'i18next';
import { act, StrictMode, useMemo, useState, type ReactNode } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { commands, userEvent } from 'vitest/browser';
import { GalleryWidgetView } from './GalleryWidgetView';
interface ListRequest {
boardId: string;
offset: number;
searchTerm: string;
starred?: boolean;
}
const transport = vi.hoisted(() => ({
listBoards: vi.fn<() => Promise<GalleryBoard[]>>(),
listItems: vi.fn<(request: ListRequest) => Promise<GalleryItemsPage>>(),
listStarred: vi.fn<(request: ListRequest) => Promise<GalleryItemsPage>>(),
}));
vi.mock('@features/gallery/data/backend', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
fetchImageIndexAvailability: () => Promise.resolve({ modelName: null, state: 'disabled' }),
listGalleryBoards: () => transport.listBoards(),
listGalleryDateBoards: () => Promise.resolve([]),
listGalleryItems: (request: ListRequest) =>
request.starred === true ? transport.listStarred(request) : transport.listItems(request),
}));
vi.mock('@features/queue/react', () => ({
useItemProgress: () => null,
useQueueItemProgressImage: () => null,
}));
const i18n = createInstance();
await i18n.use(initReactI18next).init({
interpolation: { escapeValue: false },
lng: 'en',
resources: { en: { translation: await fetch('/locales/en.json').then((response) => response.json()) } },
});
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const THUMBNAIL = `data:image/svg+xml,${encodeURIComponent(
'<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"><rect width="8" height="8" fill="#547c83"/></svg>'
)}`;
const createBoard = (id: string, name: string): GalleryBoard => ({
archived: false,
assetCount: 0,
assetVideoCount: 0,
id,
imageCount: 3,
kind: 'board',
name,
isInbox: false,
projectId: null,
videoCount: 0,
});
const BOARDS = [
createBoard('dogs', 'Dogs'),
createBoard('cats', 'Cats'),
{ ...createBoard('none', ''), kind: 'uncategorized' as const },
];
const image = (name: string, boardId: string, day = 1): GalleryItem => ({
boardId,
category: 'general',
createdAt: `2026-09-${String(day).padStart(2, '0')}T00:00:00.000Z`,
fullUrl: THUMBNAIL,
height: 8,
isIntermediate: false,
kind: 'image',
name,
starred: false,
thumbnailUrl: THUMBNAIL,
width: 8,
});
const page = (items: GalleryItem[], total = items.length): Promise<GalleryItemsPage> =>
Promise.resolve({ items, total });
const thumbnailRouteCommands = commands as typeof commands & {
endThumbnailRouteFailures: () => Promise<void>;
getThumbnailRequests: () => Promise<{ statuses: number[]; types: string[]; urls: string[] }>;
startThumbnailRoute: (pathnames: string[], options?: { failures?: number; status?: number }) => Promise<void>;
stopThumbnailRoute: () => Promise<void>;
};
const fail = (): Promise<never> => Promise.reject(new Error('Service Unavailable'));
/** The kind of item the old fallback showed in place of a failed listing. */
const OTHER_BOARD_RECENT: GeneratedImageContract = {
boardId: 'cats',
height: 8,
imageName: 'recent-elsewhere.png',
imageUrl: THUMBNAIL,
queuedAt: '2026-09-30T00:00:00.000Z',
sourceQueueItemId: 'queue-1',
thumbnailUrl: THUMBNAIL,
width: 8,
};
const DOG_ITEMS = [image('a.png', 'dogs', 3), image('b.png', 'dogs', 2), image('c.png', 'dogs', 1)];
const CAT_ITEMS = [image('tabby.png', 'cats', 3)];
const noop = () => undefined;
const runtime = { commands: { register: () => noop }, hotkeys: { register: () => noop } };
const itemActions = {
deleteItems: vi.fn(),
downloadItem: vi.fn(),
downloadItems: vi.fn(),
moveItemsToBoard: vi.fn(),
openItemInNewTab: vi.fn(),
openItemInPreview: vi.fn(),
setItemsStarred: vi.fn(),
};
const ItemActionsProvider = ({ children }: { children: ReactNode }) => (
<GalleryItemActionsProvider actions={itemActions}>{children}</GalleryItemActionsProvider>
);
const Harness = ({
initialValues,
progressSessions,
region,
}: {
initialValues: Record<string, unknown>;
progressSessions: QueueProgressSession[];
region: 'bottom' | 'center';
}) => {
const [galleryValues, setGalleryValues] = useState(initialValues);
const adapter = useMemo((): GalleryUiAdapter => {
const patch = (values: Record<string, unknown>) => setGalleryValues((current) => ({ ...current, ...values }));
return {
ItemActionsProvider,
ImageContextMenu: () => null,
antialiasProgressImages: false,
exportProject: noop,
getBoardMediaUsage: vi.fn(),
followProgressSession: noop,
followedProgressSessionId: null,
gallery: {
clearSearch: () => patch({ searchTerm: '' }),
clearSelection: noop,
commitSemanticSearch: noop,
reconcileDeletedBoardOutcome: noop,
selectBoard: (selectedBoardId: string) => patch({ galleryPage: 0, selectedBoardId }),
selectImage: noop,
selectItem: (item: GalleryItem) => patch({ selectedImageName: `${item.kind}:${item.name}` }),
setCompareImage: noop,
setCompareItem: noop,
setItemMultiSelection: noop,
setPage: (galleryPage: number) => patch({ galleryPage }),
setPageInfo: noop,
setSearchTerm: (searchTerm: string) => patch({ galleryPage: 0, searchTerm }),
setSemanticSearchMode: noop,
setSemanticSearchText: noop,
setStarredOnly: (starredOnly: boolean) => patch({ starredOnly }),
setView: (galleryView: string) => patch({ galleryView }),
toggleItemSelection: noop,
updateSettings: (settings: Record<string, unknown>) => patch(settings),
},
galleryValues: resolvedValues(galleryValues),
generateValues: {},
getItemLabel: () => Promise.resolve(null),
liveFollowEnabled: false,
notifications: { add: noop, reportError: noop },
openItemContextMenu: noop,
pinnedProgressSessionId: null,
progressSessions,
projectId: 'project-1',
projectName: 'Project',
projects: [],
widgets: { openGallery: () => true, patchGalleryValues: patch },
};
}, [galleryValues, progressSessions]);
return (
<GalleryUiProvider adapter={adapter}>
<DndContext>
<Box h="520px" w="960px">
<GalleryWidgetView presentation="compact" region={region} runtime={runtime} />
</Box>
</DndContext>
</GalleryUiProvider>
);
};
let host: HTMLDivElement | null = null;
let root: Root | null = null;
let queryClient: QueryClient | null = null;
const NO_SESSIONS: QueueProgressSession[] = [];
const PROGRESS_SESSION: QueueProgressSession = {
backendItemId: 10,
height: 768,
id: 'running',
itemCount: 1,
itemIndex: 1,
label: 'Run running',
queueItemId: 'running',
sourceId: 'generate',
state: 'running',
width: 512,
};
const renderGallery = async (
values: Record<string, unknown> = {},
region: 'bottom' | 'center' = 'center',
progressSessions = NO_SESSIONS,
{ strict = false }: { strict?: boolean } = {}
) => {
const tree = (
<I18nextProvider i18n={i18n}>
<ChakraProvider value={system}>
<QueryClientProvider client={queryClient!}>
<Harness
initialValues={{ selectedBoardId: 'dogs', ...values }}
progressSessions={progressSessions}
region={region}
/>
</QueryClientProvider>
</ChakraProvider>
</I18nextProvider>
);
await act(() => root?.render(strict ? <StrictMode>{tree}</StrictMode> : tree));
};
const listing = () => host?.querySelector('[role="list"][aria-label="Gallery items"]') ?? null;
const thumbnailNames = () =>
[...(listing()?.querySelectorAll<HTMLElement>('[role="listitem"] button[aria-pressed]') ?? [])].map(
(button) => button.getAttribute('aria-label') ?? ''
);
const alertText = () => host?.querySelector('[role="alert"]')?.textContent ?? null;
const findButton = (name: string) => host?.querySelector<HTMLButtonElement>(`button[aria-label="${name}"]`) ?? null;
const waitFor = (assertion: () => void) => vi.waitFor(assertion, { timeout: 5_000 });
const announcer = () => host?.querySelector('[role="status"][aria-live="polite"][aria-atomic="true"]') ?? null;
const gridViewport = () => listing()?.closest<HTMLElement>('[data-part="viewport"]') ?? null;
/** The drag context adds hidden instructions of its own to the host. */
const chipText = () => host?.querySelector('p')?.textContent ?? null;
const offsetsRequested = () => transport.listItems.mock.calls.map(([request]) => request.offset);
const settleFrames = async (frames = 8) => {
for (let frame = 0; frame < frames; frame += 1) {
await act(
() =>
new Promise<void>((resolve) => {
requestAnimationFrame(() => setTimeout(resolve, 10));
})
);
}
};
const dogs = (count: number, from = 0) =>
Array.from({ length: count }, (_, index) => ({
...image(`dog-${String(from + index).padStart(3, '0')}.png`, 'dogs'),
createdAt: new Date(Date.UTC(2026, 8, 30) - (from + index) * 60_000).toISOString(),
}));
const scrollToLoadMoreFailure = async () => {
await waitFor(() => expect(gridViewport()).not.toBeNull());
const viewport = gridViewport()!;
const scrollToEnd = () => {
viewport.scrollTop = viewport.scrollHeight;
viewport.dispatchEvent(new Event('scroll'));
};
await act(scrollToEnd);
await waitFor(() => expect(findButton('Retry loading more items')).not.toBeNull());
await act(scrollToEnd);
// Outlast the virtualizer's 150ms scrolling state.
await act(
() =>
new Promise<void>((resolve) => {
setTimeout(resolve, 200);
})
);
expect(viewport.scrollTop + viewport.clientHeight).toBeGreaterThanOrEqual(viewport.scrollHeight - 1);
const rect = findButton('Retry loading more items')!.parentElement!.getBoundingClientRect();
const viewportRect = viewport.getBoundingClientRect();
const tiles = [...listing()!.querySelectorAll<HTMLElement>('[role="listitem"]')].filter((tile) =>
tile.querySelector('button[aria-pressed]')
);
const lastTile = tiles.reduce((last, tile) =>
tile.getBoundingClientRect().bottom > last.getBoundingClientRect().bottom - 1 ? tile : last
);
return {
inViewport: rect.top >= viewportRect.top - 1 && rect.bottom <= viewportRect.bottom + 1,
lastTileBottom: lastTile.getBoundingClientRect().bottom,
lastTileName: lastTile.querySelector('button[aria-pressed]')?.getAttribute('aria-label'),
lastTileRect: lastTile.getBoundingClientRect(),
overlappingTiles: tiles.filter((tile) => {
const tileRect = tile.getBoundingClientRect();
return (
Boolean(tile.querySelector('button[aria-pressed]')) &&
tileRect.left < rect.right &&
tileRect.right > rect.left &&
tileRect.top < rect.bottom &&
tileRect.bottom > rect.top
);
}).length,
rect,
};
};
beforeEach(() => {
accountLifecycle.activate('gallery-load-failures');
vi.clearAllMocks();
transport.listBoards.mockImplementation(() => Promise.resolve(BOARDS));
transport.listItems.mockImplementation(({ boardId }) => page(boardId === 'cats' ? CAT_ITEMS : DOG_ITEMS));
transport.listStarred.mockImplementation(() => page([]));
host = document.createElement('div');
document.body.append(host);
root = createRoot(host);
queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
});
afterEach(async () => {
await act(() => root?.unmount());
host?.remove();
queryClient?.clear();
host = null;
root = null;
queryClient = null;
});
describe('Gallery listing failures', () => {
it('reports a failed first load instead of an empty board, then recovers with Retry and keeps the selection', async () => {
transport.listItems.mockImplementationOnce(fail);
await renderGallery({ selectedImageName: 'image:b.png' });
await waitFor(() => expect(alertText()).toContain('Could not load these items.'));
expect(alertText()).toContain('Service Unavailable');
expect(host?.textContent).not.toContain('Drop media here or click to upload');
expect(host?.textContent).not.toContain('No items match');
expect(host?.textContent).toContain('Cats');
const retry = findButton('Retry loading items');
retry?.focus();
await userEvent.keyboard('{Enter}');
await waitFor(() => expect(thumbnailNames()).toHaveLength(3));
expect(alertText()).toBeNull();
expect(
listing()?.querySelector('button[aria-label="Select b.png for preview"]')?.getAttribute('aria-current')
).toBe('true');
await waitFor(() => expect(host?.contains(document.activeElement)).toBe(true));
});
it('reloads every thumbnail a long image move refused, once, probing one image per interval meanwhile', async () => {
const moving = Array.from({ length: 6 }, (_, index) => ({
...image(`moving-${String(index)}.png`, 'dogs', 10 - index),
thumbnailUrl: `/api/v1/images/i/moving-${String(index)}.png/thumbnail`,
}));
const paths = moving.map((item) => item.thumbnailUrl);
const pathOf = (url: string) => new URL(url, window.location.href).pathname;
const countByPath = (urls: string[]) =>
Object.fromEntries(paths.map((path) => [path, urls.filter((url) => pathOf(url) === path).length]));
const eachPath = (count: number) => Object.fromEntries(paths.map((path) => [path, count]));
const imageRequests = async () => {
const log = await thumbnailRouteCommands.getThumbnailRequests();
return log.urls.filter((_, index) => log.types[index] === 'image');
};
// The probe's reads are answered in the page so they settle within the fake clock's steps.
let isMoving = true;
const browserFetch = globalThis.fetch;
const probes = vi.spyOn(globalThis, 'fetch').mockImplementation((input, init) =>
paths.includes(pathOf(String(input)))
? Promise.resolve(
isMoving
? new Response('moving', { status: 409 })
: new Response('<svg xmlns="http://www.w3.org/2000/svg"/>', {
headers: { 'content-type': 'image/svg+xml' },
})
)
: browserFetch(input, init)
);
const probedPaths = () => probes.mock.calls.map(([input]) => pathOf(String(input)));
await thumbnailRouteCommands.startThumbnailRoute(paths, { failures: 1_000_000, status: 409 });
transport.listItems.mockImplementation(() => page(moving));
vi.useFakeTimers({ shouldAdvanceTime: true, toFake: ['setTimeout', 'clearTimeout'] });
try {
await renderGallery({}, 'center', NO_SESSIONS, { strict: true });
await waitFor(() => expect(thumbnailNames()).toHaveLength(6));
const thumbnails = moving.map((item) => host!.querySelector<HTMLImageElement>(`img[alt="${item.name}"]`)!);
await expect.poll(() => thumbnails.every((thumbnail) => thumbnail.complete)).toBe(true);
expect(thumbnails.every((thumbnail) => thumbnail.naturalWidth === 0)).toBe(true);
expect(countByPath(await imageRequests())).toEqual(eachPath(1));
// Six minutes of move at one probe every 15 to 30 s.
for (let interval = 1; interval <= 12; interval += 1) {
const before = probes.mock.calls.length;
await act(() => vi.advanceTimersByTimeAsync(30_000));
expect(probes.mock.calls.length - before).toBeGreaterThanOrEqual(1);
expect(probes.mock.calls.length - before).toBeLessThanOrEqual(2);
}
expect(new Set(probedPaths()).size).toBe(1);
expect(await imageRequests()).toHaveLength(6);
expect(thumbnails.every((thumbnail) => thumbnail.naturalWidth === 0)).toBe(true);
isMoving = false;
await thumbnailRouteCommands.endThumbnailRouteFailures();
await act(() => vi.advanceTimersByTimeAsync(30_000));
await expect
.poll(() =>
moving.map((item) => host!.querySelector<HTMLImageElement>(`img[alt="${item.name}"]`)!.naturalWidth)
)
.toEqual(moving.map(() => 64));
const probeCount = probes.mock.calls.length;
await act(() => vi.advanceTimersByTimeAsync(5 * 60_000));
expect(countByPath(await imageRequests())).toEqual(eachPath(2));
expect(probes.mock.calls.length).toBe(probeCount);
} finally {
vi.useRealTimers();
probes.mockRestore();
await thumbnailRouteCommands.stopThumbnailRoute();
}
}, 30_000);
it('shows an anchored window that failed as failed, never the unfiltered recents', async () => {
transport.listItems.mockImplementation(fail);
await renderGallery({ galleryPage: 2, paginationMode: 'paginated', recentImages: [OTHER_BOARD_RECENT] });
await waitFor(() => expect(alertText()).toContain('Could not load these items.'));
expect(host?.textContent).not.toContain('recent-elsewhere.png');
expect(thumbnailNames()).toEqual([]);
});
it("shows a failed board switch as the new board's failure, not the previous board's items", async () => {
await renderGallery();
await waitFor(() => expect(thumbnailNames()).toHaveLength(3));
transport.listItems.mockImplementationOnce(fail);
const catsRow = [...(host?.querySelectorAll<HTMLButtonElement>('button') ?? [])].find((button) =>
button.textContent?.startsWith('Cats')
);
await act(() => catsRow?.click());
await waitFor(() => expect(alertText()).toContain('Could not load these items.'));
expect(thumbnailNames()).toEqual([]);
expect(host?.textContent).not.toContain('a.png');
await act(() => findButton('Retry loading items')?.click());
await waitFor(() => expect(thumbnailNames()).toEqual(['Select tabby.png for preview']));
expect(transport.listItems).toHaveBeenLastCalledWith(expect.objectContaining({ boardId: 'cats' }));
});
it('keeps loaded pages when the next page fails, offers Retry there, and substitutes no recents', async () => {
transport.listItems.mockImplementation(({ offset }) => (offset === 0 ? page(DOG_ITEMS, 61) : fail()));
await renderGallery({ recentImages: [OTHER_BOARD_RECENT] });
await scrollToLoadMoreFailure();
await waitFor(() => expect(host?.textContent).toContain('Could not load more items.'));
expect(thumbnailNames()).toEqual([
'Select a.png for preview',
'Select b.png for preview',
'Select c.png for preview',
]);
expect(host?.textContent).not.toContain('recent-elsewhere.png');
expect(alertText()).toBeNull();
const failedCalls = transport.listItems.mock.calls.length;
await settleFrames();
expect(transport.listItems.mock.calls.length).toBe(failedCalls);
transport.listItems.mockImplementation(({ offset }) =>
offset === 0 ? page(DOG_ITEMS, 61) : page([image('d.png', 'dogs', 0)], 61)
);
await act(() => findButton('Retry loading more items')?.click());
await waitFor(() => expect(thumbnailNames()).toHaveLength(4));
expect(host?.textContent).not.toContain('Could not load more items.');
expect(transport.listItems.mock.calls.slice(failedCalls).map(([request]) => request.offset)).toEqual([60]);
});
it('keeps a failed distant page retry visible when its first slot is above the viewport', async () => {
transport.listItems.mockImplementation(({ offset }) =>
offset === 420 ? fail() : page(dogs(Math.min(60, 600 - offset), offset), 600)
);
await renderGallery();
await waitFor(() => expect(offsetsRequested()).toContain(0));
const viewport = gridViewport()!;
await act(() => {
viewport.scrollTop = Math.floor((viewport.scrollHeight - viewport.clientHeight) * 0.75);
viewport.dispatchEvent(new Event('scroll'));
});
await waitFor(() => expect(offsetsRequested()).toContain(420));
await waitFor(() => expect(findButton('Retry loading more items')).not.toBeNull());
const retry = findButton('Retry loading more items');
expect(retry).not.toBeNull();
const rect = retry!.getBoundingClientRect();
const viewportRect = viewport.getBoundingClientRect();
expect(rect.top).toBeGreaterThanOrEqual(viewportRect.top - 1);
expect(rect.bottom).toBeLessThanOrEqual(viewportRect.bottom + 1);
await act(() => retry!.click());
await waitFor(() => expect(offsetsRequested().filter((offset) => offset === 420)).toHaveLength(2));
});
it('keeps a visible retry when the first failed row is only partly visible', async () => {
transport.listItems.mockImplementation(({ offset }) =>
offset === 420 ? fail() : page(dogs(Math.min(60, 600 - offset), offset), 600)
);
await renderGallery();
await waitFor(() => expect(offsetsRequested()).toContain(0));
const viewport = gridViewport()!;
await act(() => {
viewport.scrollTop = Math.floor((viewport.scrollHeight - viewport.clientHeight) * 0.75);
viewport.dispatchEvent(new Event('scroll'));
});
await waitFor(() => expect(offsetsRequested()).toContain(420));
await waitFor(() => expect(findButton('Retry loading more items')).not.toBeNull());
const retry = findButton('Retry loading more items')!;
const statusCell = retry.closest<HTMLElement>('[role="listitem"]')!;
const viewportRect = viewport.getBoundingClientRect();
const cellRect = statusCell.getBoundingClientRect();
const visibleCellHeight = cellRect.height * 0.2;
await act(() => {
viewport.scrollTop += cellRect.top - (viewportRect.top - cellRect.height + visibleCellHeight);
viewport.dispatchEvent(new Event('scroll'));
});
await settleFrames();
const visibleRetry = [
...(host?.querySelectorAll<HTMLButtonElement>('button[aria-label="Retry loading more items"]') ?? []),
].find((button) => {
const rect = button.getBoundingClientRect();
const currentViewportRect = viewport.getBoundingClientRect();
return rect.top >= currentViewportRect.top - 1 && rect.bottom <= currentViewportRect.bottom + 1;
});
const pageRetry = host?.querySelector<HTMLButtonElement>(
'[data-gallery-page-error="420"] button[aria-label="Retry loading more items"]'
);
const retryControl = visibleRetry ?? pageRetry;
expect(retryControl).not.toBeNull();
if (visibleRetry) {
const rect = visibleRetry.getBoundingClientRect();
const currentViewportRect = viewport.getBoundingClientRect();
expect(rect.top).toBeGreaterThanOrEqual(currentViewportRect.top - 1);
expect(rect.bottom).toBeLessThanOrEqual(currentViewportRect.bottom + 1);
} else {
const rect = pageRetry!.getBoundingClientRect();
expect(rect.top).toBeGreaterThanOrEqual(0);
expect(rect.bottom).toBeLessThanOrEqual(document.documentElement.clientHeight);
}
transport.listItems.mockImplementation(({ offset }) =>
offset === 420 ? page([image('recovered.png', 'dogs')], 600) : page(dogs(Math.min(60, 600 - offset), offset), 600)
);
await act(() => retryControl!.click());
await waitFor(() => expect(offsetsRequested().filter((offset) => offset === 420)).toHaveLength(2));
});
it("keeps a scope's earlier results through a failed refresh, with a notice that Retry clears", async () => {
await renderGallery();
await waitFor(() => expect(thumbnailNames()).toHaveLength(3));
const liveRegion = announcer();
expect(liveRegion?.textContent).toBe('');
transport.listItems.mockImplementation(fail);
await act(() => queryClient!.invalidateQueries({ queryKey: ['gallery', 'items', 'list'] }));
await waitFor(() => expect(host?.textContent).toContain('Could not refresh. Showing earlier results.'));
expect(thumbnailNames()).toHaveLength(3);
expect(alertText()).toBeNull();
expect(announcer()).toBe(liveRegion);
expect(liveRegion?.textContent).toBe('Could not refresh. Showing earlier results.');
expect(
[...(host?.querySelectorAll('[role="status"], [role="alert"], [aria-live]') ?? [])].filter((region) =>
region.textContent?.includes('Could not refresh.')
)
).toEqual([liveRegion]);
transport.listItems.mockImplementation(() => page([image('fresh.png', 'dogs', 9), ...DOG_ITEMS]));
await act(() => findButton('Retry loading items')?.click());
await waitFor(() => expect(thumbnailNames()).toHaveLength(4));
expect(host?.textContent).not.toContain('Could not refresh.');
});
it('keeps a failed next page failed through an unrelated refetch, without retrying it on its own', async () => {
transport.listItems.mockImplementation(({ offset }) => (offset === 0 ? page(DOG_ITEMS, 61) : fail()));
await renderGallery();
await scrollToLoadMoreFailure();
await waitFor(() => expect(host?.textContent).toContain('Could not load more items.'));
const failedPageRequests = offsetsRequested().filter((offset) => offset === 60).length;
await act(() => queryClient!.invalidateQueries({ queryKey: ['gallery', 'items', 'list'] }));
await waitFor(() => expect(offsetsRequested().filter((offset) => offset === 0)).toHaveLength(2));
await settleFrames();
expect(offsetsRequested().filter((offset) => offset === 60)).toHaveLength(failedPageRequests);
expect(host?.textContent).toContain('Could not load more items.');
expect(host?.textContent).not.toContain('Could not refresh.');
expect(findButton('Retry loading more items')?.getAttribute('aria-busy')).toBeNull();
});
it('shows the load-more failure after the last loaded row, in view at the end of the grid', async () => {
transport.listItems.mockImplementation(({ offset }) => (offset === 0 ? page(dogs(60), 61) : fail()));
await renderGallery();
await waitFor(() => expect(thumbnailNames().length).toBeGreaterThan(0));
const viewport = gridViewport()!;
const notice = await scrollToLoadMoreFailure();
expect(notice.inViewport).toBe(true);
expect(notice.overlappingTiles).toBe(0);
expect(notice.lastTileName).toBe('Select dog-059.png for preview');
expect(notice.rect.top).toBeGreaterThanOrEqual(notice.lastTileBottom);
expect(listing()!.getBoundingClientRect().bottom).toBeGreaterThanOrEqual(notice.lastTileBottom);
const lastTile = listing()!.querySelector<HTMLElement>('button[aria-label="Select dog-059.png for preview"]')!;
lastTile.focus({ preventScroll: true });
await userEvent.tab();
expect(document.activeElement?.getAttribute('aria-label')).toBe('Retry loading more items');
transport.listItems.mockImplementation(({ offset }) => page(dogs(60, offset), 61));
await userEvent.keyboard('{Enter}');
await waitFor(() => expect(findButton('Retry loading more items')).toBeNull());
await settleFrames(2);
const viewportRect = viewport.getBoundingClientRect();
const tileInPlace = [...listing()!.querySelectorAll<HTMLElement>('[role="listitem"]')].find((tile) => {
const tileRect = tile.getBoundingClientRect();
return tileRect.top < notice.rect.bottom && tileRect.bottom > notice.rect.top;
});
expect(tileInPlace?.querySelector('button[aria-pressed]')?.getAttribute('aria-label')).toMatch(
/^Select dog-0(6\d)\.png for preview$/
);
expect(tileInPlace!.getBoundingClientRect().top).toBeLessThan(viewportRect.bottom);
});
it('places the local page retry after loaded tiles below starred and progress sections, at any density', async () => {
for (const imageDensityPercent of [0, 100]) {
transport.listItems.mockImplementation(({ offset }) => (offset === 0 ? page(dogs(60), 61) : fail()));
transport.listStarred.mockImplementation(() =>
page(Array.from({ length: 4 }, (_, index) => ({ ...image(`fav-${index}.png`, 'dogs', 9), starred: true })))
);
await renderGallery({ imageDensityPercent }, 'center', [PROGRESS_SESSION]);
await waitFor(() => expect(host?.querySelector('[role="list"][aria-label="Starred"]')).not.toBeNull());
expect(host?.querySelector('[data-gallery-session-id]')).not.toBeNull();
const notice = await scrollToLoadMoreFailure();
expect(notice.inViewport, `density ${imageDensityPercent}`).toBe(true);
expect(notice.overlappingTiles, `density ${imageDensityPercent}`).toBe(0);
expect(notice.lastTileName, `density ${imageDensityPercent}`).toBe('Select dog-059.png for preview');
const followsLastTile =
notice.rect.top >= notice.lastTileBottom ||
(notice.rect.left >= notice.lastTileRect.right && notice.rect.top < notice.lastTileBottom);
expect(followsLastTile, `density ${imageDensityPercent}`).toBe(true);
await act(() => root?.render(null));
queryClient?.clear();
}
});
it('hands focus to the grid beside the new items after a load-more Retry, without scrolling', async () => {
transport.listItems.mockImplementation(({ offset }) => (offset === 0 ? page(dogs(60), 61) : fail()));
await renderGallery();
await waitFor(() => expect(thumbnailNames().length).toBeGreaterThan(0));
const viewport = gridViewport()!;
await scrollToLoadMoreFailure();
const retry = findButton('Retry loading more items')!;
const scrollTop = viewport.scrollTop;
expect(scrollTop).toBeGreaterThan(0);
let deliverNextPage: () => void = noop;
transport.listItems.mockImplementation(
({ offset }) =>
new Promise((resolve) => {
deliverNextPage = () => resolve({ items: dogs(60, offset), total: 61 });
})
);
await userEvent.click(retry);
expect(document.activeElement).toBe(retry);
expect(retry.getAttribute('aria-busy')).toBe('true');
await act(() => deliverNextPage());
await waitFor(() => expect(findButton('Retry loading more items')).toBeNull());
await settleFrames(2);
expect(Math.abs(viewport.scrollTop - scrollTop)).toBeLessThanOrEqual(1);
const focused = document.activeElement as HTMLElement;
const focusedRect = focused.getBoundingClientRect();
const viewportRect = viewport.getBoundingClientRect();
expect(focused.matches('[role="listitem"] button[aria-pressed]')).toBe(true);
expect(focusedRect.bottom).toBeGreaterThan(viewportRect.top);
expect(focusedRect.top).toBeLessThan(viewportRect.bottom);
expect(focused.getAttribute('aria-label')).not.toBe('Select dog-000.png for preview');
});
});
describe('Gallery compact status chip', () => {
it('states no count it does not know, and says when the listing could not load', async () => {
transport.listItems.mockImplementation(() => new Promise(noop));
await renderGallery({}, 'bottom');
await settleFrames(2);
expect(chipText()).toBe('Gallery');
await act(() => root?.unmount());
root = createRoot(host!);
queryClient!.clear();
transport.listItems.mockImplementation(fail);
await renderGallery({}, 'bottom');
await waitFor(() => expect(chipText()).toBe('Gallery: could not load'));
transport.listItems.mockImplementation(() => page(DOG_ITEMS, 42));
await act(() => queryClient!.refetchQueries({ queryKey: ['gallery', 'items', 'list'] }));
await waitFor(() => expect(chipText()).toBe('Gallery: 42 items'));
});
});
describe('Gallery side-section failures', () => {
it('reports a failed board list in the board panel without claiming there are none or blocking the grid', async () => {
transport.listBoards.mockImplementationOnce(fail);
await renderGallery();
await waitFor(() => expect(host?.textContent).toContain('Could not load boards.'));
await waitFor(() => expect(thumbnailNames()).toHaveLength(3));
expect(host?.textContent).not.toContain('No boards match');
expect(alertText()).toBeNull();
await act(() => findButton('Retry loading boards')?.click());
await waitFor(() => expect(host?.textContent).toContain('Cats'));
expect(host?.textContent).not.toContain('Could not load boards.');
});
it('reports a failed starred strip in its place without blocking the grid', async () => {
transport.listStarred.mockImplementationOnce(fail);
await renderGallery();
await waitFor(() => expect(host?.textContent).toContain('Could not load starred items.'));
expect(thumbnailNames()).toHaveLength(3);
transport.listStarred.mockImplementation(() => page([{ ...image('fav.png', 'dogs', 9), starred: true }]));
await act(() => findButton('Retry loading starred items')?.click());
await waitFor(() => expect(host?.textContent).not.toContain('Could not load starred items.'));
expect(
host?.querySelector('[role="list"][aria-label="Starred"] button[aria-label="Select fav.png for preview"]')
).not.toBeNull();
});
it('reports a failed refresh of an empty strip, and claims no empty board while starred items are unknown', async () => {
transport.listItems.mockImplementation(() => page([]));
await renderGallery();
await waitFor(() => expect(host?.textContent).toContain('Drop media here or click to upload'));
transport.listStarred.mockImplementation(fail);
await act(() => queryClient!.invalidateQueries({ queryKey: ['gallery', 'items', 'list'] }));
await waitFor(() => expect(host?.textContent).toContain('Could not refresh starred items.'));
expect(findButton('Retry refreshing starred items')).not.toBeNull();
expect(host?.textContent).not.toContain('Drop media here or click to upload');
expect(host?.textContent).not.toContain('No items match');
transport.listStarred.mockImplementation(() => page([]));
await act(() => findButton('Retry refreshing starred items')?.click());
await waitFor(() => expect(host?.textContent).toContain('Drop media here or click to upload'));
expect(host?.textContent).not.toContain('Could not refresh starred items.');
});
});