From fb44178f58b5019a1bf562edc734fa5765f872d8 Mon Sep 17 00:00:00 2001 From: Shaun Reich Date: Mon, 16 Nov 2026 20:02:30 -0500 Subject: [PATCH] fix(cooktrace): Import All Recipes finds the recipes Import All Recipes lists recipes with an empty search, but searchWithMeta returns early on an empty query and never makes the request. Add an allowEmpty option and use it in the import. The Foods search keeps its guard. --- scripts/cooktrace-import-all.test.js | 49 ++++++++++++++++++++ src/lib/cooktraceApi.js | 6 ++- src/routes/settings/ConnectedServices.svelte | 2 +- 3 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 scripts/cooktrace-import-all.test.js diff --git a/scripts/cooktrace-import-all.test.js b/scripts/cooktrace-import-all.test.js new file mode 100644 index 00000000..0fa0bd6e --- /dev/null +++ b/scripts/cooktrace-import-all.test.js @@ -0,0 +1,49 @@ +/** + * Import All Recipes lists the whole CookTrace catalog. + * + * The import pages through CookTrace's recipe list with an empty search, + * but searchWithMeta returned nothing for an empty search, so the import + * always reported "No CookTrace recipes found". The Foods search keeps that + * guard; the import opts out of it. + */ +import assert from 'node:assert/strict'; +import { readFileSync } from 'node:fs'; +import test from 'node:test'; + +globalThis.localStorage ??= { getItem: () => null, setItem() {}, removeItem() {} }; +const { DB } = await import('../src/lib/db.js'); +const { CookTrace } = await import('../src/lib/cooktraceApi.js'); + +DB.getSetting = (key, fallback) => ({ cooktraceBaseUrl: 'http://ct.test', cooktraceApiToken: 't' }[key] ?? fallback); + +async function withFetch(body, fn) { + const calls = []; + const real = globalThis.fetch; + globalThis.fetch = async (_url, init) => { + calls.push(JSON.parse(init.body).path); + return { ok: true, status: 200, json: async () => body }; + }; + try { return await fn(calls); } finally { globalThis.fetch = real; } +} + +test('an empty search makes no request', async () => { + await withFetch({ items: [{ id: 1 }], total: 1 }, async (calls) => { + const meta = await CookTrace.searchWithMeta('', 1, 50); + assert.deepEqual(meta.items, []); + assert.equal(calls.length, 0); + }); +}); + +test('allowEmpty lists the catalog, a page at a time', async () => { + await withFetch({ items: [{ id: 1 }, { id: 2 }], total: 5 }, async (calls) => { + const meta = await CookTrace.searchWithMeta('', 2, 2, { allowEmpty: true }); + assert.deepEqual(calls, ['/api/v1/recipes?q=&limit=2&offset=2']); + assert.equal(meta.items.length, 2); + assert.equal(meta.hasMore, true); + }); +}); + +test('Import All Recipes asks for the whole list', () => { + const src = readFileSync(new URL('../src/routes/settings/ConnectedServices.svelte', import.meta.url), 'utf8'); + assert.match(src, /CookTrace\.searchWithMeta\(''[^;]*allowEmpty: true/); +}); diff --git a/src/lib/cooktraceApi.js b/src/lib/cooktraceApi.js index 11189e1c..83961474 100644 --- a/src/lib/cooktraceApi.js +++ b/src/lib/cooktraceApi.js @@ -86,9 +86,11 @@ const CookTrace = { /** * Paginated search with metadata for infinite-scroll callers, same * envelope shape Foods.svelte expects for OFF / USDA / Mealie. + * An empty query returns nothing unless `allowEmpty` is set; Import All + * Recipes sets it to list the whole catalog. */ - async searchWithMeta(query, page = 1, perPage = 10) { - if (!query) return { items: [], totalHits: 0, page, hasMore: false }; + async searchWithMeta(query, page = 1, perPage = 10, { allowEmpty = false } = {}) { + if (!query && !allowEmpty) return { items: [], totalHits: 0, page, hasMore: false }; try { const offset = Math.max(0, (page - 1) * perPage); const p = `/api/v1/recipes?q=${encodeURIComponent(query)}&limit=${perPage}&offset=${offset}`; diff --git a/src/routes/settings/ConnectedServices.svelte b/src/routes/settings/ConnectedServices.svelte index 7e047e80..ed291d1f 100644 --- a/src/routes/settings/ConnectedServices.svelte +++ b/src/routes/settings/ConnectedServices.svelte @@ -294,7 +294,7 @@ let offset = 0; const ids = []; while (true) { - const meta = await CookTrace.searchWithMeta('', Math.floor(offset / PAGE) + 1, PAGE); + const meta = await CookTrace.searchWithMeta('', Math.floor(offset / PAGE) + 1, PAGE, { allowEmpty: true }); const items = meta?.items || []; for (const it of items) if (it?.id != null) ids.push(it.id); if (!meta?.hasMore || items.length === 0) break;