-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeToScript_test.ts
More file actions
632 lines (580 loc) · 18.8 KB
/
pipeToScript_test.ts
File metadata and controls
632 lines (580 loc) · 18.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
import {
assertEquals,
assertStringIncludes,
// deno-lint-ignore no-import-prefix no-unversioned-import
} from "jsr:@std/assert";
import { pipeToScript } from "./pipeToScript.ts";
import type { Pipe } from "./pipedown.d.ts";
function makePipe(overrides: Partial<Pipe> = {}): Pipe {
return {
name: "TestPipe",
cleanName: "TestPipe",
steps: [],
dir: ".pd/TestPipe",
absoluteDir: "/tmp/.pd/TestPipe",
fileName: "TestPipe",
mdPath: "TestPipe.md",
config: {},
...overrides,
};
}
Deno.test("pipeToScript", async (t) => {
await t.step("generates valid script with single step", async () => {
const pipe = makePipe({
steps: [
{
code: 'input.message = "hello";',
range: [0, 0],
name: "Greet",
funcName: "Greet",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, "async function Greet");
assertStringIncludes(result.script!, 'input.message = "hello";');
});
await t.step("generates funcSequence array", async () => {
const pipe = makePipe({
steps: [
{
code: "input.a = 1;",
range: [0, 0],
name: "StepA",
funcName: "StepA",
inList: false,
},
{
code: "input.b = 2;",
range: [1, 1],
name: "StepB",
funcName: "StepB",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, "StepA, StepB");
});
await t.step("extracts and hoists import statements", async () => {
const pipe = makePipe({
steps: [
{
code: 'import { foo } from "npm:bar";\ninput.x = foo();',
range: [0, 0],
name: "WithImport",
funcName: "WithImport",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
// Import should be at the top level, not inside the function
const lines = result.script!.split("\n");
const importLine = lines.find((l) => l.includes('from "npm:bar"'));
const funcLine = lines.findIndex((l) =>
l.includes("async function WithImport")
);
const importLineIndex = lines.indexOf(importLine!);
assertEquals(importLineIndex < funcLine, true);
});
await t.step("removes import statements from function body", async () => {
const pipe = makePipe({
steps: [
{
code: 'import { foo } from "npm:bar";\ninput.x = foo();',
range: [0, 0],
name: "WithImport",
funcName: "WithImport",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
// Find the function body
const funcMatch = result.script!.match(
/async function WithImport[^{]*\{([\s\S]*?)\n\}/,
);
assertEquals(funcMatch !== null, true);
assertEquals(funcMatch![1].includes("import"), false);
});
await t.step(
"does not leave blank placeholder lines when removing hoisted imports",
async () => {
const pipe = makePipe({
steps: [
{
code:
'import { foo } from "npm:bar";\nimport { baz } from "npm:baz";\n\ninput.value = foo() + baz();',
range: [0, 0],
name: "WithImport",
funcName: "WithImport",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
const lines = result.script!.split("\n");
const functionLine = lines.findIndex((line) =>
line.includes("async function WithImport")
);
assertEquals(functionLine >= 0, true);
// The markdown step intentionally has exactly one blank line between the
// import block and executable code. After hoisting imports, we should
// preserve that single user-authored blank line (line +1) and place code
// immediately after it (line +2), with no extra placeholder lines.
assertEquals(lines[functionLine + 1].trim(), "");
assertStringIncludes(
lines[functionLine + 2],
"input.value = foo() + baz();",
);
},
);
await t.step("handles multiple imports from different steps", async () => {
const pipe = makePipe({
steps: [
{
code: 'import { a } from "npm:pkg-a";\ninput.a = a();',
range: [0, 0],
name: "StepA",
funcName: "StepA",
inList: false,
},
{
code: 'import { b } from "npm:pkg-b";\ninput.b = b();',
range: [1, 1],
name: "StepB",
funcName: "StepB",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, 'from "npm:pkg-a"');
assertStringIncludes(result.script!, 'from "npm:pkg-b"');
});
await t.step("deduplicates identical imports across steps", async () => {
const pipe = makePipe({
steps: [
{
code: 'import { shared } from "npm:shared";\ninput.first = shared();',
range: [0, 0],
name: "First",
funcName: "First",
inList: false,
},
{
code:
'import { shared } from "npm:shared";\ninput.second = shared();',
range: [1, 1],
name: "Second",
funcName: "Second",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
const importCount =
result.script!.match(/import \{ shared \} from "npm:shared";/g)?.length ??
0;
assertEquals(importCount, 1);
});
await t.step(
"keeps distinct import clauses from the same library",
async () => {
const pipe = makePipe({
steps: [
{
code: 'import { a } from "npm:shared";\ninput.a = a();',
range: [0, 0],
name: "StepA",
funcName: "StepA",
inList: false,
},
{
code: 'import { b } from "npm:shared";\ninput.b = b();',
range: [1, 1],
name: "StepB",
funcName: "StepB",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
const sharedImportCount = result.script!.split("\n")
.filter((line) => line.includes('from "npm:shared"')).length;
assertEquals(sharedImportCount, 2);
},
);
await t.step("handles steps with no imports", async () => {
const pipe = makePipe({
steps: [
{
code: "input.x = 42;",
range: [0, 0],
name: "Simple",
funcName: "Simple",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, "input.x = 42;");
});
await t.step("includes standard imports", async () => {
const pipe = makePipe({
steps: [
{
code: "input.x = 1;",
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, "import Pipe from");
assertStringIncludes(result.script!, "import $p from");
assertStringIncludes(result.script!, "import rawPipe from");
});
await t.step("exports pipe and process correctly", async () => {
const pipe = makePipe({
steps: [
{
code: "input.x = 1;",
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, "export default pipe;");
assertStringIncludes(result.script!, "export { pipe, rawPipe, process }");
});
await t.step("exports step functions", async () => {
const pipe = makePipe({
steps: [
{
code: "input.x = 1;",
range: [0, 0],
name: "MyStep",
funcName: "MyStep",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, "export async function MyStep");
});
await t.step(
"hoists imports including those from commented lines",
async () => {
// Note: the regex `import.*from.*` captures the import even from
// `// import...` lines because the match starts at "import", not "//"
// This is known behavior — users should not have commented import lines
const pipe = makePipe({
steps: [
{
code:
'import { current } from "npm:current";\ninput.x = current();',
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, 'from "npm:current"');
},
);
await t.step("handles empty pipe with no steps", async () => {
const pipe = makePipe({ steps: [] });
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, "const funcSequence = [");
});
await t.step("skips dotenv import when build config present", async () => {
const pipe = makePipe({
config: { build: [{ format: "esm" }] },
steps: [
{
code: "input.x = 1;",
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertEquals(result.script!.includes("@std/dotenv"), false);
});
// --- Zod Schema ---
await t.step(
"generates schema validation code when pipe has schema",
async () => {
const pipe = makePipe({
schema:
`import { z } from "npm:zod";\n\nexport const schema = z.object({\n name: z.string(),\n result: z.string().default(""),\n});`,
steps: [
{
code: "input.result = input.name;",
range: [0, 0],
name: "Process",
funcName: "Process",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, 'import { z } from "npm:zod"');
assertStringIncludes(result.script!, "_pd_initSchema");
assertStringIncludes(result.script!, "_pd_validateSchema");
assertStringIncludes(result.script!, "export const schema = z.object");
assertStringIncludes(result.script!, "PipeInput");
},
);
await t.step("warns when schema imports are removed", async () => {
const pipe = makePipe({
schema:
'import { helper } from "./helper.ts";\nimport { z } from "npm:zod";\n\nexport const schema = z.object({\n name: z.string().transform(helper),\n});',
steps: [
{
code: "input.name = 'hi';",
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const originalWarn = console.warn;
const warnings: string[] = [];
console.warn = (message?: unknown, ...args: unknown[]) => {
warnings.push([message, ...args].map(String).join(" "));
};
try {
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertEquals(warnings.length, 1);
assertStringIncludes(
warnings[0],
"removing import statements from pipe schema block",
);
assertStringIncludes(
warnings[0],
'import { helper } from "./helper.ts";',
);
assertStringIncludes(warnings[0], 'import { z } from "npm:zod";');
} finally {
console.warn = originalWarn;
}
});
await t.step(
"funcSequence includes init and validate wrappers with schema",
async () => {
const pipe = makePipe({
schema: `export const schema = z.object({ x: z.number() });`,
steps: [
{
code: "input.x = 1;",
range: [0, 0],
name: "StepA",
funcName: "StepA",
inList: false,
},
{
code: "input.y = 2;",
range: [1, 1],
name: "StepB",
funcName: "StepB",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
// Sequence should be: _pd_initSchema, StepA, _pd_validateSchema_0_StepA, StepB, _pd_validateSchema_1_StepB
// Each validator is named after the step it follows, so errors clearly
// report which step (by name and index) left the input in a bad state.
assertStringIncludes(
result.script!,
"_pd_initSchema, StepA, _pd_validateSchema_0_StepA, StepB, _pd_validateSchema_1_StepB",
);
},
);
await t.step(
"generates schema validation with non-exported const schema",
async () => {
// Users can omit `export` -- the schema variable is still used for
// validation wrappers; it just won't be importable from outside the pipe.
const pipe = makePipe({
schema: `const schema = z.object({\n name: z.string(),\n});`,
steps: [
{
code: 'input.name = "hi";',
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, 'import { z } from "npm:zod"');
assertStringIncludes(result.script!, "_pd_initSchema");
assertStringIncludes(result.script!, "_pd_validateSchema");
assertStringIncludes(result.script!, "PipeInput");
// The `const schema` should appear WITHOUT `export` prepended by us
assertStringIncludes(result.script!, "const schema = z.object");
},
);
await t.step(
"injects helper-only zod block without validation wrappers",
async () => {
// When the zod block has no `schema` variable, definitions are still
// injected at module level so step code can use them (e.g. `.parse()`).
const pipe = makePipe({
schema:
`const AggregatedDeveloper = z.object({\n login: z.string(),\n totalPRs: z.number(),\n});`,
steps: [
{
code: "const dev = AggregatedDeveloper.parse(input.raw);",
range: [0, 0],
name: "TestParse",
funcName: "TestParse",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
// Zod import should be present — the definitions need it
assertStringIncludes(result.script!, 'import { z } from "npm:zod"');
// The definition should be at module level
assertStringIncludes(
result.script!,
"const AggregatedDeveloper = z.object",
);
// No validation wrappers since there's no `schema` variable
assertEquals(result.script!.includes("_pd_initSchema"), false);
assertEquals(result.script!.includes("_pd_validateSchema"), false);
assertEquals(result.script!.includes("PipeInput"), false);
// funcSequence should just include the step, without relying on exact whitespace
assertStringIncludes(result.script!, "TestParse");
},
);
await t.step(
"injects zod block with both schema and helper types",
async () => {
// Mixed block: helper definitions + an exported schema — both should
// appear at module level, and validation wrappers should be generated.
const pipe = makePipe({
schema:
`const Developer = z.object({ login: z.string() });\n\nexport const schema = z.object({\n developers: z.array(Developer),\n});`,
steps: [
{
code: "input.developers = [];",
range: [0, 0],
name: "Init",
funcName: "Init",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
// Helper type should be at module level
assertStringIncludes(result.script!, "const Developer = z.object");
// Schema + validation wrappers should exist
assertStringIncludes(result.script!, "export const schema = z.object");
assertStringIncludes(result.script!, "_pd_initSchema");
assertStringIncludes(result.script!, "PipeInput");
},
);
await t.step(
"does not generate schema code when pipe has no schema",
async () => {
const pipe = makePipe({
steps: [
{
code: "input.x = 1;",
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertEquals(result.script!.includes("_pd_initSchema"), false);
assertEquals(result.script!.includes("_pd_validateSchema"), false);
assertEquals(result.script!.includes("npm:zod"), false);
},
);
await t.step(
"does not render literal 'false' when build config present",
async () => {
const pipe = makePipe({
config: { build: [{ format: "esm" }] },
steps: [
{
code: "input.x = 1;",
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
// Each line of the script should not be the literal "false"
const lines = result.script!.split("\n");
const falseLines = lines.filter((l) => l.trim() === "false");
assertEquals(
falseLines.length,
0,
"Script should not contain a bare 'false' line",
);
},
);
await t.step("includes dotenv import when no build config", async () => {
const pipe = makePipe({
steps: [
{
code: "input.x = 1;",
range: [0, 0],
name: "Step",
funcName: "Step",
inList: false,
},
],
});
const result = await pipeToScript({ pipe });
assertEquals(result.success, true);
assertStringIncludes(result.script!, "@std/dotenv");
});
});