Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions scripts/cooktrace-import-all.test.js
Original file line number Diff line number Diff line change
@@ -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/);
});
6 changes: 4 additions & 2 deletions src/lib/cooktraceApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
2 changes: 1 addition & 1 deletion src/routes/settings/ConnectedServices.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down