|
| 1 | +// Mock modules before imports |
| 2 | +const mockDebug = jest.fn(); |
| 3 | +const mockFindPreviousBenchmark = jest.fn(); |
| 4 | +const mockFindInsertionIndex = jest.fn(); |
| 5 | + |
| 6 | +jest.mock('@actions/core', () => ({ |
| 7 | + debug: mockDebug, |
| 8 | +})); |
| 9 | + |
| 10 | +jest.mock('../src/gitGraph', () => ({ |
| 11 | + GitGraphAnalyzer: jest.fn().mockImplementation(() => ({ |
| 12 | + findPreviousBenchmark: mockFindPreviousBenchmark, |
| 13 | + findInsertionIndex: mockFindInsertionIndex, |
| 14 | + })), |
| 15 | +})); |
| 16 | + |
| 17 | +import { addBenchmarkEntry } from '../src/addBenchmarkEntry'; |
| 18 | +import { Benchmark } from '../src/extract'; |
| 19 | + |
| 20 | +describe('addBenchmarkEntry with Git Graph', () => { |
| 21 | + const createMockBenchmark = (id: string, timestamp?: string): Benchmark => ({ |
| 22 | + commit: { |
| 23 | + id, |
| 24 | + timestamp: timestamp ?? '2025-01-01T00:00:00Z', |
| 25 | + message: `Commit ${id}`, |
| 26 | + url: `https://github.com/test/repo/commit/${id}`, |
| 27 | + author: { username: 'testuser' }, |
| 28 | + committer: { username: 'testuser' }, |
| 29 | + }, |
| 30 | + date: Date.now(), |
| 31 | + tool: 'cargo', |
| 32 | + benches: [ |
| 33 | + { |
| 34 | + name: 'test_bench', |
| 35 | + value: 100, |
| 36 | + unit: 'ms', |
| 37 | + }, |
| 38 | + ], |
| 39 | + }); |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should use git graph analyzer to find previous benchmark', () => { |
| 46 | + const benchName = 'test-suite'; |
| 47 | + const benchEntry = createMockBenchmark('abc123'); |
| 48 | + const existingEntry = createMockBenchmark('def456'); |
| 49 | + const entries = { |
| 50 | + [benchName]: [existingEntry], |
| 51 | + }; |
| 52 | + |
| 53 | + mockFindPreviousBenchmark.mockReturnValue(existingEntry); |
| 54 | + mockFindInsertionIndex.mockReturnValue(1); |
| 55 | + |
| 56 | + const result = addBenchmarkEntry(benchName, benchEntry, entries, null); |
| 57 | + |
| 58 | + expect(mockFindPreviousBenchmark).toHaveBeenCalledWith(expect.arrayContaining([existingEntry]), 'abc123'); |
| 59 | + expect(mockDebug).toHaveBeenCalledWith('Finding previous benchmark for commit: abc123'); |
| 60 | + expect(mockDebug).toHaveBeenCalledWith('Found previous benchmark: def456'); |
| 61 | + expect(result.prevBench).toBe(existingEntry); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should handle no previous benchmark found', () => { |
| 65 | + const benchName = 'test-suite'; |
| 66 | + const benchEntry = createMockBenchmark('abc123'); |
| 67 | + const existingEntry = createMockBenchmark('def456'); |
| 68 | + const entries = { |
| 69 | + [benchName]: [existingEntry], |
| 70 | + }; |
| 71 | + |
| 72 | + mockFindPreviousBenchmark.mockReturnValue(null); |
| 73 | + mockFindInsertionIndex.mockReturnValue(1); |
| 74 | + |
| 75 | + const result = addBenchmarkEntry(benchName, benchEntry, entries, null); |
| 76 | + |
| 77 | + expect(mockDebug).toHaveBeenCalledWith('Finding previous benchmark for commit: abc123'); |
| 78 | + expect(mockDebug).toHaveBeenCalledWith('No previous benchmark found'); |
| 79 | + expect(result.prevBench).toBeNull(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should create new benchmark suite when none exists', () => { |
| 83 | + const benchName = 'test-suite'; |
| 84 | + const benchEntry = createMockBenchmark('abc123'); |
| 85 | + const entries: any = {}; |
| 86 | + |
| 87 | + mockFindPreviousBenchmark.mockReturnValue(null); |
| 88 | + |
| 89 | + const result = addBenchmarkEntry(benchName, benchEntry, entries, null); |
| 90 | + |
| 91 | + expect(entries[benchName]).toEqual([benchEntry]); |
| 92 | + expect(result.prevBench).toBeNull(); |
| 93 | + expect(result.normalizedCurrentBench).toBe(benchEntry); |
| 94 | + expect(mockDebug).toHaveBeenCalledWith( |
| 95 | + "No suite was found for benchmark 'test-suite' in existing data. Created", |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should add to existing benchmark suite', () => { |
| 100 | + const benchName = 'test-suite'; |
| 101 | + const benchEntry = createMockBenchmark('abc123'); |
| 102 | + const existingEntry = createMockBenchmark('def456'); |
| 103 | + const entries = { |
| 104 | + [benchName]: [existingEntry], |
| 105 | + }; |
| 106 | + |
| 107 | + mockFindPreviousBenchmark.mockReturnValue(existingEntry); |
| 108 | + mockFindInsertionIndex.mockReturnValue(1); |
| 109 | + |
| 110 | + const result = addBenchmarkEntry(benchName, benchEntry, entries, null); |
| 111 | + |
| 112 | + expect(entries[benchName]).toHaveLength(2); |
| 113 | + expect(entries[benchName][1]).toEqual( |
| 114 | + expect.objectContaining({ |
| 115 | + commit: benchEntry.commit, |
| 116 | + tool: benchEntry.tool, |
| 117 | + }), |
| 118 | + ); |
| 119 | + expect(result.prevBench).toBe(existingEntry); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should respect maxItems limit', () => { |
| 123 | + const benchName = 'test-suite'; |
| 124 | + const benchEntry = createMockBenchmark('new123'); |
| 125 | + const oldEntries = [createMockBenchmark('old1'), createMockBenchmark('old2'), createMockBenchmark('old3')]; |
| 126 | + const entries = { |
| 127 | + [benchName]: oldEntries, |
| 128 | + }; |
| 129 | + |
| 130 | + mockFindPreviousBenchmark.mockReturnValue(oldEntries[oldEntries.length - 1]); |
| 131 | + mockFindInsertionIndex.mockReturnValue(3); |
| 132 | + |
| 133 | + addBenchmarkEntry(benchName, benchEntry, entries, 3); |
| 134 | + |
| 135 | + // Should have 3 items total (maxItems) |
| 136 | + expect(entries[benchName]).toHaveLength(3); |
| 137 | + expect(entries[benchName][2]).toEqual( |
| 138 | + expect.objectContaining({ |
| 139 | + commit: benchEntry.commit, |
| 140 | + tool: benchEntry.tool, |
| 141 | + }), |
| 142 | + ); |
| 143 | + // Should have removed oldest entries to maintain maxItems |
| 144 | + // We started with 3 old entries + 1 new = 4, so maxItems=3 keeps the 3 most recent |
| 145 | + expect(entries[benchName]).toHaveLength(3); |
| 146 | + // The oldest entry (old1) should have been removed |
| 147 | + expect(entries[benchName].map((e) => e.commit.id)).not.toContain('old1'); |
| 148 | + expect(mockDebug).toHaveBeenCalledWith( |
| 149 | + "Number of data items for 'test-suite' was truncated to 3 due to max-items-in-charts", |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should not truncate when under maxItems limit', () => { |
| 154 | + const benchName = 'test-suite'; |
| 155 | + const benchEntry = createMockBenchmark('new123'); |
| 156 | + const oldEntries = [createMockBenchmark('old1')]; |
| 157 | + const entries = { |
| 158 | + [benchName]: oldEntries, |
| 159 | + }; |
| 160 | + |
| 161 | + mockFindPreviousBenchmark.mockReturnValue(oldEntries[0]); |
| 162 | + mockFindInsertionIndex.mockReturnValue(1); |
| 163 | + |
| 164 | + addBenchmarkEntry(benchName, benchEntry, entries, 5); |
| 165 | + |
| 166 | + expect(entries[benchName]).toHaveLength(2); |
| 167 | + // Should not call debug about truncation |
| 168 | + expect(mockDebug).not.toHaveBeenCalledWith(expect.stringContaining('was truncated to')); |
| 169 | + }); |
| 170 | +}); |
0 commit comments