@aiready/context-analyzer 0.21.1 → 0.21.6

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.
Files changed (153) hide show
  1. package/.aiready/aiready-report-20260314-222254.json +39216 -0
  2. package/.aiready/aiready-report-20260314-223947.json +3413 -0
  3. package/.aiready/aiready-report-20260314-224112.json +3413 -0
  4. package/.aiready/aiready-report-20260314-224302.json +2973 -0
  5. package/.aiready/aiready-report-20260314-224939.json +3092 -0
  6. package/.aiready/aiready-report-20260314-225154.json +3092 -0
  7. package/.turbo/turbo-build.log +13 -12
  8. package/.turbo/turbo-test.log +32 -341
  9. package/dist/__tests__/analyzer.test.js +55 -14
  10. package/dist/__tests__/analyzer.test.js.map +1 -1
  11. package/dist/__tests__/cluster-detector.test.d.ts +2 -0
  12. package/dist/__tests__/cluster-detector.test.d.ts.map +1 -0
  13. package/dist/__tests__/cluster-detector.test.js +121 -0
  14. package/dist/__tests__/cluster-detector.test.js.map +1 -0
  15. package/dist/__tests__/contract.test.d.ts +2 -0
  16. package/dist/__tests__/contract.test.d.ts.map +1 -0
  17. package/dist/__tests__/contract.test.js +59 -0
  18. package/dist/__tests__/contract.test.js.map +1 -0
  19. package/dist/__tests__/enhanced-cohesion.test.js +12 -2
  20. package/dist/__tests__/enhanced-cohesion.test.js.map +1 -1
  21. package/dist/__tests__/file-classification.test.d.ts +2 -0
  22. package/dist/__tests__/file-classification.test.d.ts.map +1 -0
  23. package/dist/__tests__/file-classification.test.js +749 -0
  24. package/dist/__tests__/file-classification.test.js.map +1 -0
  25. package/dist/__tests__/fragmentation-advanced.test.js +2 -8
  26. package/dist/__tests__/fragmentation-advanced.test.js.map +1 -1
  27. package/dist/__tests__/fragmentation-coupling.test.js +2 -2
  28. package/dist/__tests__/fragmentation-coupling.test.js.map +1 -1
  29. package/dist/__tests__/fragmentation-log.test.js +3 -7
  30. package/dist/__tests__/fragmentation-log.test.js.map +1 -1
  31. package/dist/__tests__/provider.test.d.ts +2 -0
  32. package/dist/__tests__/provider.test.d.ts.map +1 -0
  33. package/dist/__tests__/provider.test.js +72 -0
  34. package/dist/__tests__/provider.test.js.map +1 -0
  35. package/dist/__tests__/remediation.test.d.ts +2 -0
  36. package/dist/__tests__/remediation.test.d.ts.map +1 -0
  37. package/dist/__tests__/remediation.test.js +61 -0
  38. package/dist/__tests__/remediation.test.js.map +1 -0
  39. package/dist/__tests__/scoring.test.js +196 -16
  40. package/dist/__tests__/scoring.test.js.map +1 -1
  41. package/dist/__tests__/structural-cohesion.test.js +8 -2
  42. package/dist/__tests__/structural-cohesion.test.js.map +1 -1
  43. package/dist/analyzer.d.ts +31 -94
  44. package/dist/analyzer.d.ts.map +1 -1
  45. package/dist/analyzer.js +260 -678
  46. package/dist/analyzer.js.map +1 -1
  47. package/dist/analyzers/python-context.d.ts.map +1 -1
  48. package/dist/analyzers/python-context.js +10 -8
  49. package/dist/analyzers/python-context.js.map +1 -1
  50. package/dist/ast-utils.d.ts +16 -0
  51. package/dist/ast-utils.d.ts.map +1 -0
  52. package/dist/ast-utils.js +81 -0
  53. package/dist/ast-utils.js.map +1 -0
  54. package/dist/chunk-64U3PNO3.mjs +94 -0
  55. package/dist/chunk-CDIVYADN.mjs +2110 -0
  56. package/dist/chunk-D3SIHB2V.mjs +2118 -0
  57. package/dist/chunk-FNPSK3CG.mjs +1760 -0
  58. package/dist/chunk-GXTGOLZT.mjs +92 -0
  59. package/dist/chunk-LERPI33Y.mjs +2060 -0
  60. package/dist/chunk-MZP3G7TF.mjs +2118 -0
  61. package/dist/chunk-NOHK5DLU.mjs +2173 -0
  62. package/dist/chunk-ORLC5Y4J.mjs +1787 -0
  63. package/dist/chunk-OTCQL7DY.mjs +2045 -0
  64. package/dist/chunk-OUYSZZ7X.mjs +1846 -0
  65. package/dist/chunk-SFK6XTJE.mjs +2110 -0
  66. package/dist/chunk-U5R2FTCR.mjs +1803 -0
  67. package/dist/chunk-UU4HZ7ZT.mjs +1849 -0
  68. package/dist/chunk-W2KNBN6W.mjs +1849 -0
  69. package/dist/chunk-WKOZOHOU.mjs +2060 -0
  70. package/dist/chunk-XIXAWCMS.mjs +1760 -0
  71. package/dist/classifier.d.ts +114 -0
  72. package/dist/classifier.d.ts.map +1 -0
  73. package/dist/classifier.js +439 -0
  74. package/dist/classifier.js.map +1 -0
  75. package/dist/cli.js +591 -1057
  76. package/dist/cli.js.map +1 -1
  77. package/dist/cli.mjs +63 -533
  78. package/dist/cluster-detector.d.ts +8 -0
  79. package/dist/cluster-detector.d.ts.map +1 -0
  80. package/dist/cluster-detector.js +70 -0
  81. package/dist/cluster-detector.js.map +1 -0
  82. package/dist/defaults.d.ts +7 -0
  83. package/dist/defaults.d.ts.map +1 -0
  84. package/dist/defaults.js +54 -0
  85. package/dist/defaults.js.map +1 -0
  86. package/dist/graph-builder.d.ts +33 -0
  87. package/dist/graph-builder.d.ts.map +1 -0
  88. package/dist/graph-builder.js +225 -0
  89. package/dist/graph-builder.js.map +1 -0
  90. package/dist/index.d.mts +24 -31
  91. package/dist/index.d.ts +24 -31
  92. package/dist/index.d.ts.map +1 -1
  93. package/dist/index.js +822 -588
  94. package/dist/index.js.map +1 -1
  95. package/dist/index.mjs +265 -8
  96. package/dist/metrics.d.ts +34 -0
  97. package/dist/metrics.d.ts.map +1 -0
  98. package/dist/metrics.js +170 -0
  99. package/dist/metrics.js.map +1 -0
  100. package/dist/provider.d.ts +6 -0
  101. package/dist/provider.d.ts.map +1 -0
  102. package/dist/provider.js +48 -0
  103. package/dist/provider.js.map +1 -0
  104. package/dist/python-context-3GZKN3LR.mjs +162 -0
  105. package/dist/python-context-O2EN3M6Z.mjs +162 -0
  106. package/dist/remediation.d.ts +25 -0
  107. package/dist/remediation.d.ts.map +1 -0
  108. package/dist/remediation.js +98 -0
  109. package/dist/remediation.js.map +1 -0
  110. package/dist/scoring.d.ts +3 -7
  111. package/dist/scoring.d.ts.map +1 -1
  112. package/dist/scoring.js +57 -48
  113. package/dist/scoring.js.map +1 -1
  114. package/dist/semantic-analysis.d.ts +12 -23
  115. package/dist/semantic-analysis.d.ts.map +1 -1
  116. package/dist/semantic-analysis.js +172 -110
  117. package/dist/semantic-analysis.js.map +1 -1
  118. package/dist/summary.d.ts +6 -0
  119. package/dist/summary.d.ts.map +1 -0
  120. package/dist/summary.js +92 -0
  121. package/dist/summary.js.map +1 -0
  122. package/dist/types.d.ts +9 -2
  123. package/dist/types.d.ts.map +1 -1
  124. package/dist/utils/output-formatter.d.ts +14 -0
  125. package/dist/utils/output-formatter.d.ts.map +1 -0
  126. package/dist/utils/output-formatter.js +338 -0
  127. package/dist/utils/output-formatter.js.map +1 -0
  128. package/package.json +2 -2
  129. package/src/__tests__/analyzer.test.ts +1 -1
  130. package/src/__tests__/auto-detection.test.ts +1 -1
  131. package/src/__tests__/contract.test.ts +1 -1
  132. package/src/__tests__/enhanced-cohesion.test.ts +1 -1
  133. package/src/__tests__/file-classification.test.ts +1 -1
  134. package/src/__tests__/fragmentation-advanced.test.ts +1 -1
  135. package/src/__tests__/fragmentation-coupling.test.ts +1 -1
  136. package/src/__tests__/fragmentation-log.test.ts +1 -1
  137. package/src/__tests__/provider.test.ts +1 -1
  138. package/src/__tests__/scoring.test.ts +217 -9
  139. package/src/__tests__/structural-cohesion.test.ts +1 -1
  140. package/src/analyzer.ts +96 -309
  141. package/src/analyzers/python-context.ts +7 -76
  142. package/src/cli-action.ts +103 -0
  143. package/src/cli.ts +12 -693
  144. package/src/cluster-detector.ts +1 -1
  145. package/src/graph-builder.ts +9 -85
  146. package/src/index.ts +6 -0
  147. package/src/issue-analyzer.ts +143 -0
  148. package/src/scoring.ts +40 -20
  149. package/src/semantic-analysis.ts +1 -14
  150. package/src/summary.ts +62 -106
  151. package/src/utils/dependency-graph-utils.ts +126 -0
  152. package/src/utils/output-formatter.ts +411 -0
  153. package/src/utils/string-utils.ts +17 -0
