tribunal-kit 4.4.0 ā 4.4.2
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.
- package/.agent/agents/api-architect.md +66 -66
- package/.agent/agents/db-latency-auditor.md +216 -216
- package/.agent/agents/precedence-reviewer.md +250 -250
- package/.agent/agents/resilience-reviewer.md +88 -88
- package/.agent/agents/schema-reviewer.md +67 -67
- package/.agent/agents/throughput-optimizer.md +299 -299
- package/.agent/agents/ui-ux-auditor.md +292 -292
- package/.agent/agents/vitals-reviewer.md +223 -223
- package/.agent/history/architecture-graph.yaml +32 -1
- package/.agent/history/graph-cache.json +66 -19
- package/.agent/history/snapshots/bin__tribunal-kit.js.json +19 -0
- package/.agent/history/snapshots/eslint.config.js.json +9 -0
- package/.agent/history/snapshots/migrate_refs.js.json +3 -3
- package/.agent/history/snapshots/scripts__changelog.js.json +2 -1
- package/.agent/history/snapshots/scripts__sync-version.js.json +2 -1
- package/.agent/history/snapshots/scripts__validate-payload.js.json +1 -0
- package/.agent/history/snapshots/test__integration__bridges.test.js.json +2 -1
- package/.agent/history/snapshots/test__integration__init.test.js.json +1 -0
- package/.agent/history/snapshots/test__integration__routing.test.js.json +1 -0
- package/.agent/history/snapshots/test__integration__swarm_dispatcher.test.js.json +2 -1
- package/.agent/history/snapshots/test__integration__wave2.test.js.json +2 -1
- package/.agent/history/snapshots/test__unit__args.test.js.json +11 -1
- package/.agent/history/snapshots/test__unit__case_law_manager.test.js.json +1 -0
- package/.agent/history/snapshots/test__unit__context_broker.test.js.json +11 -0
- package/.agent/history/snapshots/test__unit__copyDir.test.js.json +11 -1
- package/.agent/history/snapshots/test__unit__graph_tools.test.js.json +1 -0
- package/.agent/history/snapshots/test__unit__inner_loop_validator.test.js.json +11 -0
- package/.agent/history/snapshots/test__unit__selfInstall.test.js.json +11 -1
- package/.agent/history/snapshots/test__unit__semver.test.js.json +11 -1
- package/.agent/history/snapshots/test__unit__swarm_dispatcher.test.js.json +1 -0
- package/.agent/scripts/_colors.js +154 -2
- package/.agent/scripts/_utils.js +205 -3
- package/.agent/scripts/append_flow.js +72 -72
- package/.agent/scripts/auto_preview.js +197 -197
- package/.agent/scripts/bundle_analyzer.js +90 -119
- package/.agent/scripts/case_law_manager.js +18 -13
- package/.agent/scripts/checklist.js +100 -88
- package/.agent/scripts/colors.js +7 -13
- package/.agent/scripts/compress_skills.js +141 -141
- package/.agent/scripts/consolidate_skills.js +149 -149
- package/.agent/scripts/context_broker.js +605 -609
- package/.agent/scripts/deep_compress.js +150 -150
- package/.agent/scripts/dependency_analyzer.js +68 -106
- package/.agent/scripts/graph_builder.js +341 -311
- package/.agent/scripts/graph_visualizer.js +390 -384
- package/.agent/scripts/graph_zoom.js +6 -4
- package/.agent/scripts/inner_loop_validator.js +445 -465
- package/.agent/scripts/lint_runner.js +27 -28
- package/.agent/scripts/minify_context.js +100 -100
- package/.agent/scripts/mutation_runner.js +280 -280
- package/.agent/scripts/patch_skills_meta.js +156 -156
- package/.agent/scripts/patch_skills_output.js +244 -244
- package/.agent/scripts/schema_validator.js +280 -297
- package/.agent/scripts/security_scan.js +37 -64
- package/.agent/scripts/session_manager.js +270 -276
- package/.agent/scripts/skill_evolution.js +637 -644
- package/.agent/scripts/skill_integrator.js +307 -313
- package/.agent/scripts/strengthen_skills.js +193 -193
- package/.agent/scripts/strip_tribunal.js +47 -47
- package/.agent/scripts/swarm_dispatcher.js +360 -360
- package/.agent/scripts/test_runner.js +32 -39
- package/.agent/scripts/utils.js +10 -25
- package/.agent/scripts/verify_all.js +84 -92
- package/.agent/skills/app-builder/templates/astro-static/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/chrome-extension/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/cli-tool/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/electron-desktop/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/express-api/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/flutter-app/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/monorepo-turborepo/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/nextjs-fullstack/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/nextjs-saas/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/nextjs-static/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/nuxt-app/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/python-fastapi/TEMPLATE.md +1 -1
- package/.agent/skills/app-builder/templates/react-native-app/TEMPLATE.md +1 -1
- package/.agent/skills/doc.md +1 -1
- package/.agent/skills/knowledge-graph/SKILL.md +52 -52
- package/.agent/skills/ui-ux-pro-max/SKILL.md +562 -562
- package/.agent/workflows/generate.md +183 -183
- package/.agent/workflows/tribunal-speed.md +183 -183
- package/README.md +1 -1
- package/bin/tribunal-kit.js +76 -87
- package/package.json +6 -3
- package/scripts/changelog.js +167 -167
- package/scripts/sync-version.js +81 -81
- package/.agent/history/architecture-explorer.html +0 -352
- package/.agent/scripts/__pycache__/_colors.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/_utils.cpython-311.pyc +0 -0
- package/.agent/scripts/__pycache__/case_law_manager.cpython-311.pyc +0 -0
|
@@ -1,280 +1,280 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tribunal-Kit: Testing Patterns 2.0 (Mutation Engine)
|
|
3
|
-
*
|
|
4
|
-
* Safely mutates source code and runs tests to detect false positives.
|
|
5
|
-
* Includes absolute safety net for file restoration.
|
|
6
|
-
*
|
|
7
|
-
* v2.1 ā Context-aware mutations (skips strings/comments),
|
|
8
|
-
* line number reporting, configurable --max-mutants.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
const fs = require('fs');
|
|
12
|
-
const path = require('path');
|
|
13
|
-
const { spawnSync } = require('child_process');
|
|
14
|
-
|
|
15
|
-
// āā Mutation Definitions āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
16
|
-
const MUTATIONS = [
|
|
17
|
-
{ name: 'Strict Equality', pattern: /===/g, replacement: '!==' },
|
|
18
|
-
{ name: 'Strict Inequality', pattern: /!==/g, replacement: '===' },
|
|
19
|
-
{ name: 'Logical AND', pattern: /&&/g, replacement: '||' },
|
|
20
|
-
{ name: 'Logical OR', pattern: /\|\|/g, replacement: '&&' },
|
|
21
|
-
{ name: 'True -> False', pattern: /\btrue\b/g, replacement: 'false' },
|
|
22
|
-
{ name: 'False -> True', pattern: /\bfalse\b/g, replacement: 'true' },
|
|
23
|
-
{ name: 'Greater Than', pattern: /(?<!=)>(?!=)/g, replacement: '<' },
|
|
24
|
-
{ name: 'Less Than', pattern: /(?<!=)<(?!=)/g, replacement: '>' },
|
|
25
|
-
{ name: 'Return Early Removal', pattern: /\breturn\b/g, replacement: '/* return */' },
|
|
26
|
-
];
|
|
27
|
-
|
|
28
|
-
// āā Context-Aware Token Map āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
29
|
-
// Builds a boolean mask: true = "live code", false = "inside string or comment"
|
|
30
|
-
function buildCodeMask(source) {
|
|
31
|
-
const mask = new Array(source.length).fill(true);
|
|
32
|
-
let inString = false;
|
|
33
|
-
let stringChar = '';
|
|
34
|
-
let inBlockComment = false;
|
|
35
|
-
let inLineComment = false;
|
|
36
|
-
|
|
37
|
-
for (let i = 0; i < source.length; i++) {
|
|
38
|
-
const ch = source[i];
|
|
39
|
-
const next = source[i + 1] || '';
|
|
40
|
-
|
|
41
|
-
if (inBlockComment) {
|
|
42
|
-
mask[i] = false;
|
|
43
|
-
if (ch === '*' && next === '/') {
|
|
44
|
-
mask[i + 1] = false;
|
|
45
|
-
inBlockComment = false;
|
|
46
|
-
i++;
|
|
47
|
-
}
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
if (inLineComment) {
|
|
52
|
-
mask[i] = false;
|
|
53
|
-
if (ch === '\n') {
|
|
54
|
-
inLineComment = false;
|
|
55
|
-
}
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (inString) {
|
|
60
|
-
mask[i] = false;
|
|
61
|
-
if (ch === '\\') {
|
|
62
|
-
i++;
|
|
63
|
-
if (i < source.length) mask[i] = false;
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
if (ch === stringChar) {
|
|
67
|
-
inString = false;
|
|
68
|
-
}
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// Entering block comment
|
|
73
|
-
if (ch === '/' && next === '*') {
|
|
74
|
-
mask[i] = false;
|
|
75
|
-
mask[i + 1] = false;
|
|
76
|
-
inBlockComment = true;
|
|
77
|
-
i++;
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Entering line comment
|
|
82
|
-
if (ch === '/' && next === '/') {
|
|
83
|
-
mask[i] = false;
|
|
84
|
-
mask[i + 1] = false;
|
|
85
|
-
inLineComment = true;
|
|
86
|
-
i++;
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Entering string
|
|
91
|
-
if (ch === '"' || ch === "'" || ch === '`') {
|
|
92
|
-
mask[i] = false;
|
|
93
|
-
inString = true;
|
|
94
|
-
stringChar = ch;
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Everything else is live code
|
|
99
|
-
mask[i] = true;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return mask;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// āā Line Number Lookup āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
106
|
-
function getLineNumber(source, charIndex) {
|
|
107
|
-
let line = 1;
|
|
108
|
-
for (let i = 0; i < charIndex && i < source.length; i++) {
|
|
109
|
-
if (source[i] === '\n') line++;
|
|
110
|
-
}
|
|
111
|
-
return line;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// āā Safe Restore Globals āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
115
|
-
let targetFile = null;
|
|
116
|
-
let originalContent = null;
|
|
117
|
-
let backupPath = null;
|
|
118
|
-
|
|
119
|
-
function safeRestore() {
|
|
120
|
-
if (targetFile && originalContent) {
|
|
121
|
-
try {
|
|
122
|
-
fs.writeFileSync(targetFile, originalContent, 'utf-8');
|
|
123
|
-
} catch
|
|
124
|
-
// Last resort: tell user where the backup is
|
|
125
|
-
if (backupPath) {
|
|
126
|
-
console.error(`[Tribunal] CRITICAL: Could not restore file. Manual backup at: ${backupPath}`);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
if (backupPath && fs.existsSync(backupPath)) {
|
|
130
|
-
try { fs.unlinkSync(backupPath); } catch
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// š ABSOLUTE SAFETY NET
|
|
136
|
-
process.on('SIGINT', () => {
|
|
137
|
-
safeRestore();
|
|
138
|
-
console.error('\n[Tribunal] Mutation Engine aborted. Target file restored.');
|
|
139
|
-
process.exit(1);
|
|
140
|
-
});
|
|
141
|
-
|
|
142
|
-
process.on('uncaughtException', (err) => {
|
|
143
|
-
safeRestore();
|
|
144
|
-
console.error('\n[Tribunal] Critical error. File restored.', err);
|
|
145
|
-
process.exit(1);
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
process.on('exit', safeRestore);
|
|
149
|
-
|
|
150
|
-
// āā CLI Argument Parsing āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
151
|
-
function parseCliArgs(argv) {
|
|
152
|
-
const args = argv.slice(2);
|
|
153
|
-
let maxMutants = 5; // default per mutation type
|
|
154
|
-
let fileToMutate = null;
|
|
155
|
-
let testCommandParts = [];
|
|
156
|
-
|
|
157
|
-
for (let i = 0; i < args.length; i++) {
|
|
158
|
-
if (args[i] === '--max-mutants' && args[i + 1]) {
|
|
159
|
-
maxMutants = parseInt(args[i + 1], 10) || 5;
|
|
160
|
-
i++; // skip next
|
|
161
|
-
} else if (!fileToMutate) {
|
|
162
|
-
fileToMutate = args[i];
|
|
163
|
-
} else {
|
|
164
|
-
testCommandParts.push(args[i]);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
return { fileToMutate, testCommand: testCommandParts.join(' '), maxMutants };
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
// āā Main Engine āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
172
|
-
function runMutationTesting(fileToMutate, testCommand, maxMutantsPerType) {
|
|
173
|
-
targetFile = path.resolve(fileToMutate);
|
|
174
|
-
|
|
175
|
-
if (!fs.existsSync(targetFile)) {
|
|
176
|
-
console.error(`ERROR: File not found: ${targetFile}`);
|
|
177
|
-
process.exit(1);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
originalContent = fs.readFileSync(targetFile, 'utf-8');
|
|
181
|
-
backupPath = targetFile + '.bak';
|
|
182
|
-
fs.writeFileSync(backupPath, originalContent, 'utf-8');
|
|
183
|
-
|
|
184
|
-
console.log(`\nāāā Tribunal Mutation Engine v2.1 āāā`);
|
|
185
|
-
console.log(`Target: ${fileToMutate}`);
|
|
186
|
-
console.log(`Test cmd: ${testCommand}`);
|
|
187
|
-
console.log(`Max/type: ${maxMutantsPerType}`);
|
|
188
|
-
console.log(`\nExecuting baseline test run...`);
|
|
189
|
-
|
|
190
|
-
const baseline = spawnSync(testCommand, { shell: true, stdio: 'pipe' });
|
|
191
|
-
if (baseline.status !== 0) {
|
|
192
|
-
console.error(`ERROR: Baseline test failed! Fix your tests before mutating.`);
|
|
193
|
-
console.error(baseline.stderr.toString());
|
|
194
|
-
safeRestore();
|
|
195
|
-
process.exit(1);
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
console.log(`Baseline passed. Building code mask & generating mutants...\n`);
|
|
199
|
-
|
|
200
|
-
// Build the context mask once
|
|
201
|
-
const codeMask = buildCodeMask(originalContent);
|
|
202
|
-
|
|
203
|
-
let totalMutants = 0;
|
|
204
|
-
let killedMutants = 0;
|
|
205
|
-
let survivedMutants = 0;
|
|
206
|
-
const survivors = []; // Track survivors for the report
|
|
207
|
-
|
|
208
|
-
for (const mutation of MUTATIONS) {
|
|
209
|
-
const regex = new RegExp(mutation.pattern.source, mutation.pattern.flags);
|
|
210
|
-
const matchIndices = [];
|
|
211
|
-
let m;
|
|
212
|
-
|
|
213
|
-
while ((m = regex.exec(originalContent)) !== null && matchIndices.length < maxMutantsPerType) {
|
|
214
|
-
// Context-aware: skip matches that are inside strings or comments
|
|
215
|
-
const matchStart = m.index;
|
|
216
|
-
const matchEnd = m.index + m[0].length - 1;
|
|
217
|
-
const isLiveCode = codeMask[matchStart] && codeMask[matchEnd];
|
|
218
|
-
|
|
219
|
-
if (isLiveCode) {
|
|
220
|
-
matchIndices.push({ index: m.index, length: m[0].length, string: m[0] });
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
for (const { index, length, string } of matchIndices) {
|
|
225
|
-
totalMutants++;
|
|
226
|
-
const lineNum = getLineNumber(originalContent, index);
|
|
227
|
-
const mutatedString = string.replace(new RegExp(mutation.pattern.source), mutation.replacement);
|
|
228
|
-
const mutatedContent = originalContent.substring(0, index) + mutatedString + originalContent.substring(index + length);
|
|
229
|
-
|
|
230
|
-
fs.writeFileSync(targetFile, mutatedContent, 'utf-8');
|
|
231
|
-
|
|
232
|
-
process.stdout.write(` [Mutant #${totalMutants}] ${mutation.name} (L${lineNum}) ... `);
|
|
233
|
-
const run = spawnSync(testCommand, { shell: true, stdio: 'pipe' });
|
|
234
|
-
|
|
235
|
-
if (run.status !== 0) {
|
|
236
|
-
console.log(`ā
KILLED`);
|
|
237
|
-
killedMutants++;
|
|
238
|
-
} else {
|
|
239
|
-
console.log(`ā SURVIVED`);
|
|
240
|
-
survivedMutants++;
|
|
241
|
-
survivors.push({ type: mutation.name, line: lineNum, original: string, mutated: mutatedString });
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// Restore immediately after each mutant
|
|
245
|
-
fs.writeFileSync(targetFile, originalContent, 'utf-8');
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
const score = totalMutants > 0 ? Math.round((killedMutants / totalMutants) * 100) : 100;
|
|
250
|
-
|
|
251
|
-
console.log(`\nāāā Mutation Summary āāā`);
|
|
252
|
-
console.log(` Total Mutants: ${totalMutants}`);
|
|
253
|
-
console.log(` Killed: ${killedMutants}`);
|
|
254
|
-
console.log(` Survived: ${survivedMutants}`);
|
|
255
|
-
console.log(` Score: ${score}%`);
|
|
256
|
-
|
|
257
|
-
if (survivors.length > 0) {
|
|
258
|
-
console.log(`\nāāā Surviving Mutants (Weak Test Coverage) āāā`);
|
|
259
|
-
survivors.forEach((s, i) => {
|
|
260
|
-
console.log(` ${i + 1}. Line ${s.line}: ${s.type} (${s.original} ā ${s.mutated})`);
|
|
261
|
-
});
|
|
262
|
-
console.log(`\n ā These lines have no test that catches the mutation.`);
|
|
263
|
-
console.log(` Add assertions that would FAIL if the operator were swapped.`);
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
process.exit(score < 80 ? 1 : 0);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// āā Entry Point āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
270
|
-
const { fileToMutate, testCommand, maxMutants } = parseCliArgs(process.argv);
|
|
271
|
-
|
|
272
|
-
if (!fileToMutate || !testCommand) {
|
|
273
|
-
console.log(`Usage: node mutation_runner.js <target_file> [--max-mutants N] <test_command>`);
|
|
274
|
-
console.log(`\nExamples:`);
|
|
275
|
-
console.log(` node mutation_runner.js src/math.js "npx jest src/math.test.js"`);
|
|
276
|
-
console.log(` node mutation_runner.js src/auth.js --max-mutants 10 "npx jest test/auth.test.js"`);
|
|
277
|
-
process.exit(1);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
runMutationTesting(fileToMutate, testCommand, maxMutants);
|
|
1
|
+
/**
|
|
2
|
+
* Tribunal-Kit: Testing Patterns 2.0 (Mutation Engine)
|
|
3
|
+
*
|
|
4
|
+
* Safely mutates source code and runs tests to detect false positives.
|
|
5
|
+
* Includes absolute safety net for file restoration.
|
|
6
|
+
*
|
|
7
|
+
* v2.1 ā Context-aware mutations (skips strings/comments),
|
|
8
|
+
* line number reporting, configurable --max-mutants.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const fs = require('fs');
|
|
12
|
+
const path = require('path');
|
|
13
|
+
const { spawnSync } = require('child_process');
|
|
14
|
+
|
|
15
|
+
// āā Mutation Definitions āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
16
|
+
const MUTATIONS = [
|
|
17
|
+
{ name: 'Strict Equality', pattern: /===/g, replacement: '!==' },
|
|
18
|
+
{ name: 'Strict Inequality', pattern: /!==/g, replacement: '===' },
|
|
19
|
+
{ name: 'Logical AND', pattern: /&&/g, replacement: '||' },
|
|
20
|
+
{ name: 'Logical OR', pattern: /\|\|/g, replacement: '&&' },
|
|
21
|
+
{ name: 'True -> False', pattern: /\btrue\b/g, replacement: 'false' },
|
|
22
|
+
{ name: 'False -> True', pattern: /\bfalse\b/g, replacement: 'true' },
|
|
23
|
+
{ name: 'Greater Than', pattern: /(?<!=)>(?!=)/g, replacement: '<' },
|
|
24
|
+
{ name: 'Less Than', pattern: /(?<!=)<(?!=)/g, replacement: '>' },
|
|
25
|
+
{ name: 'Return Early Removal', pattern: /\breturn\b/g, replacement: '/* return */' },
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
// āā Context-Aware Token Map āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
29
|
+
// Builds a boolean mask: true = "live code", false = "inside string or comment"
|
|
30
|
+
function buildCodeMask(source) {
|
|
31
|
+
const mask = new Array(source.length).fill(true);
|
|
32
|
+
let inString = false;
|
|
33
|
+
let stringChar = '';
|
|
34
|
+
let inBlockComment = false;
|
|
35
|
+
let inLineComment = false;
|
|
36
|
+
|
|
37
|
+
for (let i = 0; i < source.length; i++) {
|
|
38
|
+
const ch = source[i];
|
|
39
|
+
const next = source[i + 1] || '';
|
|
40
|
+
|
|
41
|
+
if (inBlockComment) {
|
|
42
|
+
mask[i] = false;
|
|
43
|
+
if (ch === '*' && next === '/') {
|
|
44
|
+
mask[i + 1] = false;
|
|
45
|
+
inBlockComment = false;
|
|
46
|
+
i++;
|
|
47
|
+
}
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (inLineComment) {
|
|
52
|
+
mask[i] = false;
|
|
53
|
+
if (ch === '\n') {
|
|
54
|
+
inLineComment = false;
|
|
55
|
+
}
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (inString) {
|
|
60
|
+
mask[i] = false;
|
|
61
|
+
if (ch === '\\') {
|
|
62
|
+
i++;
|
|
63
|
+
if (i < source.length) mask[i] = false;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
if (ch === stringChar) {
|
|
67
|
+
inString = false;
|
|
68
|
+
}
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Entering block comment
|
|
73
|
+
if (ch === '/' && next === '*') {
|
|
74
|
+
mask[i] = false;
|
|
75
|
+
mask[i + 1] = false;
|
|
76
|
+
inBlockComment = true;
|
|
77
|
+
i++;
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Entering line comment
|
|
82
|
+
if (ch === '/' && next === '/') {
|
|
83
|
+
mask[i] = false;
|
|
84
|
+
mask[i + 1] = false;
|
|
85
|
+
inLineComment = true;
|
|
86
|
+
i++;
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Entering string
|
|
91
|
+
if (ch === '"' || ch === "'" || ch === '`') {
|
|
92
|
+
mask[i] = false;
|
|
93
|
+
inString = true;
|
|
94
|
+
stringChar = ch;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Everything else is live code
|
|
99
|
+
mask[i] = true;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return mask;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// āā Line Number Lookup āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
106
|
+
function getLineNumber(source, charIndex) {
|
|
107
|
+
let line = 1;
|
|
108
|
+
for (let i = 0; i < charIndex && i < source.length; i++) {
|
|
109
|
+
if (source[i] === '\n') line++;
|
|
110
|
+
}
|
|
111
|
+
return line;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// āā Safe Restore Globals āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
115
|
+
let targetFile = null;
|
|
116
|
+
let originalContent = null;
|
|
117
|
+
let backupPath = null;
|
|
118
|
+
|
|
119
|
+
function safeRestore() {
|
|
120
|
+
if (targetFile && originalContent) {
|
|
121
|
+
try {
|
|
122
|
+
fs.writeFileSync(targetFile, originalContent, 'utf-8');
|
|
123
|
+
} catch {
|
|
124
|
+
// Last resort: tell user where the backup is
|
|
125
|
+
if (backupPath) {
|
|
126
|
+
console.error(`[Tribunal] CRITICAL: Could not restore file. Manual backup at: ${backupPath}`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
if (backupPath && fs.existsSync(backupPath)) {
|
|
130
|
+
try { fs.unlinkSync(backupPath); } catch {}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// š ABSOLUTE SAFETY NET
|
|
136
|
+
process.on('SIGINT', () => {
|
|
137
|
+
safeRestore();
|
|
138
|
+
console.error('\n[Tribunal] Mutation Engine aborted. Target file restored.');
|
|
139
|
+
process.exit(1);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
process.on('uncaughtException', (err) => {
|
|
143
|
+
safeRestore();
|
|
144
|
+
console.error('\n[Tribunal] Critical error. File restored.', err);
|
|
145
|
+
process.exit(1);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
process.on('exit', safeRestore);
|
|
149
|
+
|
|
150
|
+
// āā CLI Argument Parsing āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
151
|
+
function parseCliArgs(argv) {
|
|
152
|
+
const args = argv.slice(2);
|
|
153
|
+
let maxMutants = 5; // default per mutation type
|
|
154
|
+
let fileToMutate = null;
|
|
155
|
+
let testCommandParts = [];
|
|
156
|
+
|
|
157
|
+
for (let i = 0; i < args.length; i++) {
|
|
158
|
+
if (args[i] === '--max-mutants' && args[i + 1]) {
|
|
159
|
+
maxMutants = parseInt(args[i + 1], 10) || 5;
|
|
160
|
+
i++; // skip next
|
|
161
|
+
} else if (!fileToMutate) {
|
|
162
|
+
fileToMutate = args[i];
|
|
163
|
+
} else {
|
|
164
|
+
testCommandParts.push(args[i]);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return { fileToMutate, testCommand: testCommandParts.join(' '), maxMutants };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// āā Main Engine āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
172
|
+
function runMutationTesting(fileToMutate, testCommand, maxMutantsPerType) {
|
|
173
|
+
targetFile = path.resolve(fileToMutate);
|
|
174
|
+
|
|
175
|
+
if (!fs.existsSync(targetFile)) {
|
|
176
|
+
console.error(`ERROR: File not found: ${targetFile}`);
|
|
177
|
+
process.exit(1);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
originalContent = fs.readFileSync(targetFile, 'utf-8');
|
|
181
|
+
backupPath = targetFile + '.bak';
|
|
182
|
+
fs.writeFileSync(backupPath, originalContent, 'utf-8');
|
|
183
|
+
|
|
184
|
+
console.log(`\nāāā Tribunal Mutation Engine v2.1 āāā`);
|
|
185
|
+
console.log(`Target: ${fileToMutate}`);
|
|
186
|
+
console.log(`Test cmd: ${testCommand}`);
|
|
187
|
+
console.log(`Max/type: ${maxMutantsPerType}`);
|
|
188
|
+
console.log(`\nExecuting baseline test run...`);
|
|
189
|
+
|
|
190
|
+
const baseline = spawnSync(testCommand, { shell: true, stdio: 'pipe' });
|
|
191
|
+
if (baseline.status !== 0) {
|
|
192
|
+
console.error(`ERROR: Baseline test failed! Fix your tests before mutating.`);
|
|
193
|
+
console.error(baseline.stderr.toString());
|
|
194
|
+
safeRestore();
|
|
195
|
+
process.exit(1);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
console.log(`Baseline passed. Building code mask & generating mutants...\n`);
|
|
199
|
+
|
|
200
|
+
// Build the context mask once
|
|
201
|
+
const codeMask = buildCodeMask(originalContent);
|
|
202
|
+
|
|
203
|
+
let totalMutants = 0;
|
|
204
|
+
let killedMutants = 0;
|
|
205
|
+
let survivedMutants = 0;
|
|
206
|
+
const survivors = []; // Track survivors for the report
|
|
207
|
+
|
|
208
|
+
for (const mutation of MUTATIONS) {
|
|
209
|
+
const regex = new RegExp(mutation.pattern.source, mutation.pattern.flags);
|
|
210
|
+
const matchIndices = [];
|
|
211
|
+
let m;
|
|
212
|
+
|
|
213
|
+
while ((m = regex.exec(originalContent)) !== null && matchIndices.length < maxMutantsPerType) {
|
|
214
|
+
// Context-aware: skip matches that are inside strings or comments
|
|
215
|
+
const matchStart = m.index;
|
|
216
|
+
const matchEnd = m.index + m[0].length - 1;
|
|
217
|
+
const isLiveCode = codeMask[matchStart] && codeMask[matchEnd];
|
|
218
|
+
|
|
219
|
+
if (isLiveCode) {
|
|
220
|
+
matchIndices.push({ index: m.index, length: m[0].length, string: m[0] });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
for (const { index, length, string } of matchIndices) {
|
|
225
|
+
totalMutants++;
|
|
226
|
+
const lineNum = getLineNumber(originalContent, index);
|
|
227
|
+
const mutatedString = string.replace(new RegExp(mutation.pattern.source), mutation.replacement);
|
|
228
|
+
const mutatedContent = originalContent.substring(0, index) + mutatedString + originalContent.substring(index + length);
|
|
229
|
+
|
|
230
|
+
fs.writeFileSync(targetFile, mutatedContent, 'utf-8');
|
|
231
|
+
|
|
232
|
+
process.stdout.write(` [Mutant #${totalMutants}] ${mutation.name} (L${lineNum}) ... `);
|
|
233
|
+
const run = spawnSync(testCommand, { shell: true, stdio: 'pipe' });
|
|
234
|
+
|
|
235
|
+
if (run.status !== 0) {
|
|
236
|
+
console.log(`ā
KILLED`);
|
|
237
|
+
killedMutants++;
|
|
238
|
+
} else {
|
|
239
|
+
console.log(`ā SURVIVED`);
|
|
240
|
+
survivedMutants++;
|
|
241
|
+
survivors.push({ type: mutation.name, line: lineNum, original: string, mutated: mutatedString });
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Restore immediately after each mutant
|
|
245
|
+
fs.writeFileSync(targetFile, originalContent, 'utf-8');
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const score = totalMutants > 0 ? Math.round((killedMutants / totalMutants) * 100) : 100;
|
|
250
|
+
|
|
251
|
+
console.log(`\nāāā Mutation Summary āāā`);
|
|
252
|
+
console.log(` Total Mutants: ${totalMutants}`);
|
|
253
|
+
console.log(` Killed: ${killedMutants}`);
|
|
254
|
+
console.log(` Survived: ${survivedMutants}`);
|
|
255
|
+
console.log(` Score: ${score}%`);
|
|
256
|
+
|
|
257
|
+
if (survivors.length > 0) {
|
|
258
|
+
console.log(`\nāāā Surviving Mutants (Weak Test Coverage) āāā`);
|
|
259
|
+
survivors.forEach((s, i) => {
|
|
260
|
+
console.log(` ${i + 1}. Line ${s.line}: ${s.type} (${s.original} ā ${s.mutated})`);
|
|
261
|
+
});
|
|
262
|
+
console.log(`\n ā These lines have no test that catches the mutation.`);
|
|
263
|
+
console.log(` Add assertions that would FAIL if the operator were swapped.`);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
process.exit(score < 80 ? 1 : 0);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// āā Entry Point āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
270
|
+
const { fileToMutate, testCommand, maxMutants } = parseCliArgs(process.argv);
|
|
271
|
+
|
|
272
|
+
if (!fileToMutate || !testCommand) {
|
|
273
|
+
console.log(`Usage: node mutation_runner.js <target_file> [--max-mutants N] <test_command>`);
|
|
274
|
+
console.log(`\nExamples:`);
|
|
275
|
+
console.log(` node mutation_runner.js src/math.js "npx jest src/math.test.js"`);
|
|
276
|
+
console.log(` node mutation_runner.js src/auth.js --max-mutants 10 "npx jest test/auth.test.js"`);
|
|
277
|
+
process.exit(1);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
runMutationTesting(fileToMutate, testCommand, maxMutants);
|