trickle-cli 0.1.207 → 0.1.209

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