|
| 1 | +import { vol } from 'memfs'; |
| 2 | +import { join } from 'node:path'; |
| 3 | +import { Commit, Report, reportsDiffSchema } from '@code-pushup/models'; |
| 4 | +import { |
| 5 | + COMMIT_ALT_MOCK, |
| 6 | + COMMIT_MOCK, |
| 7 | + MEMFS_VOLUME, |
| 8 | + MINIMAL_REPORT_MOCK, |
| 9 | + REPORT_MOCK, |
| 10 | + reportMock, |
| 11 | +} from '@code-pushup/test-utils'; |
| 12 | +import { Diff, readJsonFile } from '@code-pushup/utils'; |
| 13 | +import { compareReportFiles, compareReports } from './compare'; |
| 14 | + |
| 15 | +describe('compareReports', () => { |
| 16 | + const mockCommits: Diff<Commit> = { |
| 17 | + before: COMMIT_MOCK, |
| 18 | + after: COMMIT_ALT_MOCK, |
| 19 | + }; |
| 20 | + |
| 21 | + describe('unchanged reports', () => { |
| 22 | + const mockReport = reportMock(); |
| 23 | + const mockReports: Diff<Report> = { |
| 24 | + before: { ...mockReport, commit: mockCommits.before }, |
| 25 | + after: { ...mockReport, commit: mockCommits.after }, |
| 26 | + }; |
| 27 | + |
| 28 | + it('should create valid report diff', () => { |
| 29 | + const reportsDiff = compareReports(mockReports); |
| 30 | + expect(() => reportsDiffSchema.parse(reportsDiff)).not.toThrow(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should include commits from both reports', () => { |
| 34 | + const reportsDiff = compareReports(mockReports); |
| 35 | + expect(reportsDiff.commits).toEqual(mockCommits); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should have no changes, additions or removals', () => { |
| 39 | + const reportsDiff = compareReports(mockReports); |
| 40 | + expect(reportsDiff.categories.changed).toHaveLength(0); |
| 41 | + expect(reportsDiff.categories.added).toHaveLength(0); |
| 42 | + expect(reportsDiff.categories.removed).toHaveLength(0); |
| 43 | + expect(reportsDiff.groups.changed).toHaveLength(0); |
| 44 | + expect(reportsDiff.groups.added).toHaveLength(0); |
| 45 | + expect(reportsDiff.groups.removed).toHaveLength(0); |
| 46 | + expect(reportsDiff.audits.changed).toHaveLength(0); |
| 47 | + expect(reportsDiff.audits.added).toHaveLength(0); |
| 48 | + expect(reportsDiff.audits.removed).toHaveLength(0); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should contain all categories/groups/audits in unchanged arrays', () => { |
| 52 | + const reportsDiff = compareReports(mockReports); |
| 53 | + expect(reportsDiff.categories.unchanged).toHaveLength( |
| 54 | + mockReport.categories.length, |
| 55 | + ); |
| 56 | + expect(reportsDiff.groups.unchanged).toHaveLength( |
| 57 | + mockReport.plugins.reduce((acc, { groups }) => acc + groups!.length, 0), |
| 58 | + ); |
| 59 | + expect(reportsDiff.audits.unchanged).toHaveLength( |
| 60 | + mockReport.plugins.reduce((acc, { audits }) => acc + audits.length, 0), |
| 61 | + ); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('changed reports', () => { |
| 66 | + const mockReports: Diff<Report> = { |
| 67 | + before: { ...MINIMAL_REPORT_MOCK, commit: mockCommits.before }, |
| 68 | + after: { ...REPORT_MOCK, commit: mockCommits.after }, |
| 69 | + }; |
| 70 | + |
| 71 | + it('should create valid report diff', () => { |
| 72 | + const reportsDiff = compareReports(mockReports); |
| 73 | + expect(() => reportsDiffSchema.parse(reportsDiff)).not.toThrow(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should include commits from both reports', () => { |
| 77 | + const reportsDiff = compareReports(mockReports); |
| 78 | + expect(reportsDiff.commits).toEqual(mockCommits); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should only have added categories (minimal report has none)', () => { |
| 82 | + const reportsDiff = compareReports(mockReports); |
| 83 | + expect(reportsDiff.categories.added).toHaveLength( |
| 84 | + REPORT_MOCK.categories.length, |
| 85 | + ); |
| 86 | + expect(reportsDiff.categories.removed).toHaveLength(0); |
| 87 | + expect(reportsDiff.categories.changed).toHaveLength(0); |
| 88 | + expect(reportsDiff.categories.unchanged).toHaveLength(0); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should only have added groups (minimal report has none)', () => { |
| 92 | + const reportsDiff = compareReports(mockReports); |
| 93 | + expect(reportsDiff.groups.added.length).toBeGreaterThan(0); |
| 94 | + expect(reportsDiff.groups.removed).toHaveLength(0); |
| 95 | + expect(reportsDiff.groups.changed).toHaveLength(0); |
| 96 | + expect(reportsDiff.groups.unchanged).toHaveLength(0); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should mark multiple audits as added and 1 as removed (single audit from minimal report unmatched)', () => { |
| 100 | + const reportsDiff = compareReports(mockReports); |
| 101 | + expect(reportsDiff.audits.added.length).toBeGreaterThan(1); |
| 102 | + expect(reportsDiff.audits.removed).toHaveLength(1); |
| 103 | + expect(reportsDiff.audits.changed).toHaveLength(0); |
| 104 | + expect(reportsDiff.audits.unchanged).toHaveLength(0); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +describe('compareReportFiles', () => { |
| 110 | + beforeEach(() => { |
| 111 | + vol.fromJSON( |
| 112 | + { |
| 113 | + 'source-report.json': JSON.stringify(MINIMAL_REPORT_MOCK), |
| 114 | + 'target-report.json': JSON.stringify(REPORT_MOCK), |
| 115 | + }, |
| 116 | + MEMFS_VOLUME, |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should create valid reports-diff.json from report.json files', async () => { |
| 121 | + await compareReportFiles( |
| 122 | + { |
| 123 | + before: join(MEMFS_VOLUME, 'source-report.json'), |
| 124 | + after: join(MEMFS_VOLUME, 'target-report.json'), |
| 125 | + }, |
| 126 | + join(MEMFS_VOLUME, 'reports-diff.json'), |
| 127 | + ); |
| 128 | + |
| 129 | + const reportsDiffPromise = readJsonFile( |
| 130 | + join(MEMFS_VOLUME, 'reports-diff.json'), |
| 131 | + ); |
| 132 | + await expect(reportsDiffPromise).resolves.toBeTruthy(); |
| 133 | + |
| 134 | + const reportsDiff = await reportsDiffPromise; |
| 135 | + expect(() => reportsDiffSchema.parse(reportsDiff)).not.toThrow(); |
| 136 | + }); |
| 137 | +}); |
0 commit comments