-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
284 lines (235 loc) · 8.76 KB
/
Copy pathcli.js
File metadata and controls
284 lines (235 loc) · 8.76 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
#!/usr/bin/env node
const { program } = require("commander");
const chalk = require("chalk");
const ora = require("ora");
const inquirer = require("inquirer").default || require("inquirer");
const fs = require("fs-extra");
const path = require("path");
const {displayBanner} = require('./banner')
const {
analyzeProject,
scanProjectFiles,
detectProjectType,
} = require("./src/analyzer");
const { generateTests } = require("./src/generator");
const {analyzeFileContent,shouldGenerateTest} = require('./src/utils')
const packageJson = require("./package.json");
program.configureHelp({
beforeAll: () => {
displayBanner({version: packageJson.version});
return '';
}
});
program
.name("qtest-cli")
.description(
"AI-powered test generation for JavaScript/TypeScript projects"
)
.version(packageJson.version);
program.action(() => {
displayBanner({version: packageJson.version});
program.help();
});
program
.command("analyze [path]")
.description("Analyze project and generate test files")
.option(
"-f, --framework <framework>",
"Test framework (jest, vitest, mocha)",
"jest"
)
.option(
"-o, --output <path>",
"Output directory for generated tests",
"./tests"
)
.action(async (projectPath, options) => {
const targetPath = projectPath || process.cwd();
console.log(chalk.blue("\n🔍 AI Test Generator"));
console.log(chalk.gray(`Analyzing: ${targetPath}\n`));
const spinner = ora("Scanning projects files...").start();
try {
const stat = await fs.stat(targetPath);
let files;
let analysisType;
if (stat.isFile()) {
// Single file analysis
if (!targetPath.match(/\.(js|ts|jsx|tsx)$/)) {
throw new Error(
"File must be a JavaScript/TypeScript file (.js, .ts, .jsx, .tsx)"
);
}
const content = await fs.readFile(targetPath, "utf8");
files = [
{
name: path.basename(targetPath),
path: targetPath,
relativePath: path.basename(targetPath),
content: content,
size: stat.size,
},
];
analysisType = "file";
} else {
// Directory analysis
files = await scanProjectFiles(targetPath);
analysisType = "directory";
}
const projectType = detectProjectType(files);
spinner.succeed(
`Found ${files.length} files to analyze (${projectType} project)`
);
await displayAnalysisResults(files, projectType, options.framework);
const generateSpinner = ora("Generating tests with AI...").start();
const generatedTests = await generateTests(files, options.framework);
generateSpinner.succeed("Test generation complete!");
await fs.ensureDir(options.output);
let savedCount = 0;
for (const test of generatedTests) {
const outputPath = path.join(options.output, test.filename);
await fs.writeFile(outputPath, test.content);
savedCount++;
console.log(chalk.gray(` ✓ ${test.filename}`));
}
console.log(
chalk.green(
`\n✅ Generated ${savedCount} test files in ${options.output}`
)
);
console.log(chalk.yellow("\nNext steps:"));
console.log(chalk.yellow(" npm test # Run your generated tests"));
} catch (error) {
spinner.fail("Analysis failed");
console.error(chalk.red(`Error: ${error.message}`));
process.exit(1);
}
});
async function displayAnalysisResults(files, projectType, framework) {
console.log(chalk.blue("\n📊 PROJECT ANALYSIS"));
console.log(chalk.blue("====================\n"));
// Project Overview
console.log(chalk.white(`📁 Project Type: ${chalk.cyan(projectType)}`));
console.log(chalk.white(`🧪 Framework: ${chalk.cyan(framework)}`));
console.log(chalk.white(`📄 Total Files: ${chalk.cyan(files.length)}\n`));
// Get testable files
const testableFiles = files.filter(file => shouldGenerateTest(file));
console.log(chalk.white("🔍 Files to test:"));
testableFiles.slice(0, 8).forEach((file, index) => {
const analysis = analyzeFileContent(file.content);
const size = (file.size / 1024).toFixed(1);
console.log(chalk.gray(` ${index + 1}. ${chalk.white(file.name)} (${size}KB)`));
if (analysis.functions.length > 0) {
const funcList = analysis.functions.slice(0, 3).join(', ');
const extra = analysis.functions.length > 3 ? ` +${analysis.functions.length - 3} more` : '';
console.log(chalk.gray(` Functions: ${funcList}${extra}`));
}
const features = [];
if (analysis.hasAsync) features.push('async');
if (analysis.isReactComponent) features.push('React');
if (analysis.isApiRoute) features.push('API');
if (features.length > 0) {
console.log(chalk.gray(` Features: ${features.join(', ')}`));
}
});
if (testableFiles.length > 8) {
console.log(chalk.gray(` ... and ${testableFiles.length - 8} more files`));
}
// Summary
const totalComplexity = testableFiles.reduce((sum, file) => {
const analysis = analyzeFileContent(file.content);
return sum + analysis.complexity;
}, 0);
const avgComplexity = testableFiles.length > 0 ? (totalComplexity / testableFiles.length).toFixed(1) : '0';
console.log(chalk.white(`\n📈 Summary:`));
console.log(chalk.gray(` Test files to generate: ${chalk.cyan(testableFiles.length)}`));
console.log(chalk.gray(` Average complexity: ${chalk.cyan(avgComplexity)}`));
const estimatedTime = Math.max(1, Math.ceil(testableFiles.length * 0.5));
console.log(chalk.gray(` Estimated time: ~${chalk.cyan(estimatedTime)} minutes`));
}
program
.command("watch [path]")
.description("Watch for file changes and auto-generate tests")
.option(
"-f, --framework <framework>",
"Test framework (jest, vitest, mocha)",
"jest"
)
.action(async (projectPath, options) => {
const targetPath = projectPath || process.cwd();
console.log(chalk.blue("\n👁️ AI Test Watcher"));
console.log(chalk.gray(`Watching: ${targetPath}`));
console.log(chalk.yellow("Press Ctrl+C to stop\n"));
const chokidar = require("chokidar");
const watcher = chokidar.watch(targetPath, {
ignored: /(^|[\/\\])\../,
persistent: true,
});
watcher.on("change", async (filePath) => {
if (filePath.match(/\.(js|ts|jsx|tsx)$/)) {
console.log(chalk.cyan(`📝 File changed: ${path.basename(filePath)}`));
try {
const spinner = ora("Regenerating tests...").start();
const files = await scanProjectFiles(targetPath);
const generatedTests = await generateTests(files, options.framework);
await fs.ensureDir("./tests");
for (const test of generatedTests) {
const outputPath = path.join("./tests", test.filename);
await fs.writeFile(outputPath, test.content);
}
spinner.succeed(`Updated ${generatedTests.length} test files`);
} catch (error) {
console.error(chalk.red(`Watch error: ${error.message}`));
}
}
});
watcher.on("ready", () => {
console.log(chalk.green("🟢 Watching for changes..."));
});
});
program
.command("init")
.description("Initialize AI test configuration")
.action(async () => {
displayBanner({version: packageJson.version});
console.log(chalk.blue("\n🚀 Initialize AI Test\n"));
const answers = await inquirer.prompt([
{
type: "list",
name: "framework",
message: "Which test framework do you want to use?",
choices: ["jest", "vitest", "mocha"],
default: "jest",
},
{
type: "input",
name: "outputDir",
message: "Where should test files be saved?",
default: "./tests",
},
]);
const config = {
framework: answers.framework,
outputDir: answers.outputDir,
filePatterns: ["**/*.js", "**/*.ts", "**/*.jsx", "**/*.tsx"],
excludePatterns: [
"node_modules/**",
"dist/**",
"build/**",
"**/*.test.*",
],
};
await fs.writeFile(".qtest-cli.json", JSON.stringify(config, null, 2));
console.log(chalk.green("\n✅ Configuration saved to .qtest-cli.json"));
console.log(chalk.yellow("\n📦 Install your test framework:"));
if (answers.framework === "jest") {
console.log(chalk.cyan(" npm install jest --save-dev"));
} else if (answers.framework === "vitest") {
console.log(chalk.cyan(" npm install vitest --save-dev"));
} else if (answers.framework === "mocha") {
console.log(chalk.cyan(" npm install mocha --save-dev"));
}
console.log(chalk.yellow("\nYou can now run:"));
console.log(chalk.yellow(" qtest-cli analyze"));
console.log(chalk.yellow(" qtest-cli watch"));
});
program.parse();