trickle-cli 0.1.208 → 0.1.210
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/dist/commands/test-gen.d.ts +9 -4
- package/dist/commands/test-gen.js +574 -59
- package/dist/index.js +4 -1
- package/package.json +1 -1
- package/src/commands/test-gen.ts +657 -67
- package/src/index.ts +4 -1
- package/dist/commands/rn.test.d.ts +0 -1
- package/dist/commands/rn.test.js +0 -138
|
@@ -42,44 +42,29 @@ const path = __importStar(require("path"));
|
|
|
42
42
|
const chalk_1 = __importDefault(require("chalk"));
|
|
43
43
|
const api_client_1 = require("../api-client");
|
|
44
44
|
/**
|
|
45
|
-
* `trickle test --generate` — Generate
|
|
45
|
+
* `trickle test --generate` — Generate test files from runtime observations.
|
|
46
46
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* Two modes:
|
|
48
|
+
* Default: API route tests (HTTP endpoint integration tests)
|
|
49
|
+
* --unit: Function-level unit tests from observed inputs/outputs
|
|
50
|
+
*
|
|
51
|
+
* Frameworks: vitest, jest, pytest
|
|
50
52
|
*/
|
|
51
53
|
async function testGenCommand(opts) {
|
|
52
|
-
const framework = opts.framework || "vitest";
|
|
54
|
+
const framework = opts.framework || (opts.unit ? "vitest" : "vitest");
|
|
53
55
|
const baseUrl = opts.baseUrl || "http://localhost:3000";
|
|
54
|
-
|
|
56
|
+
const supportedFrameworks = ["vitest", "jest", "pytest"];
|
|
57
|
+
if (!supportedFrameworks.includes(framework)) {
|
|
55
58
|
console.error(chalk_1.default.red(`\n Unsupported framework: ${framework}`));
|
|
56
|
-
console.error(chalk_1.default.gray(
|
|
59
|
+
console.error(chalk_1.default.gray(` Supported: ${supportedFrameworks.join(", ")}\n`));
|
|
57
60
|
process.exit(1);
|
|
58
61
|
}
|
|
59
62
|
try {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
console.error(chalk_1.default.yellow("\n No API routes observed yet."));
|
|
63
|
-
console.error(chalk_1.default.gray(" Instrument your app and make some requests first.\n"));
|
|
64
|
-
process.exit(1);
|
|
65
|
-
}
|
|
66
|
-
const testCode = generateTestFile(routes, framework, baseUrl);
|
|
67
|
-
if (opts.out) {
|
|
68
|
-
const resolvedPath = path.resolve(opts.out);
|
|
69
|
-
const dir = path.dirname(resolvedPath);
|
|
70
|
-
if (!fs.existsSync(dir)) {
|
|
71
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
72
|
-
}
|
|
73
|
-
fs.writeFileSync(resolvedPath, testCode, "utf-8");
|
|
74
|
-
console.log("");
|
|
75
|
-
console.log(chalk_1.default.green(` Tests written to ${chalk_1.default.bold(opts.out)}`));
|
|
76
|
-
console.log(chalk_1.default.gray(` ${routes.length} route tests generated (${framework})`));
|
|
77
|
-
console.log(chalk_1.default.gray(` Run with: npx ${framework === "vitest" ? "vitest run" : "jest"} ${opts.out}`));
|
|
78
|
-
console.log("");
|
|
63
|
+
if (opts.unit) {
|
|
64
|
+
await generateUnitTests(opts, framework);
|
|
79
65
|
}
|
|
80
66
|
else {
|
|
81
|
-
|
|
82
|
-
console.log(testCode);
|
|
67
|
+
await generateRouteTests(opts, framework, baseUrl);
|
|
83
68
|
}
|
|
84
69
|
}
|
|
85
70
|
catch (err) {
|
|
@@ -89,21 +74,239 @@ async function testGenCommand(opts) {
|
|
|
89
74
|
process.exit(1);
|
|
90
75
|
}
|
|
91
76
|
}
|
|
92
|
-
|
|
77
|
+
// ── Route tests (existing behavior) ──
|
|
78
|
+
async function generateRouteTests(opts, framework, baseUrl) {
|
|
79
|
+
if (framework === "pytest") {
|
|
80
|
+
console.error(chalk_1.default.red("\n pytest is only supported with --unit mode"));
|
|
81
|
+
console.error(chalk_1.default.gray(" For API route tests, use vitest or jest\n"));
|
|
82
|
+
process.exit(1);
|
|
83
|
+
}
|
|
84
|
+
const { routes } = await (0, api_client_1.fetchMockConfig)();
|
|
85
|
+
if (routes.length === 0) {
|
|
86
|
+
console.error(chalk_1.default.yellow("\n No API routes observed yet."));
|
|
87
|
+
console.error(chalk_1.default.gray(" Instrument your app and make some requests first.\n"));
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
const testCode = generateRouteTestFile(routes, framework, baseUrl);
|
|
91
|
+
outputTestCode(testCode, opts, routes.length, "route", framework);
|
|
92
|
+
}
|
|
93
|
+
async function generateUnitTests(opts, framework) {
|
|
94
|
+
// Fetch all functions with their sample data
|
|
95
|
+
const { functions } = await (0, api_client_1.listFunctions)({ limit: 500 });
|
|
96
|
+
if (functions.length === 0) {
|
|
97
|
+
console.error(chalk_1.default.yellow("\n No functions observed yet."));
|
|
98
|
+
console.error(chalk_1.default.gray(" Run your app with trickle first: trickle run <command>\n"));
|
|
99
|
+
process.exit(1);
|
|
100
|
+
}
|
|
101
|
+
// Filter by function name or module if specified
|
|
102
|
+
let filtered = functions;
|
|
103
|
+
if (opts.function) {
|
|
104
|
+
const searchTerm = opts.function.toLowerCase();
|
|
105
|
+
filtered = functions.filter(f => f.function_name.toLowerCase().includes(searchTerm));
|
|
106
|
+
}
|
|
107
|
+
if (opts.module) {
|
|
108
|
+
const searchTerm = opts.module.toLowerCase();
|
|
109
|
+
filtered = filtered.filter(f => f.module.toLowerCase().includes(searchTerm));
|
|
110
|
+
}
|
|
111
|
+
// Skip route handlers (GET /api/..., POST /api/...) — those are covered by route tests
|
|
112
|
+
filtered = filtered.filter(f => !isRouteHandler(f.function_name));
|
|
113
|
+
if (filtered.length === 0) {
|
|
114
|
+
console.error(chalk_1.default.yellow("\n No matching functions found."));
|
|
115
|
+
if (opts.function || opts.module) {
|
|
116
|
+
console.error(chalk_1.default.gray(` Try without --function or --module filters\n`));
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
console.error(chalk_1.default.gray(" Only route handlers were found. Try without --unit for API route tests.\n"));
|
|
120
|
+
}
|
|
121
|
+
process.exit(1);
|
|
122
|
+
}
|
|
123
|
+
// Collect sample data for each function
|
|
124
|
+
const functionSamples = [];
|
|
125
|
+
for (const fn of filtered) {
|
|
126
|
+
try {
|
|
127
|
+
const { snapshots } = await (0, api_client_1.listTypes)(fn.id, { limit: 10 });
|
|
128
|
+
const samples = [];
|
|
129
|
+
for (const snap of snapshots) {
|
|
130
|
+
if (snap.sample_input !== undefined || snap.sample_output !== undefined) {
|
|
131
|
+
samples.push({
|
|
132
|
+
input: snap.sample_input,
|
|
133
|
+
output: snap.sample_output,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (samples.length > 0) {
|
|
138
|
+
functionSamples.push({
|
|
139
|
+
functionName: fn.function_name,
|
|
140
|
+
module: fn.module,
|
|
141
|
+
language: fn.language,
|
|
142
|
+
samples,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
catch {
|
|
147
|
+
// Skip functions with no snapshot data
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (functionSamples.length === 0) {
|
|
151
|
+
console.error(chalk_1.default.yellow("\n No functions with sample data found."));
|
|
152
|
+
console.error(chalk_1.default.gray(" Run your app with trickle to capture function inputs/outputs first.\n"));
|
|
153
|
+
process.exit(1);
|
|
154
|
+
}
|
|
155
|
+
// Auto-detect language if framework doesn't specify
|
|
156
|
+
const isPython = framework === "pytest";
|
|
157
|
+
const isJS = framework === "vitest" || framework === "jest";
|
|
158
|
+
// Filter samples by language
|
|
159
|
+
const languageFiltered = functionSamples.filter(f => isPython ? f.language === "python" : f.language !== "python");
|
|
160
|
+
if (languageFiltered.length === 0) {
|
|
161
|
+
const lang = isPython ? "Python" : "JavaScript/TypeScript";
|
|
162
|
+
console.error(chalk_1.default.yellow(`\n No ${lang} functions with sample data found.`));
|
|
163
|
+
console.error(chalk_1.default.gray(` Try --framework ${isPython ? "vitest" : "pytest"} for the other language\n`));
|
|
164
|
+
process.exit(1);
|
|
165
|
+
}
|
|
166
|
+
const testCode = isPython
|
|
167
|
+
? generatePytestFile(languageFiltered)
|
|
168
|
+
: generateUnitTestFile(languageFiltered, framework);
|
|
169
|
+
outputTestCode(testCode, opts, languageFiltered.length, "function", framework);
|
|
170
|
+
}
|
|
171
|
+
function outputTestCode(testCode, opts, count, kind, framework) {
|
|
172
|
+
if (opts.out) {
|
|
173
|
+
const resolvedPath = path.resolve(opts.out);
|
|
174
|
+
const dir = path.dirname(resolvedPath);
|
|
175
|
+
if (!fs.existsSync(dir)) {
|
|
176
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
177
|
+
}
|
|
178
|
+
fs.writeFileSync(resolvedPath, testCode, "utf-8");
|
|
179
|
+
console.log("");
|
|
180
|
+
console.log(chalk_1.default.green(` Tests written to ${chalk_1.default.bold(opts.out)}`));
|
|
181
|
+
console.log(chalk_1.default.gray(` ${count} ${kind} tests generated (${framework})`));
|
|
182
|
+
const runCmd = framework === "pytest"
|
|
183
|
+
? `pytest ${opts.out} -v`
|
|
184
|
+
: `npx ${framework === "vitest" ? "vitest run" : "jest"} ${opts.out}`;
|
|
185
|
+
console.log(chalk_1.default.gray(` Run with: ${runCmd}`));
|
|
186
|
+
console.log("");
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
console.log("");
|
|
190
|
+
console.log(testCode);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// ── JS/TS unit test generation ──
|
|
194
|
+
function generateUnitTestFile(functions, framework) {
|
|
195
|
+
const lines = [];
|
|
196
|
+
lines.push("// Auto-generated unit tests by trickle");
|
|
197
|
+
lines.push(`// Generated at ${new Date().toISOString()}`);
|
|
198
|
+
lines.push("// Based on observed runtime behavior — re-run `trickle test --generate --unit` to update");
|
|
199
|
+
lines.push("");
|
|
200
|
+
if (framework === "vitest") {
|
|
201
|
+
lines.push('import { describe, it, expect } from "vitest";');
|
|
202
|
+
lines.push("");
|
|
203
|
+
}
|
|
204
|
+
// Group functions by module for import organization
|
|
205
|
+
const byModule = groupByModule(functions);
|
|
206
|
+
// Generate import statements
|
|
207
|
+
for (const [mod, fns] of Object.entries(byModule)) {
|
|
208
|
+
const importPath = normalizeImportPath(mod);
|
|
209
|
+
const fnNames = fns.map(f => sanitizeFnName(f.functionName));
|
|
210
|
+
lines.push(`import { ${fnNames.join(", ")} } from "${importPath}";`);
|
|
211
|
+
}
|
|
212
|
+
lines.push("");
|
|
213
|
+
// Generate test blocks
|
|
214
|
+
for (const [mod, fns] of Object.entries(byModule)) {
|
|
215
|
+
for (const fn of fns) {
|
|
216
|
+
const safeName = sanitizeFnName(fn.functionName);
|
|
217
|
+
lines.push(`describe("${safeName}", () => {`);
|
|
218
|
+
for (let i = 0; i < fn.samples.length; i++) {
|
|
219
|
+
const sample = fn.samples[i];
|
|
220
|
+
const testName = describeTestCase(sample.input, sample.output, i);
|
|
221
|
+
const isAsync = isPromiseOutput(sample.output);
|
|
222
|
+
lines.push(` it("${testName}", ${isAsync ? "async " : ""}() => {`);
|
|
223
|
+
// Build function call
|
|
224
|
+
const argsStr = formatArgs(sample.input);
|
|
225
|
+
const resultVar = isAsync ? `await ${safeName}(${argsStr})` : `${safeName}(${argsStr})`;
|
|
226
|
+
lines.push(` const result = ${resultVar};`);
|
|
227
|
+
// Generate assertions based on output
|
|
228
|
+
if (sample.output !== undefined && sample.output !== null) {
|
|
229
|
+
const assertions = generateOutputAssertions(sample.output, "result");
|
|
230
|
+
for (const assertion of assertions) {
|
|
231
|
+
lines.push(` ${assertion}`);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else if (sample.output === null) {
|
|
235
|
+
lines.push(" expect(result).toBeNull();");
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
lines.push(" expect(result).toBeDefined();");
|
|
239
|
+
}
|
|
240
|
+
lines.push(" });");
|
|
241
|
+
lines.push("");
|
|
242
|
+
}
|
|
243
|
+
lines.push("});");
|
|
244
|
+
lines.push("");
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return lines.join("\n").trimEnd() + "\n";
|
|
248
|
+
}
|
|
249
|
+
// ── Python pytest generation ──
|
|
250
|
+
function generatePytestFile(functions) {
|
|
251
|
+
const lines = [];
|
|
252
|
+
lines.push("# Auto-generated unit tests by trickle");
|
|
253
|
+
lines.push(`# Generated at ${new Date().toISOString()}`);
|
|
254
|
+
lines.push("# Based on observed runtime behavior — re-run `trickle test --generate --unit --framework pytest` to update");
|
|
255
|
+
lines.push("");
|
|
256
|
+
// Group by module for imports
|
|
257
|
+
const byModule = groupByModule(functions);
|
|
258
|
+
// Generate import statements
|
|
259
|
+
for (const [mod, fns] of Object.entries(byModule)) {
|
|
260
|
+
const importModule = normalizePythonImport(mod);
|
|
261
|
+
const fnNames = fns.map(f => sanitizePythonName(f.functionName));
|
|
262
|
+
lines.push(`from ${importModule} import ${fnNames.join(", ")}`);
|
|
263
|
+
}
|
|
264
|
+
lines.push("");
|
|
265
|
+
lines.push("");
|
|
266
|
+
// Generate test functions
|
|
267
|
+
for (const fn of functions) {
|
|
268
|
+
const safeName = sanitizePythonName(fn.functionName);
|
|
269
|
+
for (let i = 0; i < fn.samples.length; i++) {
|
|
270
|
+
const sample = fn.samples[i];
|
|
271
|
+
const testSuffix = fn.samples.length > 1 ? `_case_${i + 1}` : "";
|
|
272
|
+
const testFnName = `test_${safeName}${testSuffix}`;
|
|
273
|
+
lines.push(`def ${testFnName}():`);
|
|
274
|
+
lines.push(` """Test ${safeName} with observed runtime data."""`);
|
|
275
|
+
// Format input args
|
|
276
|
+
const argsStr = formatPythonArgs(sample.input);
|
|
277
|
+
lines.push(` result = ${safeName}(${argsStr})`);
|
|
278
|
+
// Generate assertions
|
|
279
|
+
if (sample.output !== undefined && sample.output !== null) {
|
|
280
|
+
const assertions = generatePythonAssertions(sample.output, "result");
|
|
281
|
+
for (const assertion of assertions) {
|
|
282
|
+
lines.push(` ${assertion}`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
else if (sample.output === null) {
|
|
286
|
+
lines.push(" assert result is None");
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
lines.push(" assert result is not None");
|
|
290
|
+
}
|
|
291
|
+
lines.push("");
|
|
292
|
+
lines.push("");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return lines.join("\n").trimEnd() + "\n";
|
|
296
|
+
}
|
|
297
|
+
// ── Route test generation (existing logic, preserved) ──
|
|
298
|
+
function generateRouteTestFile(routes, framework, baseUrl) {
|
|
93
299
|
const lines = [];
|
|
94
300
|
lines.push("// Auto-generated API tests by trickle");
|
|
95
301
|
lines.push(`// Generated at ${new Date().toISOString()}`);
|
|
96
302
|
lines.push("// Do not edit manually — re-run `trickle test --generate` to update");
|
|
97
303
|
lines.push("");
|
|
98
|
-
// Import block
|
|
99
304
|
if (framework === "vitest") {
|
|
100
305
|
lines.push('import { describe, it, expect } from "vitest";');
|
|
101
306
|
}
|
|
102
|
-
// jest needs no import — globals are available
|
|
103
307
|
lines.push("");
|
|
104
308
|
lines.push(`const BASE_URL = process.env.TEST_API_URL || "${baseUrl}";`);
|
|
105
309
|
lines.push("");
|
|
106
|
-
// Group routes by resource path prefix
|
|
107
310
|
const groups = groupByResource(routes);
|
|
108
311
|
for (const [resource, resourceRoutes] of Object.entries(groups)) {
|
|
109
312
|
lines.push(`describe("${resource}", () => {`);
|
|
@@ -111,9 +314,7 @@ function generateTestFile(routes, framework, baseUrl) {
|
|
|
111
314
|
const testName = `${route.method} ${route.path}`;
|
|
112
315
|
const hasBody = ["POST", "PUT", "PATCH"].includes(route.method);
|
|
113
316
|
lines.push(` it("${testName} — returns expected shape", async () => {`);
|
|
114
|
-
// Build fetch call
|
|
115
317
|
const fetchPath = route.path.replace(/:(\w+)/g, (_, param) => {
|
|
116
|
-
// Use sample data to get a real param value if available
|
|
117
318
|
const sampleValue = extractParamFromSample(route.sampleInput, param);
|
|
118
319
|
return sampleValue || `test-${param}`;
|
|
119
320
|
});
|
|
@@ -128,11 +329,16 @@ function generateTestFile(routes, framework, baseUrl) {
|
|
|
128
329
|
}
|
|
129
330
|
lines.push(" });");
|
|
130
331
|
lines.push("");
|
|
131
|
-
//
|
|
332
|
+
// POST typically returns 201, others return 200
|
|
333
|
+
// Use expect(res.ok) which covers 200-299 range
|
|
132
334
|
lines.push(" expect(res.ok).toBe(true);");
|
|
133
|
-
|
|
335
|
+
if (hasBody) {
|
|
336
|
+
lines.push(" expect(res.status === 200 || res.status === 201).toBe(true);");
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
lines.push(` expect(res.status).toBe(200);`);
|
|
340
|
+
}
|
|
134
341
|
lines.push("");
|
|
135
|
-
// Response body assertions
|
|
136
342
|
lines.push(" const body = await res.json();");
|
|
137
343
|
if (route.sampleOutput && typeof route.sampleOutput === "object") {
|
|
138
344
|
const assertions = generateAssertions(route.sampleOutput, "body");
|
|
@@ -148,14 +354,318 @@ function generateTestFile(routes, framework, baseUrl) {
|
|
|
148
354
|
}
|
|
149
355
|
return lines.join("\n").trimEnd() + "\n";
|
|
150
356
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
357
|
+
// ── Helpers: grouping & naming ──
|
|
358
|
+
function isRouteHandler(name) {
|
|
359
|
+
return /^(GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS)\s+\//.test(name);
|
|
360
|
+
}
|
|
361
|
+
function groupByModule(functions) {
|
|
362
|
+
const groups = {};
|
|
363
|
+
for (const fn of functions) {
|
|
364
|
+
const mod = fn.module || "unknown";
|
|
365
|
+
if (!groups[mod])
|
|
366
|
+
groups[mod] = [];
|
|
367
|
+
groups[mod].push(fn);
|
|
368
|
+
}
|
|
369
|
+
return groups;
|
|
370
|
+
}
|
|
371
|
+
function sanitizeFnName(name) {
|
|
372
|
+
// Handle names like "MyClass.method" → "method" (keep class context in describe)
|
|
373
|
+
// Handle names with special chars
|
|
374
|
+
return name
|
|
375
|
+
.replace(/[^a-zA-Z0-9_$]/g, "_")
|
|
376
|
+
.replace(/^_+|_+$/g, "")
|
|
377
|
+
.replace(/_+/g, "_");
|
|
378
|
+
}
|
|
379
|
+
function sanitizePythonName(name) {
|
|
380
|
+
return name
|
|
381
|
+
.replace(/[^a-zA-Z0-9_]/g, "_")
|
|
382
|
+
.replace(/^_+|_+$/g, "")
|
|
383
|
+
.replace(/_+/g, "_");
|
|
384
|
+
}
|
|
385
|
+
function normalizeImportPath(mod) {
|
|
386
|
+
// Convert file paths to relative import paths
|
|
387
|
+
// e.g., "/Users/.../src/utils.ts" → "./src/utils"
|
|
388
|
+
// e.g., "src/helpers/math.js" → "./src/helpers/math"
|
|
389
|
+
let p = mod;
|
|
390
|
+
// Strip file extension
|
|
391
|
+
p = p.replace(/\.(ts|tsx|js|jsx|mjs|cjs)$/, "");
|
|
392
|
+
// If absolute path, try to make it relative to CWD
|
|
393
|
+
if (path.isAbsolute(p)) {
|
|
394
|
+
const cwd = process.cwd();
|
|
395
|
+
p = path.relative(cwd, p);
|
|
396
|
+
}
|
|
397
|
+
// Ensure starts with ./ or ../
|
|
398
|
+
if (!p.startsWith(".") && !p.startsWith("/")) {
|
|
399
|
+
p = "./" + p;
|
|
400
|
+
}
|
|
401
|
+
return p;
|
|
402
|
+
}
|
|
403
|
+
function normalizePythonImport(mod) {
|
|
404
|
+
// Convert file paths to Python module paths
|
|
405
|
+
// e.g., "app/utils.py" → "app.utils"
|
|
406
|
+
// e.g., "/abs/path/app/models.py" → "app.models"
|
|
407
|
+
let p = mod;
|
|
408
|
+
// Strip .py extension
|
|
409
|
+
p = p.replace(/\.py$/, "");
|
|
410
|
+
// If absolute path, try to make relative
|
|
411
|
+
if (path.isAbsolute(p)) {
|
|
412
|
+
const cwd = process.cwd();
|
|
413
|
+
p = path.relative(cwd, p);
|
|
414
|
+
}
|
|
415
|
+
// Convert path separators to dots
|
|
416
|
+
p = p.replace(/[/\\]/g, ".");
|
|
417
|
+
// Remove leading dots
|
|
418
|
+
p = p.replace(/^\.+/, "");
|
|
419
|
+
// Handle __init__ modules
|
|
420
|
+
p = p.replace(/\.__init__$/, "");
|
|
421
|
+
return p || "app";
|
|
422
|
+
}
|
|
423
|
+
function describeTestCase(input, output, index) {
|
|
424
|
+
// Generate a human-readable test name from the input/output
|
|
425
|
+
if (input === undefined && output === undefined) {
|
|
426
|
+
return `case ${index + 1}`;
|
|
427
|
+
}
|
|
428
|
+
const parts = [];
|
|
429
|
+
// Describe input
|
|
430
|
+
if (input !== undefined) {
|
|
431
|
+
if (input === null) {
|
|
432
|
+
parts.push("given null input");
|
|
433
|
+
}
|
|
434
|
+
else if (Array.isArray(input)) {
|
|
435
|
+
if (input.length === 0) {
|
|
436
|
+
parts.push("given empty args");
|
|
437
|
+
}
|
|
438
|
+
else {
|
|
439
|
+
const argSummaries = input.slice(0, 3).map(summarizeValue);
|
|
440
|
+
parts.push(`given ${argSummaries.join(", ")}`);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
else if (typeof input === "object") {
|
|
444
|
+
// Named args or single object arg
|
|
445
|
+
const keys = Object.keys(input);
|
|
446
|
+
if (keys.length <= 3) {
|
|
447
|
+
parts.push(`given ${keys.join(", ")}`);
|
|
448
|
+
}
|
|
449
|
+
else {
|
|
450
|
+
parts.push(`given ${keys.length} params`);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
else {
|
|
454
|
+
parts.push(`given ${summarizeValue(input)}`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// Describe expected output briefly
|
|
458
|
+
if (output !== undefined) {
|
|
459
|
+
if (output === null) {
|
|
460
|
+
parts.push("returns null");
|
|
461
|
+
}
|
|
462
|
+
else if (Array.isArray(output)) {
|
|
463
|
+
parts.push(`returns array(${output.length})`);
|
|
464
|
+
}
|
|
465
|
+
else if (typeof output === "object") {
|
|
466
|
+
const keys = Object.keys(output);
|
|
467
|
+
parts.push(`returns {${keys.slice(0, 3).join(", ")}${keys.length > 3 ? ", ..." : ""}}`);
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
parts.push(`returns ${summarizeValue(output)}`);
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
const name = parts.join(", ") || `case ${index + 1}`;
|
|
474
|
+
// Escape double quotes for use inside it("...") strings
|
|
475
|
+
return name.replace(/"/g, '\\"');
|
|
476
|
+
}
|
|
477
|
+
function summarizeValue(val) {
|
|
478
|
+
if (val === null)
|
|
479
|
+
return "null";
|
|
480
|
+
if (val === undefined)
|
|
481
|
+
return "undefined";
|
|
482
|
+
if (typeof val === "string") {
|
|
483
|
+
const escaped = val.replace(/"/g, '\\"');
|
|
484
|
+
return escaped.length > 20 ? `"${escaped.slice(0, 17)}..."` : `"${escaped}"`;
|
|
485
|
+
}
|
|
486
|
+
if (typeof val === "number")
|
|
487
|
+
return String(val);
|
|
488
|
+
if (typeof val === "boolean")
|
|
489
|
+
return String(val);
|
|
490
|
+
if (Array.isArray(val))
|
|
491
|
+
return `[${val.length} items]`;
|
|
492
|
+
if (typeof val === "object") {
|
|
493
|
+
const keys = Object.keys(val);
|
|
494
|
+
return `{${keys.length} keys}`;
|
|
495
|
+
}
|
|
496
|
+
return String(val);
|
|
497
|
+
}
|
|
498
|
+
function isPromiseOutput(output) {
|
|
499
|
+
// Heuristic: if the type node says "Promise" it's async
|
|
500
|
+
if (output && typeof output === "object" && "type" in output) {
|
|
501
|
+
const t = output.type;
|
|
502
|
+
if (typeof t === "string" && t.includes("Promise"))
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
// ── Helpers: argument formatting ──
|
|
508
|
+
function formatArgs(input) {
|
|
509
|
+
if (input === undefined || input === null)
|
|
510
|
+
return "";
|
|
511
|
+
// If input is an array, it's positional args
|
|
512
|
+
if (Array.isArray(input)) {
|
|
513
|
+
return input.map(v => formatValue(v)).join(", ");
|
|
514
|
+
}
|
|
515
|
+
// If input is an object, check if it looks like named args or a single object arg
|
|
516
|
+
if (typeof input === "object") {
|
|
517
|
+
const obj = input;
|
|
518
|
+
const keys = Object.keys(obj);
|
|
519
|
+
// If it has typical Express-like keys (params, body, query), it's a route handler — skip
|
|
520
|
+
if (keys.some(k => ["params", "body", "query", "headers"].includes(k))) {
|
|
521
|
+
return formatValue(input);
|
|
522
|
+
}
|
|
523
|
+
// Treat as a single object argument
|
|
524
|
+
return formatValue(input);
|
|
525
|
+
}
|
|
526
|
+
return formatValue(input);
|
|
527
|
+
}
|
|
528
|
+
function formatValue(val) {
|
|
529
|
+
if (val === undefined)
|
|
530
|
+
return "undefined";
|
|
531
|
+
if (val === null)
|
|
532
|
+
return "null";
|
|
533
|
+
if (typeof val === "string")
|
|
534
|
+
return JSON.stringify(val);
|
|
535
|
+
if (typeof val === "number" || typeof val === "boolean")
|
|
536
|
+
return String(val);
|
|
537
|
+
if (Array.isArray(val)) {
|
|
538
|
+
if (val.length === 0)
|
|
539
|
+
return "[]";
|
|
540
|
+
if (val.length <= 5) {
|
|
541
|
+
return `[${val.map(v => formatValue(v)).join(", ")}]`;
|
|
542
|
+
}
|
|
543
|
+
// For large arrays, use JSON.stringify with formatting
|
|
544
|
+
return JSON.stringify(val);
|
|
545
|
+
}
|
|
546
|
+
if (typeof val === "object") {
|
|
547
|
+
return JSON.stringify(val);
|
|
548
|
+
}
|
|
549
|
+
return String(val);
|
|
550
|
+
}
|
|
551
|
+
function formatPythonArgs(input) {
|
|
552
|
+
if (input === undefined || input === null)
|
|
553
|
+
return "";
|
|
554
|
+
if (Array.isArray(input)) {
|
|
555
|
+
return input.map(v => formatPythonValue(v)).join(", ");
|
|
556
|
+
}
|
|
557
|
+
if (typeof input === "object") {
|
|
558
|
+
return formatPythonValue(input);
|
|
559
|
+
}
|
|
560
|
+
return formatPythonValue(input);
|
|
561
|
+
}
|
|
562
|
+
function formatPythonValue(val) {
|
|
563
|
+
if (val === undefined)
|
|
564
|
+
return "None";
|
|
565
|
+
if (val === null)
|
|
566
|
+
return "None";
|
|
567
|
+
if (typeof val === "boolean")
|
|
568
|
+
return val ? "True" : "False";
|
|
569
|
+
if (typeof val === "string")
|
|
570
|
+
return JSON.stringify(val); // JSON string syntax works in Python
|
|
571
|
+
if (typeof val === "number")
|
|
572
|
+
return String(val);
|
|
573
|
+
if (Array.isArray(val)) {
|
|
574
|
+
if (val.length === 0)
|
|
575
|
+
return "[]";
|
|
576
|
+
if (val.length <= 5) {
|
|
577
|
+
return `[${val.map(v => formatPythonValue(v)).join(", ")}]`;
|
|
578
|
+
}
|
|
579
|
+
return toPythonLiteral(val);
|
|
580
|
+
}
|
|
581
|
+
if (typeof val === "object") {
|
|
582
|
+
return toPythonLiteral(val);
|
|
583
|
+
}
|
|
584
|
+
return String(val);
|
|
585
|
+
}
|
|
586
|
+
function toPythonLiteral(val) {
|
|
587
|
+
// Convert JS objects/arrays to Python dict/list syntax
|
|
588
|
+
const json = JSON.stringify(val);
|
|
589
|
+
return json
|
|
590
|
+
.replace(/\bnull\b/g, "None")
|
|
591
|
+
.replace(/\btrue\b/g, "True")
|
|
592
|
+
.replace(/\bfalse\b/g, "False");
|
|
593
|
+
}
|
|
594
|
+
// ── Helpers: assertion generation ──
|
|
595
|
+
function generateOutputAssertions(output, varName) {
|
|
596
|
+
if (output === null)
|
|
597
|
+
return [`expect(${varName}).toBeNull();`];
|
|
598
|
+
if (output === undefined)
|
|
599
|
+
return [`expect(${varName}).toBeDefined();`];
|
|
600
|
+
if (typeof output === "string") {
|
|
601
|
+
return [`expect(typeof ${varName}).toBe("string");`];
|
|
602
|
+
}
|
|
603
|
+
if (typeof output === "number") {
|
|
604
|
+
return [`expect(typeof ${varName}).toBe("number");`];
|
|
605
|
+
}
|
|
606
|
+
if (typeof output === "boolean") {
|
|
607
|
+
return [`expect(typeof ${varName}).toBe("boolean");`];
|
|
608
|
+
}
|
|
609
|
+
if (Array.isArray(output)) {
|
|
610
|
+
const assertions = [`expect(Array.isArray(${varName})).toBe(true);`];
|
|
611
|
+
if (output.length > 0 && typeof output[0] === "object" && output[0] !== null) {
|
|
612
|
+
assertions.push(`expect(${varName}.length).toBeGreaterThan(0);`);
|
|
613
|
+
const itemAssertions = generateOutputAssertions(output[0], `${varName}[0]`);
|
|
614
|
+
assertions.push(...itemAssertions);
|
|
615
|
+
}
|
|
616
|
+
return assertions;
|
|
617
|
+
}
|
|
618
|
+
if (typeof output === "object") {
|
|
619
|
+
return generateAssertions(output, varName);
|
|
620
|
+
}
|
|
621
|
+
return [`expect(${varName}).toBeDefined();`];
|
|
622
|
+
}
|
|
623
|
+
function generatePythonAssertions(output, varName, depth = 0) {
|
|
624
|
+
if (depth > 3)
|
|
625
|
+
return [];
|
|
626
|
+
if (output === null)
|
|
627
|
+
return [`assert ${varName} is None`];
|
|
628
|
+
if (output === undefined)
|
|
629
|
+
return [`assert ${varName} is not None`];
|
|
630
|
+
if (typeof output === "string") {
|
|
631
|
+
return [`assert isinstance(${varName}, str)`];
|
|
632
|
+
}
|
|
633
|
+
if (typeof output === "number") {
|
|
634
|
+
return [`assert isinstance(${varName}, (int, float))`];
|
|
635
|
+
}
|
|
636
|
+
if (typeof output === "boolean") {
|
|
637
|
+
return [`assert isinstance(${varName}, bool)`];
|
|
638
|
+
}
|
|
639
|
+
if (Array.isArray(output)) {
|
|
640
|
+
const assertions = [`assert isinstance(${varName}, list)`];
|
|
641
|
+
if (output.length > 0 && typeof output[0] === "object" && output[0] !== null) {
|
|
642
|
+
assertions.push(`assert len(${varName}) > 0`);
|
|
643
|
+
const itemAssertions = generatePythonAssertions(output[0], `${varName}[0]`, depth + 1);
|
|
644
|
+
assertions.push(...itemAssertions);
|
|
645
|
+
}
|
|
646
|
+
return assertions;
|
|
647
|
+
}
|
|
648
|
+
if (typeof output === "object") {
|
|
649
|
+
const obj = output;
|
|
650
|
+
const assertions = [`assert isinstance(${varName}, dict)`];
|
|
651
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
652
|
+
if (depth < 2) {
|
|
653
|
+
assertions.push(`assert "${key}" in ${varName}`);
|
|
654
|
+
if (value !== null && value !== undefined && typeof value !== "object") {
|
|
655
|
+
const typeAssertions = generatePythonAssertions(value, `${varName}["${key}"]`, depth + 1);
|
|
656
|
+
assertions.push(...typeAssertions);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return assertions;
|
|
661
|
+
}
|
|
662
|
+
return [`assert ${varName} is not None`];
|
|
663
|
+
}
|
|
664
|
+
// ── Route test helpers (preserved from original) ──
|
|
154
665
|
function groupByResource(routes) {
|
|
155
666
|
const groups = {};
|
|
156
667
|
for (const route of routes) {
|
|
157
668
|
const parts = route.path.split("/").filter(Boolean);
|
|
158
|
-
// /api/users → "api/users", /users → "users"
|
|
159
669
|
let resource;
|
|
160
670
|
if (parts[0] === "api" && parts.length >= 2) {
|
|
161
671
|
resource = `/api/${parts[1]}`;
|
|
@@ -169,14 +679,10 @@ function groupByResource(routes) {
|
|
|
169
679
|
}
|
|
170
680
|
return groups;
|
|
171
681
|
}
|
|
172
|
-
/**
|
|
173
|
-
* Try to extract a path param value from sample input.
|
|
174
|
-
*/
|
|
175
682
|
function extractParamFromSample(sampleInput, param) {
|
|
176
683
|
if (!sampleInput || typeof sampleInput !== "object")
|
|
177
684
|
return null;
|
|
178
685
|
const input = sampleInput;
|
|
179
|
-
// Check params object
|
|
180
686
|
if (input.params && typeof input.params === "object") {
|
|
181
687
|
const params = input.params;
|
|
182
688
|
if (params[param] !== undefined)
|
|
@@ -184,9 +690,6 @@ function extractParamFromSample(sampleInput, param) {
|
|
|
184
690
|
}
|
|
185
691
|
return null;
|
|
186
692
|
}
|
|
187
|
-
/**
|
|
188
|
-
* Extract request body from sample input.
|
|
189
|
-
*/
|
|
190
693
|
function extractBodyFromSample(sampleInput) {
|
|
191
694
|
if (!sampleInput || typeof sampleInput !== "object")
|
|
192
695
|
return null;
|
|
@@ -196,26 +699,32 @@ function extractBodyFromSample(sampleInput) {
|
|
|
196
699
|
}
|
|
197
700
|
return null;
|
|
198
701
|
}
|
|
199
|
-
/**
|
|
200
|
-
* Generate expect() assertions for a sample response object.
|
|
201
|
-
* Checks structure (property existence and types), not exact values.
|
|
202
|
-
*/
|
|
203
702
|
function generateAssertions(obj, path, depth = 0) {
|
|
204
703
|
if (depth > 3)
|
|
205
|
-
return [];
|
|
704
|
+
return [];
|
|
206
705
|
const assertions = [];
|
|
207
706
|
for (const [key, value] of Object.entries(obj)) {
|
|
208
707
|
const propPath = `${path}.${key}`;
|
|
708
|
+
// Skip truncated values from sanitizeSample — they have wrong types
|
|
709
|
+
if (value === "[truncated]") {
|
|
710
|
+
assertions.push(`expect(${propPath}).toBeDefined();`);
|
|
711
|
+
continue;
|
|
712
|
+
}
|
|
209
713
|
if (value === null) {
|
|
210
714
|
assertions.push(`expect(${propPath}).toBeNull();`);
|
|
211
715
|
}
|
|
212
716
|
else if (Array.isArray(value)) {
|
|
213
717
|
assertions.push(`expect(Array.isArray(${propPath})).toBe(true);`);
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
718
|
+
if (value.length > 0) {
|
|
719
|
+
// Skip if first item is truncated
|
|
720
|
+
if (value[0] === "[truncated]") {
|
|
721
|
+
assertions.push(`expect(${propPath}.length).toBeGreaterThan(0);`);
|
|
722
|
+
}
|
|
723
|
+
else if (typeof value[0] === "object" && value[0] !== null) {
|
|
724
|
+
assertions.push(`expect(${propPath}.length).toBeGreaterThan(0);`);
|
|
725
|
+
const itemAssertions = generateAssertions(value[0], `${propPath}[0]`, depth + 1);
|
|
726
|
+
assertions.push(...itemAssertions);
|
|
727
|
+
}
|
|
219
728
|
}
|
|
220
729
|
}
|
|
221
730
|
else if (typeof value === "object") {
|
|
@@ -224,7 +733,13 @@ function generateAssertions(obj, path, depth = 0) {
|
|
|
224
733
|
assertions.push(...nestedAssertions);
|
|
225
734
|
}
|
|
226
735
|
else if (typeof value === "string") {
|
|
227
|
-
|
|
736
|
+
// Check if this looks like a truncated string from sanitizeSample
|
|
737
|
+
if (typeof value === "string" && value.endsWith("...")) {
|
|
738
|
+
assertions.push(`expect(typeof ${propPath}).toBe("string");`);
|
|
739
|
+
}
|
|
740
|
+
else {
|
|
741
|
+
assertions.push(`expect(typeof ${propPath}).toBe("string");`);
|
|
742
|
+
}
|
|
228
743
|
}
|
|
229
744
|
else if (typeof value === "number") {
|
|
230
745
|
assertions.push(`expect(typeof ${propPath}).toBe("number");`);
|