@@ -0,0 +1,749 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { classifyFile, adjustFragmentationForClassification, adjustCohesionForClassification, getClassificationRecommendations, } from '../analyzer';
3
+ describe('file classification', () => {
4
+ const createNode = (overrides) => ({
5
+ file: 'test.ts',
6
+ imports: [],
7
+ exports: [],
8
+ tokenCost: 100,
9
+ linesOfCode: 50,
10
+ ...overrides,
11
+ });
12
+ describe('classifyFile', () => {
13
+ it('should classify barrel export files correctly', () => {
14
+ const node = createNode({
15
+ file: 'src/index.ts',
16
+ imports: ['../module1', '../module2', '../module3'],
17
+ exports: [
18
+ { name: 'func1', type: 'function', inferredDomain: 'module1' },
19
+ { name: 'func2', type: 'function', inferredDomain: 'module2' },
20
+ { name: 'func3', type: 'function', inferredDomain: 'module3' },
21
+ ],
22
+ linesOfCode: 20, // Sparse code
23
+ });
24
+ const classification = classifyFile(node, 0.5, [
25
+ 'module1',
26
+ 'module2',
27
+ 'module3',
28
+ ]);
29
+ expect(classification).toBe('barrel-export');
30
+ });
31
+ it('should classify type definition files correctly', () => {
32
+ const node = createNode({
33
+ file: 'src/types.ts',
34
+ exports: [
35
+ { name: 'User', type: 'interface', inferredDomain: 'user' },
36
+ { name: 'Order', type: 'interface', inferredDomain: 'order' },
37
+ { name: 'Product', type: 'type', inferredDomain: 'product' },
38
+ { name: 'Status', type: 'type', inferredDomain: 'unknown' },
39
+ ],
40
+ linesOfCode: 100,
41
+ });
42
+ const classification = classifyFile(node, 0.5, [
43
+ 'user',
44
+ 'order',
45
+ 'product',
46
+ ]);
47
+ expect(classification).toBe('type-definition');
48
+ });
49
+ it('should classify files in /types/ directory as type-definition', () => {
50
+ const node = createNode({
51
+ file: 'shared/src/types/audit/parser.ts',
52
+ exports: [
53
+ {
54
+ name: 'AuditParserConfig',
55
+ type: 'interface',
56
+ inferredDomain: 'audit',
57
+ },
58
+ { name: 'ParseResult', type: 'type', inferredDomain: 'parse' },
59
+ ],
60
+ linesOfCode: 50,
61
+ });
62
+ const classification = classifyFile(node, 0.5, ['audit', 'parse']);
63
+ expect(classification).toBe('type-definition');
64
+ });
65
+ it('should classify files in nested /types/ subdirectories as type-definition', () => {
66
+ const node = createNode({
67
+ file: 'shared/src/types/audit/status.ts',
68
+ exports: [
69
+ { name: 'AuditStatus', type: 'type', inferredDomain: 'audit' },
70
+ {
71
+ name: 'StatusMapping',
72
+ type: 'interface',
73
+ inferredDomain: 'status',
74
+ },
75
+ ],
76
+ linesOfCode: 30,
77
+ });
78
+ const classification = classifyFile(node, 0.5, ['audit', 'status']);
79
+ expect(classification).toBe('type-definition');
80
+ });
81
+ it('should classify pure type files (only type/interface exports) as type-definition', () => {
82
+ const node = createNode({
83
+ file: 'src/models/user-models.ts', // NOT in /types/ but only type exports
84
+ exports: [
85
+ {
86
+ name: 'UserCreateInput',
87
+ type: 'interface',
88
+ inferredDomain: 'user',
89
+ },
90
+ {
91
+ name: 'UserUpdateInput',
92
+ type: 'interface',
93
+ inferredDomain: 'user',
94
+ },
95
+ { name: 'UserFilter', type: 'type', inferredDomain: 'user' },
96
+ ],
97
+ linesOfCode: 80,
98
+ });
99
+ const classification = classifyFile(node, 0.5, ['user']);
100
+ expect(classification).toBe('type-definition');
101
+ });
102
+ it('should classify cohesive module files correctly', () => {
103
+ const node = createNode({
104
+ file: 'src/calculator.ts',
105
+ exports: [
106
+ { name: 'calculate', type: 'function', inferredDomain: 'calc' },
107
+ { name: 'format', type: 'function', inferredDomain: 'calc' },
108
+ { name: 'validate', type: 'function', inferredDomain: 'calc' },
109
+ ],
110
+ imports: ['../utils'],
111
+ linesOfCode: 300,
112
+ });
113
+ const classification = classifyFile(node, 0.8, ['calc']);
114
+ expect(classification).toBe('cohesive-module');
115
+ });
116
+ it('should classify mixed concerns files correctly', () => {
117
+ const node = createNode({
118
+ file: 'src/audit.ts',
119
+ exports: [
120
+ { name: 'auditStatus', type: 'function', inferredDomain: 'audit' },
121
+ { name: 'createJob', type: 'function', inferredDomain: 'job' },
122
+ { name: 'LineItem', type: 'interface', inferredDomain: 'order' },
123
+ { name: 'SupportingDoc', type: 'type', inferredDomain: 'doc' },
124
+ ],
125
+ imports: ['../auth', '../job', '../order'],
126
+ linesOfCode: 384,
127
+ });
128
+ const classification = classifyFile(node, 0.3, [
129
+ 'audit',
130
+ 'job',
131
+ 'order',
132
+ 'doc',
133
+ ]);
134
+ expect(classification).toBe('mixed-concerns');
135
+ });
136
+ it('should classify files with multiple domains and very low cohesion as mixed-concerns', () => {
137
+ const node = createNode({
138
+ file: 'src/modules/mixed-module.ts', // NOT a utility/config/service path
139
+ exports: [
140
+ { name: 'DateCalculator', type: 'class', inferredDomain: 'date' }, // Use class to avoid utility detection
141
+ { name: 'ReportBuilder', type: 'class', inferredDomain: 'report' },
142
+ { name: 'AuditLogger', type: 'class', inferredDomain: 'audit' },
143
+ ],
144
+ imports: [],
145
+ linesOfCode: 150,
146
+ });
147
+ // Multiple domains + very low cohesion (< 0.4) = mixed concerns
148
+ // Note: NOT in /utils/ or /helpers/ or /services/ path
149
+ const classification = classifyFile(node, 0.3, [
150
+ 'date',
151
+ 'report',
152
+ 'audit',
153
+ ]);
154
+ expect(classification).toBe('mixed-concerns');
155
+ });
156
+ it('should classify single domain files as cohesive-module regardless of cohesion', () => {
157
+ const node = createNode({
158
+ file: 'src/component.ts',
159
+ exports: [
160
+ { name: 'Component', type: 'function', inferredDomain: 'ui' },
161
+ ],
162
+ imports: ['react'],
163
+ linesOfCode: 100,
164
+ });
165
+ // Single domain = cohesive module (even with medium cohesion)
166
+ const classification = classifyFile(node, 0.6, ['ui']);
167
+ expect(classification).toBe('cohesive-module');
168
+ });
169
+ it('should classify utility files as cohesive-module by design', () => {
170
+ const node = createNode({
171
+ file: 'src/utils/helpers.ts',
172
+ exports: [
173
+ { name: 'formatDate', type: 'function', inferredDomain: 'date' },
174
+ { name: 'stringifyJSON', type: 'function', inferredDomain: 'json' },
175
+ { name: 'validateEmail', type: 'function', inferredDomain: 'email' },
176
+ ],
177
+ imports: [],
178
+ linesOfCode: 150,
179
+ });
180
+ // Utility files are classified as cohesive by design
181
+ const classification = classifyFile(node, 0.4, ['date', 'json', 'email']);
182
+ expect(classification).toBe('utility-module');
183
+ });
184
+ it('should classify config/schema files as cohesive-module', () => {
185
+ const node = createNode({
186
+ file: 'src/db-schema.ts',
187
+ exports: [
188
+ { name: 'userTable', type: 'const', inferredDomain: 'db' },
189
+ { name: 'userSchema', type: 'const', inferredDomain: 'schema' },
190
+ ],
191
+ imports: ['../db'],
192
+ linesOfCode: 81,
193
+ });
194
+ // Config/schema files are classified as cohesive
195
+ const classification = classifyFile(node, 0.4, ['db', 'schema']);
196
+ expect(classification).toBe('cohesive-module');
197
+ });
198
+ });
199
+ describe('adjustFragmentationForClassification', () => {
200
+ it('should return 0 fragmentation for barrel exports', () => {
201
+ const result = adjustFragmentationForClassification(0.8, 'barrel-export');
202
+ expect(result).toBe(0);
203
+ });
204
+ it('should return 0 fragmentation for type definitions', () => {
205
+ const result = adjustFragmentationForClassification(0.9, 'type-definition');
206
+ expect(result).toBe(0);
207
+ });
208
+ it('should reduce fragmentation by 70% for cohesive modules', () => {
209
+ const result = adjustFragmentationForClassification(0.6, 'cohesive-module');
210
+ expect(result).toBeCloseTo(0.18, 2); // 0.6 * 0.3
211
+ });
212
+ it('should keep full fragmentation for mixed concerns', () => {
213
+ const result = adjustFragmentationForClassification(0.7, 'mixed-concerns');
214
+ expect(result).toBe(0.7);
215
+ });
216
+ it('should reduce fragmentation by 30% for unknown classification', () => {
217
+ const result = adjustFragmentationForClassification(0.5, 'unknown');
218
+ expect(result).toBeCloseTo(0.35, 2); // 0.5 * 0.7
219
+ });
220
+ });
221
+ describe('getClassificationRecommendations', () => {
222
+ it('should provide barrel export recommendations', () => {
223
+ const recommendations = getClassificationRecommendations('barrel-export', 'src/index.ts', ['High fragmentation']);
224
+ expect(recommendations).toContain('Barrel export file detected - multiple domains are expected here');
225
+ });
226
+ it('should provide type definition recommendations', () => {
227
+ const recommendations = getClassificationRecommendations('type-definition', 'src/types.ts', ['High fragmentation']);
228
+ expect(recommendations).toContain('Type definition file - centralized types improve consistency');
229
+ });
230
+ it('should provide cohesive module recommendations', () => {
231
+ const recommendations = getClassificationRecommendations('cohesive-module', 'src/calculator.ts', []);
232
+ expect(recommendations).toContain('Module has good cohesion despite its size');
233
+ });
234
+ it('should provide mixed concerns recommendations', () => {
235
+ const recommendations = getClassificationRecommendations('mixed-concerns', 'src/audit.ts', ['Multiple domains detected']);
236
+ expect(recommendations).toContain('Consider splitting this file by domain');
237
+ });
238
+ });
239
+ describe('integration: barrel export detection edge cases', () => {
240
+ it('should detect barrel export even for non-index files with re-export patterns', () => {
241
+ const node = createNode({
242
+ file: 'src/exports.ts',
243
+ imports: [
244
+ '../module1',
245
+ '../module2',
246
+ '../module3',
247
+ '../module4',
248
+ '../module5',
249
+ ],
250
+ exports: [
251
+ { name: 'a', type: 'function' },
252
+ { name: 'b', type: 'function' },
253
+ { name: 'c', type: 'function' },
254
+ { name: 'd', type: 'function' },
255
+ { name: 'e', type: 'function' },
256
+ ],
257
+ linesOfCode: 25, // Very sparse - mostly re-exports
258
+ });
259
+ const classification = classifyFile(node, 0.5, ['module1', 'module2']);
260
+ expect(classification).toBe('barrel-export');
261
+ });
262
+ it('should not misclassify large component files as barrel exports', () => {
263
+ const node = createNode({
264
+ file: 'src/components/Calculator.tsx', // NOT an index file
265
+ imports: ['react', '../hooks', '../utils'],
266
+ exports: [{ name: 'Calculator', type: 'function' }],
267
+ linesOfCode: 346, // Substantial code
268
+ });
269
+ // Single domain, high cohesion
270
+ const classification = classifyFile(node, 0.9, ['calculator']);
271
+ expect(classification).toBe('cohesive-module');
272
+ });
273
+ });
274
+ describe('new classification patterns', () => {
275
+ describe('lambda-handler detection', () => {
276
+ it('should classify files in handlers directory as lambda-handler', () => {
277
+ const node = createNode({
278
+ file: 'src/handlers/tier1-immediate.ts',
279
+ exports: [
280
+ { name: 'handler', type: 'function', inferredDomain: 'match' },
281
+ ],
282
+ imports: ['../services/matcher', '../services/db'],
283
+ linesOfCode: 150,
284
+ });
285
+ const classification = classifyFile(node, 0.21, ['match', 'db']);
286
+ expect(classification).toBe('lambda-handler');
287
+ });
288
+ it('should classify files with handler export as lambda-handler', () => {
289
+ const node = createNode({
290
+ file: 'src/api/process.ts',
291
+ exports: [
292
+ {
293
+ name: 'processHandler',
294
+ type: 'function',
295
+ inferredDomain: 'process',
296
+ },
297
+ ],
298
+ imports: ['../services/queue'],
299
+ linesOfCode: 80,
300
+ });
301
+ const classification = classifyFile(node, 0.3, ['process']);
302
+ expect(classification).toBe('lambda-handler');
303
+ });
304
+ it('should classify files in lambdas directory as lambda-handler', () => {
305
+ const node = createNode({
306
+ file: 'src/lambdas/match-single-document.ts',
307
+ exports: [
308
+ { name: 'handler', type: 'function', inferredDomain: 'match' },
309
+ ],
310
+ imports: ['../services/matcher'],
311
+ linesOfCode: 100,
312
+ });
313
+ const classification = classifyFile(node, 0.22, ['match']);
314
+ expect(classification).toBe('lambda-handler');
315
+ });
316
+ it('should classify single export functions as lambda-handler', () => {
317
+ const node = createNode({
318
+ file: 'src/functions/process.ts',
319
+ exports: [
320
+ { name: 'default', type: 'default', inferredDomain: 'process' },
321
+ ],
322
+ imports: ['../services/processor'],
323
+ linesOfCode: 60,
324
+ });
325
+ const classification = classifyFile(node, 0.4, ['process']);
326
+ expect(classification).toBe('lambda-handler');
327
+ });
328
+ });
329
+ describe('service-file detection', () => {
330
+ it('should classify files with -service.ts pattern as service-file', () => {
331
+ const node = createNode({
332
+ file: 'src/services/email-service.ts',
333
+ exports: [
334
+ { name: 'EmailService', type: 'class', inferredDomain: 'email' },
335
+ ],
336
+ imports: ['../utils/smtp', '../templates'],
337
+ linesOfCode: 200,
338
+ });
339
+ const classification = classifyFile(node, 0.07, [
340
+ 'email',
341
+ 'smtp',
342
+ 'templates',
343
+ ]);
344
+ expect(classification).toBe('service-file');
345
+ });
346
+ it('should classify files in services directory as service-file', () => {
347
+ const node = createNode({
348
+ file: 'src/services/notification.ts',
349
+ exports: [
350
+ {
351
+ name: 'sendNotification',
352
+ type: 'function',
353
+ inferredDomain: 'notification',
354
+ },
355
+ {
356
+ name: 'queueNotification',
357
+ type: 'function',
358
+ inferredDomain: 'notification',
359
+ },
360
+ ],
361
+ imports: ['../db', '../email'],
362
+ linesOfCode: 120,
363
+ });
364
+ const classification = classifyFile(node, 0.35, ['notification']);
365
+ expect(classification).toBe('service-file');
366
+ });
367
+ it('should classify class exports in services directory as service-file', () => {
368
+ const node = createNode({
369
+ file: 'src/api/user-service.ts',
370
+ exports: [
371
+ { name: 'UserService', type: 'class', inferredDomain: 'user' },
372
+ ],
373
+ imports: ['../db', '../auth'],
374
+ linesOfCode: 180,
375
+ });
376
+ const classification = classifyFile(node, 0.25, ['user']);
377
+ expect(classification).toBe('service-file');
378
+ });
379
+ });
380
+ describe('email-template detection', () => {
381
+ it('should classify receipt-writer files as email-template', () => {
382
+ const node = createNode({
383
+ file: 'src/emails/receipt-writer.ts',
384
+ exports: [
385
+ {
386
+ name: 'generateReceipt',
387
+ type: 'function',
388
+ inferredDomain: 'receipt',
389
+ },
390
+ ],
391
+ imports: ['../templates/base', '../services/db'],
392
+ linesOfCode: 150,
393
+ });
394
+ const classification = classifyFile(node, 0.08, [
395
+ 'receipt',
396
+ 'templates',
397
+ 'db',
398
+ ]);
399
+ expect(classification).toBe('email-template');
400
+ });
401
+ it('should classify files in emails directory as email-template', () => {
402
+ const node = createNode({
403
+ file: 'src/emails/welcome.ts',
404
+ exports: [
405
+ {
406
+ name: 'renderWelcomeEmail',
407
+ type: 'function',
408
+ inferredDomain: 'email',
409
+ },
410
+ ],
411
+ imports: ['../templates/layout'],
412
+ linesOfCode: 80,
413
+ });
414
+ const classification = classifyFile(node, 0.15, ['email']);
415
+ expect(classification).toBe('email-template');
416
+ });
417
+ it('should classify files with template patterns as email-template', () => {
418
+ const node = createNode({
419
+ file: 'src/templates/invoice-template.ts',
420
+ exports: [
421
+ {
422
+ name: 'renderInvoice',
423
+ type: 'function',
424
+ inferredDomain: 'invoice',
425
+ },
426
+ ],
427
+ imports: ['../services/pdf'],
428
+ linesOfCode: 100,
429
+ });
430
+ const classification = classifyFile(node, 0.2, ['invoice']);
431
+ expect(classification).toBe('email-template');
432
+ });
433
+ });
434
+ describe('parser-file detection', () => {
435
+ it('should classify parser files correctly', () => {
436
+ const node = createNode({
437
+ file: 'src/parsers/base-parser-deterministic.ts',
438
+ exports: [
439
+ {
440
+ name: 'parseDeterministic',
441
+ type: 'function',
442
+ inferredDomain: 'parse',
443
+ },
444
+ {
445
+ name: 'parseNonDeterministic',
446
+ type: 'function',
447
+ inferredDomain: 'parse',
448
+ },
449
+ ],
450
+ imports: ['../utils/transform'],
451
+ linesOfCode: 120,
452
+ });
453
+ const classification = classifyFile(node, 0.15, ['parse']);
454
+ expect(classification).toBe('parser-file');
455
+ });
456
+ it('should classify files with parser in name as parser-file', () => {
457
+ const node = createNode({
458
+ file: 'src/parsers/data-parser.ts',
459
+ exports: [
460
+ { name: 'parseData', type: 'function', inferredDomain: 'data' },
461
+ { name: 'transformData', type: 'function', inferredDomain: 'data' },
462
+ ],
463
+ imports: [],
464
+ linesOfCode: 90,
465
+ });
466
+ const classification = classifyFile(node, 0.25, ['data']);
467
+ expect(classification).toBe('parser-file');
468
+ });
469
+ it('should classify converter files as parser-file', () => {
470
+ const node = createNode({
471
+ file: 'src/converters/xml-converter.ts',
472
+ exports: [
473
+ {
474
+ name: 'convertXmlToJson',
475
+ type: 'function',
476
+ inferredDomain: 'xml',
477
+ },
478
+ {
479
+ name: 'convertJsonToXml',
480
+ type: 'function',
481
+ inferredDomain: 'xml',
482
+ },
483
+ ],
484
+ imports: ['xml2js'],
485
+ linesOfCode: 60,
486
+ });
487
+ const classification = classifyFile(node, 0.3, ['xml']);
488
+ expect(classification).toBe('parser-file');
489
+ });
490
+ });
491
+ describe('utility-module detection', () => {
492
+ it('should classify dynamodb-utils.ts as utility-module', () => {
493
+ const node = createNode({
494
+ file: 'src/utils/dynamodb-utils.ts',
495
+ exports: [
496
+ { name: 'getItem', type: 'function', inferredDomain: 'db' },
497
+ { name: 'putItem', type: 'function', inferredDomain: 'db' },
498
+ { name: 'queryItems', type: 'function', inferredDomain: 'db' },
499
+ ],
500
+ imports: ['aws-sdk'],
501
+ linesOfCode: 100,
502
+ });
503
+ const classification = classifyFile(node, 0.21, ['db']);
504
+ expect(classification).toBe('utility-module');
505
+ });
506
+ it('should classify s3-utils.ts as utility-module', () => {
507
+ const node = createNode({
508
+ file: 'src/utils/s3-utils.ts',
509
+ exports: [
510
+ { name: 'uploadFile', type: 'function', inferredDomain: 's3' },
511
+ { name: 'downloadFile', type: 'function', inferredDomain: 's3' },
512
+ { name: 'deleteFile', type: 'function', inferredDomain: 's3' },
513
+ ],
514
+ imports: ['aws-sdk'],
515
+ linesOfCode: 80,
516
+ });
517
+ const classification = classifyFile(node, 0.26, ['s3']);
518
+ expect(classification).toBe('utility-module');
519
+ });
520
+ it('should classify files ending with -utils.ts as utility-module', () => {
521
+ const node = createNode({
522
+ file: 'src/helpers/date-utils.ts',
523
+ exports: [
524
+ { name: 'formatDate', type: 'function', inferredDomain: 'date' },
525
+ { name: 'validateDate', type: 'function', inferredDomain: 'date' },
526
+ ],
527
+ imports: [],
528
+ linesOfCode: 50,
529
+ });
530
+ const classification = classifyFile(node, 0.3, ['date']);
531
+ expect(classification).toBe('utility-module');
532
+ });
533
+ });
534
+ describe('session file detection', () => {
535
+ it('should classify session.ts as cohesive-module', () => {
536
+ const node = createNode({
537
+ file: 'src/session.ts',
538
+ exports: [
539
+ {
540
+ name: 'createSession',
541
+ type: 'function',
542
+ inferredDomain: 'session',
543
+ },
544
+ { name: 'getSession', type: 'function', inferredDomain: 'session' },
545
+ {
546
+ name: 'destroySession',
547
+ type: 'function',
548
+ inferredDomain: 'session',
549
+ },
550
+ ],
551
+ imports: ['../db', '../auth'],
552
+ linesOfCode: 100,
553
+ });
554
+ const classification = classifyFile(node, 0.26, ['session']);
555
+ expect(classification).toBe('cohesive-module');
556
+ });
557
+ });
558
+ });
559
+ describe('adjustCohesionForClassification', () => {
560
+ it('should return 1 for barrel exports', () => {
561
+ const result = adjustCohesionForClassification(0.3, 'barrel-export');
562
+ expect(result).toBe(1);
563
+ });
564
+ it('should return 1 for type definitions', () => {
565
+ const result = adjustCohesionForClassification(0.2, 'type-definition');
566
+ expect(result).toBe(1);
567
+ });
568
+ it('should boost cohesion for utility modules', () => {
569
+ const result = adjustCohesionForClassification(0.21, 'utility-module');
570
+ expect(result).toBeGreaterThan(0.21);
571
+ expect(result).toBeLessThanOrEqual(1);
572
+ });
573
+ it('should boost cohesion for service files', () => {
574
+ const result = adjustCohesionForClassification(0.07, 'service-file');
575
+ expect(result).toBeGreaterThan(0.07);
576
+ expect(result).toBeLessThanOrEqual(1);
577
+ });
578
+ it('should boost cohesion for lambda handlers', () => {
579
+ const result = adjustCohesionForClassification(0.21, 'lambda-handler');
580
+ expect(result).toBeGreaterThan(0.21);
581
+ expect(result).toBeLessThanOrEqual(1);
582
+ });
583
+ it('should boost cohesion for email templates', () => {
584
+ const result = adjustCohesionForClassification(0.08, 'email-template');
585
+ expect(result).toBeGreaterThan(0.08);
586
+ expect(result).toBeLessThanOrEqual(1);
587
+ });
588
+ it('should boost cohesion for parser files', () => {
589
+ const result = adjustCohesionForClassification(0.15, 'parser-file');
590
+ expect(result).toBeGreaterThan(0.15);
591
+ expect(result).toBeLessThanOrEqual(1);
592
+ });
593
+ it('should keep original cohesion for mixed-concerns', () => {
594
+ const result = adjustCohesionForClassification(0.3, 'mixed-concerns');
595
+ expect(result).toBe(0.3);
596
+ });
597
+ it('should boost cohesion for utility module with related export names', () => {
598
+ const node = createNode({
599
+ file: 'src/utils/dynamodb-utils.ts',
600
+ exports: [
601
+ { name: 'getItem', type: 'function', inferredDomain: 'db' },
602
+ { name: 'putItem', type: 'function', inferredDomain: 'db' },
603
+ { name: 'deleteItem', type: 'function', inferredDomain: 'db' },
604
+ ],
605
+ imports: ['aws-sdk'],
606
+ linesOfCode: 100,
607
+ });
608
+ // Related names (all Item operations) should get higher boost
609
+ const result = adjustCohesionForClassification(0.21, 'utility-module', node);
610
+ expect(result).toBeGreaterThan(0.5); // Significant boost for related names
611
+ });
612
+ it('should boost cohesion for lambda handler with single entry point', () => {
613
+ const node = createNode({
614
+ file: 'src/handlers/process.ts',
615
+ exports: [
616
+ { name: 'handler', type: 'function', inferredDomain: 'process' },
617
+ ],
618
+ imports: ['../services/queue'],
619
+ linesOfCode: 80,
620
+ });
621
+ // Single entry point should get higher boost
622
+ const result = adjustCohesionForClassification(0.22, 'lambda-handler', node);
623
+ expect(result).toBeGreaterThan(0.5); // Significant boost for single entry
624
+ });
625
+ it('should boost cohesion for class-based service files', () => {
626
+ const node = createNode({
627
+ file: 'src/services/email-service.ts',
628
+ exports: [
629
+ { name: 'EmailService', type: 'class', inferredDomain: 'email' },
630
+ ],
631
+ imports: ['../utils/smtp'],
632
+ linesOfCode: 200,
633
+ });
634
+ // Class-based service should get higher boost
635
+ const result = adjustCohesionForClassification(0.15, 'service-file', node);
636
+ expect(result).toBeGreaterThan(0.45); // Significant boost for class-based
637
+ });
638
+ });
639
+ describe('new classification recommendations', () => {
640
+ it('should provide utility-module recommendations', () => {
641
+ const recommendations = getClassificationRecommendations('utility-module', 'src/utils/helpers.ts', ['Low cohesion']);
642
+ expect(recommendations).toContain('Utility module detected - multiple domains are acceptable here');
643
+ });
644
+ it('should provide service-file recommendations', () => {
645
+ const recommendations = getClassificationRecommendations('service-file', 'src/services/email.ts', ['Multiple domains']);
646
+ expect(recommendations).toContain('Service file detected - orchestration of multiple dependencies is expected');
647
+ });
648
+ it('should provide lambda-handler recommendations', () => {
649
+ const recommendations = getClassificationRecommendations('lambda-handler', 'src/handlers/process.ts', ['Low cohesion']);
650
+ expect(recommendations).toContain('Lambda handler detected - coordination of services is expected');
651
+ });
652
+ it('should provide email-template recommendations', () => {
653
+ const recommendations = getClassificationRecommendations('email-template', 'src/emails/receipt.ts', ['Multiple domains']);
654
+ expect(recommendations).toContain('Email template detected - references multiple domains for rendering');
655
+ });
656
+ it('should provide parser-file recommendations', () => {
657
+ const recommendations = getClassificationRecommendations('parser-file', 'src/parsers/data.ts', ['Multiple domains']);
658
+ expect(recommendations).toContain('Parser/transformer file detected - handles multiple data sources');
659
+ });
660
+ });
661
+ describe('Next.js page detection', () => {
662
+ it('should classify Next.js calculator pages as nextjs-page', () => {
663
+ const node = createNode({
664
+ file: 'app/cents-per-km-calculator/page.tsx',
665
+ exports: [
666
+ { name: 'metadata', type: 'const', inferredDomain: 'seo' },
667
+ { name: 'faqJsonLd', type: 'const', inferredDomain: 'jsonld' },
668
+ { name: 'default', type: 'default', inferredDomain: 'page' },
669
+ { name: 'icon', type: 'const', inferredDomain: 'ui' },
670
+ ],
671
+ imports: ['../components/Calculator', '../lib/utils'],
672
+ linesOfCode: 208,
673
+ });
674
+ const classification = classifyFile(node, 0.25, [
675
+ 'seo',
676
+ 'jsonld',
677
+ 'page',
678
+ 'ui',
679
+ ]);
680
+ expect(classification).toBe('nextjs-page');
681
+ });
682
+ it('should classify investment property calculator page as nextjs-page', () => {
683
+ const node = createNode({
684
+ file: 'app/investment-property-tax-calculator/page.tsx',
685
+ exports: [
686
+ { name: 'metadata', type: 'const', inferredDomain: 'seo' },
687
+ { name: 'faqJsonLd', type: 'const', inferredDomain: 'jsonld' },
688
+ { name: 'default', type: 'default', inferredDomain: 'page' },
689
+ ],
690
+ imports: ['../components/Form'],
691
+ linesOfCode: 204,
692
+ });
693
+ const classification = classifyFile(node, 0.3, ['seo', 'jsonld', 'page']);
694
+ expect(classification).toBe('nextjs-page');
695
+ });
696
+ it('should not classify non-page.tsx files in /app/ as nextjs-page', () => {
697
+ const node = createNode({
698
+ file: 'app/components/Header.tsx',
699
+ exports: [{ name: 'Header', type: 'function', inferredDomain: 'ui' }],
700
+ imports: ['react'],
701
+ linesOfCode: 50,
702
+ });
703
+ const classification = classifyFile(node, 0.8, ['ui']);
704
+ expect(classification).toBe('cohesive-module');
705
+ });
706
+ it('should not classify page.tsx files outside /app/ as nextjs-page', () => {
707
+ const node = createNode({
708
+ file: 'src/pages/page.tsx', // Pages Router, not App Router
709
+ exports: [{ name: 'default', type: 'default', inferredDomain: 'page' }],
710
+ imports: ['react'],
711
+ linesOfCode: 100,
712
+ });
713
+ const classification = classifyFile(node, 0.5, ['page']);
714
+ expect(classification).not.toBe('nextjs-page');
715
+ });
716
+ it('should classify Next.js page with generateMetadata as nextjs-page', () => {
717
+ const node = createNode({
718
+ file: 'app/dynamic-page/page.tsx',
719
+ exports: [
720
+ { name: 'generateMetadata', type: 'function', inferredDomain: 'seo' },
721
+ { name: 'default', type: 'default', inferredDomain: 'page' },
722
+ ],
723
+ imports: ['../lib/api'],
724
+ linesOfCode: 150,
725
+ });
726
+ const classification = classifyFile(node, 0.4, ['seo', 'page']);
727
+ expect(classification).toBe('nextjs-page');
728
+ });
729
+ });
730
+ describe('nextjs-page cohesion adjustment', () => {
731
+ it('should return 1 for nextjs-page', () => {
732
+ const result = adjustCohesionForClassification(0.25, 'nextjs-page');
733
+ expect(result).toBe(1);
734
+ });
735
+ });
736
+ describe('nextjs-page fragmentation adjustment', () => {
737
+ it('should reduce fragmentation by 80% for nextjs-page', () => {
738
+ const result = adjustFragmentationForClassification(0.5, 'nextjs-page');
739
+ expect(result).toBe(0.1); // 0.5 * 0.2
740
+ });
741
+ });
742
+ describe('nextjs-page recommendations', () => {
743
+ it('should provide nextjs-page recommendations', () => {
744
+ const recommendations = getClassificationRecommendations('nextjs-page', 'app/calculator/page.tsx', ['Low cohesion']);
745
+ expect(recommendations).toContain('Next.js App Router page detected - metadata/JSON-LD/component pattern is cohesive');
746
+ });
747
+ });
748
+ });
749
+ //# sourceMappingURL=file-classification.test.js.map