@aiready/testability 0.2.3 → 0.2.5

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.
@@ -1,6 +1,6 @@
1
1
 
2
2
  
3
- > @aiready/testability@0.2.2 build /Users/pengcao/projects/aiready/packages/testability
3
+ > @aiready/testability@0.2.4 build /Users/pengcao/projects/aiready/packages/testability
4
4
  > tsup src/index.ts src/cli.ts --format cjs,esm --dts
5
5
 
6
6
  CLI Building entry: src/cli.ts, src/index.ts
@@ -9,15 +9,15 @@
9
9
  CLI Target: es2020
10
10
  CJS Build start
11
11
  ESM Build start
12
- CJS dist/index.js 12.28 KB
13
- CJS dist/cli.js 18.38 KB
14
- CJS ⚡️ Build success in 38ms
15
- ESM dist/chunk-7MM2QISW.mjs 11.02 KB
16
- ESM dist/index.mjs 152.00 B
17
12
  ESM dist/cli.mjs 5.75 KB
18
- ESM ⚡️ Build success in 38ms
13
+ ESM dist/chunk-ULOUVO3Q.mjs 11.02 KB
14
+ ESM dist/index.mjs 152.00 B
15
+ ESM ⚡️ Build success in 133ms
16
+ CJS dist/cli.js 18.38 KB
17
+ CJS dist/index.js 12.28 KB
18
+ CJS ⚡️ Build success in 136ms
19
19
  DTS Build start
20
- DTS ⚡️ Build success in 2233ms
20
+ DTS ⚡️ Build success in 4086ms
21
21
  DTS dist/cli.d.ts 20.00 B
22
22
  DTS dist/index.d.ts 2.66 KB
23
23
  DTS dist/cli.d.mts 20.00 B
@@ -1,16 +1,16 @@
1
1
 
2
2
  
3
- > @aiready/testability@0.2.2 test /Users/pengcao/projects/aiready/packages/testability
3
+ > @aiready/testability@0.2.4 test /Users/pengcao/projects/aiready/packages/testability
4
4
  > vitest run
5
5
 
