tribunal-kit 4.3.1 → 4.4.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.
- package/.agent/history/architecture-explorer.html +352 -0
- package/.agent/history/architecture-graph.yaml +109 -0
- package/.agent/history/graph-cache.json +215 -0
- package/.agent/history/snapshots/migrate_refs.js.json +11 -0
- package/.agent/history/snapshots/scripts__changelog.js.json +12 -0
- package/.agent/history/snapshots/scripts__sync-version.js.json +11 -0
- package/.agent/history/snapshots/scripts__validate-payload.js.json +11 -0
- package/.agent/history/snapshots/test__integration__bridges.test.js.json +13 -0
- package/.agent/history/snapshots/test__integration__init.test.js.json +13 -0
- package/.agent/history/snapshots/test__integration__routing.test.js.json +11 -0
- package/.agent/history/snapshots/test__integration__swarm_dispatcher.test.js.json +13 -0
- package/.agent/history/snapshots/test__integration__wave2.test.js.json +13 -0
- package/.agent/history/snapshots/test__unit__args.test.js.json +10 -0
- package/.agent/history/snapshots/test__unit__case_law_manager.test.js.json +10 -0
- package/.agent/history/snapshots/test__unit__copyDir.test.js.json +13 -0
- package/.agent/history/snapshots/test__unit__graph_tools.test.js.json +11 -0
- package/.agent/history/snapshots/test__unit__selfInstall.test.js.json +13 -0
- package/.agent/history/snapshots/test__unit__semver.test.js.json +10 -0
- package/.agent/history/snapshots/test__unit__swarm_dispatcher.test.js.json +11 -0
- package/.agent/scripts/dependency_analyzer.js +1 -1
- package/.agent/scripts/graph_builder.js +311 -199
- package/.agent/scripts/graph_visualizer.js +384 -0
- package/.agent/scripts/mutation_runner.js +280 -0
- package/.agent/skills/knowledge-graph/SKILL.md +52 -36
- package/.agent/skills/testing-patterns/SKILL.md +19 -2
- package/.agent/skills/ui-ux-pro-max/SKILL.md +562 -125
- package/bin/tribunal-kit.js +129 -1
- package/package.json +1 -1
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* graph_visualizer.js — Tribunal Kit Architecture Visualizer
|
|
4
|
+
* Reads the graph cache and generates a standalone HTML visualizer.
|
|
5
|
+
* Uses a native zero-dependency Canvas force-directed graph.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
const fs = require('fs');
|
|
11
|
+
const path = require('path');
|
|
12
|
+
|
|
13
|
+
const AGENT_DIR = path.join(process.cwd(), '.agent');
|
|
14
|
+
const HISTORY_DIR = path.join(AGENT_DIR, 'history');
|
|
15
|
+
const CACHE_FILE = path.join(HISTORY_DIR, 'graph-cache.json');
|
|
16
|
+
const HTML_FILE = path.join(HISTORY_DIR, 'architecture-explorer.html');
|
|
17
|
+
|
|
18
|
+
function main() {
|
|
19
|
+
if (!fs.existsSync(CACHE_FILE)) {
|
|
20
|
+
console.error('\x1b[31m✖ Error: graph-cache.json not found. Run graph_builder.js first.\x1b[0m');
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const cacheData = fs.readFileSync(CACHE_FILE, 'utf8');
|
|
25
|
+
|
|
26
|
+
const htmlContent = `<!DOCTYPE html>
|
|
27
|
+
<html lang="en">
|
|
28
|
+
<head>
|
|
29
|
+
<meta charset="UTF-8">
|
|
30
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
31
|
+
<title>Tribunal Architecture Explorer</title>
|
|
32
|
+
<style>
|
|
33
|
+
:root {
|
|
34
|
+
--bg: #09090b;
|
|
35
|
+
--panel-bg: rgba(24, 24, 27, 0.8);
|
|
36
|
+
--border: #27272a;
|
|
37
|
+
--text: #e4e4e7;
|
|
38
|
+
--text-dim: #a1a1aa;
|
|
39
|
+
--critical: #ef4444;
|
|
40
|
+
--high: #f97316;
|
|
41
|
+
--medium: #eab308;
|
|
42
|
+
--low: #3b82f6;
|
|
43
|
+
--edge: rgba(255, 255, 255, 0.1);
|
|
44
|
+
}
|
|
45
|
+
body {
|
|
46
|
+
margin: 0;
|
|
47
|
+
padding: 0;
|
|
48
|
+
background: var(--bg);
|
|
49
|
+
color: var(--text);
|
|
50
|
+
font-family: system-ui, -apple-system, sans-serif;
|
|
51
|
+
overflow: hidden;
|
|
52
|
+
}
|
|
53
|
+
canvas {
|
|
54
|
+
display: block;
|
|
55
|
+
width: 100vw;
|
|
56
|
+
height: 100vh;
|
|
57
|
+
}
|
|
58
|
+
#ui-panel {
|
|
59
|
+
position: absolute;
|
|
60
|
+
top: 20px;
|
|
61
|
+
left: 20px;
|
|
62
|
+
width: 320px;
|
|
63
|
+
background: var(--panel-bg);
|
|
64
|
+
backdrop-filter: blur(12px);
|
|
65
|
+
border: 1px solid var(--border);
|
|
66
|
+
border-radius: 12px;
|
|
67
|
+
padding: 20px;
|
|
68
|
+
box-shadow: 0 10px 30px rgba(0,0,0,0.5);
|
|
69
|
+
pointer-events: none; /* Let clicks pass to canvas if not on panel */
|
|
70
|
+
}
|
|
71
|
+
h1 { margin: 0 0 10px 0; font-size: 1.2rem; font-weight: 600; }
|
|
72
|
+
.stat { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 0.9rem; }
|
|
73
|
+
.stat .val { font-family: monospace; color: white; }
|
|
74
|
+
.legend { margin-top: 20px; display: grid; gap: 8px; font-size: 0.85rem; }
|
|
75
|
+
.legend-item { display: flex; align-items: center; gap: 8px; }
|
|
76
|
+
.dot { width: 10px; height: 10px; border-radius: 50%; }
|
|
77
|
+
|
|
78
|
+
#node-details {
|
|
79
|
+
margin-top: 20px;
|
|
80
|
+
padding-top: 20px;
|
|
81
|
+
border-top: 1px solid var(--border);
|
|
82
|
+
display: none;
|
|
83
|
+
pointer-events: auto;
|
|
84
|
+
}
|
|
85
|
+
#node-details h2 { margin: 0 0 10px 0; font-size: 1rem; word-break: break-all; }
|
|
86
|
+
.detail-row { font-size: 0.85rem; margin-bottom: 4px; color: var(--text-dim); }
|
|
87
|
+
.badge { display: inline-block; padding: 2px 6px; border-radius: 4px; font-size: 0.75rem; font-weight: bold; color: black; }
|
|
88
|
+
ul { margin: 8px 0; padding-left: 20px; font-size: 0.85rem; color: var(--text-dim); max-height: 150px; overflow-y: auto; }
|
|
89
|
+
</style>
|
|
90
|
+
</head>
|
|
91
|
+
<body>
|
|
92
|
+
|
|
93
|
+
<canvas id="graph"></canvas>
|
|
94
|
+
|
|
95
|
+
<div id="ui-panel">
|
|
96
|
+
<h1>Tribunal Architecture</h1>
|
|
97
|
+
<div class="stat"><span>Nodes</span><span class="val" id="stat-nodes">0</span></div>
|
|
98
|
+
<div class="stat"><span>Edges</span><span class="val" id="stat-edges">0</span></div>
|
|
99
|
+
|
|
100
|
+
<div class="legend">
|
|
101
|
+
<div class="legend-item"><div class="dot" style="background: var(--critical)"></div> Critical (>10 dependents)</div>
|
|
102
|
+
<div class="legend-item"><div class="dot" style="background: var(--high)"></div> High (5-10 dependents)</div>
|
|
103
|
+
<div class="legend-item"><div class="dot" style="background: var(--medium)"></div> Medium (2-4 dependents)</div>
|
|
104
|
+
<div class="legend-item"><div class="dot" style="background: var(--low)"></div> Low (0-1 dependents)</div>
|
|
105
|
+
</div>
|
|
106
|
+
|
|
107
|
+
<div id="node-details">
|
|
108
|
+
<h2 id="nd-title">file.js</h2>
|
|
109
|
+
<div class="detail-row">Risk Score: <span id="nd-risk" class="badge">Low</span></div>
|
|
110
|
+
<div class="detail-row">Blast Radius: <span id="nd-blast" style="color:white;font-weight:bold">0</span> files</div>
|
|
111
|
+
<div class="detail-row" style="margin-top:10px">Imports:</div>
|
|
112
|
+
<ul id="nd-imports"></ul>
|
|
113
|
+
<div class="detail-row">Dependents:</div>
|
|
114
|
+
<ul id="nd-dependents"></ul>
|
|
115
|
+
</div>
|
|
116
|
+
</div>
|
|
117
|
+
|
|
118
|
+
<script>
|
|
119
|
+
const rawData = JSON.parse(decodeURIComponent("${encodeURIComponent(cacheData)}"));
|
|
120
|
+
|
|
121
|
+
const nodes = [];
|
|
122
|
+
const edges = [];
|
|
123
|
+
const nodeMap = new Map();
|
|
124
|
+
|
|
125
|
+
// Build Nodes
|
|
126
|
+
Object.keys(rawData).forEach(file => {
|
|
127
|
+
const info = rawData[file];
|
|
128
|
+
const node = {
|
|
129
|
+
id: file,
|
|
130
|
+
imports: info.imports || [],
|
|
131
|
+
dependents: info.dependents || [],
|
|
132
|
+
riskScore: info.riskScore || 'Low',
|
|
133
|
+
blastRadius: info.blastRadius || 0,
|
|
134
|
+
x: Math.random() * window.innerWidth,
|
|
135
|
+
y: Math.random() * window.innerHeight,
|
|
136
|
+
vx: 0,
|
|
137
|
+
vy: 0,
|
|
138
|
+
radius: Math.min(20, Math.max(5, 5 + (info.blastRadius * 1.5)))
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
if (node.riskScore === 'Critical') node.color = '#ef4444';
|
|
142
|
+
else if (node.riskScore === 'High') node.color = '#f97316';
|
|
143
|
+
else if (node.riskScore === 'Medium') node.color = '#eab308';
|
|
144
|
+
else node.color = '#3b82f6';
|
|
145
|
+
|
|
146
|
+
nodes.push(node);
|
|
147
|
+
nodeMap.set(file, node);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Build Edges
|
|
151
|
+
nodes.forEach(node => {
|
|
152
|
+
node.imports.forEach(imp => {
|
|
153
|
+
if (imp.startsWith('.')) {
|
|
154
|
+
// Try to resolve
|
|
155
|
+
const dir = node.id.split('/').slice(0, -1).join('/');
|
|
156
|
+
let resolved = dir ? dir + '/' + imp.replace('./', '') : imp.replace('./', '');
|
|
157
|
+
|
|
158
|
+
// Normalize standard paths relative to array
|
|
159
|
+
let target = nodes.find(n => n.id === resolved || n.id === resolved + '.js' || n.id === resolved + '.ts');
|
|
160
|
+
if (target) {
|
|
161
|
+
edges.push({ source: node, target: target });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
document.getElementById('stat-nodes').innerText = nodes.length;
|
|
168
|
+
document.getElementById('stat-edges').innerText = edges.length;
|
|
169
|
+
|
|
170
|
+
// Force Directed Graph Simulation
|
|
171
|
+
const canvas = document.getElementById('graph');
|
|
172
|
+
const ctx = canvas.getContext('2d');
|
|
173
|
+
|
|
174
|
+
function resize() {
|
|
175
|
+
canvas.width = window.innerWidth;
|
|
176
|
+
canvas.height = window.innerHeight;
|
|
177
|
+
}
|
|
178
|
+
window.addEventListener('resize', resize);
|
|
179
|
+
resize();
|
|
180
|
+
|
|
181
|
+
let hoveredNode = null;
|
|
182
|
+
let selectedNode = null;
|
|
183
|
+
let isDragging = false;
|
|
184
|
+
|
|
185
|
+
// Physics constants
|
|
186
|
+
const REPULSION = 2000;
|
|
187
|
+
const SPRING_LENGTH = 100;
|
|
188
|
+
const SPRING_STRENGTH = 0.05;
|
|
189
|
+
const DAMPING = 0.85;
|
|
190
|
+
|
|
191
|
+
function simulate() {
|
|
192
|
+
// Repulsion
|
|
193
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
194
|
+
for (let j = i + 1; j < nodes.length; j++) {
|
|
195
|
+
const n1 = nodes[i];
|
|
196
|
+
const n2 = nodes[j];
|
|
197
|
+
const dx = n2.x - n1.x;
|
|
198
|
+
const dy = n2.y - n1.y;
|
|
199
|
+
let dist = Math.sqrt(dx*dx + dy*dy) || 1;
|
|
200
|
+
if (dist < 300) {
|
|
201
|
+
const force = REPULSION / (dist * dist);
|
|
202
|
+
const fx = (dx / dist) * force;
|
|
203
|
+
const fy = (dy / dist) * force;
|
|
204
|
+
n1.vx -= fx;
|
|
205
|
+
n1.vy -= fy;
|
|
206
|
+
n2.vx += fx;
|
|
207
|
+
n2.vy += fy;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Attraction (Springs)
|
|
213
|
+
edges.forEach(edge => {
|
|
214
|
+
const dx = edge.target.x - edge.source.x;
|
|
215
|
+
const dy = edge.target.y - edge.source.y;
|
|
216
|
+
const dist = Math.sqrt(dx*dx + dy*dy) || 1;
|
|
217
|
+
const force = (dist - SPRING_LENGTH) * SPRING_STRENGTH;
|
|
218
|
+
const fx = (dx / dist) * force;
|
|
219
|
+
const fy = (dy / dist) * force;
|
|
220
|
+
|
|
221
|
+
edge.source.vx += fx;
|
|
222
|
+
edge.source.vy += fy;
|
|
223
|
+
edge.target.vx -= fx;
|
|
224
|
+
edge.target.vy -= fy;
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// Center gravity
|
|
228
|
+
const cx = canvas.width / 2;
|
|
229
|
+
const cy = canvas.height / 2;
|
|
230
|
+
nodes.forEach(n => {
|
|
231
|
+
n.vx += (cx - n.x) * 0.01;
|
|
232
|
+
n.vy += (cy - n.y) * 0.01;
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
// Update positions
|
|
236
|
+
nodes.forEach(n => {
|
|
237
|
+
if (n === selectedNode && isDragging) return; // don't move dragged node
|
|
238
|
+
n.vx *= DAMPING;
|
|
239
|
+
n.vy *= DAMPING;
|
|
240
|
+
n.x += n.vx;
|
|
241
|
+
n.y += n.vy;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function draw() {
|
|
246
|
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
|
247
|
+
|
|
248
|
+
// Draw edges
|
|
249
|
+
ctx.lineWidth = 1;
|
|
250
|
+
edges.forEach(edge => {
|
|
251
|
+
let isHighlighted = false;
|
|
252
|
+
if (hoveredNode) {
|
|
253
|
+
isHighlighted = (edge.source === hoveredNode || edge.target === hoveredNode);
|
|
254
|
+
} else if (selectedNode) {
|
|
255
|
+
isHighlighted = (edge.source === selectedNode || edge.target === selectedNode);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (hoveredNode || selectedNode) {
|
|
259
|
+
ctx.strokeStyle = isHighlighted ? 'rgba(255,255,255,0.8)' : 'rgba(255,255,255,0.05)';
|
|
260
|
+
ctx.lineWidth = isHighlighted ? 2 : 1;
|
|
261
|
+
} else {
|
|
262
|
+
ctx.strokeStyle = 'rgba(255,255,255,0.15)';
|
|
263
|
+
ctx.lineWidth = 1;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
ctx.beginPath();
|
|
267
|
+
ctx.moveTo(edge.source.x, edge.source.y);
|
|
268
|
+
ctx.lineTo(edge.target.x, edge.target.y);
|
|
269
|
+
ctx.stroke();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// Draw nodes
|
|
273
|
+
nodes.forEach(n => {
|
|
274
|
+
let opacity = 1;
|
|
275
|
+
if (hoveredNode && n !== hoveredNode && !edges.some(e => (e.source===hoveredNode && e.target===n) || (e.target===hoveredNode && e.source===n))) {
|
|
276
|
+
opacity = 0.2;
|
|
277
|
+
} else if (selectedNode && !hoveredNode && n !== selectedNode && !edges.some(e => (e.source===selectedNode && e.target===n) || (e.target===selectedNode && e.source===n))) {
|
|
278
|
+
opacity = 0.2;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
ctx.beginPath();
|
|
282
|
+
ctx.arc(n.x, n.y, n.radius, 0, Math.PI * 2);
|
|
283
|
+
ctx.fillStyle = n.color;
|
|
284
|
+
ctx.globalAlpha = opacity;
|
|
285
|
+
ctx.fill();
|
|
286
|
+
|
|
287
|
+
if (n === hoveredNode || n === selectedNode) {
|
|
288
|
+
ctx.strokeStyle = '#fff';
|
|
289
|
+
ctx.lineWidth = 2;
|
|
290
|
+
ctx.stroke();
|
|
291
|
+
|
|
292
|
+
ctx.globalAlpha = 1;
|
|
293
|
+
ctx.fillStyle = '#fff';
|
|
294
|
+
ctx.font = '12px system-ui';
|
|
295
|
+
ctx.fillText(n.id.split('/').pop(), n.x + n.radius + 5, n.y + 4);
|
|
296
|
+
}
|
|
297
|
+
ctx.globalAlpha = 1;
|
|
298
|
+
});
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
function loop() {
|
|
302
|
+
simulate();
|
|
303
|
+
draw();
|
|
304
|
+
requestAnimationFrame(loop);
|
|
305
|
+
}
|
|
306
|
+
loop();
|
|
307
|
+
|
|
308
|
+
// Interaction
|
|
309
|
+
canvas.addEventListener('mousemove', e => {
|
|
310
|
+
const rect = canvas.getBoundingClientRect();
|
|
311
|
+
const mx = e.clientX - rect.left;
|
|
312
|
+
const my = e.clientY - rect.top;
|
|
313
|
+
|
|
314
|
+
if (isDragging && selectedNode) {
|
|
315
|
+
selectedNode.x = mx;
|
|
316
|
+
selectedNode.y = my;
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
hoveredNode = null;
|
|
321
|
+
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
322
|
+
const n = nodes[i];
|
|
323
|
+
const dx = mx - n.x;
|
|
324
|
+
const dy = my - n.y;
|
|
325
|
+
if (dx*dx + dy*dy < (n.radius + 5)**2) {
|
|
326
|
+
hoveredNode = n;
|
|
327
|
+
break;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
canvas.style.cursor = hoveredNode ? 'pointer' : 'default';
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
canvas.addEventListener('mousedown', e => {
|
|
334
|
+
if (hoveredNode) {
|
|
335
|
+
selectedNode = hoveredNode;
|
|
336
|
+
isDragging = true;
|
|
337
|
+
showDetails(selectedNode);
|
|
338
|
+
} else {
|
|
339
|
+
selectedNode = null;
|
|
340
|
+
document.getElementById('node-details').style.display = 'none';
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
canvas.addEventListener('mouseup', () => { isDragging = false; });
|
|
345
|
+
|
|
346
|
+
function showDetails(n) {
|
|
347
|
+
const panel = document.getElementById('node-details');
|
|
348
|
+
panel.style.display = 'block';
|
|
349
|
+
document.getElementById('nd-title').innerText = n.id;
|
|
350
|
+
|
|
351
|
+
const riskEl = document.getElementById('nd-risk');
|
|
352
|
+
riskEl.innerText = n.riskScore;
|
|
353
|
+
riskEl.style.backgroundColor = n.color;
|
|
354
|
+
|
|
355
|
+
document.getElementById('nd-blast').innerText = n.blastRadius;
|
|
356
|
+
|
|
357
|
+
const importsUl = document.getElementById('nd-imports');
|
|
358
|
+
importsUl.innerHTML = '';
|
|
359
|
+
n.imports.forEach(i => {
|
|
360
|
+
const li = document.createElement('li');
|
|
361
|
+
li.innerText = i;
|
|
362
|
+
importsUl.appendChild(li);
|
|
363
|
+
});
|
|
364
|
+
if(n.imports.length===0) importsUl.innerHTML = '<li>None</li>';
|
|
365
|
+
|
|
366
|
+
const depsUl = document.getElementById('nd-dependents');
|
|
367
|
+
depsUl.innerHTML = '';
|
|
368
|
+
n.dependents.forEach(d => {
|
|
369
|
+
const li = document.createElement('li');
|
|
370
|
+
li.innerText = d;
|
|
371
|
+
depsUl.appendChild(li);
|
|
372
|
+
});
|
|
373
|
+
if(n.dependents.length===0) depsUl.innerHTML = '<li>None</li>';
|
|
374
|
+
}
|
|
375
|
+
</script>
|
|
376
|
+
</body>
|
|
377
|
+
</html>`;
|
|
378
|
+
|
|
379
|
+
fs.writeFileSync(HTML_FILE, htmlContent);
|
|
380
|
+
console.log(`\x1b[32m✔ Interactive visualizer generated.\x1b[0m`);
|
|
381
|
+
console.log(` \x1b[2mSaved to: ${HTML_FILE}\x1b[0m`);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
main();
|
|
@@ -0,0 +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 (e) {
|
|
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 (e) {}
|
|
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);
|