mo3ta-coverage 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,322 @@
1
+ import fs from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
+ import { execSync } from 'child_process';
5
+ import {
6
+ checkChangedLineCoverage,
7
+ listChangedFiles,
8
+ mapRelatedTests,
9
+ Mo3taError
10
+ } from './api.js';
11
+
12
+ function git(repo, command) {
13
+ execSync(command, { cwd: repo, stdio: 'pipe' });
14
+ }
15
+
16
+ function initRepo(prefix) {
17
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
18
+ git(root, 'git init -b main -q');
19
+ git(root, 'git config user.email test@example.com');
20
+ git(root, 'git config user.name Test');
21
+ fs.mkdirSync(path.join(root, 'src'));
22
+ fs.writeFileSync(path.join(root, 'src', 'keep.js'), 'module.exports = { keep: 1 };\n');
23
+ git(root, 'git add src/keep.js && git commit -q -m base');
24
+ return root;
25
+ }
26
+
27
+ function writeMathFeature(root) {
28
+ git(root, 'git checkout -q -b feature');
29
+ fs.writeFileSync(path.join(root, 'src', 'math.js'), [
30
+ 'function add(a, b) {',
31
+ ' return a + b;',
32
+ '}',
33
+ 'function sub(a, b) {',
34
+ ' return a - b;',
35
+ '}',
36
+ 'module.exports = { add, sub };',
37
+ ''
38
+ ].join('\n'));
39
+ fs.writeFileSync(path.join(root, 'src', 'math.test.js'), [
40
+ "const { add } = require('./math');",
41
+ "test('add', () => {",
42
+ ' expect(add(1, 2)).toBe(3);',
43
+ '});',
44
+ ''
45
+ ].join('\n'));
46
+ fs.mkdirSync(path.join(root, 'specs'));
47
+ fs.writeFileSync(path.join(root, 'src', 'bare.js'), 'module.exports = { bare: 1 };\n');
48
+ fs.writeFileSync(path.join(root, 'specs', 'custom-name.spec.js'), "const { bare } = require('../src/bare');\ntest('bare', () => expect(bare.bare).toBe(1));\n");
49
+ git(root, 'git add src/math.js src/math.test.js src/bare.js specs/custom-name.spec.js && git commit -q -m feature');
50
+ }
51
+
52
+ describe('programmatic coverage API', () => {
53
+ let root;
54
+
55
+ afterEach(() => {
56
+ if (root && fs.existsSync(root)) {
57
+ fs.rmSync(root, { recursive: true, force: true });
58
+ }
59
+ root = undefined;
60
+ });
61
+
62
+ it('rejects a directory that is not a git repo', () => {
63
+ root = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-not-git-'));
64
+ expect(() => listChangedFiles({ root, useConfig: false })).toThrow(Mo3taError);
65
+ try {
66
+ listChangedFiles({ root, useConfig: false });
67
+ } catch (error) {
68
+ expect(error.code).toBe('NOT_A_GIT_REPO');
69
+ expect(error.hint).toMatch(/checkout/);
70
+ }
71
+ });
72
+
73
+ it('rejects a missing base ref with a fetch hint', () => {
74
+ root = initRepo('mo3ta-bad-base-');
75
+ expect(() => listChangedFiles({ root, base: 'origin/missing', useConfig: false })).toThrow(Mo3taError);
76
+ try {
77
+ listChangedFiles({ root, base: 'origin/missing', useConfig: false });
78
+ } catch (error) {
79
+ expect(error.code).toBe('INVALID_BASE_REF');
80
+ expect(error.message).toContain('origin/missing');
81
+ expect(error.hint).toMatch(/git fetch/);
82
+ }
83
+ });
84
+
85
+ it('lists committed and unstaged JS/TS changes versus the base ref', () => {
86
+ root = initRepo('mo3ta-list-');
87
+ writeMathFeature(root);
88
+ fs.writeFileSync(path.join(root, 'src', 'keep.js'), 'module.exports = { keep: 2 };\n');
89
+
90
+ const result = listChangedFiles({ root, base: 'main', useConfig: false });
91
+
92
+ expect(result.detection).toBe('git');
93
+ expect(result.sourceFiles).toEqual(['src/bare.js', 'src/keep.js', 'src/math.js']);
94
+ expect(result.testFiles).toEqual(['specs/custom-name.spec.js', 'src/math.test.js']);
95
+ expect(result.summary).toContain('src/math.js');
96
+ });
97
+
98
+ it('scans an explicit list and does not fall back to git when nothing scannable remains', () => {
99
+ root = initRepo('mo3ta-explicit-');
100
+ writeMathFeature(root);
101
+
102
+ const explicit = listChangedFiles({
103
+ root,
104
+ base: 'main',
105
+ files: ['src/math.js'],
106
+ useConfig: false
107
+ });
108
+ expect(explicit.detection).toBe('explicit');
109
+ expect(explicit.sourceFiles).toEqual(['src/math.js']);
110
+
111
+ const empty = listChangedFiles({
112
+ root,
113
+ base: 'main',
114
+ files: ['node_modules/pkg/index.js'],
115
+ useConfig: false
116
+ });
117
+ expect(empty.files).toEqual([]);
118
+ expect(empty.warning).toMatch(/was not used/);
119
+ });
120
+
121
+ it('maps naming matches and the fast-mode import fallback', () => {
122
+ root = initRepo('mo3ta-map-');
123
+ writeMathFeature(root);
124
+
125
+ const smart = mapRelatedTests({
126
+ root,
127
+ base: 'main',
128
+ files: ['src/math.js', 'src/bare.js'],
129
+ mode: 'fast',
130
+ useConfig: false
131
+ });
132
+
133
+ const math = smart.mappings.find(mapping => mapping.source === 'src/math.js');
134
+ const bare = smart.mappings.find(mapping => mapping.source === 'src/bare.js');
135
+ expect(math.matchType).toBe('naming convention');
136
+ expect(math.tests).toContain('src/math.test.js');
137
+ expect(bare.matchType).toBe('import scan');
138
+ expect(bare.tests).toEqual(['specs/custom-name.spec.js']);
139
+ expect(smart.testsToRun).toEqual(expect.arrayContaining([
140
+ 'src/math.test.js',
141
+ 'specs/custom-name.spec.js'
142
+ ]));
143
+ });
144
+
145
+ it('tells the caller that full mode cannot expand tests without Jest', () => {
146
+ root = initRepo('mo3ta-full-');
147
+ writeMathFeature(root);
148
+
149
+ const result = mapRelatedTests({
150
+ root,
151
+ base: 'main',
152
+ files: ['src/math.js'],
153
+ mode: 'full',
154
+ useConfig: false
155
+ });
156
+
157
+ expect(result.delegatedToJest).toBe(true);
158
+ expect(result.testsToRun).toEqual([]);
159
+ expect(result.jestTargets).toEqual(['src/math.js']);
160
+ expect(result.warning).toMatch(/findRelatedTests/);
161
+ });
162
+
163
+ it('reads base and mode from .mo3ta-coverage.json unless arguments override them', () => {
164
+ root = initRepo('mo3ta-config-');
165
+ fs.writeFileSync(path.join(root, '.mo3ta-coverage.json'), JSON.stringify({
166
+ base: 'main',
167
+ mode: 'fast',
168
+ depth: 1
169
+ }));
170
+
171
+ const fromFile = mapRelatedTests({ root, files: ['src/keep.js'] });
172
+ expect(fromFile.base).toBe('main');
173
+ expect(fromFile.mode).toBe('fast');
174
+
175
+ const override = mapRelatedTests({ root, files: ['src/keep.js'], mode: 'full' });
176
+ expect(override.mode).toBe('full');
177
+ });
178
+
179
+ it('reports an invalid config file instead of throwing a raw parse error', () => {
180
+ root = initRepo('mo3ta-bad-config-');
181
+ fs.writeFileSync(path.join(root, '.mo3ta-coverage.json'), '{');
182
+
183
+ expect(() => listChangedFiles({ root })).toThrow(Mo3taError);
184
+ try {
185
+ listChangedFiles({ root });
186
+ } catch (error) {
187
+ expect(error.code).toBe('INVALID_CONFIG');
188
+ }
189
+ });
190
+
191
+ it('grades an existing lcov file and returns the uncovered source line', () => {
192
+ root = initRepo('mo3ta-lcov-');
193
+ writeMathFeature(root);
194
+ const source = path.join(root, 'src', 'math.js');
195
+ const lcov = path.join(root, 'coverage', 'lcov.info');
196
+ fs.mkdirSync(path.dirname(lcov), { recursive: true });
197
+ fs.writeFileSync(lcov, [
198
+ 'TN:',
199
+ `SF:${source}`,
200
+ 'DA:2,1',
201
+ 'DA:5,0',
202
+ 'end_of_record',
203
+ ''
204
+ ].join('\n'));
205
+
206
+ const result = checkChangedLineCoverage({
207
+ root,
208
+ base: 'main',
209
+ files: ['src/math.js'],
210
+ mode: 'fast',
211
+ limit: 80,
212
+ runTests: false,
213
+ showCovered: true,
214
+ ciReport: 'coverage/mo3ta-coverage-report.md',
215
+ useConfig: false
216
+ });
217
+
218
+ expect(result.coveragePassed).toBe(false);
219
+ expect(result.ok).toBe(false);
220
+ expect(result.cliWouldExit).toBe(1);
221
+ expect(result.totals).toEqual({ changed: 2, covered: 1, uncovered: 1, coverage: 50 });
222
+ const math = result.files.find(file => file.file === 'src/math.js');
223
+ expect(math.uncoveredLines).toEqual([5]);
224
+ expect(math.coveredLines).toEqual([2]);
225
+ expect(math.uncovered).toEqual([{ line: 5, text: ' return a - b;' }]);
226
+ expect(result.summary).toContain('FAILED');
227
+ expect(result.summary).toContain('return a - b');
228
+ const report = fs.readFileSync(path.join(root, 'coverage', 'mo3ta-coverage-report.md'), 'utf8');
229
+ expect(report).toContain('src/math.js');
230
+ expect(report).toContain('50.00%');
231
+ });
232
+
233
+ it('fails clearly when Jest is not installed and tests were requested', () => {
234
+ root = initRepo('mo3ta-no-jest-');
235
+ writeMathFeature(root);
236
+
237
+ expect(() => checkChangedLineCoverage({
238
+ root,
239
+ base: 'main',
240
+ files: ['src/math.js'],
241
+ runTests: true,
242
+ useConfig: false
243
+ })).toThrow(Mo3taError);
244
+
245
+ try {
246
+ checkChangedLineCoverage({
247
+ root,
248
+ base: 'main',
249
+ files: ['src/math.js'],
250
+ runTests: true,
251
+ useConfig: false
252
+ });
253
+ } catch (error) {
254
+ expect(error.code).toBe('JEST_NOT_FOUND');
255
+ expect(error.hint).toMatch(/node_modules\/jest/);
256
+ }
257
+ });
258
+
259
+ it('does not run Jest when no test file is selected', () => {
260
+ root = initRepo('mo3ta-no-tests-');
261
+
262
+ const result = checkChangedLineCoverage({
263
+ root,
264
+ base: 'main',
265
+ files: ['src/keep.js'],
266
+ mode: 'fast',
267
+ runTests: true,
268
+ useConfig: false
269
+ });
270
+
271
+ expect(result.ok).toBe(false);
272
+ expect(result.testsNotRunReason).toBe('no-tests');
273
+ expect(result.cliWouldExit).toBe(0);
274
+ expect(result.jest.ran).toBe(false);
275
+ expect(result.summary).toMatch(/exit 0/);
276
+ });
277
+
278
+ it('reports a missing lcov file instead of crashing', () => {
279
+ root = initRepo('mo3ta-no-lcov-');
280
+ writeMathFeature(root);
281
+
282
+ expect(() => checkChangedLineCoverage({
283
+ root,
284
+ base: 'main',
285
+ files: ['src/math.js'],
286
+ runTests: false,
287
+ useConfig: false
288
+ })).toThrow(Mo3taError);
289
+
290
+ try {
291
+ checkChangedLineCoverage({
292
+ root,
293
+ base: 'main',
294
+ files: ['src/math.js'],
295
+ runTests: false,
296
+ useConfig: false
297
+ });
298
+ } catch (error) {
299
+ expect(error.code).toBe('LCOV_NOT_FOUND');
300
+ expect(error.hint).toMatch(/runTests/);
301
+ }
302
+ });
303
+
304
+ it('rejects an unknown mode and an out-of-range limit', () => {
305
+ root = initRepo('mo3ta-invalid-');
306
+
307
+ expect(() => mapRelatedTests({ root, mode: 'turbo', useConfig: false })).toThrow(Mo3taError);
308
+ try {
309
+ mapRelatedTests({ root, mode: 'turbo', useConfig: false });
310
+ } catch (error) {
311
+ expect(error.code).toBe('INVALID_MODE');
312
+ }
313
+
314
+ try {
315
+ checkChangedLineCoverage({ root, limit: 140, runTests: false, useConfig: false });
316
+ throw new Error('expected limit to throw');
317
+ } catch (error) {
318
+ expect(error).toBeInstanceOf(Mo3taError);
319
+ expect(error.code).toBe('INVALID_LIMIT');
320
+ }
321
+ });
322
+ });