|
| 1 | +import { describe, expect, test } from 'bun:test' |
| 2 | + |
| 3 | +import { |
| 4 | + WORKSPACE_STATS_MAX_LANGUAGES, |
| 5 | + buildStoredWorkspaceProfile, |
| 6 | + computeWorkspaceStats, |
| 7 | + countLines, |
| 8 | + isTestPath, |
| 9 | + languageForPath, |
| 10 | + shouldCountLines, |
| 11 | + workspaceStatsSchema, |
| 12 | +} from '../stats' |
| 13 | + |
| 14 | +describe('countLines', () => { |
| 15 | + test('counts a final line with or without a newline', () => { |
| 16 | + expect(countLines('')).toBe(0) |
| 17 | + expect(countLines('a')).toBe(1) |
| 18 | + expect(countLines('a\n')).toBe(1) |
| 19 | + expect(countLines('a\nb')).toBe(2) |
| 20 | + expect(countLines('a\nb\n')).toBe(2) |
| 21 | + expect(countLines('\n\n')).toBe(2) |
| 22 | + expect(countLines('a\r\nb\r\n')).toBe(2) |
| 23 | + }) |
| 24 | +}) |
| 25 | + |
| 26 | +describe('languageForPath', () => { |
| 27 | + test('maps extensions and well-known file names', () => { |
| 28 | + expect(languageForPath('src/app.tsx')?.name).toBe('TypeScript') |
| 29 | + expect(languageForPath('C:\\proj\\main.PY')?.name).toBe('Python') |
| 30 | + expect(languageForPath('Dockerfile')?.name).toBe('Dockerfile') |
| 31 | + expect(languageForPath('deploy/Dockerfile.prod')?.name).toBe('Dockerfile') |
| 32 | + expect(languageForPath('Gemfile')?.name).toBe('Ruby') |
| 33 | + expect(languageForPath('README.md')).toEqual({ |
| 34 | + name: 'Markdown', |
| 35 | + kind: 'prose', |
| 36 | + }) |
| 37 | + expect(languageForPath('styles.css')?.kind).toBe('markup') |
| 38 | + expect(languageForPath('package.json')?.kind).toBe('data') |
| 39 | + }) |
| 40 | + |
| 41 | + test('skips lockfiles, minified bundles, maps and unknown types', () => { |
| 42 | + expect(languageForPath('package-lock.json')).toBeNull() |
| 43 | + expect(languageForPath('sub/bun.lock')).toBeNull() |
| 44 | + expect(languageForPath('Cargo.lock')).toBeNull() |
| 45 | + expect(languageForPath('dist/app.min.js')).toBeNull() |
| 46 | + expect(languageForPath('dist/app.js.map')).toBeNull() |
| 47 | + expect(languageForPath('logo.png')).toBeNull() |
| 48 | + expect(languageForPath('.gitignore')).toBeNull() |
| 49 | + expect(languageForPath('LICENSE')).toBeNull() |
| 50 | + expect(shouldCountLines('logo.png')).toBe(false) |
| 51 | + expect(shouldCountLines('index.ts')).toBe(true) |
| 52 | + }) |
| 53 | +}) |
| 54 | + |
| 55 | +describe('isTestPath', () => { |
| 56 | + test('recognizes common test layouts', () => { |
| 57 | + expect(isTestPath('src/__tests__/a.ts')).toBe(true) |
| 58 | + expect(isTestPath('src/a.test.ts')).toBe(true) |
| 59 | + expect(isTestPath('src/a.spec.tsx')).toBe(true) |
| 60 | + expect(isTestPath('tests/test_api.py')).toBe(true) |
| 61 | + expect(isTestPath('pkg/handler_test.go')).toBe(true) |
| 62 | + expect(isTestPath('e2e\\login.ts')).toBe(true) |
| 63 | + expect(isTestPath('src/contest.ts')).toBe(false) |
| 64 | + expect(isTestPath('src/latest/index.ts')).toBe(false) |
| 65 | + }) |
| 66 | +}) |
| 67 | + |
| 68 | +describe('computeWorkspaceStats', () => { |
| 69 | + test('totals files, test files, code lines and the language mix', () => { |
| 70 | + const stats = computeWorkspaceStats({ |
| 71 | + fileCountIsLowerBound: false, |
| 72 | + files: [ |
| 73 | + { path: 'src/index.ts', lines: 100 }, |
| 74 | + { path: 'src/index.test.ts', lines: 40 }, |
| 75 | + { path: 'src/view.tsx', lines: 60 }, |
| 76 | + { path: 'scripts/build.py', lines: 30 }, |
| 77 | + { path: 'README.md', lines: 500 }, |
| 78 | + { path: 'package.json', lines: 20 }, |
| 79 | + { path: 'bun.lock', lines: 9000 }, |
| 80 | + { path: 'logo.png' }, |
| 81 | + ], |
| 82 | + }) |
| 83 | + expect(workspaceStatsSchema.parse(stats)).toEqual(stats) |
| 84 | + expect(stats).toEqual({ |
| 85 | + fileCount: 8, |
| 86 | + fileCountIsLowerBound: false, |
| 87 | + testFileCount: 1, |
| 88 | + codeLineCount: 230, |
| 89 | + lineCountIsLowerBound: false, |
| 90 | + languages: [ |
| 91 | + { name: 'Markdown', kind: 'prose', files: 1, lines: 500 }, |
| 92 | + { name: 'TypeScript', kind: 'code', files: 3, lines: 200 }, |
| 93 | + { name: 'Python', kind: 'code', files: 1, lines: 30 }, |
| 94 | + { name: 'JSON', kind: 'data', files: 1, lines: 20 }, |
| 95 | + ], |
| 96 | + }) |
| 97 | + }) |
| 98 | + |
| 99 | + test('an unread file of a counted language makes lines a floor', () => { |
| 100 | + const stats = computeWorkspaceStats({ |
| 101 | + fileCountIsLowerBound: false, |
| 102 | + files: [{ path: 'a.ts', lines: 10 }, { path: 'b.ts' }], |
| 103 | + }) |
| 104 | + expect(stats.lineCountIsLowerBound).toBe(true) |
| 105 | + expect(stats.languages).toEqual([ |
| 106 | + { name: 'TypeScript', kind: 'code', files: 2, lines: 10 }, |
| 107 | + ]) |
| 108 | + }) |
| 109 | + |
| 110 | + test('a capped listing makes both counts floors', () => { |
| 111 | + const stats = computeWorkspaceStats({ |
| 112 | + fileCountIsLowerBound: true, |
| 113 | + files: [{ path: 'a.ts', lines: 10 }], |
| 114 | + }) |
| 115 | + expect(stats.fileCountIsLowerBound).toBe(true) |
| 116 | + expect(stats.lineCountIsLowerBound).toBe(true) |
| 117 | + }) |
| 118 | + |
| 119 | + test('keeps the top languages by lines, then files', () => { |
| 120 | + const extensions = [ |
| 121 | + '.ts', |
| 122 | + '.py', |
| 123 | + '.go', |
| 124 | + '.rs', |
| 125 | + '.java', |
| 126 | + '.kt', |
| 127 | + '.swift', |
| 128 | + '.c', |
| 129 | + '.cpp', |
| 130 | + '.cs', |
| 131 | + '.php', |
| 132 | + '.dart', |
| 133 | + '.lua', |
| 134 | + '.r', |
| 135 | + '.ex', |
| 136 | + '.hs', |
| 137 | + '.ml', |
| 138 | + '.zig', |
| 139 | + '.nim', |
| 140 | + '.sol', |
| 141 | + '.pl', |
| 142 | + '.sh', |
| 143 | + '.sql', |
| 144 | + '.vue', |
| 145 | + '.svelte', |
| 146 | + ] |
| 147 | + const stats = computeWorkspaceStats({ |
| 148 | + fileCountIsLowerBound: false, |
| 149 | + files: extensions.map((ext, i) => ({ path: `f${ext}`, lines: i + 1 })), |
| 150 | + }) |
| 151 | + expect(stats.languages).toHaveLength(WORKSPACE_STATS_MAX_LANGUAGES) |
| 152 | + expect(stats.languages[0]).toEqual({ |
| 153 | + name: 'Svelte', |
| 154 | + kind: 'code', |
| 155 | + files: 1, |
| 156 | + lines: extensions.length, |
| 157 | + }) |
| 158 | + expect(workspaceStatsSchema.safeParse(stats).success).toBe(true) |
| 159 | + }) |
| 160 | +}) |
| 161 | + |
| 162 | +describe('buildStoredWorkspaceProfile', () => { |
| 163 | + const stats = computeWorkspaceStats({ |
| 164 | + files: [ |
| 165 | + { path: 'src/a.ts', lines: 10 }, |
| 166 | + { path: 'src/a.test.ts', lines: 4 }, |
| 167 | + ], |
| 168 | + fileCountIsLowerBound: false, |
| 169 | + }) |
| 170 | + |
| 171 | + test('adds the counts as the size section beside the answers', () => { |
| 172 | + expect( |
| 173 | + buildStoredWorkspaceProfile( |
| 174 | + { languages: { languages: ['TypeScript'] } }, |
| 175 | + stats, |
| 176 | + ), |
| 177 | + ).toEqual({ |
| 178 | + languages: { languages: ['TypeScript'] }, |
| 179 | + size: { |
| 180 | + file_count: 2, |
| 181 | + file_count_is_lower_bound: false, |
| 182 | + test_file_count: 1, |
| 183 | + code_line_count: 14, |
| 184 | + line_count_is_lower_bound: false, |
| 185 | + languages: [{ name: 'TypeScript', kind: 'code', files: 2, lines: 14 }], |
| 186 | + }, |
| 187 | + }) |
| 188 | + }) |
| 189 | + |
| 190 | + test('size is always the counts, whatever the answers carried', () => { |
| 191 | + // A model can put anything in its answer; the stored size never comes from it. |
| 192 | + const answers = { size: { file_count: '999999' } } |
| 193 | + const stored = buildStoredWorkspaceProfile(answers, stats) |
| 194 | + expect(stored.size).toMatchObject({ file_count: 2 }) |
| 195 | + }) |
| 196 | +}) |
0 commit comments