@theihtisham/ai-testgen 1.0.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.
Files changed (149) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +383 -0
  3. package/dist/analyzers/analyzer.d.ts +10 -0
  4. package/dist/analyzers/analyzer.d.ts.map +1 -0
  5. package/dist/analyzers/analyzer.js +131 -0
  6. package/dist/analyzers/analyzer.js.map +1 -0
  7. package/dist/analyzers/go-analyzer.d.ts +3 -0
  8. package/dist/analyzers/go-analyzer.d.ts.map +1 -0
  9. package/dist/analyzers/go-analyzer.js +244 -0
  10. package/dist/analyzers/go-analyzer.js.map +1 -0
  11. package/dist/analyzers/index.d.ts +5 -0
  12. package/dist/analyzers/index.d.ts.map +1 -0
  13. package/dist/analyzers/index.js +15 -0
  14. package/dist/analyzers/index.js.map +1 -0
  15. package/dist/analyzers/js-ts-analyzer.d.ts +3 -0
  16. package/dist/analyzers/js-ts-analyzer.d.ts.map +1 -0
  17. package/dist/analyzers/js-ts-analyzer.js +299 -0
  18. package/dist/analyzers/js-ts-analyzer.js.map +1 -0
  19. package/dist/analyzers/python-analyzer.d.ts +3 -0
  20. package/dist/analyzers/python-analyzer.d.ts.map +1 -0
  21. package/dist/analyzers/python-analyzer.js +306 -0
  22. package/dist/analyzers/python-analyzer.js.map +1 -0
  23. package/dist/cli.d.ts +3 -0
  24. package/dist/cli.d.ts.map +1 -0
  25. package/dist/cli.js +381 -0
  26. package/dist/cli.js.map +1 -0
  27. package/dist/config/defaults.d.ts +6 -0
  28. package/dist/config/defaults.d.ts.map +1 -0
  29. package/dist/config/defaults.js +80 -0
  30. package/dist/config/defaults.js.map +1 -0
  31. package/dist/config/index.d.ts +3 -0
  32. package/dist/config/index.d.ts.map +1 -0
  33. package/dist/config/index.js +14 -0
  34. package/dist/config/index.js.map +1 -0
  35. package/dist/config/loader.d.ts +6 -0
  36. package/dist/config/loader.d.ts.map +1 -0
  37. package/dist/config/loader.js +126 -0
  38. package/dist/config/loader.js.map +1 -0
  39. package/dist/coverage.d.ts +4 -0
  40. package/dist/coverage.d.ts.map +1 -0
  41. package/dist/coverage.js +108 -0
  42. package/dist/coverage.js.map +1 -0
  43. package/dist/generators/ai-generator.d.ts +4 -0
  44. package/dist/generators/ai-generator.d.ts.map +1 -0
  45. package/dist/generators/ai-generator.js +175 -0
  46. package/dist/generators/ai-generator.js.map +1 -0
  47. package/dist/generators/generator.d.ts +4 -0
  48. package/dist/generators/generator.d.ts.map +1 -0
  49. package/dist/generators/generator.js +121 -0
  50. package/dist/generators/generator.js.map +1 -0
  51. package/dist/generators/go-generator.d.ts +3 -0
  52. package/dist/generators/go-generator.d.ts.map +1 -0
  53. package/dist/generators/go-generator.js +175 -0
  54. package/dist/generators/go-generator.js.map +1 -0
  55. package/dist/generators/index.d.ts +6 -0
  56. package/dist/generators/index.d.ts.map +1 -0
  57. package/dist/generators/index.js +16 -0
  58. package/dist/generators/index.js.map +1 -0
  59. package/dist/generators/js-ts-generator.d.ts +3 -0
  60. package/dist/generators/js-ts-generator.d.ts.map +1 -0
  61. package/dist/generators/js-ts-generator.js +331 -0
  62. package/dist/generators/js-ts-generator.js.map +1 -0
  63. package/dist/generators/python-generator.d.ts +3 -0
  64. package/dist/generators/python-generator.d.ts.map +1 -0
  65. package/dist/generators/python-generator.js +180 -0
  66. package/dist/generators/python-generator.js.map +1 -0
  67. package/dist/incremental.d.ts +16 -0
  68. package/dist/incremental.d.ts.map +1 -0
  69. package/dist/incremental.js +146 -0
  70. package/dist/incremental.js.map +1 -0
  71. package/dist/index.d.ts +9 -0
  72. package/dist/index.d.ts.map +1 -0
  73. package/dist/index.js +44 -0
  74. package/dist/index.js.map +1 -0
  75. package/dist/mutation/index.d.ts +2 -0
  76. package/dist/mutation/index.d.ts.map +1 -0
  77. package/dist/mutation/index.js +9 -0
  78. package/dist/mutation/index.js.map +1 -0
  79. package/dist/mutation/mutator.d.ts +6 -0
  80. package/dist/mutation/mutator.d.ts.map +1 -0
  81. package/dist/mutation/mutator.js +237 -0
  82. package/dist/mutation/mutator.js.map +1 -0
  83. package/dist/types.d.ts +199 -0
  84. package/dist/types.d.ts.map +1 -0
  85. package/dist/types.js +4 -0
  86. package/dist/types.js.map +1 -0
  87. package/dist/utils/file.d.ts +10 -0
  88. package/dist/utils/file.d.ts.map +1 -0
  89. package/dist/utils/file.js +108 -0
  90. package/dist/utils/file.js.map +1 -0
  91. package/dist/utils/index.d.ts +4 -0
  92. package/dist/utils/index.d.ts.map +1 -0
  93. package/dist/utils/index.js +24 -0
  94. package/dist/utils/index.js.map +1 -0
  95. package/dist/utils/language.d.ts +8 -0
  96. package/dist/utils/language.d.ts.map +1 -0
  97. package/dist/utils/language.js +137 -0
  98. package/dist/utils/language.js.map +1 -0
  99. package/dist/utils/logger.d.ts +13 -0
  100. package/dist/utils/logger.d.ts.map +1 -0
  101. package/dist/utils/logger.js +57 -0
  102. package/dist/utils/logger.js.map +1 -0
  103. package/dist/watcher/index.d.ts +2 -0
  104. package/dist/watcher/index.d.ts.map +1 -0
  105. package/dist/watcher/index.js +6 -0
  106. package/dist/watcher/index.js.map +1 -0
  107. package/dist/watcher/watcher.d.ts +19 -0
  108. package/dist/watcher/watcher.d.ts.map +1 -0
  109. package/dist/watcher/watcher.js +122 -0
  110. package/dist/watcher/watcher.js.map +1 -0
  111. package/package.json +63 -0
  112. package/src/analyzers/analyzer.ts +180 -0
  113. package/src/analyzers/go-analyzer.ts +235 -0
  114. package/src/analyzers/index.ts +4 -0
  115. package/src/analyzers/js-ts-analyzer.ts +324 -0
  116. package/src/analyzers/python-analyzer.ts +306 -0
  117. package/src/cli.ts +416 -0
  118. package/src/config/defaults.ts +81 -0
  119. package/src/config/index.ts +2 -0
  120. package/src/config/loader.ts +114 -0
  121. package/src/coverage.ts +128 -0
  122. package/src/generators/ai-generator.ts +170 -0
  123. package/src/generators/generator.ts +117 -0
  124. package/src/generators/go-generator.ts +183 -0
  125. package/src/generators/index.ts +5 -0
  126. package/src/generators/js-ts-generator.ts +379 -0
  127. package/src/generators/python-generator.ts +201 -0
  128. package/src/incremental.ts +131 -0
  129. package/src/index.ts +8 -0
  130. package/src/mutation/index.ts +1 -0
  131. package/src/mutation/mutator.ts +314 -0
  132. package/src/types.ts +240 -0
  133. package/src/utils/file.ts +73 -0
  134. package/src/utils/index.ts +3 -0
  135. package/src/utils/language.ts +114 -0
  136. package/src/utils/logger.ts +61 -0
  137. package/src/watcher/index.ts +1 -0
  138. package/src/watcher/watcher.ts +103 -0
  139. package/tests/analyzer.test.ts +429 -0
  140. package/tests/config.test.ts +171 -0
  141. package/tests/coverage.test.ts +197 -0
  142. package/tests/file-utils.test.ts +121 -0
  143. package/tests/generators.test.ts +383 -0
  144. package/tests/incremental.test.ts +108 -0
  145. package/tests/language.test.ts +90 -0
  146. package/tests/mutation.test.ts +286 -0
  147. package/tests/watcher.test.ts +35 -0
  148. package/tsconfig.json +26 -0
  149. package/vitest.config.ts +25 -0
