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