@elyracode/test-gen 0.5.2
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.
- package/README.md +41 -0
- package/extensions/index.ts +307 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @elyracode/test-gen
|
|
2
|
+
|
|
3
|
+
Intelligent test generation for Elyra -- Pest for Laravel, Vitest for TypeScript.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
elyra install npm:@elyracode/test-gen
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Tools
|
|
12
|
+
|
|
13
|
+
| Tool | Description |
|
|
14
|
+
|------|-------------|
|
|
15
|
+
| `generate_tests` | Read a source file and generate comprehensive tests with real assertions |
|
|
16
|
+
| `find_untested` | Scan the project for source files without corresponding test files |
|
|
17
|
+
|
|
18
|
+
## Commands
|
|
19
|
+
|
|
20
|
+
| Command | Description |
|
|
21
|
+
|---------|-------------|
|
|
22
|
+
| `/test-gen <path>` | Generate tests for a specific file |
|
|
23
|
+
| `/untested` | Find all untested source files |
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
> Generate tests for app/Http/Controllers/ProductController.php
|
|
29
|
+
> Write Pest tests for the User model focusing on validation
|
|
30
|
+
> Find files without tests
|
|
31
|
+
/test-gen app/Services/PaymentService.php
|
|
32
|
+
/untested
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- Auto-detects framework: Pest for PHP, Vitest for TS/JS
|
|
38
|
+
- Reads actual source code for context-aware tests
|
|
39
|
+
- Suggests correct test file paths (Laravel/TS conventions)
|
|
40
|
+
- Analyzes functions, classes, and dependencies
|
|
41
|
+
- Handles existing test files (extends coverage, doesn't duplicate)
|
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { basename, dirname, extname, join } from "node:path";
|
|
3
|
+
import type { ExtensionAPI } from "@elyracode/coding-agent";
|
|
4
|
+
import { Type } from "typebox";
|
|
5
|
+
|
|
6
|
+
export default function (elyra: ExtensionAPI): void {
|
|
7
|
+
|
|
8
|
+
// ── Tool: generate_tests ──
|
|
9
|
+
elyra.registerTool({
|
|
10
|
+
name: "generate_tests",
|
|
11
|
+
label: "Generate Tests",
|
|
12
|
+
description:
|
|
13
|
+
"Read a source file and generate comprehensive tests for it. " +
|
|
14
|
+
"Auto-detects the test framework: Pest for PHP/Laravel, Vitest for TypeScript/JavaScript. " +
|
|
15
|
+
"Generates real tests with assertions, edge cases, and mocks -- not stubs. " +
|
|
16
|
+
"Reads the source code to understand the actual logic.",
|
|
17
|
+
parameters: Type.Object({
|
|
18
|
+
file_path: Type.String({
|
|
19
|
+
description: "Path to the source file to generate tests for",
|
|
20
|
+
}),
|
|
21
|
+
focus: Type.Optional(
|
|
22
|
+
Type.String({
|
|
23
|
+
description: "Focus on specific methods or functionality (e.g., 'validation', 'edge cases', 'error handling')",
|
|
24
|
+
}),
|
|
25
|
+
),
|
|
26
|
+
}),
|
|
27
|
+
execute: async (_toolCallId, params) => {
|
|
28
|
+
try {
|
|
29
|
+
if (!existsSync(params.file_path)) {
|
|
30
|
+
return {
|
|
31
|
+
content: [{ type: "text", text: `File not found: ${params.file_path}` }],
|
|
32
|
+
details: {},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const content = readFileSync(params.file_path, "utf-8");
|
|
37
|
+
const ext = extname(params.file_path);
|
|
38
|
+
const fileName = basename(params.file_path, ext);
|
|
39
|
+
const dir = dirname(params.file_path);
|
|
40
|
+
|
|
41
|
+
const framework = detectTestFramework(ext, dir);
|
|
42
|
+
const testPath = suggestTestPath(params.file_path, framework);
|
|
43
|
+
const existingTest = existsSync(testPath) ? readFileSync(testPath, "utf-8") : undefined;
|
|
44
|
+
|
|
45
|
+
const analysis = analyzeSourceFile(content, ext);
|
|
46
|
+
|
|
47
|
+
const lines: string[] = [
|
|
48
|
+
`# Test Generation: ${params.file_path}`,
|
|
49
|
+
"",
|
|
50
|
+
`**Framework**: ${framework.name}`,
|
|
51
|
+
`**Test file**: ${testPath}`,
|
|
52
|
+
`**Functions/methods found**: ${analysis.functions.length}`,
|
|
53
|
+
`**Classes found**: ${analysis.classes.length}`,
|
|
54
|
+
"",
|
|
55
|
+
];
|
|
56
|
+
|
|
57
|
+
if (existingTest) {
|
|
58
|
+
lines.push("## Existing Test File");
|
|
59
|
+
lines.push("```");
|
|
60
|
+
lines.push(existingTest.slice(0, 5000));
|
|
61
|
+
lines.push("```");
|
|
62
|
+
lines.push("");
|
|
63
|
+
lines.push("Update the existing tests to improve coverage. Don't duplicate existing test cases.");
|
|
64
|
+
lines.push("");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
lines.push("## Source Code");
|
|
68
|
+
lines.push("```");
|
|
69
|
+
lines.push(content.slice(0, 20000));
|
|
70
|
+
lines.push("```");
|
|
71
|
+
lines.push("");
|
|
72
|
+
|
|
73
|
+
lines.push("## Analysis");
|
|
74
|
+
if (analysis.functions.length > 0) {
|
|
75
|
+
lines.push(`Functions: ${analysis.functions.join(", ")}`);
|
|
76
|
+
}
|
|
77
|
+
if (analysis.classes.length > 0) {
|
|
78
|
+
lines.push(`Classes: ${analysis.classes.join(", ")}`);
|
|
79
|
+
}
|
|
80
|
+
if (analysis.dependencies.length > 0) {
|
|
81
|
+
lines.push(`Dependencies: ${analysis.dependencies.join(", ")}`);
|
|
82
|
+
}
|
|
83
|
+
lines.push("");
|
|
84
|
+
|
|
85
|
+
lines.push("## Instructions");
|
|
86
|
+
lines.push("");
|
|
87
|
+
lines.push(`Generate ${framework.name} tests for this file. Requirements:`);
|
|
88
|
+
lines.push("- Test each public method/function");
|
|
89
|
+
lines.push("- Include happy path, edge cases, and error cases");
|
|
90
|
+
lines.push("- Use proper assertions (not just 'no error thrown')");
|
|
91
|
+
if (framework.type === "pest") {
|
|
92
|
+
lines.push("- Use Pest syntax: it(), expect(), beforeEach()");
|
|
93
|
+
lines.push("- Use Laravel factories for model data");
|
|
94
|
+
lines.push("- Use RefreshDatabase trait for database tests");
|
|
95
|
+
lines.push("- Mock external services with Mockery or fakes");
|
|
96
|
+
} else {
|
|
97
|
+
lines.push("- Use describe/it blocks with clear names");
|
|
98
|
+
lines.push("- Use vi.mock() for mocking dependencies");
|
|
99
|
+
lines.push("- Use vi.spyOn() for partial mocks");
|
|
100
|
+
lines.push("- Test async functions with await");
|
|
101
|
+
}
|
|
102
|
+
if (params.focus) {
|
|
103
|
+
lines.push(`- Focus especially on: ${params.focus}`);
|
|
104
|
+
}
|
|
105
|
+
lines.push("");
|
|
106
|
+
lines.push(`Write the tests to: ${testPath}`);
|
|
107
|
+
|
|
108
|
+
return {
|
|
109
|
+
content: [{ type: "text", text: lines.join("\n") }],
|
|
110
|
+
details: {
|
|
111
|
+
framework: framework.name,
|
|
112
|
+
testPath,
|
|
113
|
+
functions: analysis.functions.length,
|
|
114
|
+
classes: analysis.classes.length,
|
|
115
|
+
},
|
|
116
|
+
};
|
|
117
|
+
} catch (error) {
|
|
118
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
119
|
+
return {
|
|
120
|
+
content: [{ type: "text", text: `Test generation failed: ${msg}` }],
|
|
121
|
+
details: {},
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// ── Tool: find_untested ──
|
|
128
|
+
elyra.registerTool({
|
|
129
|
+
name: "find_untested",
|
|
130
|
+
label: "Find Untested Files",
|
|
131
|
+
description:
|
|
132
|
+
"Scan the project for source files that don't have corresponding test files. " +
|
|
133
|
+
"Returns a list of files that need tests, prioritized by importance.",
|
|
134
|
+
parameters: Type.Object({
|
|
135
|
+
path: Type.Optional(
|
|
136
|
+
Type.String({ description: "Directory to scan (default: current directory)" }),
|
|
137
|
+
),
|
|
138
|
+
}),
|
|
139
|
+
execute: async (_toolCallId, params) => {
|
|
140
|
+
try {
|
|
141
|
+
const { execSync } = await import("node:child_process");
|
|
142
|
+
const cwd = process.cwd();
|
|
143
|
+
const scanPath = params.path ?? ".";
|
|
144
|
+
|
|
145
|
+
// Find PHP source files
|
|
146
|
+
const phpFiles: string[] = [];
|
|
147
|
+
try {
|
|
148
|
+
const result = execSync(
|
|
149
|
+
`find ${scanPath} -name "*.php" -not -path "*/vendor/*" -not -path "*/node_modules/*" -not -path "*/test*" -not -path "*/Test*" -not -name "*Test.php" -not -name "*test.php" | head -100`,
|
|
150
|
+
{ cwd, timeout: 15000, encoding: "utf-8" },
|
|
151
|
+
);
|
|
152
|
+
phpFiles.push(...result.split("\n").filter((l) => l.trim()));
|
|
153
|
+
} catch { /* ignore */ }
|
|
154
|
+
|
|
155
|
+
// Find TS/JS source files
|
|
156
|
+
const tsFiles: string[] = [];
|
|
157
|
+
try {
|
|
158
|
+
const result = execSync(
|
|
159
|
+
`find ${scanPath} -name "*.ts" -o -name "*.tsx" | grep -v node_modules | grep -v dist | grep -v ".test." | grep -v ".spec." | grep -v "__tests__" | head -100`,
|
|
160
|
+
{ cwd, timeout: 15000, encoding: "utf-8" },
|
|
161
|
+
);
|
|
162
|
+
tsFiles.push(...result.split("\n").filter((l) => l.trim()));
|
|
163
|
+
} catch { /* ignore */ }
|
|
164
|
+
|
|
165
|
+
const untested: string[] = [];
|
|
166
|
+
for (const file of [...phpFiles, ...tsFiles]) {
|
|
167
|
+
const ext = extname(file);
|
|
168
|
+
const framework = detectTestFramework(ext, dirname(file));
|
|
169
|
+
const testPath = suggestTestPath(file, framework);
|
|
170
|
+
if (!existsSync(join(cwd, testPath)) && !existsSync(testPath)) {
|
|
171
|
+
untested.push(`${file} -> ${testPath}`);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (untested.length === 0) {
|
|
176
|
+
return {
|
|
177
|
+
content: [{ type: "text", text: "All source files have corresponding test files." }],
|
|
178
|
+
details: { untestedCount: 0 },
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const lines = [
|
|
183
|
+
`# Untested Files (${untested.length})`,
|
|
184
|
+
"",
|
|
185
|
+
"Source files without corresponding test files:",
|
|
186
|
+
"",
|
|
187
|
+
...untested.map((f) => `- ${f}`),
|
|
188
|
+
];
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
content: [{ type: "text", text: lines.join("\n") }],
|
|
192
|
+
details: { untestedCount: untested.length },
|
|
193
|
+
};
|
|
194
|
+
} catch (error) {
|
|
195
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
196
|
+
return {
|
|
197
|
+
content: [{ type: "text", text: `Scan failed: ${msg}` }],
|
|
198
|
+
details: {},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
// ── Commands ──
|
|
205
|
+
elyra.registerCommand("test-gen", {
|
|
206
|
+
description: "Generate tests for a file: /test-gen <path>",
|
|
207
|
+
handler: async (args, _ctx) => {
|
|
208
|
+
if (!args.trim()) {
|
|
209
|
+
elyra.sendUserMessage("Usage: /test-gen <file-path>\n\nExample: /test-gen app/Http/Controllers/ProductController.php");
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
elyra.sendUserMessage(`Generate comprehensive tests for ${args.trim()}`);
|
|
213
|
+
},
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
elyra.registerCommand("untested", {
|
|
217
|
+
description: "Find source files without tests",
|
|
218
|
+
handler: async (_args, _ctx) => {
|
|
219
|
+
elyra.sendUserMessage("Find all source files that don't have corresponding test files.");
|
|
220
|
+
},
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
interface TestFramework {
|
|
225
|
+
type: "pest" | "vitest";
|
|
226
|
+
name: string;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function detectTestFramework(ext: string, dir: string): TestFramework {
|
|
230
|
+
if (ext === ".php") {
|
|
231
|
+
return { type: "pest", name: "Pest (Laravel)" };
|
|
232
|
+
}
|
|
233
|
+
return { type: "vitest", name: "Vitest" };
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function suggestTestPath(sourcePath: string, framework: TestFramework): string {
|
|
237
|
+
const ext = extname(sourcePath);
|
|
238
|
+
const name = basename(sourcePath, ext);
|
|
239
|
+
|
|
240
|
+
if (framework.type === "pest") {
|
|
241
|
+
// Laravel convention: app/Http/Controllers/Foo.php -> tests/Feature/Http/Controllers/FooTest.php
|
|
242
|
+
if (sourcePath.includes("app/")) {
|
|
243
|
+
const relative = sourcePath.replace(/^.*?app\//, "");
|
|
244
|
+
return `tests/Feature/${relative.replace(ext, `Test${ext}`)}`;
|
|
245
|
+
}
|
|
246
|
+
return `tests/Feature/${name}Test${ext}`;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// TS/JS convention: src/foo.ts -> test/foo.test.ts or __tests__/foo.test.ts
|
|
250
|
+
if (sourcePath.includes("src/")) {
|
|
251
|
+
const relative = sourcePath.replace(/^.*?src\//, "");
|
|
252
|
+
return `test/${relative.replace(ext, `.test${ext}`)}`;
|
|
253
|
+
}
|
|
254
|
+
return `test/${name}.test${ext}`;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
interface SourceAnalysis {
|
|
258
|
+
functions: string[];
|
|
259
|
+
classes: string[];
|
|
260
|
+
dependencies: string[];
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function analyzeSourceFile(content: string, ext: string): SourceAnalysis {
|
|
264
|
+
const functions: string[] = [];
|
|
265
|
+
const classes: string[] = [];
|
|
266
|
+
const dependencies: string[] = [];
|
|
267
|
+
|
|
268
|
+
if (ext === ".php") {
|
|
269
|
+
// PHP classes
|
|
270
|
+
const classMatches = content.match(/class\s+(\w+)/g) ?? [];
|
|
271
|
+
for (const m of classMatches) {
|
|
272
|
+
classes.push(m.replace("class ", ""));
|
|
273
|
+
}
|
|
274
|
+
// PHP methods
|
|
275
|
+
const methodMatches = content.match(/(?:public|protected|private)\s+(?:static\s+)?function\s+(\w+)/g) ?? [];
|
|
276
|
+
for (const m of methodMatches) {
|
|
277
|
+
const name = m.match(/function\s+(\w+)/)?.[1];
|
|
278
|
+
if (name && name !== "__construct") functions.push(name);
|
|
279
|
+
}
|
|
280
|
+
// PHP dependencies (use statements)
|
|
281
|
+
const useMatches = content.match(/use\s+[\w\\]+(?:\s+as\s+\w+)?;/g) ?? [];
|
|
282
|
+
for (const m of useMatches) {
|
|
283
|
+
const dep = m.match(/use\s+([\w\\]+)/)?.[1];
|
|
284
|
+
if (dep) dependencies.push(dep.split("\\").pop() ?? dep);
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
// TS/JS functions
|
|
288
|
+
const fnMatches = content.match(/(?:export\s+)?(?:async\s+)?function\s+(\w+)/g) ?? [];
|
|
289
|
+
for (const m of fnMatches) {
|
|
290
|
+
const name = m.match(/function\s+(\w+)/)?.[1];
|
|
291
|
+
if (name) functions.push(name);
|
|
292
|
+
}
|
|
293
|
+
// TS/JS classes
|
|
294
|
+
const tsClassMatches = content.match(/(?:export\s+)?class\s+(\w+)/g) ?? [];
|
|
295
|
+
for (const m of tsClassMatches) {
|
|
296
|
+
classes.push(m.match(/class\s+(\w+)/)?.[1] ?? "");
|
|
297
|
+
}
|
|
298
|
+
// TS/JS imports
|
|
299
|
+
const importMatches = content.match(/import\s+.*?from\s+["']([^"']+)["']/g) ?? [];
|
|
300
|
+
for (const m of importMatches) {
|
|
301
|
+
const dep = m.match(/from\s+["']([^"']+)["']/)?.[1];
|
|
302
|
+
if (dep && !dep.startsWith(".")) dependencies.push(dep);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return { functions: functions.filter(Boolean), classes: classes.filter(Boolean), dependencies };
|
|
307
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@elyracode/test-gen",
|
|
3
|
+
"version": "0.5.2",
|
|
4
|
+
"description": "Elyra extension for intelligent test generation -- Pest for Laravel, Vitest for TypeScript",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": ["elyra-package", "test", "pest", "vitest", "testing", "generator"],
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "Knut W. Horne",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/kwhorne/elyra.git",
|
|
12
|
+
"directory": "packages/test-gen"
|
|
13
|
+
},
|
|
14
|
+
"elyra": {
|
|
15
|
+
"extensions": ["./extensions/index.ts"]
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@elyracode/coding-agent": "*",
|
|
19
|
+
"typebox": "*"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"clean": "echo 'nothing to clean'",
|
|
23
|
+
"build": "echo 'nothing to build'",
|
|
24
|
+
"check": "echo 'nothing to check'"
|
|
25
|
+
}
|
|
26
|
+
}
|