6
6
  [?25l
7
7
   RUN  v4.0.18 /Users/pengcao/projects/aiready/packages/testability
8
8
 
9
- ✓ src/__tests__/analyzer.test.ts (3 tests) 217ms
9
+ ✓ src/__tests__/analyzer.test.ts (3 tests) 200ms
10
10
 
11
11
   Test Files  1 passed (1)
12
12
   Tests  3 passed (3)
13
-  Start at  20:01:55
14
-  Duration  2.14s (transform 162ms, setup 0ms, import 1.21s, tests 217ms, environment 0ms)
13
+  Start at  21:39:02
14
+  Duration  1.24s (transform 226ms, setup 0ms, import 767ms, tests 200ms, environment 0ms)
15
15
 
16
16
  [?25h
@@ -0,0 +1,338 @@
1
+ // src/analyzer.ts
2
+ import {
3
+ scanFiles,
4
+ calculateTestabilityIndex,
5
+ Severity,
6
+ IssueType
7
+ } from "@aiready/core";
8
+ import { readFileSync, existsSync } from "fs";
9
+ import { join } from "path";
10
+ import { parse } from "@typescript-eslint/typescript-estree";
11
+ function countMethodsInInterface(node) {
12
+ if (node.type === "TSInterfaceDeclaration") {
13
+ return node.body.body.filter(
14
+ (m) => m.type === "TSMethodSignature" || m.type === "TSPropertySignature"
15
+ ).length;
16
+ }
17
+ if (node.type === "TSTypeAliasDeclaration" && node.typeAnnotation.type === "TSTypeLiteral") {
18
+ return node.typeAnnotation.members.length;
19
+ }
20
+ return 0;
21
+ }
22
+ function hasDependencyInjection(node) {
23
+ for (const member of node.body.body) {
24
+ if (member.type === "MethodDefinition" && member.key.type === "Identifier" && member.key.name === "constructor") {
25
+ const fn = member.value;
26
+ if (fn.params && fn.params.length > 0) {
27
+ const typedParams = fn.params.filter((p) => {
28
+ const param = p;
29
+ return param.typeAnnotation != null || param.parameter?.typeAnnotation != null;
30
+ });
31
+ if (typedParams.length > 0) return true;
32
+ }
33
+ }
34
+ }
35
+ return false;
36
+ }
37
+ function isPureFunction(fn) {
38
+ let hasReturn = false;
39
+ let hasSideEffect = false;
40
+ function walk(node) {
41
+ if (node.type === "ReturnStatement" && node.argument) hasReturn = true;
42
+ if (node.type === "AssignmentExpression" && node.left.type === "MemberExpression")
43
+ hasSideEffect = true;
44
+ if (node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.object.type === "Identifier" && ["console", "process", "window", "document", "fs"].includes(
45
+ node.callee.object.name
46
+ ))
47
+ hasSideEffect = true;
48
+ for (const key of Object.keys(node)) {
49
+ if (key === "parent") continue;
50
+ const child = node[key];
51
+ if (child && typeof child === "object") {
52
+ if (Array.isArray(child)) {
53
+ child.forEach((c) => c?.type && walk(c));
54
+ } else if (child.type) {
55
+ walk(child);
56
+ }
57
+ }
58
+ }
59
+ }
60
+ if (fn.body?.type === "BlockStatement") {
61
+ fn.body.body.forEach((s) => walk(s));
62
+ } else if (fn.body) {
63
+ hasReturn = true;
64
+ }
65
+ return hasReturn && !hasSideEffect;
66
+ }
67
+ function hasExternalStateMutation(fn) {
68
+ let found = false;
69
+ function walk(node) {
70
+ if (found) return;
71
+ if (node.type === "AssignmentExpression" && node.left.type === "MemberExpression")
72
+ found = true;
73
+ for (const key of Object.keys(node)) {
74
+ if (key === "parent") continue;
75
+ const child = node[key];
76
+ if (child && typeof child === "object") {
77
+ if (Array.isArray(child)) child.forEach((c) => c?.type && walk(c));
78
+ else if (child.type) walk(child);
79
+ }
80
+ }
81
+ }
82
+ if (fn.body?.type === "BlockStatement") fn.body.body.forEach((s) => walk(s));
83
+ return found;
84
+ }
85
+ function analyzeFileTestability(filePath) {
86
+ const result = {
87
+ pureFunctions: 0,
88
+ totalFunctions: 0,
89
+ injectionPatterns: 0,
90
+ totalClasses: 0,
91
+ bloatedInterfaces: 0,
92
+ totalInterfaces: 0,
93
+ externalStateMutations: 0
94
+ };
95
+ let code;
96
+ try {
97
+ code = readFileSync(filePath, "utf-8");
98
+ } catch {
99
+ return result;
100
+ }
101
+ let ast;
102
+ try {
103
+ ast = parse(code, {
104
+ jsx: filePath.endsWith(".tsx") || filePath.endsWith(".jsx"),
105
+ range: false,
106
+ loc: false
107
+ });
108
+ } catch {
109
+ return result;
110
+ }
111
+ function visit(node) {
112
+ if (node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
113
+ result.totalFunctions++;
114
+ if (isPureFunction(node)) result.pureFunctions++;
115
+ if (hasExternalStateMutation(node)) result.externalStateMutations++;
116
+ }
117
+ if (node.type === "ClassDeclaration" || node.type === "ClassExpression") {
118
+ result.totalClasses++;
119
+ if (hasDependencyInjection(node)) result.injectionPatterns++;
120
+ }
121
+ if (node.type === "TSInterfaceDeclaration" || node.type === "TSTypeAliasDeclaration") {
122
+ result.totalInterfaces++;
123
+ const methodCount = countMethodsInInterface(node);
124
+ if (methodCount > 10) result.bloatedInterfaces++;
125
+ }
126
+ for (const key of Object.keys(node)) {
127
+ if (key === "parent") continue;
128
+ const child = node[key];
129
+ if (child && typeof child === "object") {
130
+ if (Array.isArray(child)) child.forEach((c) => c?.type && visit(c));
131
+ else if (child.type) visit(child);
132
+ }
133
+ }
134
+ }
135
+ ast.body.forEach(visit);
136
+ return result;
137
+ }
138
+ function detectTestFramework(rootDir) {
139
+ const pkgPath = join(rootDir, "package.json");
140
+ if (!existsSync(pkgPath)) return false;
141
+ try {
142
+ const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
143
+ const allDeps = {
144
+ ...pkg.dependencies ?? {},
145
+ ...pkg.devDependencies ?? {}
146
+ };
147
+ const testFrameworks = [
148
+ "jest",
149
+ "vitest",
150
+ "mocha",
151
+ "jasmine",
152
+ "ava",
153
+ "tap",
154
+ "pytest",
155
+ "unittest"
156
+ ];
157
+ return testFrameworks.some((fw) => allDeps[fw]);
158
+ } catch {
159
+ return false;
160
+ }
161
+ }
162
+ var TEST_PATTERNS = [
163
+ /\.(test|spec)\.(ts|tsx|js|jsx)$/,
164
+ /__tests__\//,
165
+ /\/tests?\//,
166
+ /\/e2e\//,
167
+ /\/fixtures\//
168
+ ];
169
+ function isTestFile(filePath, extra) {
170
+ if (TEST_PATTERNS.some((p) => p.test(filePath))) return true;
171
+ if (extra) return extra.some((p) => filePath.includes(p));
172
+ return false;
173
+ }
174
+ async function analyzeTestability(options) {
175
+ const allFiles = await scanFiles({
176
+ ...options,
177
+ include: options.include || ["**/*.{ts,tsx,js,jsx}"],
178
+ includeTests: true
179
+ });
180
+ const sourceFiles = allFiles.filter(
181
+ (f) => !isTestFile(f, options.testPatterns)
182
+ );
183
+ const testFiles = allFiles.filter((f) => isTestFile(f, options.testPatterns));
184
+ const aggregated = {
185
+ pureFunctions: 0,
186
+ totalFunctions: 0,
187
+ injectionPatterns: 0,
188
+ totalClasses: 0,
189
+ bloatedInterfaces: 0,
190
+ totalInterfaces: 0,
191
+ externalStateMutations: 0
192
+ };
193
+ let processed = 0;
194
+ for (const f of sourceFiles) {
195
+ processed++;
196
+ options.onProgress?.(
197
+ processed,
198
+ sourceFiles.length,
199
+ `testability: analyzing files`
200
+ );
201
+ const a = analyzeFileTestability(f);
202
+ for (const key of Object.keys(aggregated)) {
203
+ aggregated[key] += a[key];
204
+ }
205
+ }
206
+ const hasTestFramework = detectTestFramework(options.rootDir);
207
+ const indexResult = calculateTestabilityIndex({
208
+ testFiles: testFiles.length,
209
+ sourceFiles: sourceFiles.length,
210
+ pureFunctions: aggregated.pureFunctions,
211
+ totalFunctions: Math.max(1, aggregated.totalFunctions),
212
+ injectionPatterns: aggregated.injectionPatterns,
213
+ totalClasses: Math.max(1, aggregated.totalClasses),
214
+ bloatedInterfaces: aggregated.bloatedInterfaces,
215
+ totalInterfaces: Math.max(1, aggregated.totalInterfaces),
216
+ externalStateMutations: aggregated.externalStateMutations,
217
+ hasTestFramework
218
+ });
219
+ const issues = [];
220
+ const minCoverage = options.minCoverageRatio ?? 0.3;
221
+ const actualRatio = sourceFiles.length > 0 ? testFiles.length / sourceFiles.length : 0;
222
+ if (!hasTestFramework) {
223
+ issues.push({
224
+ type: IssueType.LowTestability,
225
+ dimension: "framework",
226
+ severity: Severity.Critical,
227
+ message: "No testing framework detected in package.json \u2014 AI changes cannot be verified at all.",
228
+ location: { file: options.rootDir, line: 0 },
229
+ suggestion: "Add Jest, Vitest, or another testing framework as a devDependency."
230
+ });
231
+ }
232
+ if (actualRatio < minCoverage) {
233
+ const needed = Math.ceil(sourceFiles.length * minCoverage) - testFiles.length;
234
+ issues.push({
235
+ type: IssueType.LowTestability,
236
+ dimension: "test-coverage",
237
+ severity: actualRatio === 0 ? Severity.Critical : Severity.Major,
238
+ message: `Test ratio is ${Math.round(actualRatio * 100)}% (${testFiles.length} test files for ${sourceFiles.length} source files). Need at least ${Math.round(minCoverage * 100)}%.`,
239
+ location: { file: options.rootDir, line: 0 },
240
+ suggestion: `Add ~${needed} test file(s) to reach the ${Math.round(minCoverage * 100)}% minimum for safe AI assistance.`
241
+ });
242
+ }
243
+ if (indexResult.dimensions.purityScore < 50) {
244
+ issues.push({
245
+ type: IssueType.LowTestability,
246
+ dimension: "purity",
247
+ severity: Severity.Major,
248
+ message: `Only ${indexResult.dimensions.purityScore}% of functions are pure \u2014 side-effectful functions require complex test setup.`,
249
+ location: { file: options.rootDir, line: 0 },
250
+ suggestion: "Extract pure transformation logic from I/O and mutation code."
251
+ });
252
+ }
253
+ if (indexResult.dimensions.observabilityScore < 50) {
254
+ issues.push({
255
+ type: IssueType.LowTestability,
256
+ dimension: "observability",
257
+ severity: Severity.Major,
258
+ message: `Many functions mutate external state directly \u2014 outputs are invisible to unit tests.`,
259
+ location: { file: options.rootDir, line: 0 },
260
+ suggestion: "Prefer returning values over mutating shared state."
261
+ });
262
+ }
263
+ return {
264
+ summary: {
265
+ sourceFiles: sourceFiles.length,
266
+ testFiles: testFiles.length,
267
+ coverageRatio: Math.round(actualRatio * 100) / 100,
268
+ score: indexResult.score,
269
+ rating: indexResult.rating,
270
+ aiChangeSafetyRating: indexResult.aiChangeSafetyRating,
271
+ dimensions: indexResult.dimensions
272
+ },
273
+ issues,
274
+ rawData: {
275
+ sourceFiles: sourceFiles.length,
276
+ testFiles: testFiles.length,
277
+ ...aggregated,
278
+ hasTestFramework
279
+ },
280
+ recommendations: indexResult.recommendations
281
+ };
282
+ }
283
+
284
+ // src/scoring.ts
285
+ function calculateTestabilityScore(report) {
286
+ const { summary, rawData, recommendations } = report;
287
+ const factors = [
288
+ {
289
+ name: "Test Coverage",
290
+ impact: Math.round(summary.dimensions.testCoverageRatio - 50),
291
+ description: `${rawData.testFiles} test files / ${rawData.sourceFiles} source files (${Math.round(summary.coverageRatio * 100)}%)`
292
+ },
293
+ {
294
+ name: "Function Purity",
295
+ impact: Math.round(summary.dimensions.purityScore - 50),
296
+ description: `${rawData.pureFunctions}/${rawData.totalFunctions} functions are pure`
297
+ },
298
+ {
299
+ name: "Dependency Injection",
300
+ impact: Math.round(summary.dimensions.dependencyInjectionScore - 50),
301
+ description: `${rawData.injectionPatterns}/${rawData.totalClasses} classes use DI`
302
+ },
303
+ {
304
+ name: "Interface Focus",
305
+ impact: Math.round(summary.dimensions.interfaceFocusScore - 50),
306
+ description: `${rawData.bloatedInterfaces} interfaces have >10 methods`
307
+ },
308
+ {
309
+ name: "Observability",
310
+ impact: Math.round(summary.dimensions.observabilityScore - 50),
311
+ description: `${rawData.externalStateMutations} functions mutate external state`
312
+ }
313
+ ];
314
+ const recs = recommendations.map(
315
+ (action) => ({
316
+ action,
317
+ estimatedImpact: summary.aiChangeSafetyRating === "blind-risk" ? 15 : 8,
318
+ priority: summary.aiChangeSafetyRating === "blind-risk" || summary.aiChangeSafetyRating === "high-risk" ? "high" : "medium"
319
+ })
320
+ );
321
+ return {
322
+ toolName: "testability",
323
+ score: summary.score,
324
+ rawMetrics: {
325
+ ...rawData,
326
+ rating: summary.rating,
327
+ aiChangeSafetyRating: summary.aiChangeSafetyRating,
328
+ coverageRatio: summary.coverageRatio
329
+ },
330
+ factors,
331
+ recommendations: recs
332
+ };
333
+ }
334
+
335
+ export {
336
+ analyzeTestability,
337
+ calculateTestabilityScore
338
+ };
package/dist/cli.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  import {
3
3
  analyzeTestability,
4
4
  calculateTestabilityScore
5
- } from "./chunk-7MM2QISW.mjs";
5
+ } from "./chunk-ULOUVO3Q.mjs";
6
6
 
7
7
  // src/cli.ts
8
8
  import { Command } from "commander";
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  analyzeTestability,
3
3
  calculateTestabilityScore
4
- } from "./chunk-7MM2QISW.mjs";
4
+ } from "./chunk-ULOUVO3Q.mjs";
5
5
  export {
6
6
  analyzeTestability,
7
7
  calculateTestabilityScore
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiready/testability",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "Measures how safely and verifiably AI-generated changes can be made to your codebase",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -40,7 +40,7 @@
40
40
  "chalk": "^5.3.0",
41
41
  "commander": "^14.0.0",
42
42
  "glob": "^13.0.0",
43
- "@aiready/core": "0.19.3"
43
+ "@aiready/core": "0.19.5"
44
44
  },
45
45
  "devDependencies": {
46
46
  "@types/node": "^24.0.0",
package/src/analyzer.ts CHANGED
@@ -10,7 +10,12 @@
10
10
  * 5. Observability (return values vs. external state mutations)
11
11
  */
12
12
 
13
- import { scanFiles, calculateTestabilityIndex, Severity, IssueType } from '@aiready/core';
13
+ import {
14
+ scanFiles,
15
+ calculateTestabilityIndex,
16
+ Severity,
17
+ IssueType,
18
+ } from '@aiready/core';
14
19
  import { readFileSync, existsSync } from 'fs';
15
20
  import { join } from 'path';
16
21
  import { parse } from '@typescript-eslint/typescript-estree';