|
| 1 | +/** |
| 2 | + * Unit tests for scripts/check-file-sizes.ts |
| 3 | + * |
| 4 | + * The script is the CI guard that fails when god-files grow beyond their |
| 5 | + * recorded budget. These tests pin its evaluation logic against a fixture |
| 6 | + * tree built in a temp directory so they don't break when real source files |
| 7 | + * change line counts. |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 11 | +import { mkdtempSync, mkdirSync, writeFileSync, rmSync } from 'node:fs'; |
| 12 | +import { tmpdir } from 'node:os'; |
| 13 | +import { join } from 'node:path'; |
| 14 | +import { |
| 15 | + evaluateFile, |
| 16 | + evaluateAll, |
| 17 | + FILE_LIMITS, |
| 18 | +} from '../../../scripts/check-file-sizes.js'; |
| 19 | + |
| 20 | +let tmpRoot: string; |
| 21 | + |
| 22 | +beforeAll(() => { |
| 23 | + tmpRoot = mkdtempSync(join(tmpdir(), 'sparsetree-filesize-')); |
| 24 | + mkdirSync(join(tmpRoot, 'src'), { recursive: true }); |
| 25 | + |
| 26 | + // 100 lines, no trailing newline |
| 27 | + writeFileSync( |
| 28 | + join(tmpRoot, 'src', 'tight.ts'), |
| 29 | + Array.from({ length: 100 }, (_, i) => `// line ${i + 1}`).join('\n'), |
| 30 | + ); |
| 31 | + |
| 32 | + // 100 lines, with trailing newline (line count should still be 100) |
| 33 | + writeFileSync( |
| 34 | + join(tmpRoot, 'src', 'trailing-newline.ts'), |
| 35 | + Array.from({ length: 100 }, (_, i) => `// line ${i + 1}`).join('\n') + '\n', |
| 36 | + ); |
| 37 | + |
| 38 | + // 200 lines — well under any reasonable limit |
| 39 | + writeFileSync( |
| 40 | + join(tmpRoot, 'src', 'shrinkable.ts'), |
| 41 | + Array.from({ length: 200 }, (_, i) => `// line ${i + 1}`).join('\n'), |
| 42 | + ); |
| 43 | + |
| 44 | + // 0 lines — empty file |
| 45 | + writeFileSync(join(tmpRoot, 'src', 'empty.ts'), ''); |
| 46 | +}); |
| 47 | + |
| 48 | +afterAll(() => { |
| 49 | + rmSync(tmpRoot, { recursive: true, force: true }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe('evaluateFile', () => { |
| 53 | + it('returns ok when within limit', () => { |
| 54 | + const result = evaluateFile({ path: 'src/tight.ts', limit: 100 }, tmpRoot); |
| 55 | + expect(result.status).toBe('ok'); |
| 56 | + expect(result.lines).toBe(100); |
| 57 | + expect(result.slack).toBe(0); |
| 58 | + }); |
| 59 | + |
| 60 | + it('does not double-count a trailing newline', () => { |
| 61 | + const result = evaluateFile({ path: 'src/trailing-newline.ts', limit: 100 }, tmpRoot); |
| 62 | + expect(result.lines).toBe(100); |
| 63 | + expect(result.status).toBe('ok'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('flags files over the limit', () => { |
| 67 | + const result = evaluateFile({ path: 'src/tight.ts', limit: 50 }, tmpRoot); |
| 68 | + expect(result.status).toBe('over'); |
| 69 | + expect(result.slack).toBeLessThan(0); |
| 70 | + expect(result.lines).toBe(100); |
| 71 | + expect(result.limit).toBe(50); |
| 72 | + }); |
| 73 | + |
| 74 | + it('flags files significantly under the limit as shrinkable', () => { |
| 75 | + const result = evaluateFile({ path: 'src/shrinkable.ts', limit: 1000 }, tmpRoot); |
| 76 | + expect(result.status).toBe('shrinkable'); |
| 77 | + expect(result.slack).toBeGreaterThan(50); |
| 78 | + }); |
| 79 | + |
| 80 | + it('reports missing files with status "missing"', () => { |
| 81 | + const result = evaluateFile({ path: 'src/does-not-exist.ts', limit: 100 }, tmpRoot); |
| 82 | + expect(result.status).toBe('missing'); |
| 83 | + expect(result.lines).toBe(0); |
| 84 | + }); |
| 85 | + |
| 86 | + it('treats empty files as 0 lines', () => { |
| 87 | + const result = evaluateFile({ path: 'src/empty.ts', limit: 200 }, tmpRoot); |
| 88 | + expect(result.lines).toBe(0); |
| 89 | + expect(result.status).toBe('shrinkable'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('preserves the note from the limit entry', () => { |
| 93 | + const result = evaluateFile( |
| 94 | + { path: 'src/tight.ts', limit: 200, note: 'extract submodule X' }, |
| 95 | + tmpRoot, |
| 96 | + ); |
| 97 | + expect(result.note).toBe('extract submodule X'); |
| 98 | + }); |
| 99 | +}); |
| 100 | + |
| 101 | +describe('evaluateAll', () => { |
| 102 | + it('evaluates each entry exactly once and preserves order', () => { |
| 103 | + const limits = [ |
| 104 | + { path: 'src/tight.ts', limit: 100 }, |
| 105 | + { path: 'src/shrinkable.ts', limit: 1000 }, |
| 106 | + ]; |
| 107 | + const results = evaluateAll(limits, tmpRoot); |
| 108 | + expect(results).toHaveLength(2); |
| 109 | + expect(results[0].path).toBe('src/tight.ts'); |
| 110 | + expect(results[1].path).toBe('src/shrinkable.ts'); |
| 111 | + }); |
| 112 | +}); |
| 113 | + |
| 114 | +describe('FILE_LIMITS configuration', () => { |
| 115 | + it('declares unique paths', () => { |
| 116 | + const paths = FILE_LIMITS.map(e => e.path); |
| 117 | + expect(new Set(paths).size).toBe(paths.length); |
| 118 | + }); |
| 119 | + |
| 120 | + it('has positive integer limits', () => { |
| 121 | + for (const entry of FILE_LIMITS) { |
| 122 | + expect(entry.limit).toBeGreaterThan(0); |
| 123 | + expect(Number.isInteger(entry.limit)).toBe(true); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + it('has all current files under their declared limit', () => { |
| 128 | + // This is the contract: every tracked file must be at or under its |
| 129 | + // budget at HEAD. If this fails, either split the file or raise the |
| 130 | + // limit (with rationale in the PR). |
| 131 | + const results = evaluateAll(); |
| 132 | + const over = results.filter(r => r.status === 'over'); |
| 133 | + const missing = results.filter(r => r.status === 'missing'); |
| 134 | + expect(over).toEqual([]); |
| 135 | + expect(missing).toEqual([]); |
| 136 | + }); |
| 137 | +}); |
0 commit comments