mo3ta-coverage 1.2.1 → 1.3.3

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,290 @@
1
+ import assert from 'node:assert/strict';
2
+ import { spawn } from 'node:child_process';
3
+ import fs from 'node:fs';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+ import { execSync } from 'node:child_process';
7
+ import test from 'node:test';
8
+ import { Client } from '@modelcontextprotocol/sdk/client/index.js';
9
+ import { InMemoryTransport } from '@modelcontextprotocol/sdk/inMemory.js';
10
+ import { createServer } from './createServer.js';
11
+
12
+ const REPO_ROOT = path.resolve(path.dirname(new URL(import.meta.url).pathname), '..');
13
+
14
+ function git(repo, command) {
15
+ execSync(command, { cwd: repo, stdio: 'pipe' });
16
+ }
17
+
18
+ function initFixture() {
19
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-mcp-'));
20
+ git(root, 'git init -b main -q');
21
+ git(root, 'git config user.email test@example.com');
22
+ git(root, 'git config user.name Test');
23
+ fs.mkdirSync(path.join(root, 'src'));
24
+ fs.writeFileSync(path.join(root, 'src', 'keep.js'), 'module.exports = { keep: 1 };\n');
25
+ git(root, 'git add src/keep.js && git commit -q -m base');
26
+ git(root, 'git checkout -q -b feature');
27
+ fs.writeFileSync(path.join(root, 'src', 'math.js'), [
28
+ 'function add(a, b) {',
29
+ ' return a + b;',
30
+ '}',
31
+ 'function sub(a, b) {',
32
+ ' return a - b;',
33
+ '}',
34
+ 'module.exports = { add, sub };',
35
+ ''
36
+ ].join('\n'));
37
+ fs.writeFileSync(path.join(root, 'src', 'math.test.js'), [
38
+ "const { add } = require('./math');",
39
+ "test('adds', () => {",
40
+ ' expect(add(1, 2)).toBe(3);',
41
+ '});',
42
+ ''
43
+ ].join('\n'));
44
+ fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify({
45
+ name: 'mo3ta-mcp-fixture',
46
+ jest: {
47
+ testEnvironment: 'node',
48
+ transform: {},
49
+ coverageReporters: ['lcov', 'text-summary']
50
+ }
51
+ }));
52
+ git(root, 'git add src/math.js src/math.test.js package.json && git commit -q -m feature');
53
+ return root;
54
+ }
55
+
56
+ async function connect() {
57
+ const server = createServer({ version: 'test' });
58
+ const client = new Client({ name: 'mo3ta-mcp-test', version: '0.0.0' });
59
+ const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
60
+ await server.connect(serverTransport);
61
+ await client.connect(clientTransport);
62
+ return {
63
+ client,
64
+ async close() {
65
+ await client.close();
66
+ await server.close();
67
+ }
68
+ };
69
+ }
70
+
71
+ function dataOf(result) {
72
+ assert.ok(result.structuredContent, 'tool result should include structuredContent');
73
+ return result.structuredContent;
74
+ }
75
+
76
+ test('registers the coverage tools', async () => {
77
+ const session = await connect();
78
+ try {
79
+ const listed = await session.client.listTools();
80
+ const names = listed.tools.map(tool => tool.name).sort();
81
+ assert.deepEqual(names, [
82
+ 'check_changed_line_coverage',
83
+ 'list_changed_files',
84
+ 'map_related_tests'
85
+ ]);
86
+ const check = listed.tools.find(tool => tool.name === 'check_changed_line_coverage');
87
+ assert.match(check.description, /uncovered/);
88
+ } finally {
89
+ await session.close();
90
+ }
91
+ });
92
+
93
+ test('list and map tools return changed files and their tests', async () => {
94
+ const root = initFixture();
95
+ const session = await connect();
96
+ try {
97
+ const listed = await session.client.callTool({
98
+ name: 'list_changed_files',
99
+ arguments: { root, base: 'main', useConfig: false }
100
+ });
101
+ const listedData = dataOf(listed);
102
+ assert.equal(listed.isError, false);
103
+ assert.deepEqual(listedData.sourceFiles, ['src/math.js']);
104
+ assert.deepEqual(listedData.testFiles, ['src/math.test.js']);
105
+ assert.match(listed.content[0].text, /src\/math\.js/);
106
+
107
+ const mapped = await session.client.callTool({
108
+ name: 'map_related_tests',
109
+ arguments: { root, base: 'main', mode: 'fast', useConfig: false }
110
+ });
111
+ const mappedData = dataOf(mapped);
112
+ assert.equal(mappedData.testsToRun.includes('src/math.test.js'), true);
113
+ assert.match(mappedData.summary, /math\.test\.js/);
114
+ } finally {
115
+ await session.close();
116
+ fs.rmSync(root, { recursive: true, force: true });
117
+ }
118
+ });
119
+
120
+ test('map_related_tests narrows the selected specs with a filter regex', async () => {
121
+ const root = initFixture();
122
+ fs.writeFileSync(path.join(root, 'src', 'other.test.js'), [
123
+ "const { add } = require('./math');",
124
+ "test('other', () => {",
125
+ ' expect(add(1, 1)).toBe(2);',
126
+ '});',
127
+ ''
128
+ ].join('\n'));
129
+ git(root, 'git add src/other.test.js && git commit -q -m other');
130
+ const session = await connect();
131
+ try {
132
+ const mapped = await session.client.callTool({
133
+ name: 'map_related_tests',
134
+ arguments: { root, base: 'main', mode: 'fast', filter: 'math', useConfig: false }
135
+ });
136
+ const data = dataOf(mapped);
137
+ assert.equal(mapped.isError, false);
138
+ assert.equal(data.filter, 'math');
139
+ assert.deepEqual(data.testsToRun, ['src/math.test.js']);
140
+ assert.ok(data.allTestsToRun.includes('src/other.test.js'));
141
+ assert.match(data.summary, /Filter \/math\/ kept 1 of 2/);
142
+ } finally {
143
+ await session.close();
144
+ fs.rmSync(root, { recursive: true, force: true });
145
+ }
146
+ });
147
+
148
+ test('turns a missing repo and a bad base ref into tool errors', async () => {
149
+ const session = await connect();
150
+ const missing = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-mcp-missing-'));
151
+ fs.rmSync(missing, { recursive: true, force: true });
152
+ const notGit = fs.mkdtempSync(path.join(os.tmpdir(), 'mo3ta-mcp-notgit-'));
153
+ try {
154
+ const missingResult = await session.client.callTool({
155
+ name: 'list_changed_files',
156
+ arguments: { root: missing, useConfig: false }
157
+ });
158
+ assert.equal(missingResult.isError, true);
159
+ assert.equal(dataOf(missingResult).code, 'REPO_NOT_FOUND');
160
+
161
+ const notGitResult = await session.client.callTool({
162
+ name: 'list_changed_files',
163
+ arguments: { root: notGit, useConfig: false }
164
+ });
165
+ assert.equal(notGitResult.isError, true);
166
+ assert.equal(dataOf(notGitResult).code, 'NOT_A_GIT_REPO');
167
+ assert.match(dataOf(notGitResult).hint, /checkout/);
168
+
169
+ const root = initFixture();
170
+ try {
171
+ const badBase = await session.client.callTool({
172
+ name: 'map_related_tests',
173
+ arguments: { root, base: 'no-such-ref', useConfig: false }
174
+ });
175
+ assert.equal(badBase.isError, true);
176
+ assert.equal(dataOf(badBase).code, 'INVALID_BASE_REF');
177
+ assert.match(dataOf(badBase).hint, /git fetch/);
178
+ } finally {
179
+ fs.rmSync(root, { recursive: true, force: true });
180
+ }
181
+ } finally {
182
+ await session.close();
183
+ fs.rmSync(notGit, { recursive: true, force: true });
184
+ }
185
+ });
186
+
187
+ test('check_changed_line_coverage runs Jest and returns the uncovered line', async () => {
188
+ const root = initFixture();
189
+ fs.symlinkSync(path.join(REPO_ROOT, 'node_modules'), path.join(root, 'node_modules'), 'dir');
190
+ const session = await connect();
191
+ try {
192
+ const result = await session.client.callTool({
193
+ name: 'check_changed_line_coverage',
194
+ arguments: {
195
+ root,
196
+ base: 'main',
197
+ mode: 'fast',
198
+ limit: 100,
199
+ useConfig: false,
200
+ timeoutMs: 120000,
201
+ jest: ['--silent']
202
+ }
203
+ });
204
+ const data = dataOf(result);
205
+ assert.equal(result.isError, false);
206
+ assert.deepEqual(data.jestArgs, ['--silent']);
207
+ assert.equal(data.testsFailed, false);
208
+ assert.equal(data.jest.ran, true);
209
+ assert.equal(data.coveragePassed, false);
210
+ assert.equal(data.ok, false);
211
+ const math = data.files.find(file => file.file === 'src/math.js');
212
+ assert.ok(math, 'expected src/math.js in the coverage result');
213
+ assert.ok(
214
+ math.uncovered.some(snippet => snippet.text.includes('return a - b')),
215
+ `expected the untested sub() line in uncovered snippets, got ${JSON.stringify(math.uncovered)}`
216
+ );
217
+ assert.match(data.summary, /FAILED/);
218
+ assert.match(data.summary, /return a - b/);
219
+ } finally {
220
+ await session.close();
221
+ fs.rmSync(root, { recursive: true, force: true });
222
+ }
223
+ });
224
+
225
+ test('check_changed_line_coverage reports a missing Jest install', async () => {
226
+ const root = initFixture();
227
+ const session = await connect();
228
+ try {
229
+ const result = await session.client.callTool({
230
+ name: 'check_changed_line_coverage',
231
+ arguments: { root, base: 'main', mode: 'fast', useConfig: false }
232
+ });
233
+ assert.equal(result.isError, true);
234
+ assert.equal(dataOf(result).code, 'JEST_NOT_FOUND');
235
+ assert.match(dataOf(result).hint, /npm|dependencies|node_modules/i);
236
+ } finally {
237
+ await session.close();
238
+ fs.rmSync(root, { recursive: true, force: true });
239
+ }
240
+ });
241
+
242
+ test('stops a Jest run that exceeds timeoutMs', async () => {
243
+ const root = initFixture();
244
+ fs.writeFileSync(path.join(root, 'src', 'math.test.js'), [
245
+ "const { add } = require('./math');",
246
+ "test('slow', () => {",
247
+ ' const start = Date.now();',
248
+ ' while (Date.now() - start < 20000) {}',
249
+ ' expect(add(1, 1)).toBe(2);',
250
+ '});',
251
+ ''
252
+ ].join('\n'));
253
+ fs.symlinkSync(path.join(REPO_ROOT, 'node_modules'), path.join(root, 'node_modules'), 'dir');
254
+ const session = await connect();
255
+ try {
256
+ const result = await session.client.callTool({
257
+ name: 'check_changed_line_coverage',
258
+ arguments: {
259
+ root,
260
+ base: 'main',
261
+ files: ['src/math.js'],
262
+ mode: 'fast',
263
+ useConfig: false,
264
+ timeoutMs: 2000
265
+ }
266
+ });
267
+ assert.equal(result.isError, true);
268
+ assert.equal(dataOf(result).code, 'JEST_TIMEOUT');
269
+ assert.match(dataOf(result).hint, /timeoutMs/);
270
+ } finally {
271
+ await session.close();
272
+ fs.rmSync(root, { recursive: true, force: true });
273
+ }
274
+ });
275
+
276
+ test('the stdio entry does not write protocol output before a client connects', async () => {
277
+ const child = spawn(process.execPath, [path.join(REPO_ROOT, 'mcp', 'server.js')], {
278
+ cwd: REPO_ROOT,
279
+ stdio: ['pipe', 'pipe', 'pipe']
280
+ });
281
+ let stdout = '';
282
+ child.stdout.setEncoding('utf8');
283
+ child.stdout.on('data', chunk => {
284
+ stdout += chunk;
285
+ });
286
+ await new Promise(resolve => setTimeout(resolve, 400));
287
+ child.kill('SIGTERM');
288
+ await new Promise(resolve => child.once('exit', resolve));
289
+ assert.equal(stdout, '');
290
+ });
package/package.json CHANGED
@@ -1,13 +1,16 @@
1
1
  {
2
2
  "name": "mo3ta-coverage",
3
- "version": "1.2.1",
3
+ "version": "1.3.3",
4
4
  "description": "Run tests and report uncovered changed lines in a PR",
5
+ "homepage": "https://mo3ta.top/mo3ta-coverage/",
5
6
  "main": "index.js",
6
7
  "bin": {
7
- "mo3ta-coverage": "./index.js"
8
+ "mo3ta-coverage": "./index.js",
9
+ "mo3ta-coverage-mcp": "./mcp/server.js"
8
10
  },
9
11
  "scripts": {
10
- "test": "jest --coverage",
12
+ "test": "jest --coverage && node --test --test-concurrency=1 mcp/server.test.js",
13
+ "mcp": "node mcp/server.js",
11
14
  "build": "babel index.js -o dist/index.js"
12
15
  },
13
16
  "author": "Mahmoud Mabrok",
@@ -21,7 +24,8 @@
21
24
  "ci",
22
25
  "code-coverage",
23
26
  "pull-request",
24
- "diff"
27
+ "diff",
28
+ "mcp"
25
29
  ],
26
30
  "repository": {
27
31
  "type": "git",
@@ -37,7 +41,9 @@
37
41
  "jest": "30.5.2"
38
42
  },
39
43
  "dependencies": {
40
- "commander": "15.1.0"
44
+ "@modelcontextprotocol/sdk": "1.32.2",
45
+ "commander": "15.1.0",
46
+ "zod": "4.6.7"
41
47
  },
42
48
  "overrides": {
43
49
  "glob": "14.0.6",