diff --git a/docs/superpowers/plans/2026-08-12-search-result-download.md b/docs/superpowers/plans/2026-08-12-search-result-download.md new file mode 100644 index 0000000..fa24809 --- /dev/null +++ b/docs/superpowers/plans/2026-08-12-search-result-download.md @@ -0,0 +1,166 @@ +# Search Result Download Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Show the existing `Original laden` workflow on every authorized global search result, even when Immich omits the asset's album association. + +**Architecture:** The server-side album catalog already fans a global search out into one request per granted album. Normalize each returned page at that boundary by adding the queried granted album ID when the asset does not include it; the existing gallery then receives enough trusted context to render its unchanged `UsageDialog` on both the tile and lightbox. + +**Tech Stack:** Next.js 16, React 19, TypeScript, Vitest, Testing Library, Immich API adapter + +## Global Constraints + +- Reuse the existing `UsageDialog`, download API, pricing, prior-usage, authorization, and accounting logic. +- Album context must come from the server-side granted-album query, never from browser input. +- Do not issue an additional Immich request per search result. +- Preserve album associations already returned by Immich. +- Do not change the database schema or add a migration. +- Production code must follow a witnessed RED/GREEN TDD cycle. + +--- + +### Task 1: Restore Authorized Album Context on Search Results + +**Files:** +- Modify: `src/modules/albums/catalog.test.ts` +- Modify: `src/modules/albums/catalog.ts` +- Modify: `src/modules/albums/catalog-content.test.tsx` + +**Interfaces:** +- Consumes: `ImmichSearchPage`, the granted `albumIds` passed to the existing private `searchAlbum` function, and `PhotoGallery`'s existing `downloadOptions` +- Produces: each returned `ImmichAsset` contains the queried authorized `{ id: string }` album association; `PhotoGallery` therefore renders its existing `UsageDialog` + +- [ ] **Step 1: Write the failing catalog regression test** + +Add an asset fixture whose `albums` array is empty, make `searchPhotos` return it for the granted album `allowed`, and assert the catalog result contains the server-selected association: + +```ts +test("restores the queried granted album when Immich omits an asset album association", async () => { + const { catalog, immich } = createCatalog({ grantedAlbumIds: ["allowed"] }); + immich.searchPhotos.mockResolvedValue({ + items: [{ + id: "asset-1", + type: "IMAGE", + originalFileName: "tor.jpg", + originalMimeType: "image/jpeg", + localDateTime: null, + fileCreatedAt: null, + albums: [], + }], + total: 1, + nextPage: null, + previousPage: null, + }); + + const result = await catalog.searchVisiblePhotos("editorial-1", { page: 1 }); + + expect(result.items[0]?.albums).toEqual([{ id: "allowed" }]); +}); +``` + +- [ ] **Step 2: Run the focused catalog test and verify RED** + +Run: + +```bash +./node_modules/.bin/vitest run src/modules/albums/catalog.test.ts +``` + +Expected: the new assertion fails with `[]` instead of `[{ id: "allowed" }]`; existing tests remain green. + +- [ ] **Step 3: Implement minimal album-context normalization** + +In `searchAlbum`, await the existing Immich response and return a copied page. For every item, preserve it unchanged when its `albums` already contains the queried album. Otherwise prepend the queried album association: + +```ts +const searchPage = await immich.searchPhotos({ + albumIds, + ...(description === undefined ? {} : { description }), + type: "IMAGE", + page, + size, +}); +const queriedAlbumId = albumIds[0]; +return { + ...searchPage, + items: queriedAlbumId + ? searchPage.items.map((asset) => asset.albums.some(({ id }) => id === queriedAlbumId) + ? asset + : { ...asset, albums: [{ id: queriedAlbumId }, ...asset.albums] }) + : searchPage.items, +}; +``` + +- [ ] **Step 4: Run the focused catalog test and verify GREEN** + +Run: + +```bash +./node_modules/.bin/vitest run src/modules/albums/catalog.test.ts +``` + +Expected: all catalog tests pass. + +- [ ] **Step 5: Add the gallery regression test** + +In `catalog-content.test.tsx`, render `PhotoGallery` without its top-level `albumId`, but with an item containing the restored album association and valid `downloadOptions`. Assert the tile offers `Original laden`, open the photo lightbox, and assert the lightbox dialog also contains `Original laden`: + +```tsx +test("offers the existing download workflow on a global search result and its lightbox", () => { + render( + , + ); + + expect(screen.getByRole("button", { name: /original laden/i })).not.toBeNull(); + fireEvent.click(screen.getByRole("button", { name: /tor-des-sieges\.jpg vergrößern/i })); + expect(within(screen.getByRole("dialog")).getByRole("button", { name: /original laden/i })).not.toBeNull(); +}); +``` + +Import `within` from Testing Library. This test protects reuse of the existing dialog in both search-result surfaces without testing framework internals. + +- [ ] **Step 6: Run both focused suites** + +Run: + +```bash +./node_modules/.bin/vitest run src/modules/albums/catalog.test.ts src/modules/albums/catalog-content.test.tsx +``` + +Expected: both files and all contained tests pass without warnings. + +- [ ] **Step 7: Run full deterministic verification** + +Run: + +```bash +./node_modules/.bin/vitest run src +./node_modules/.bin/tsc --noEmit +./node_modules/.bin/eslint . +./node_modules/.bin/drizzle-kit check +git diff --check +``` + +Expected: every command exits with status 0. + +- [ ] **Step 8: Run the production build** + +Run `./node_modules/.bin/next build --webpack` with the documented non-secret build-time environment values. + +Expected: the optimized production build completes with exit status 0. + +- [ ] **Step 9: Commit the implementation** + +```bash +git add src/modules/albums/catalog.test.ts src/modules/albums/catalog.ts src/modules/albums/catalog-content.test.tsx +git commit -m "feat: enable downloads from search results" +```