|
| 1 | +import { describe, expect, it, mock } from "bun:test"; |
| 2 | +import type { CachedDoc, CacheManagerInterface, Logger } from "../src/testing"; |
| 3 | +import { createDocService } from "../src/testing"; |
| 4 | + |
| 5 | +describe("DocService", () => { |
| 6 | + const mockCache: CacheManagerInterface = { |
| 7 | + getIndexPath: mock(() => "/mock/index.json"), |
| 8 | + getDocPath: mock(() => "/mock/doc.json"), |
| 9 | + isValid: mock(), |
| 10 | + read: mock(), |
| 11 | + write: mock(), |
| 12 | + runGarbageCollection: mock(() => ({ scanned: 0, deleted: 0, errors: 0 })), |
| 13 | + }; |
| 14 | + |
| 15 | + const mockLogger: Logger = { |
| 16 | + info: mock(), |
| 17 | + error: mock(), |
| 18 | + }; |
| 19 | + |
| 20 | + const docService = createDocService(mockCache, mockLogger); |
| 21 | + |
| 22 | + describe("getIndex", () => { |
| 23 | + it("should return cached index if valid", async () => { |
| 24 | + const cachedIndex = { entries: [] }; |
| 25 | + mockCache.isValid.mockReturnValue(true); |
| 26 | + mockCache.read.mockReturnValue(cachedIndex); |
| 27 | + |
| 28 | + const result = await docService.getIndex("3.12"); |
| 29 | + |
| 30 | + expect(result).toEqual(cachedIndex); |
| 31 | + expect(mockCache.isValid).toHaveBeenCalledWith("/mock/index.json", expect.any(Number)); |
| 32 | + expect(mockCache.read).toHaveBeenCalledWith("/mock/index.json"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should fetch and cache index if invalid", async () => { |
| 36 | + const fetchedIndex = { entries: [{ name: "test", path: "test.html", type: "module" }] }; |
| 37 | + mockCache.isValid.mockReturnValue(false); |
| 38 | + global.fetch = mock(() => |
| 39 | + Promise.resolve({ |
| 40 | + ok: true, |
| 41 | + text: () => Promise.resolve(JSON.stringify(fetchedIndex)), |
| 42 | + } as Response), |
| 43 | + ); |
| 44 | + |
| 45 | + const result = await docService.getIndex("3.12"); |
| 46 | + |
| 47 | + expect(result).toEqual(fetchedIndex); |
| 48 | + expect(mockCache.write).toHaveBeenCalledWith("/mock/index.json", fetchedIndex); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("getDoc", () => { |
| 53 | + it("should return cached doc if valid and has anchorIndex", async () => { |
| 54 | + const cachedDoc: CachedDoc = { |
| 55 | + markdown: "# test", |
| 56 | + anchorIndex: [], |
| 57 | + fetchedAt: Date.now(), |
| 58 | + }; |
| 59 | + mockCache.isValid.mockReturnValue(true); |
| 60 | + mockCache.read.mockReturnValue(cachedDoc); |
| 61 | + |
| 62 | + const result = await docService.getDoc("3.12", "test"); |
| 63 | + |
| 64 | + expect(result).toEqual({ ...cachedDoc, fromCache: true, path: "test" }); |
| 65 | + expect(mockCache.isValid).toHaveBeenCalledWith("/mock/doc.json", expect.any(Number)); |
| 66 | + expect(mockCache.read).toHaveBeenCalledWith("/mock/doc.json"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should refetch if cache is invalid", async () => { |
| 70 | + mockCache.isValid.mockReturnValue(false); |
| 71 | + global.fetch = mock(() => |
| 72 | + Promise.resolve({ |
| 73 | + ok: true, |
| 74 | + text: () => Promise.resolve("<html><body>test</body></html>"), |
| 75 | + } as Response), |
| 76 | + ); |
| 77 | + |
| 78 | + const result = await docService.getDoc("3.12", "test"); |
| 79 | + |
| 80 | + expect(result.fromCache).toBe(false); |
| 81 | + expect(result.path).toBe("test"); |
| 82 | + expect(result.markdown).toBeDefined(); |
| 83 | + expect(result.anchorIndex).toBeDefined(); |
| 84 | + expect(mockCache.write).toHaveBeenCalled(); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should refetch if cached doc lacks anchorIndex", async () => { |
| 88 | + const cachedDocWithoutAnchorIndex = { |
| 89 | + markdown: "# test", |
| 90 | + fetchedAt: Date.now(), |
| 91 | + // no anchorIndex |
| 92 | + }; |
| 93 | + mockCache.isValid.mockReturnValue(true); |
| 94 | + mockCache.read.mockReturnValue(cachedDocWithoutAnchorIndex); |
| 95 | + global.fetch = mock(() => |
| 96 | + Promise.resolve({ |
| 97 | + ok: true, |
| 98 | + text: () => Promise.resolve("<html><body>test</body></html>"), |
| 99 | + } as Response), |
| 100 | + ); |
| 101 | + |
| 102 | + const result = await docService.getDoc("3.12", "test"); |
| 103 | + |
| 104 | + expect(result.fromCache).toBe(false); |
| 105 | + expect(result.path).toBe("test"); |
| 106 | + expect(result.markdown).toBeDefined(); |
| 107 | + expect(result.anchorIndex).toBeDefined(); |
| 108 | + expect(mockCache.write).toHaveBeenCalled(); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments