-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.test.ts
More file actions
753 lines (656 loc) · 25 KB
/
index.test.ts
File metadata and controls
753 lines (656 loc) · 25 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
import { describe, it, expect } from "vitest";
import { analyzeGraph } from "./index.js";
import { buildGraph } from "../graph/index.js";
import { cloudGroup } from "../cloud-group.js";
import type { ParsedFile, ParsedExport } from "../types/index.js";
function makeFile(relativePath: string, overrides?: Partial<ParsedFile>): ParsedFile {
return {
path: `/root/${relativePath}`,
relativePath,
loc: 10,
exports: [],
imports: [],
callSites: [],
churn: 0,
isTestFile: false,
...overrides,
};
}
function imp(resolvedFrom: string, symbols: string[] = ["x"], isTypeOnly = false): ParsedFile["imports"][number] {
return { from: `./${resolvedFrom}`, resolvedFrom, symbols, isTypeOnly };
}
describe("analyzeGraph", () => {
it("returns correct stats for a simple graph", () => {
const files = [
makeFile("a.ts", { exports: [{ name: "foo", type: "function", loc: 5, isDefault: false, complexity: 1 }] }),
makeFile("b.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
expect(result.stats.totalFiles).toBe(2);
expect(result.stats.totalFunctions).toBe(1);
expect(result.stats.totalDependencies).toBe(0);
expect(result.stats.circularDeps).toHaveLength(0);
});
it("computes pageRank for all file nodes", () => {
const files = [
makeFile("a.ts", { imports: [imp("b.ts")] }),
makeFile("b.ts", { imports: [imp("c.ts")] }),
makeFile("c.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
// c.ts is most depended on (end of chain) → highest pageRank
const prC = result.fileMetrics.get("c.ts")?.pageRank ?? 0;
const prA = result.fileMetrics.get("a.ts")?.pageRank ?? 0;
expect(prC).toBeGreaterThan(prA);
});
it("computes betweenness centrality", () => {
// b.ts is the bridge: a→b→c
const files = [
makeFile("a.ts", { imports: [imp("b.ts")] }),
makeFile("b.ts", { imports: [imp("c.ts")] }),
makeFile("c.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
const btwnB = result.fileMetrics.get("b.ts")?.betweenness ?? 0;
const btwnA = result.fileMetrics.get("a.ts")?.betweenness ?? 0;
const btwnC = result.fileMetrics.get("c.ts")?.betweenness ?? 0;
expect(btwnB).toBeGreaterThanOrEqual(btwnA);
expect(btwnB).toBeGreaterThanOrEqual(btwnC);
});
it("computes fan-in and fan-out correctly", () => {
const files = [
makeFile("a.ts", { imports: [imp("c.ts")] }),
makeFile("b.ts", { imports: [imp("c.ts")] }),
makeFile("c.ts", { imports: [imp("d.ts")] }),
makeFile("d.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
// c.ts: 2 files import it (fan-in=2), imports 1 file (fan-out=1)
const metricsC = result.fileMetrics.get("c.ts");
expect(metricsC?.fanIn).toBe(2);
expect(metricsC?.fanOut).toBe(1);
// d.ts: 1 file imports it, imports nothing
const metricsD = result.fileMetrics.get("d.ts");
expect(metricsD?.fanIn).toBe(1);
expect(metricsD?.fanOut).toBe(0);
});
it("computes coupling = fanOut / (max(fanIn, 1) + fanOut)", () => {
const files = [
makeFile("a.ts", { imports: [imp("b.ts"), imp("c.ts")] }),
makeFile("b.ts"),
makeFile("c.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
// a.ts: fanIn=0, fanOut=2 → coupling = 2/(max(0,1)+2) = 2/3 ≈ 0.667
expect(result.fileMetrics.get("a.ts")?.coupling).toBeCloseTo(2 / 3, 5);
// b.ts: fanIn=1, fanOut=0 → coupling = 0/(1+0) = 0
expect(result.fileMetrics.get("b.ts")?.coupling).toBe(0);
});
it("coupling is 0 for isolated nodes", () => {
const files = [makeFile("lonely.ts")];
const built = buildGraph(files);
const result = analyzeGraph(built);
expect(result.fileMetrics.get("lonely.ts")?.coupling).toBe(0);
});
it("detects bridge files with betweenness > 0.1", () => {
const files = [
makeFile("a.ts", { imports: [imp("b.ts"), imp("c.ts")] }),
makeFile("b.ts"),
makeFile("c.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
// Bridges depend on graph structure; just verify the property is set
for (const [, metrics] of result.fileMetrics) {
expect(metrics.isBridge).toBe(metrics.betweenness > 0.1);
}
});
it("computes module metrics with cohesion", () => {
// Two modules: src/a/ and src/b/
const files = [
makeFile("src/a/x.ts", { imports: [imp("src/a/y.ts")] }),
makeFile("src/a/y.ts"),
makeFile("src/b/z.ts", { imports: [imp("src/a/x.ts")] }),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
// Module src/a/: 1 internal dep (x→y), 0 external deps → cohesion = 1
const modA = result.moduleMetrics.get("src/a/");
expect(modA).toBeDefined();
expect(modA?.files).toBe(2);
expect(modA?.cohesion).toBe(1);
// Module src/b/: 0 internal deps, 1 external dep → cohesion = 0
const modB = result.moduleMetrics.get("src/b/");
expect(modB).toBeDefined();
expect(modB?.cohesion).toBe(0);
});
it("module dependsOn and dependedBy are correct", () => {
const files = [
makeFile("src/a/x.ts", { imports: [imp("src/b/y.ts")] }),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
const modA = result.moduleMetrics.get("src/a/");
expect(modA?.dependsOn).toContain("src/b/");
const modB = result.moduleMetrics.get("src/b/");
expect(modB?.dependedBy).toContain("src/a/");
});
it("computes force analysis with cohesion verdicts", () => {
const files = [
makeFile("src/a/x.ts", { imports: [imp("src/a/y.ts")] }),
makeFile("src/a/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
expect(result.forceAnalysis.moduleCohesion).toBeDefined();
expect(result.forceAnalysis.moduleCohesion.length).toBeGreaterThan(0);
const verdicts = result.forceAnalysis.moduleCohesion.map((m) => m.verdict);
expect(verdicts.every((v) => ["COHESIVE", "MODERATE", "JUNK_DRAWER", "LEAF"].includes(v))).toBe(true);
});
it("detects tension files pulled by multiple modules", () => {
// utils.ts is imported by both mod-a and mod-b (different modules)
const files = [
makeFile("utils.ts", {
imports: [imp("src/a/x.ts"), imp("src/b/y.ts")],
}),
makeFile("src/a/x.ts"),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
// utils.ts has outgoing edges to 2 different modules → potential tension
// Whether it qualifies depends on the tension threshold (>0.3)
expect(result.forceAnalysis.tensionFiles).toBeDefined();
});
it("summary reports healthy when no issues found", () => {
const files = [
makeFile("src/a/x.ts", { imports: [imp("src/a/y.ts")] }),
makeFile("src/a/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
expect(result.forceAnalysis.summary).toContain("healthy");
});
it("handles circular dependencies in stats", () => {
const files = [
makeFile("a.ts", { imports: [imp("b.ts")] }),
makeFile("b.ts", { imports: [imp("a.ts")] }),
];
const built = buildGraph(files);
const result = analyzeGraph(built);
expect(result.stats.circularDeps.length).toBeGreaterThan(0);
});
it("handles single-file graph", () => {
const files = [makeFile("solo.ts", { loc: 42 })];
const built = buildGraph(files);
const result = analyzeGraph(built);
expect(result.stats.totalFiles).toBe(1);
expect(result.fileMetrics.get("solo.ts")).toBeDefined();
expect(result.fileMetrics.get("solo.ts")?.coupling).toBe(0);
});
it("returns all required fields in CodebaseGraph", () => {
const files = [makeFile("a.ts")];
const built = buildGraph(files);
const result = analyzeGraph(built);
expect(result).toHaveProperty("nodes");
expect(result).toHaveProperty("edges");
expect(result).toHaveProperty("fileMetrics");
expect(result).toHaveProperty("moduleMetrics");
expect(result).toHaveProperty("groups");
expect(result).toHaveProperty("forceAnalysis");
expect(result).toHaveProperty("stats");
expect(result.forceAnalysis).toHaveProperty("moduleCohesion");
expect(result.forceAnalysis).toHaveProperty("tensionFiles");
expect(result.forceAnalysis).toHaveProperty("bridgeFiles");
expect(result.forceAnalysis).toHaveProperty("extractionCandidates");
expect(result.forceAnalysis).toHaveProperty("summary");
});
});
describe("dead export detection", () => {
function exp(name: string, type: ParsedExport["type"] = "function"): ParsedExport {
return { name, type, loc: 1, isDefault: false, complexity: 1 };
}
function callSite(
callerFile: string,
callerSymbol: string,
calleeFile: string,
calleeSymbol: string,
): ParsedFile["callSites"][number] {
return { callerFile, callerSymbol, calleeFile, calleeSymbol, confidence: "type-resolved" };
}
it("detects truly dead exports", () => {
const files = [
makeFile("a.ts", {
exports: [exp("usedFn"), exp("deadFn")],
}),
makeFile("b.ts", {
imports: [imp("a.ts", ["usedFn"])],
}),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const dead = result.fileMetrics.get("a.ts")?.deadExports;
expect(dead).toContain("deadFn");
expect(dead).not.toContain("usedFn");
});
it("type-only imports count as consumed (not dead)", () => {
const files = [
makeFile("types.ts", {
exports: [exp("MyType", "interface")],
}),
makeFile("consumer.ts", {
imports: [imp("types.ts", ["MyType"], true)],
}),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const dead = result.fileMetrics.get("types.ts")?.deadExports;
expect(dead).not.toContain("MyType");
});
it("duplicate imports to same target merge symbols", () => {
const files = [
makeFile("lib.ts", {
exports: [exp("valFn"), exp("TypeA", "interface")],
}),
makeFile("user.ts", {
imports: [
imp("lib.ts", ["valFn"], false),
imp("lib.ts", ["TypeA"], true),
],
}),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const dead = result.fileMetrics.get("lib.ts")?.deadExports;
expect(dead).not.toContain("valFn");
expect(dead).not.toContain("TypeA");
});
it("same-file calls count as consumed (not dead)", () => {
const files = [
makeFile("module.ts", {
exports: [exp("helper"), exp("main")],
callSites: [callSite("module.ts", "main", "module.ts", "helper")],
}),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const dead = result.fileMetrics.get("module.ts")?.deadExports;
expect(dead).not.toContain("helper");
// main is exported but never called by anyone — should be dead
expect(dead).toContain("main");
});
it("class method calls mark the class as consumed", () => {
const files = [
makeFile("service.ts", {
exports: [exp("AuthService", "class")],
}),
makeFile("handler.ts", {
imports: [imp("service.ts", ["AuthService"])],
callSites: [callSite("handler.ts", "<module>", "service.ts", "AuthService.validate")],
}),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const dead = result.fileMetrics.get("service.ts")?.deadExports;
expect(dead).not.toContain("AuthService");
});
it("class consumed via call graph only (no import edge)", () => {
const files = [
makeFile("service.ts", {
exports: [exp("MyClass", "class")],
}),
makeFile("caller.ts", {
// No import edge — only a call site references the class
callSites: [callSite("caller.ts", "init", "service.ts", "MyClass.create")],
}),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const dead = result.fileMetrics.get("service.ts")?.deadExports;
expect(dead).not.toContain("MyClass");
});
it("mixed dead and alive exports only flags dead ones", () => {
const files = [
makeFile("mixed.ts", {
exports: [exp("alive1"), exp("alive2", "interface"), exp("dead1"), exp("dead2")],
}),
makeFile("consumer.ts", {
imports: [
imp("mixed.ts", ["alive1"], false),
imp("mixed.ts", ["alive2"], true),
],
}),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const dead = result.fileMetrics.get("mixed.ts")?.deadExports;
expect(dead).toContain("dead1");
expect(dead).toContain("dead2");
expect(dead).not.toContain("alive1");
expect(dead).not.toContain("alive2");
});
it("duplicate edge merge syncs graphology attrs and edges array", () => {
const files = [
makeFile("target.ts", {
exports: [exp("valFn"), exp("TypeB", "interface")],
}),
makeFile("source.ts", {
imports: [
imp("target.ts", ["valFn"], false),
imp("target.ts", ["TypeB"], true),
],
}),
];
const built = buildGraph(files);
// edges array should have merged symbols
const edge = built.edges.find((e) => e.source === "source.ts" && e.target === "target.ts");
expect(edge).toBeDefined();
expect(edge?.symbols).toContain("valFn");
expect(edge?.symbols).toContain("TypeB");
expect(edge?.weight).toBe(2);
// isTypeOnly should be false (value import present)
expect(edge?.isTypeOnly).toBe(false);
// graphology edge attrs should match
const graphSymbols = built.graph.getEdgeAttribute("source.ts", "target.ts", "symbols") as string[];
const graphWeight = built.graph.getEdgeAttribute("source.ts", "target.ts", "weight") as number;
const graphIsTypeOnly = built.graph.getEdgeAttribute("source.ts", "target.ts", "isTypeOnly") as boolean;
expect(graphSymbols).toContain("valFn");
expect(graphSymbols).toContain("TypeB");
expect(graphWeight).toBe(2);
expect(graphIsTypeOnly).toBe(false);
});
});
describe("cloudGroup", () => {
it("collapses src/ to second segment", () => {
expect(cloudGroup("src/components/")).toBe("components");
expect(cloudGroup("src/utils/")).toBe("utils");
});
it("collapses lib/ app/ packages/ apps/ to second segment", () => {
expect(cloudGroup("lib/helpers/")).toBe("helpers");
expect(cloudGroup("app/api/")).toBe("api");
expect(cloudGroup("packages/shared/")).toBe("shared");
expect(cloudGroup("apps/web/")).toBe("web");
});
it("uses two-level grouping for non-source dirs with subdirs", () => {
expect(cloudGroup("convex/agents/")).toBe("convex/agents");
expect(cloudGroup("convex/auth/")).toBe("convex/auth");
expect(cloudGroup("e2e/tests/")).toBe("e2e/tests");
expect(cloudGroup("scripts/")).toBe("scripts");
});
it("uses two-level grouping for deep source-dir paths", () => {
expect(cloudGroup("src/app/(dashboard)/")).toBe("app/(dashboard)");
expect(cloudGroup("src/components/ui/")).toBe("components/ui");
expect(cloudGroup("app/api/graph/")).toBe("api/graph");
});
it("returns root for empty or dot paths", () => {
expect(cloudGroup("")).toBe("root");
expect(cloudGroup(".")).toBe("root");
expect(cloudGroup("./")).toBe("root");
});
it("handles paths without trailing slash", () => {
expect(cloudGroup("src/components")).toBe("components");
expect(cloudGroup("convex")).toBe("convex");
});
it("returns src if only src/ with no sub-segment", () => {
expect(cloudGroup("src")).toBe("src");
});
});
describe("computeGroups", () => {
it("aggregates files by cloud group", () => {
const files = [
makeFile("src/components/button.ts", { loc: 50 }),
makeFile("src/components/input.ts", { loc: 30 }),
makeFile("src/utils/helpers.ts", { loc: 20 }),
makeFile("convex/schema.ts", { loc: 40 }),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const groups = result.groups;
expect(groups.length).toBeGreaterThan(0);
const components = groups.find((g) => g.name === "components");
expect(components).toBeDefined();
expect(components?.files).toBe(2);
expect(components?.loc).toBe(80);
const convex = groups.find((g) => g.name === "convex");
expect(convex).toBeDefined();
expect(convex?.files).toBe(1);
});
it("sorts groups by importance (PageRank) descending", () => {
const files = [
makeFile("src/a/x.ts", { loc: 100 }),
makeFile("src/b/y.ts", { imports: [imp("src/a/x.ts")], loc: 50 }),
makeFile("src/c/z.ts", { imports: [imp("src/a/x.ts")], loc: 50 }),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const groups = result.groups;
for (let i = 1; i < groups.length; i++) {
expect(groups[i - 1].importance).toBeGreaterThanOrEqual(groups[i].importance);
}
});
it("assigns colors to groups", () => {
const files = [
makeFile("src/a/x.ts"),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
for (const group of result.groups) {
expect(group.color).toMatch(/^#[0-9a-f]{6}$/i);
}
});
it("returns empty array for zero files", () => {
const built = buildGraph([]);
const result = analyzeGraph(built, []);
expect(result.groups).toEqual([]);
});
it("returns all groups without cap", () => {
const files = Array.from({ length: 25 }, (_, i) =>
makeFile(`dir${i}/file.ts`, { loc: 10 }),
);
const built = buildGraph(files);
const result = analyzeGraph(built, files);
expect(result.groups.length).toBe(25);
});
it("includes fanIn and fanOut per group", () => {
const files = [
makeFile("src/a/x.ts", { imports: [imp("src/b/y.ts")] }),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
for (const group of result.groups) {
expect(group).toHaveProperty("fanIn");
expect(group).toHaveProperty("fanOut");
}
});
});
describe("LEAF verdict for single-file modules", () => {
it("AC-1: single non-test file module gets LEAF verdict", () => {
const files = [
makeFile("src/search/index.ts"),
makeFile("src/parser/a.ts", { imports: [imp("src/parser/b.ts")] }),
makeFile("src/parser/b.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const searchMod = result.forceAnalysis.moduleCohesion.find((m) => m.path === "src/search/");
expect(searchMod).toBeDefined();
expect(searchMod?.verdict).toBe("LEAF");
});
it("AC-E2: 2-file module with 0 internal deps gets JUNK_DRAWER, not LEAF", () => {
const files = [
makeFile("src/grab/a.ts", { imports: [imp("src/other/x.ts")] }),
makeFile("src/grab/b.ts", { imports: [imp("src/other/y.ts")] }),
makeFile("src/other/x.ts"),
makeFile("src/other/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const grabMod = result.forceAnalysis.moduleCohesion.find((m) => m.path === "src/grab/");
expect(grabMod).toBeDefined();
expect(grabMod?.verdict).toBe("JUNK_DRAWER");
});
it("EC1: 1-file 0-dep module gets LEAF", () => {
const files = [
makeFile("src/lonely/index.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const mod = result.forceAnalysis.moduleCohesion.find((m) => m.path === "src/lonely/");
expect(mod).toBeDefined();
expect(mod?.verdict).toBe("LEAF");
});
it("EC2: 1-file module with outgoing deps still gets LEAF", () => {
const files = [
makeFile("src/single/index.ts", { imports: [imp("src/other/a.ts"), imp("src/other/b.ts")] }),
makeFile("src/other/a.ts"),
makeFile("src/other/b.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const singleMod = result.forceAnalysis.moduleCohesion.find((m) => m.path === "src/single/");
expect(singleMod).toBeDefined();
expect(singleMod?.verdict).toBe("LEAF");
});
it("AC-10/EC6: module with 1 prod file + 1 test file gets LEAF", () => {
const files = [
makeFile("src/community/index.ts"),
makeFile("src/community/index.test.ts", { isTestFile: true }),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const mod = result.forceAnalysis.moduleCohesion.find((m) => m.path === "src/community/");
expect(mod).toBeDefined();
expect(mod?.verdict).toBe("LEAF");
});
it("EC7: module with 2 prod files + 1 test file is NOT LEAF", () => {
const files = [
makeFile("src/mod/a.ts", { imports: [imp("src/other/x.ts")] }),
makeFile("src/mod/b.ts", { imports: [imp("src/other/y.ts")] }),
makeFile("src/mod/a.test.ts", { isTestFile: true }),
makeFile("src/other/x.ts"),
makeFile("src/other/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const mod = result.forceAnalysis.moduleCohesion.find((m) => m.path === "src/mod/");
expect(mod).toBeDefined();
expect(mod?.verdict).not.toBe("LEAF");
});
it("AC-6: summary does not count LEAF modules as junk-drawer", () => {
const files = [
makeFile("src/search/index.ts"),
makeFile("src/grab/a.ts", { imports: [imp("src/other/x.ts")] }),
makeFile("src/grab/b.ts", { imports: [imp("src/other/y.ts")] }),
makeFile("src/other/x.ts"),
makeFile("src/other/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
// search/ is LEAF and should NOT be counted in junk-drawer summary
expect(result.forceAnalysis.summary).not.toContain("src/search/");
// grab/ is JUNK_DRAWER and should be in summary
expect(result.forceAnalysis.summary).toContain("src/grab/");
});
});
describe("tension suppression for type hubs and entry points", () => {
it("AC-2: type hub file gets suppressed split recommendation", () => {
// types/index.ts is pulled by two different modules
const files = [
makeFile("src/types/index.ts", {
imports: [imp("src/a/x.ts"), imp("src/b/y.ts")],
exports: [
{ name: "MyType", type: "interface", loc: 1, isDefault: false, complexity: 1 },
],
}),
makeFile("src/a/x.ts"),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const typesFile = result.forceAnalysis.tensionFiles.find(
(t) => t.file === "src/types/index.ts"
);
if (typesFile) {
expect(typesFile.recommendation).toContain("not recommended");
expect(typesFile.recommendation).not.toContain("Split into");
}
});
it("AC-3: entry point file gets suppressed split recommendation", () => {
// cli.ts is pulled by two different modules
const files = [
makeFile("cli.ts", {
imports: [imp("src/a/x.ts"), imp("src/b/y.ts")],
}),
makeFile("src/a/x.ts"),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const cliFile = result.forceAnalysis.tensionFiles.find(
(t) => t.file === "cli.ts"
);
if (cliFile) {
expect(cliFile.recommendation).toContain("not recommended");
expect(cliFile.recommendation).not.toContain("Split into");
}
});
it("EC4: types.ts in nested module gets suppressed split rec", () => {
const files = [
makeFile("src/core/types.ts", {
imports: [imp("src/a/x.ts"), imp("src/b/y.ts")],
}),
makeFile("src/a/x.ts"),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const typesFile = result.forceAnalysis.tensionFiles.find(
(t) => t.file === "src/core/types.ts"
);
if (typesFile) {
expect(typesFile.recommendation).toContain("not recommended");
}
});
it("EC5: entry point at root (main.ts, server.ts) gets suppressed", () => {
const files = [
makeFile("main.ts", {
imports: [imp("src/a/x.ts"), imp("src/b/y.ts")],
}),
makeFile("src/a/x.ts"),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const mainFile = result.forceAnalysis.tensionFiles.find(
(t) => t.file === "main.ts"
);
if (mainFile) {
expect(mainFile.recommendation).toContain("not recommended");
}
});
it("regular file with tension still gets split recommendation", () => {
const files = [
makeFile("utils.ts", {
imports: [imp("src/a/x.ts"), imp("src/b/y.ts")],
}),
makeFile("src/a/x.ts"),
makeFile("src/b/y.ts"),
];
const built = buildGraph(files);
const result = analyzeGraph(built, files);
const utilsFile = result.forceAnalysis.tensionFiles.find(
(t) => t.file === "utils.ts"
);
if (utilsFile) {
expect(utilsFile.recommendation).toContain("Split into");
}
});
});