Persistent server search cache: 200 results per query, 250k queries, 15-day stale-while-revalidate, refresh=1

This commit is contained in:
Claude
2026-10-02 02:10:39 +00:00
parent 14e2307a92
commit c69cc3a4bc
6 changed files with 231 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
import { test, expect } from 'bun:test';
import { readFileSync } from 'node:fs';
import { parseSearch, lengthToSeconds, search } from './innertube.js';
import { parseSearch, lengthToSeconds, search, searchDeep, parseContinuation } from './innertube.js';
const fixture = JSON.parse(readFileSync(new URL('./fixtures/innertube-search.json', import.meta.url)));
test('parses video renderers into slim cards', () => {
@@ -27,3 +27,23 @@ test('search() throws on HTTP error', async () => {
const fetchImpl = async () => new Response('no', { status: 429 });
await expect(search('x', { fetchImpl })).rejects.toThrow('429');
});
const card = (id) => ({ videoRenderer: { videoId: id, title: { runs: [{ text: 't' + id }] }, ownerText: { runs: [{ text: 'c' }] } } });
test('searchDeep follows continuation tokens, de-duplicates and stops at the limit', async () => {
const calls = [];
const fetchImpl = async (_u, init) => {
const body = JSON.parse(init.body);
calls.push(body.continuation || 'first');
const mk = (ids, token) => [{ itemSectionRenderer: { contents: ids.map(card) } }, ...(token ? [{ continuationItemRenderer: { continuationEndpoint: { continuationCommand: { token } } } }] : [])];
if (!body.continuation) return Response.json({ contents: { twoColumnSearchResultsRenderer: { primaryContents: { sectionListRenderer: { contents: mk(['a', 'b', 'c'], 'T1') } } } } });
if (body.continuation === 'T1') return Response.json({ onResponseReceivedCommands: [{ appendContinuationItemsAction: { continuationItems: mk(['c', 'd', 'e'], 'T2') } }] });
return Response.json({ onResponseReceivedCommands: [{ appendContinuationItemsAction: { continuationItems: mk(['f', 'g'], '') } }] });
};
const all = await searchDeep('x', { fetchImpl, limit: 200 });
expect(all.map((c) => c.id)).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g']);
expect(calls).toEqual(['first', 'T1', 'T2']);
expect((await searchDeep('x', { fetchImpl, limit: 4 })).length).toBe(4);
});
test('parseContinuation of an empty response is empty', () => {
expect(parseContinuation({})).toEqual({ cards: [], next: '' });
});