-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathaudit.test.ts
More file actions
279 lines (243 loc) · 10.8 KB
/
Copy pathaudit.test.ts
File metadata and controls
279 lines (243 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import Database from 'better-sqlite3';
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
import { initSchema } from '../../src/db/index.js';
import { auditData } from '../../src/features/audit.js';
// ─── Helpers ───────────────────────────────────────────────────────────
function insertNode(db, name, kind, file, line, endLine = null, role = null) {
return db
.prepare('INSERT INTO nodes (name, kind, file, line, end_line, role) VALUES (?, ?, ?, ?, ?, ?)')
.run(name, kind, file, line, endLine, role).lastInsertRowid;
}
function insertEdge(db, sourceId, targetId, kind = 'calls') {
db.prepare(
'INSERT INTO edges (source_id, target_id, kind, confidence, dynamic) VALUES (?, ?, ?, 1.0, 0)',
).run(sourceId, targetId, kind);
}
function insertComplexity(db, nodeId, opts = {}) {
db.prepare(
`INSERT INTO function_complexity
(node_id, cognitive, cyclomatic, max_nesting, loc, sloc, comment_lines,
halstead_volume, halstead_difficulty, halstead_effort, halstead_bugs,
maintainability_index)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
).run(
nodeId,
opts.cognitive ?? 5,
opts.cyclomatic ?? 3,
opts.maxNesting ?? 2,
opts.loc ?? 30,
opts.sloc ?? 25,
opts.commentLines ?? 5,
opts.halsteadVolume ?? 120.5,
opts.halsteadDifficulty ?? 8.2,
opts.halsteadEffort ?? 988.1,
opts.halsteadBugs ?? 0.04,
opts.mi ?? 72.5,
);
}
// ─── Fixture DB ────────────────────────────────────────────────────────
let tmpDir: string, dbPath: string;
beforeAll(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-audit-'));
fs.mkdirSync(path.join(tmpDir, '.codegraph'));
dbPath = path.join(tmpDir, '.codegraph', 'graph.db');
const db = new Database(dbPath);
db.pragma('journal_mode = WAL');
initSchema(db);
// ── File nodes (required for file-level explain) ──
insertNode(db, 'src/builder.js', 'file', 'src/builder.js', 0);
insertNode(db, 'src/parser.js', 'file', 'src/parser.js', 0);
insertNode(db, 'src/resolve.js', 'file', 'src/resolve.js', 0);
insertNode(db, 'src/utils.js', 'file', 'src/utils.js', 0);
insertNode(db, 'tests/builder.test.js', 'file', 'tests/builder.test.js', 0);
// ── Function/class nodes ──
const fnBuild = insertNode(
db,
'buildGraph',
'function',
'src/builder.js',
10,
80,
'orchestrator',
);
const fnCollect = insertNode(db, 'collectFiles', 'function', 'src/builder.js', 90, 120);
const fnParse = insertNode(db, 'parseFile', 'function', 'src/parser.js', 5, 50);
const fnResolve = insertNode(db, 'resolveImport', 'function', 'src/resolve.js', 1, 30);
const fnHelper = insertNode(db, 'formatOutput', 'method', 'src/utils.js', 10, 20);
const fnTestBuild = insertNode(db, 'testBuild', 'function', 'tests/builder.test.js', 5, 40);
insertNode(db, 'Parser', 'class', 'src/parser.js', 1, 100);
// ── Edges: buildGraph -> collectFiles -> parseFile -> resolveImport ──
insertEdge(db, fnBuild, fnCollect);
insertEdge(db, fnBuild, fnParse);
insertEdge(db, fnCollect, fnParse);
insertEdge(db, fnParse, fnResolve);
insertEdge(db, fnBuild, fnHelper);
// testBuild -> buildGraph (test caller)
insertEdge(db, fnTestBuild, fnBuild);
// ── Complexity rows ──
insertComplexity(db, fnBuild, {
cognitive: 20,
cyclomatic: 12,
maxNesting: 5,
loc: 70,
sloc: 55,
commentLines: 10,
halsteadVolume: 500.3,
halsteadDifficulty: 15.1,
halsteadEffort: 7554.5,
halsteadBugs: 0.17,
mi: 45.2,
});
insertComplexity(db, fnCollect, {
cognitive: 8,
cyclomatic: 5,
maxNesting: 3,
loc: 30,
sloc: 25,
commentLines: 3,
mi: 68.0,
});
insertComplexity(db, fnParse, { cognitive: 3, cyclomatic: 2, maxNesting: 1, mi: 85.0 });
insertComplexity(db, fnResolve, { cognitive: 2, cyclomatic: 1, maxNesting: 0, mi: 90.0 });
db.close();
});
afterAll(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
// ─── Function target ──────────────────────────────────────────────────
describe('auditData — function target', () => {
test('returns correct structure for a matching function', () => {
const data = auditData('buildGraph', dbPath);
expect(data.target).toBe('buildGraph');
expect(data.kind).toBe('function');
expect(data.functions.length).toBeGreaterThanOrEqual(1);
const fn = data.functions.find((f) => f.name === 'buildGraph');
expect(fn).toBeDefined();
expect(fn.kind).toBe('function');
expect(fn.file).toBe('src/builder.js');
expect(fn.line).toBe(10);
expect(fn.endLine).toBe(80);
expect(fn.role).toBe('orchestrator');
expect(fn.lineCount).toBe(71);
});
test('includes callees and callers', () => {
const data = auditData('buildGraph', dbPath);
const fn = data.functions.find((f) => f.name === 'buildGraph');
expect(fn.callees.length).toBeGreaterThanOrEqual(3); // collectFiles, parseFile, formatOutput
expect(fn.callers.length).toBeGreaterThanOrEqual(1); // testBuild
});
test('includes related tests', () => {
const data = auditData('buildGraph', dbPath);
const fn = data.functions.find((f) => f.name === 'buildGraph');
expect(fn.relatedTests.length).toBeGreaterThanOrEqual(1);
expect(fn.relatedTests[0].file).toContain('test');
});
test('health metrics are populated', () => {
const data = auditData('buildGraph', dbPath);
const fn = data.functions.find((f) => f.name === 'buildGraph');
expect(fn.health).toBeDefined();
expect(fn.health.cognitive).toBe(20);
expect(fn.health.cyclomatic).toBe(12);
expect(fn.health.maxNesting).toBe(5);
expect(fn.health.maintainabilityIndex).toBe(45.2);
expect(fn.health.halstead.volume).toBeCloseTo(500.3, 0);
expect(fn.health.halstead.difficulty).toBeCloseTo(15.1, 0);
expect(fn.health.halstead.effort).toBeCloseTo(7554.5, 0);
expect(fn.health.halstead.bugs).toBeCloseTo(0.17, 1);
expect(fn.health.loc).toBe(70);
expect(fn.health.sloc).toBe(55);
expect(fn.health.commentLines).toBe(10);
});
test('threshold breaches are flagged', () => {
const data = auditData('buildGraph', dbPath);
const fn = data.functions.find((f) => f.name === 'buildGraph');
// cognitive=20 >= warn=15, cyclomatic=12 >= warn=10, maxNesting=5 >= warn=4
expect(fn.health.thresholdBreaches.length).toBeGreaterThanOrEqual(3);
const cogBreach = fn.health.thresholdBreaches.find((b) => b.metric === 'cognitive');
expect(cogBreach).toBeDefined();
expect(cogBreach.value).toBe(20);
expect(cogBreach.threshold).toBe(15);
expect(cogBreach.level).toBe('warn');
});
test('impact levels are populated', () => {
const data = auditData('buildGraph', dbPath);
const fn = data.functions.find((f) => f.name === 'buildGraph');
expect(fn.impact).toBeDefined();
expect(fn.impact.totalDependents).toBeGreaterThanOrEqual(1);
// testBuild calls buildGraph, so level 1 should have testBuild
expect(fn.impact.levels[1]).toBeDefined();
});
test('Phase 4.4 fields are null (graceful)', () => {
const data = auditData('buildGraph', dbPath);
const fn = data.functions.find((f) => f.name === 'buildGraph');
expect(fn.riskScore).toBeNull();
expect(fn.complexityNotes).toBeNull();
expect(fn.sideEffects).toBeNull();
});
});
// ─── File target ──────────────────────────────────────────────────────
describe('auditData — file target', () => {
test('returns correct structure for a file', () => {
const data = auditData('src/builder.js', dbPath);
expect(data.target).toBe('src/builder.js');
expect(data.kind).toBe('file');
expect(data.functions.length).toBeGreaterThanOrEqual(2); // buildGraph, collectFiles
});
test('each function has health and impact', () => {
const data = auditData('src/builder.js', dbPath);
for (const fn of data.functions) {
expect(fn.health).toBeDefined();
expect(fn.impact).toBeDefined();
expect(typeof fn.impact.totalDependents).toBe('number');
}
});
});
// ─── Filters ──────────────────────────────────────────────────────────
describe('auditData — filters', () => {
test('--noTests excludes test file callers from impact', () => {
const data = auditData('buildGraph', dbPath, { noTests: true });
const fn = data.functions.find((f) => f.name === 'buildGraph');
expect(fn.callers.every((c) => !c.file.includes('test'))).toBe(true);
});
test('--file filter scopes to matching file', () => {
const data = auditData('parseFile', dbPath, { file: 'src/parser.js' });
expect(data.functions.length).toBe(1);
expect(data.functions[0].file).toBe('src/parser.js');
});
test('--kind filter restricts symbol kind', () => {
const data = auditData('Parser', dbPath, { kind: 'class' });
expect(data.functions.length).toBe(1);
expect(data.functions[0].kind).toBe('class');
});
test('--kind with file target filters symbols', () => {
const data = auditData('src/parser.js', dbPath, { kind: 'class' });
expect(data.functions.every((f) => f.kind === 'class')).toBe(true);
});
});
// ─── Edge cases ─────────────────────────────────────────────────────
describe('auditData — edge cases', () => {
test('no match returns empty functions array', () => {
const data = auditData('nonExistentFunction', dbPath);
expect(data.functions).toEqual([]);
});
test('function with no complexity row has null health values', () => {
const data = auditData('formatOutput', dbPath);
const fn = data.functions.find((f) => f.name === 'formatOutput');
expect(fn).toBeDefined();
expect(fn.health.cognitive).toBeNull();
expect(fn.health.thresholdBreaches).toEqual([]);
});
test('function with no callers has zero impact', () => {
const data = auditData('resolveImport', dbPath);
const fn = data.functions.find(
(f) => f.name === 'resolveImport' && f.file === 'src/resolve.js',
);
expect(fn).toBeDefined();
// resolveImport is called by parseFile, so impact > 0
// But the leaf function with no callers at all would have 0
expect(typeof fn.impact.totalDependents).toBe('number');
});
});