@@ -0,0 +1,286 @@
1
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+ import * as fs from 'fs';
3
+ import * as path from 'path';
4
+ import * as os from 'os';
5
+ import {
6
+ generateMutants,
7
+ applyMutant,
8
+ calculateMutationScore,
9
+ } from '../src/mutation/mutator.js';
10
+
11
+ describe('Mutation Testing', () => {
12
+ let tempDir: string;
13
+
14
+ beforeEach(() => {
15
+ tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'testgen-mutation-'));
16
+ });
17
+
18
+ afterEach(() => {
19
+ fs.rmSync(tempDir, { recursive: true, force: true });
20
+ });
21
+
22
+ describe('generateMutants', () => {
23
+ it('generates arithmetic operator mutants for TypeScript', () => {
24
+ const filePath = path.join(tempDir, 'math.ts');
25
+ fs.writeFileSync(filePath, `
26
+ export function add(a: number, b: number): number {
27
+ return a + b;
28
+ }
29
+ `);
30
+ const mutants = generateMutants(filePath, {
31
+ filePath,
32
+ language: 'typescript',
33
+ functions: [],
34
+ classes: [],
35
+ interfaces: [],
36
+ exports: [],
37
+ imports: [],
38
+ dependencies: [],
39
+ linesOfCode: 4,
40
+ cyclomaticComplexity: 1,
41
+ });
42
+
43
+ expect(mutants.length).toBeGreaterThan(0);
44
+ expect(mutants.some((m) => m.type === 'arithmetic-operator')).toBe(true);
45
+ });
46
+
47
+ it('generates comparison operator mutants', () => {
48
+ const filePath = path.join(tempDir, 'compare.ts');
49
+ fs.writeFileSync(filePath, `
50
+ export function isGreater(a: number, b: number): boolean {
51
+ return a > b;
52
+ }
53
+ `);
54
+ const mutants = generateMutants(filePath, {
55
+ filePath,
56
+ language: 'typescript',
57
+ functions: [],
58
+ classes: [],
59
+ interfaces: [],
60
+ exports: [],
61
+ imports: [],
62
+ dependencies: [],
63
+ linesOfCode: 4,
64
+ cyclomaticComplexity: 1,
65
+ });
66
+
67
+ expect(mutants.some((m) => m.type === 'comparison-operator')).toBe(true);
68
+ });
69
+
70
+ it('generates logical operator mutants', () => {
71
+ const filePath = path.join(tempDir, 'logic.ts');
72
+ fs.writeFileSync(filePath, `
73
+ export function isValid(a: boolean, b: boolean): boolean {
74
+ return a && b;
75
+ }
76
+ `);
77
+ const mutants = generateMutants(filePath, {
78
+ filePath,
79
+ language: 'typescript',
80
+ functions: [],
81
+ classes: [],
82
+ interfaces: [],
83
+ exports: [],
84
+ imports: [],
85
+ dependencies: [],
86
+ linesOfCode: 4,
87
+ cyclomaticComplexity: 1,
88
+ });
89
+
90
+ expect(mutants.some((m) => m.type === 'logical-operator')).toBe(true);
91
+ });
92
+
93
+ it('generates boolean literal mutants', () => {
94
+ const filePath = path.join(tempDir, 'bool.ts');
95
+ fs.writeFileSync(filePath, `
96
+ export function alwaysTrue(): boolean {
97
+ return true;
98
+ }
99
+ `);
100
+ const mutants = generateMutants(filePath, {
101
+ filePath,
102
+ language: 'typescript',
103
+ functions: [],
104
+ classes: [],
105
+ interfaces: [],
106
+ exports: [],
107
+ imports: [],
108
+ dependencies: [],
109
+ linesOfCode: 4,
110
+ cyclomaticComplexity: 1,
111
+ });
112
+
113
+ expect(mutants.some((m) => m.type === 'boolean-literal')).toBe(true);
114
+ });
115
+
116
+ it('generates string literal mutants', () => {
117
+ const filePath = path.join(tempDir, 'string.ts');
118
+ fs.writeFileSync(filePath, `
119
+ export function greet(name: string): string {
120
+ return "Hello, " + name;
121
+ }
122
+ `);
123
+ const mutants = generateMutants(filePath, {
124
+ filePath,
125
+ language: 'typescript',
126
+ functions: [],
127
+ classes: [],
128
+ interfaces: [],
129
+ exports: [],
130
+ imports: [],
131
+ dependencies: [],
132
+ linesOfCode: 4,
133
+ cyclomaticComplexity: 1,
134
+ });
135
+
136
+ expect(mutants.some((m) => m.type === 'string-literal')).toBe(true);
137
+ });
138
+
139
+ it('generates Python-specific mutants', () => {
140
+ const filePath = path.join(tempDir, 'math.py');
141
+ fs.writeFileSync(filePath, `
142
+ def is_valid(a: int, b: int) -> bool:
143
+ return a > 0 and b > 0
144
+ `);
145
+ const mutants = generateMutants(filePath, {
146
+ filePath,
147
+ language: 'python',
148
+ functions: [],
149
+ classes: [],
150
+ interfaces: [],
151
+ exports: [],
152
+ imports: [],
153
+ dependencies: [],
154
+ linesOfCode: 3,
155
+ cyclomaticComplexity: 1,
156
+ });
157
+
158
+ expect(mutants.length).toBeGreaterThan(0);
159
+ expect(mutants.some((m) => m.type === 'comparison-operator' || m.type === 'logical-operator')).toBe(true);
160
+ });
161
+
162
+ it('skips comment lines', () => {
163
+ const filePath = path.join(tempDir, 'commented.ts');
164
+ fs.writeFileSync(filePath, `
165
+ // This is a comment
166
+ export function add(a: number, b: number): number {
167
+ return a + b;
168
+ }
169
+ `);
170
+ const mutants = generateMutants(filePath, {
171
+ filePath,
172
+ language: 'typescript',
173
+ functions: [],
174
+ classes: [],
175
+ interfaces: [],
176
+ exports: [],
177
+ imports: [],
178
+ dependencies: [],
179
+ linesOfCode: 5,
180
+ cyclomaticComplexity: 1,
181
+ });
182
+
183
+ // Should not generate mutants for the comment line
184
+ for (const mutant of mutants) {
185
+ expect(mutant.line).not.toBe(1);
186
+ }
187
+ });
188
+
189
+ it('skips import lines', () => {
190
+ const filePath = path.join(tempDir, 'imports.ts');
191
+ fs.writeFileSync(filePath, `import { something } from 'module';
192
+ export function add(a: number, b: number): number {
193
+ return a + b;
194
+ }
195
+ `);
196
+ const mutants = generateMutants(filePath, {
197
+ filePath,
198
+ language: 'typescript',
199
+ functions: [],
200
+ classes: [],
201
+ interfaces: [],
202
+ exports: [],
203
+ imports: [],
204
+ dependencies: [],
205
+ linesOfCode: 4,
206
+ cyclomaticComplexity: 1,
207
+ });
208
+
209
+ for (const mutant of mutants) {
210
+ expect(mutant.line).not.toBe(1);
211
+ }
212
+ });
213
+ });
214
+
215
+ describe('applyMutant', () => {
216
+ it('correctly applies a mutation to source code', () => {
217
+ const source = `return a + b;`;
218
+ const mutant = {
219
+ id: 'test-1',
220
+ sourceFile: 'test.ts',
221
+ line: 1,
222
+ column: 9,
223
+ originalCode: '+',
224
+ mutatedCode: '-',
225
+ type: 'arithmetic-operator' as const,
226
+ status: 'pending' as const,
227
+ };
228
+
229
+ const mutated = applyMutant(source, mutant);
230
+ expect(mutated).toBe('return a - b;');
231
+ });
232
+
233
+ it('only modifies the specified line', () => {
234
+ const source = `line one\nreturn a + b;\nline three`;
235
+ const mutant = {
236
+ id: 'test-1',
237
+ sourceFile: 'test.ts',
238
+ line: 2,
239
+ column: 9,
240
+ originalCode: '+',
241
+ mutatedCode: '-',
242
+ type: 'arithmetic-operator' as const,
243
+ status: 'pending' as const,
244
+ };
245
+
246
+ const mutated = applyMutant(source, mutant);
247
+ const lines = mutated.split('\n');
248
+ expect(lines[0]).toBe('line one');
249
+ expect(lines[1]).toBe('return a - b;');
250
+ expect(lines[2]).toBe('line three');
251
+ });
252
+ });
253
+
254
+ describe('calculateMutationScore', () => {
255
+ it('calculates correct scores', () => {
256
+ const mutants = [
257
+ { id: '1', sourceFile: '', line: 1, column: 0, originalCode: '+', mutatedCode: '-', type: 'arithmetic-operator' as const, status: 'killed' as const },
258
+ { id: '2', sourceFile: '', line: 2, column: 0, originalCode: '+', mutatedCode: '-', type: 'arithmetic-operator' as const, status: 'killed' as const },
259
+ { id: '3', sourceFile: '', line: 3, column: 0, originalCode: '+', mutatedCode: '-', type: 'arithmetic-operator' as const, status: 'survived' as const },
260
+ { id: '4', sourceFile: '', line: 4, column: 0, originalCode: '+', mutatedCode: '-', type: 'arithmetic-operator' as const, status: 'survived' as const },
261
+ ];
262
+
263
+ const result = calculateMutationScore(mutants);
264
+ expect(result.total).toBe(4);
265
+ expect(result.killed).toBe(2);
266
+ expect(result.survived).toBe(2);
267
+ expect(result.mutationScore).toBe(50);
268
+ });
269
+
270
+ it('handles empty mutant list', () => {
271
+ const result = calculateMutationScore([]);
272
+ expect(result.total).toBe(0);
273
+ expect(result.mutationScore).toBe(0);
274
+ });
275
+
276
+ it('handles all killed', () => {
277
+ const mutants = [
278
+ { id: '1', sourceFile: '', line: 1, column: 0, originalCode: '+', mutatedCode: '-', type: 'arithmetic-operator' as const, status: 'killed' as const },
279
+ { id: '2', sourceFile: '', line: 2, column: 0, originalCode: '+', mutatedCode: '-', type: 'arithmetic-operator' as const, status: 'killed' as const },
280
+ ];
281
+
282
+ const result = calculateMutationScore(mutants);
283
+ expect(result.mutationScore).toBe(100);
284
+ });
285
+ });
286
+ });
@@ -0,0 +1,35 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import { FileWatcher } from '../src/watcher/watcher.js';
3
+ import { DEFAULT_CONFIG, TestGenConfig } from '../src/types.js';
4
+
5
+ describe('FileWatcher', () => {
6
+ it('can be created without errors', () => {
7
+ const config: TestGenConfig = {
8
+ ...DEFAULT_CONFIG,
9
+ watch: {
10
+ enabled: true,
11
+ ignorePatterns: ['node_modules'],
12
+ debounceMs: 100,
13
+ },
14
+ };
15
+
16
+ const callback = async () => {};
17
+ const watcher = new FileWatcher(config, callback);
18
+ expect(watcher.isRunning()).toBe(false);
19
+ });
20
+
21
+ it('stop is safe when not running', () => {
22
+ const config: TestGenConfig = {
23
+ ...DEFAULT_CONFIG,
24
+ watch: {
25
+ enabled: true,
26
+ ignorePatterns: [],
27
+ debounceMs: 100,
28
+ },
29
+ };
30
+
31
+ const callback = async () => {};
32
+ const watcher = new FileWatcher(config, callback);
33
+ expect(() => watcher.stop()).not.toThrow();
34
+ });
35
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "commonjs",
5
+ "lib": ["ES2022"],
6
+ "outDir": "./dist",
7
+ "rootDir": "./src",
8
+ "strict": true,
9
+ "esModuleInterop": true,
10
+ "skipLibCheck": true,
11
+ "forceConsistentCasingInFileNames": true,
12
+ "resolveJsonModule": true,
13
+ "declaration": true,
14
+ "declarationMap": true,
15
+ "sourceMap": true,
16
+ "noUnusedLocals": true,
17
+ "noUnusedParameters": true,
18
+ "noImplicitReturns": true,
19
+ "noFallthroughCasesInSwitch": true,
20
+ "noUncheckedIndexedAccess": true,
21
+ "exactOptionalPropertyTypes": false,
22
+ "noPropertyAccessFromIndexSignature": true
23
+ },
24
+ "include": ["src/**/*"],
25
+ "exclude": ["node_modules", "dist", "tests"]
26
+ }
@@ -0,0 +1,25 @@
1
+ import { defineConfig } from 'vitest/config';
2
+
3
+ export default defineConfig({
4
+ test: {
5
+ globals: true,
6
+ environment: 'node',
7
+ // Run in main process to avoid ts-morph OOM in workers
8
+ pool: 'forks',
9
+ poolOptions: {
10
+ forks: {
11
+ singleFork: true,
12
+ },
13
+ },
14
+ isolate: false,
15
+ fileParallelism: false,
16
+ coverage: {
17
+ provider: 'v8',
18
+ reporter: ['text', 'lcov'],
19
+ include: ['src/**/*.ts'],
20
+ exclude: ['src/cli.ts'],
21
+ },
22
+ include: ['tests/**/*.test.ts'],
23
+ testTimeout: 60000,
24
+ },
25
+ });