| 1 | import { GitDiffCache } from "./gitDiffCache"; |
| 2 | |
| 3 | beforeEach(() => { |
| 4 | // Add this line to GitDiffCache class to make instance accessible |
| 5 | (GitDiffCache as any).instance = null; |
| 6 | }); |
| 7 | |
| 8 | test("GitDiffCache returns cached results within cache time", async () => { |
| 9 | const mockDiff = ["file1.ts", "file2.ts"]; |
| 10 | const getDiffFn = jest.fn().mockResolvedValue(mockDiff); |
| 11 | const cache = GitDiffCache.getInstance(getDiffFn, 1); // 1 second cache |
| 12 | |
| 13 | const result1 = await cache.get(); |
| 14 | const result2 = await cache.get(); |
| 15 | |
| 16 | expect(result1).toEqual(mockDiff); |
| 17 | expect(result2).toEqual(mockDiff); |
| 18 | expect(getDiffFn).toHaveBeenCalledTimes(1); |
| 19 | }); |
| 20 | |
| 21 | test("GitDiffCache refreshes cache after expiration", async () => { |
| 22 | const mockDiff = ["file1.ts"]; |
| 23 | const getDiffFn = jest.fn().mockResolvedValue(mockDiff); |
| 24 | const cache = GitDiffCache.getInstance(getDiffFn, 0.1); // 100ms cache |
| 25 | |
| 26 | const result1 = await cache.get(); |
| 27 | await new Promise((resolve) => setTimeout(resolve, 200)); // Wait for cache to expire |
| 28 | const result2 = await cache.get(); |
| 29 | |
| 30 | expect(getDiffFn).toHaveBeenCalledTimes(2); |
| 31 | }); |
| 32 | |
| 33 | test("GitDiffCache returns empty array on error", async () => { |
| 34 | const getDiffFn = jest.fn().mockRejectedValue(new Error("Git error")); |
| 35 | const cache = GitDiffCache.getInstance(getDiffFn); |
| 36 | |
| 37 | const result = await cache.get(); |
| 38 | expect(result).toEqual([]); |
| 39 | }); |
| 40 | |
| 41 | test("GitDiffCache reuses pending request", async () => { |
| 42 | const mockDiff = ["file1.ts"]; |
| 43 | let resolvePromise: (value: string[]) => void; |
| 44 | const getDiffFn = jest.fn().mockImplementation(() => { |
| 45 | return new Promise((resolve) => { |
| 46 | resolvePromise = resolve; |
| 47 | }); |
| 48 | }); |
| 49 | |
| 50 | const cache = GitDiffCache.getInstance(getDiffFn); |
| 51 | |
| 52 | const promise1 = cache.get(); |
| 53 | const promise2 = cache.get(); |
| 54 | |
| 55 | resolvePromise!(mockDiff); |
| 56 | |
| 57 | const [result1, result2] = await Promise.all([promise1, promise2]); |
| 58 | |
| 59 | expect(result1).toEqual(mockDiff); |
| 60 | expect(result2).toEqual(mockDiff); |
| 61 | expect(getDiffFn).toHaveBeenCalledTimes(1); |
| 62 | }); |
| 63 | |
| 64 | test("GitDiffCache invalidate clears cache", async () => { |
| 65 | const mockDiff = ["file1.ts"]; |
| 66 | const getDiffFn = jest.fn().mockResolvedValue(mockDiff); |
| 67 | const cache = GitDiffCache.getInstance(getDiffFn); |
| 68 | |
| 69 | await cache.get(); |
| 70 | cache.invalidate(); |
| 71 | await cache.get(); |
| 72 | |
| 73 | expect(getDiffFn).toHaveBeenCalledTimes(2); |
| 74 | }); |
| 75 | |
| 76 | test("GitDiffCache maintains singleton instance", () => { |
| 77 | const getDiffFn1 = jest.fn(); |
| 78 | const getDiffFn2 = jest.fn(); |
| 79 | |
| 80 | const instance1 = GitDiffCache.getInstance(getDiffFn1); |
| 81 | const instance2 = GitDiffCache.getInstance(getDiffFn2); |
| 82 | |
| 83 | expect(instance1).toBe(instance2); |
| 84 | }); |