๐Ÿ“„ src/downloader/test/cache-store.test.ts
D-OPEN SOVEREIGN
1import { describe, it, beforeEach, expect } from 'vitest';
2import { installCacheMock } from './cache-mock';
3import {
4    openPdfCache, openPreviewCache, openPagesCache,
5    matchPdf, matchPreview, matchPage,
6    putPdf, putPreview, putPage,
7    deletePdf, deletePreview,
8    pdfKeys, previewKeys, pageKeys,
9} from '../cache-store';
10
11beforeEach(() => {
12    installCacheMock();
13});
14
15describe('cache-store', () => {
16    it('put then match round-trips a PDF response', async () => {
17        await putPdf('tx1', new Response('pdf-bytes'));
18        const hit = await matchPdf('tx1');
19        expect(hit).toBeDefined();
20        expect(await hit!.text()).toBe('pdf-bytes');
21    });
22
23    it('put then match round-trips a preview response', async () => {
24        await putPreview('tx2', new Response('preview-bytes'));
25        const hit = await matchPreview('tx2');
26        expect(await hit!.text()).toBe('preview-bytes');
27    });
28
29    it('put then match round-trips a page response', async () => {
30        await putPage('tx3', 2, new Response('page-bytes'));
31        const hit = await matchPage('tx3', 2);
32        expect(await hit!.text()).toBe('page-bytes');
33    });
34
35    it('matchPdf returns undefined for a miss', async () => {
36        expect(await matchPdf('missing')).toBeUndefined();
37    });
38
39    it('pdf and preview caches are distinct even for the same txId (ยง shared /ar/<txId> path)', async () => {
40        await putPdf('same-id', new Response('pdf-version'));
41        await putPreview('same-id', new Response('preview-version'));
42        expect(await (await matchPdf('same-id'))!.text()).toBe('pdf-version');
43        expect(await (await matchPreview('same-id'))!.text()).toBe('preview-version');
44    });
45
46    it('deletePdf removes the entry and reports whether it existed', async () => {
47        await putPdf('tx4', new Response('x'));
48        expect(await deletePdf('tx4')).toBe(true);
49        expect(await matchPdf('tx4')).toBeUndefined();
50        expect(await deletePdf('tx4')).toBe(false);
51    });
52
53    it('deletePreview removes the entry', async () => {
54        await putPreview('tx5', new Response('x'));
55        expect(await deletePreview('tx5')).toBe(true);
56        expect(await matchPreview('tx5')).toBeUndefined();
57    });
58
59    it('pdfKeys/previewKeys/pageKeys extract txIds from cache keys', async () => {
60        await putPdf('pdf-a', new Response('1'));
61        await putPdf('pdf-b', new Response('1'));
62        await putPreview('prev-a', new Response('1'));
63        await putPage('page-a', 0, new Response('1'));
64        await putPage('page-a', 1, new Response('1'));
65
66        expect((await pdfKeys()).sort()).toEqual(['pdf-a', 'pdf-b']);
67        expect(await previewKeys()).toEqual(['prev-a']);
68        // Both pages for the same txId collapse to one entry per key format,
69        // but txIdFromPageKey should still recover 'page-a' for each.
70        expect(await pageKeys()).toEqual(['page-a', 'page-a']);
71    });
72
73    it('openPdfCache/openPreviewCache/openPagesCache open the correctly named caches', async () => {
74        const pdfCache = await openPdfCache();
75        const previewCache = await openPreviewCache();
76        const pagesCache = await openPagesCache();
77        expect(pdfCache).toBeDefined();
78        expect(previewCache).toBeDefined();
79        expect(pagesCache).toBeDefined();
80        // Re-opening returns the same underlying cache (persists puts).
81        await putPdf('persist-check', new Response('1'));
82        expect(await (await openPdfCache()).match('/ar/persist-check')).toBeDefined();
83    });
84});
85