webpipe-js 0.1.0

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/parser.js ADDED
@@ -0,0 +1,594 @@
1
+ class Parser {
2
+ text;
3
+ len;
4
+ pos = 0;
5
+ diagnostics = [];
6
+ pipelineRanges = new Map();
7
+ variableRanges = new Map();
8
+ constructor(text) {
9
+ this.text = text;
10
+ this.len = text.length;
11
+ }
12
+ getDiagnostics() {
13
+ return this.diagnostics.slice();
14
+ }
15
+ getPipelineRanges() {
16
+ return new Map(this.pipelineRanges);
17
+ }
18
+ getVariableRanges() {
19
+ return new Map(this.variableRanges);
20
+ }
21
+ report(message, start, end, severity) {
22
+ this.diagnostics.push({ message, start, end, severity });
23
+ }
24
+ findLineStart(pos) {
25
+ let i = Math.max(0, Math.min(pos, this.len));
26
+ while (i > 0 && this.text[i - 1] !== '\n')
27
+ i--;
28
+ return i;
29
+ }
30
+ findLineEnd(pos) {
31
+ let i = Math.max(0, Math.min(pos, this.len));
32
+ while (i < this.text.length && this.text[i] !== '\n')
33
+ i++;
34
+ return i;
35
+ }
36
+ parseProgram() {
37
+ this.skipSpaces();
38
+ const configs = [];
39
+ const pipelines = [];
40
+ const variables = [];
41
+ const routes = [];
42
+ const describes = [];
43
+ while (!this.eof()) {
44
+ this.skipSpaces();
45
+ if (this.eof())
46
+ break;
47
+ const start = this.pos;
48
+ const cfg = this.tryParse(() => this.parseConfig());
49
+ if (cfg) {
50
+ configs.push(cfg);
51
+ continue;
52
+ }
53
+ const namedPipe = this.tryParse(() => this.parseNamedPipeline());
54
+ if (namedPipe) {
55
+ pipelines.push(namedPipe);
56
+ continue;
57
+ }
58
+ const variable = this.tryParse(() => this.parseVariable());
59
+ if (variable) {
60
+ variables.push(variable);
61
+ continue;
62
+ }
63
+ const route = this.tryParse(() => this.parseRoute());
64
+ if (route) {
65
+ routes.push(route);
66
+ continue;
67
+ }
68
+ const describe = this.tryParse(() => this.parseDescribe());
69
+ if (describe) {
70
+ describes.push(describe);
71
+ continue;
72
+ }
73
+ if (this.pos === start) {
74
+ const lineStart = this.findLineStart(this.pos);
75
+ const lineEnd = this.findLineEnd(this.pos);
76
+ this.report('Unrecognized or unsupported syntax', lineStart, lineEnd, 'warning');
77
+ this.skipToEol();
78
+ if (this.cur() === '\n')
79
+ this.pos++;
80
+ this.consumeWhile((c) => c === '\n');
81
+ }
82
+ }
83
+ const backtickCount = (this.text.match(/`/g) || []).length;
84
+ if (backtickCount % 2 === 1) {
85
+ const idx = this.text.lastIndexOf('`');
86
+ const start = Math.max(0, idx);
87
+ this.report('Unclosed backtick-delimited string', start, start + 1, 'warning');
88
+ }
89
+ return { configs, pipelines, variables, routes, describes };
90
+ }
91
+ eof() { return this.pos >= this.len; }
92
+ peek() { return this.text[this.pos] ?? '\0'; }
93
+ cur() { return this.text[this.pos] ?? '\0'; }
94
+ ahead(n) { return this.text[this.pos + n] ?? '\0'; }
95
+ tryParse(fn) {
96
+ const save = this.pos;
97
+ try {
98
+ const value = fn();
99
+ return value;
100
+ }
101
+ catch (_e) {
102
+ this.pos = save;
103
+ return null;
104
+ }
105
+ }
106
+ skipSpaces() {
107
+ while (true) {
108
+ this.consumeWhile((ch) => ch === ' ' || ch === '\t' || ch === '\r' || ch === '\n');
109
+ if (this.text.startsWith('#', this.pos)) {
110
+ this.skipToEol();
111
+ if (this.cur() === '\n')
112
+ this.pos++;
113
+ continue;
114
+ }
115
+ if (this.text.startsWith('//', this.pos)) {
116
+ this.skipToEol();
117
+ if (this.cur() === '\n')
118
+ this.pos++;
119
+ continue;
120
+ }
121
+ break;
122
+ }
123
+ }
124
+ skipInlineSpaces() {
125
+ this.consumeWhile((ch) => ch === ' ' || ch === '\t' || ch === '\r');
126
+ }
127
+ consumeWhile(pred) {
128
+ const start = this.pos;
129
+ while (!this.eof() && pred(this.text[this.pos]))
130
+ this.pos++;
131
+ return this.text.slice(start, this.pos);
132
+ }
133
+ match(str) {
134
+ if (this.text.startsWith(str, this.pos)) {
135
+ this.pos += str.length;
136
+ return true;
137
+ }
138
+ return false;
139
+ }
140
+ expect(str) {
141
+ if (!this.match(str))
142
+ throw new ParseFailure(`expected '${str}'`, this.pos);
143
+ }
144
+ skipToEol() {
145
+ while (!this.eof() && this.cur() !== '\n')
146
+ this.pos++;
147
+ }
148
+ isIdentStart(ch) {
149
+ return /[A-Za-z_]/.test(ch);
150
+ }
151
+ isIdentCont(ch) {
152
+ return /[A-Za-z0-9_\-]/.test(ch);
153
+ }
154
+ parseIdentifier() {
155
+ if (!this.isIdentStart(this.cur()))
156
+ throw new ParseFailure('identifier', this.pos);
157
+ const start = this.pos;
158
+ this.pos++;
159
+ while (!this.eof() && this.isIdentCont(this.cur()))
160
+ this.pos++;
161
+ return this.text.slice(start, this.pos);
162
+ }
163
+ parseNumber() {
164
+ const start = this.pos;
165
+ const digits = this.consumeWhile((c) => /[0-9]/.test(c));
166
+ if (digits.length === 0)
167
+ throw new ParseFailure('number', this.pos);
168
+ return parseInt(this.text.slice(start, this.pos), 10);
169
+ }
170
+ parseQuotedString() {
171
+ this.expect('"');
172
+ const start = this.pos;
173
+ while (!this.eof()) {
174
+ const ch = this.cur();
175
+ if (ch === '"')
176
+ break;
177
+ this.pos++;
178
+ }
179
+ const content = this.text.slice(start, this.pos);
180
+ this.expect('"');
181
+ return content;
182
+ }
183
+ parseBacktickString() {
184
+ this.expect('`');
185
+ const start = this.pos;
186
+ while (!this.eof()) {
187
+ const ch = this.cur();
188
+ if (ch === '`')
189
+ break;
190
+ this.pos++;
191
+ }
192
+ const content = this.text.slice(start, this.pos);
193
+ this.expect('`');
194
+ return content;
195
+ }
196
+ parseMethod() {
197
+ const methods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'];
198
+ for (const m of methods) {
199
+ if (this.text.startsWith(m, this.pos)) {
200
+ this.pos += m.length;
201
+ return m;
202
+ }
203
+ }
204
+ throw new ParseFailure('method', this.pos);
205
+ }
206
+ parseStepConfig() {
207
+ const bt = this.tryParse(() => this.parseBacktickString());
208
+ if (bt !== null)
209
+ return bt;
210
+ const dq = this.tryParse(() => this.parseQuotedString());
211
+ if (dq !== null)
212
+ return dq;
213
+ const id = this.tryParse(() => this.parseIdentifier());
214
+ if (id !== null)
215
+ return id;
216
+ throw new ParseFailure('step-config', this.pos);
217
+ }
218
+ parseConfigValue() {
219
+ const envWithDefault = this.tryParse(() => {
220
+ this.expect('$');
221
+ const variable = this.parseIdentifier();
222
+ this.skipInlineSpaces();
223
+ this.expect('||');
224
+ this.skipInlineSpaces();
225
+ const def = this.parseQuotedString();
226
+ return { kind: 'EnvVar', var: variable, default: def };
227
+ });
228
+ if (envWithDefault)
229
+ return envWithDefault;
230
+ const envNoDefault = this.tryParse(() => {
231
+ this.expect('$');
232
+ const variable = this.parseIdentifier();
233
+ return { kind: 'EnvVar', var: variable };
234
+ });
235
+ if (envNoDefault)
236
+ return envNoDefault;
237
+ const str = this.tryParse(() => this.parseQuotedString());
238
+ if (str !== null)
239
+ return { kind: 'String', value: str };
240
+ const bool = this.tryParse(() => {
241
+ if (this.match('true'))
242
+ return true;
243
+ if (this.match('false'))
244
+ return false;
245
+ throw new ParseFailure('bool', this.pos);
246
+ });
247
+ if (bool !== null)
248
+ return { kind: 'Boolean', value: bool };
249
+ const num = this.tryParse(() => this.parseNumber());
250
+ if (num !== null)
251
+ return { kind: 'Number', value: num };
252
+ throw new ParseFailure('config-value', this.pos);
253
+ }
254
+ parseConfigProperty() {
255
+ this.skipSpaces();
256
+ const key = this.parseIdentifier();
257
+ this.skipInlineSpaces();
258
+ this.expect(':');
259
+ this.skipInlineSpaces();
260
+ const value = this.parseConfigValue();
261
+ return { key, value };
262
+ }
263
+ parseConfig() {
264
+ this.expect('config');
265
+ this.skipInlineSpaces();
266
+ const name = this.parseIdentifier();
267
+ this.skipInlineSpaces();
268
+ this.expect('{');
269
+ this.skipSpaces();
270
+ const properties = [];
271
+ while (true) {
272
+ const prop = this.tryParse(() => this.parseConfigProperty());
273
+ if (!prop)
274
+ break;
275
+ properties.push(prop);
276
+ this.skipSpaces();
277
+ }
278
+ this.skipSpaces();
279
+ this.expect('}');
280
+ this.skipSpaces();
281
+ return { name, properties };
282
+ }
283
+ parsePipelineStep() {
284
+ const result = this.tryParse(() => this.parseResultStep());
285
+ if (result)
286
+ return result;
287
+ return this.parseRegularStep();
288
+ }
289
+ parseRegularStep() {
290
+ this.skipSpaces();
291
+ this.expect('|>');
292
+ this.skipInlineSpaces();
293
+ const name = this.parseIdentifier();
294
+ this.expect(':');
295
+ this.skipInlineSpaces();
296
+ const config = this.parseStepConfig();
297
+ this.skipSpaces();
298
+ return { kind: 'Regular', name, config };
299
+ }
300
+ parseResultStep() {
301
+ this.skipSpaces();
302
+ this.expect('|>');
303
+ this.skipInlineSpaces();
304
+ this.expect('result');
305
+ this.skipSpaces();
306
+ const branches = [];
307
+ while (true) {
308
+ const br = this.tryParse(() => this.parseResultBranch());
309
+ if (!br)
310
+ break;
311
+ branches.push(br);
312
+ }
313
+ return { kind: 'Result', branches };
314
+ }
315
+ parseResultBranch() {
316
+ this.skipSpaces();
317
+ const branchIdent = this.parseIdentifier();
318
+ let branchType;
319
+ if (branchIdent === 'ok')
320
+ branchType = { kind: 'Ok' };
321
+ else if (branchIdent === 'default')
322
+ branchType = { kind: 'Default' };
323
+ else
324
+ branchType = { kind: 'Custom', name: branchIdent };
325
+ this.expect('(');
326
+ const statusCode = this.parseNumber();
327
+ if (statusCode < 100 || statusCode > 599) {
328
+ this.report(`Invalid HTTP status code: ${statusCode}`, this.pos - String(statusCode).length, this.pos, 'error');
329
+ }
330
+ this.expect(')');
331
+ this.expect(':');
332
+ this.skipSpaces();
333
+ const pipeline = this.parsePipeline();
334
+ return { branchType, statusCode, pipeline };
335
+ }
336
+ parsePipeline() {
337
+ const steps = [];
338
+ while (true) {
339
+ const save = this.pos;
340
+ this.skipSpaces();
341
+ if (!this.text.startsWith('|>', this.pos)) {
342
+ this.pos = save;
343
+ break;
344
+ }
345
+ const step = this.parsePipelineStep();
346
+ steps.push(step);
347
+ }
348
+ return { steps };
349
+ }
350
+ parseNamedPipeline() {
351
+ const start = this.pos;
352
+ this.expect('pipeline');
353
+ this.skipInlineSpaces();
354
+ const name = this.parseIdentifier();
355
+ this.skipInlineSpaces();
356
+ this.expect('=');
357
+ this.skipInlineSpaces();
358
+ const beforePipeline = this.pos;
359
+ const pipeline = this.parsePipeline();
360
+ const end = this.pos;
361
+ this.pipelineRanges.set(name, { start, end });
362
+ this.skipSpaces();
363
+ return { name, pipeline };
364
+ }
365
+ parsePipelineRef() {
366
+ const inline = this.tryParse(() => this.parsePipeline());
367
+ if (inline && inline.steps.length > 0)
368
+ return { kind: 'Inline', pipeline: inline };
369
+ const named = this.tryParse(() => {
370
+ this.skipSpaces();
371
+ this.expect('|>');
372
+ this.skipInlineSpaces();
373
+ this.expect('pipeline:');
374
+ this.skipInlineSpaces();
375
+ const name = this.parseIdentifier();
376
+ return { kind: 'Named', name };
377
+ });
378
+ if (named)
379
+ return named;
380
+ throw new Error('pipeline-ref');
381
+ }
382
+ parseVariable() {
383
+ const start = this.pos;
384
+ const varType = this.parseIdentifier();
385
+ this.skipInlineSpaces();
386
+ const name = this.parseIdentifier();
387
+ this.skipInlineSpaces();
388
+ this.expect('=');
389
+ this.skipInlineSpaces();
390
+ const value = this.parseBacktickString();
391
+ const end = this.pos;
392
+ this.variableRanges.set(`${varType}::${name}`, { start, end });
393
+ this.skipSpaces();
394
+ return { varType, name, value };
395
+ }
396
+ parseRoute() {
397
+ const method = this.parseMethod();
398
+ this.skipInlineSpaces();
399
+ const path = this.consumeWhile((c) => c !== ' ' && c !== '\n');
400
+ this.skipSpaces();
401
+ const pipeline = this.parsePipelineRef();
402
+ this.skipSpaces();
403
+ return { method, path, pipeline };
404
+ }
405
+ parseWhen() {
406
+ const calling = this.tryParse(() => {
407
+ this.expect('calling');
408
+ this.skipInlineSpaces();
409
+ const method = this.parseMethod();
410
+ this.skipInlineSpaces();
411
+ const path = this.consumeWhile((c) => c !== '\n');
412
+ return { kind: 'CallingRoute', method, path };
413
+ });
414
+ if (calling)
415
+ return calling;
416
+ const executingPipeline = this.tryParse(() => {
417
+ this.expect('executing');
418
+ this.skipInlineSpaces();
419
+ this.expect('pipeline');
420
+ this.skipInlineSpaces();
421
+ const name = this.parseIdentifier();
422
+ return { kind: 'ExecutingPipeline', name };
423
+ });
424
+ if (executingPipeline)
425
+ return executingPipeline;
426
+ const executingVariable = this.tryParse(() => {
427
+ this.expect('executing');
428
+ this.skipInlineSpaces();
429
+ this.expect('variable');
430
+ this.skipInlineSpaces();
431
+ const varType = this.parseIdentifier();
432
+ this.skipInlineSpaces();
433
+ const name = this.parseIdentifier();
434
+ return { kind: 'ExecutingVariable', varType, name };
435
+ });
436
+ if (executingVariable)
437
+ return executingVariable;
438
+ throw new ParseFailure('when', this.pos);
439
+ }
440
+ parseCondition() {
441
+ this.skipSpaces();
442
+ const ct = (() => {
443
+ if (this.match('then'))
444
+ return 'Then';
445
+ if (this.match('and'))
446
+ return 'And';
447
+ throw new Error('condition-type');
448
+ })();
449
+ this.skipInlineSpaces();
450
+ const field = this.consumeWhile((c) => c !== ' ' && c !== '\n' && c !== '`');
451
+ this.skipInlineSpaces();
452
+ const jqExpr = this.tryParse(() => this.parseBacktickString());
453
+ this.skipInlineSpaces();
454
+ const comparison = this.consumeWhile((c) => c !== ' ' && c !== '\n');
455
+ this.skipInlineSpaces();
456
+ const value = (() => {
457
+ const v1 = this.tryParse(() => this.parseBacktickString());
458
+ if (v1 !== null)
459
+ return v1;
460
+ const v2 = this.tryParse(() => this.parseQuotedString());
461
+ if (v2 !== null)
462
+ return v2;
463
+ return this.consumeWhile((c) => c !== '\n');
464
+ })();
465
+ return { conditionType: ct, field, jqExpr: jqExpr ?? undefined, comparison, value };
466
+ }
467
+ parseMockHead(prefixWord) {
468
+ this.skipSpaces();
469
+ this.expect(prefixWord);
470
+ this.skipInlineSpaces();
471
+ this.expect('mock');
472
+ this.skipInlineSpaces();
473
+ const target = this.consumeWhile((c) => c !== ' ' && c !== '\n');
474
+ this.skipInlineSpaces();
475
+ this.expect('returning');
476
+ this.skipInlineSpaces();
477
+ const returnValue = this.parseBacktickString();
478
+ this.skipSpaces();
479
+ return { target, returnValue };
480
+ }
481
+ parseMock() {
482
+ return this.parseMockHead('with');
483
+ }
484
+ parseAndMock() {
485
+ return this.parseMockHead('and');
486
+ }
487
+ parseIt() {
488
+ this.skipSpaces();
489
+ this.expect('it');
490
+ this.skipInlineSpaces();
491
+ this.expect('"');
492
+ const name = this.consumeWhile((c) => c !== '"');
493
+ this.expect('"');
494
+ this.skipSpaces();
495
+ const mocks = [];
496
+ while (true) {
497
+ const m = this.tryParse(() => this.parseMock());
498
+ if (!m)
499
+ break;
500
+ mocks.push(m);
501
+ }
502
+ this.expect('when');
503
+ this.skipInlineSpaces();
504
+ const when = this.parseWhen();
505
+ this.skipSpaces();
506
+ const input = this.tryParse(() => {
507
+ this.expect('with');
508
+ this.skipInlineSpaces();
509
+ this.expect('input');
510
+ this.skipInlineSpaces();
511
+ const v = this.parseBacktickString();
512
+ this.skipSpaces();
513
+ return v;
514
+ }) ?? undefined;
515
+ const extraMocks = [];
516
+ while (true) {
517
+ const m = this.tryParse(() => this.parseAndMock());
518
+ if (!m)
519
+ break;
520
+ extraMocks.push(m);
521
+ this.skipSpaces();
522
+ }
523
+ const conditions = [];
524
+ while (true) {
525
+ const c = this.tryParse(() => this.parseCondition());
526
+ if (!c)
527
+ break;
528
+ conditions.push(c);
529
+ }
530
+ return { name, mocks: [...mocks, ...extraMocks], when, input, conditions };
531
+ }
532
+ parseDescribe() {
533
+ this.skipSpaces();
534
+ this.expect('describe');
535
+ this.skipInlineSpaces();
536
+ this.expect('"');
537
+ const name = this.consumeWhile((c) => c !== '"');
538
+ this.expect('"');
539
+ this.skipSpaces();
540
+ const mocks = [];
541
+ while (true) {
542
+ const m = this.tryParse(() => this.parseMock());
543
+ if (!m)
544
+ break;
545
+ mocks.push(m);
546
+ this.skipSpaces();
547
+ }
548
+ const tests = [];
549
+ while (true) {
550
+ const it = this.tryParse(() => this.parseIt());
551
+ if (!it)
552
+ break;
553
+ tests.push(it);
554
+ }
555
+ return { name, mocks, tests };
556
+ }
557
+ }
558
+ export function parseProgram(text) {
559
+ const parser = new Parser(text);
560
+ return parser.parseProgram();
561
+ }
562
+ export function parseProgramWithDiagnostics(text) {
563
+ const parser = new Parser(text);
564
+ const program = parser.parseProgram();
565
+ return { program, diagnostics: parser.getDiagnostics() };
566
+ }
567
+ export function getPipelineRanges(text) {
568
+ const parser = new Parser(text);
569
+ parser.parseProgram();
570
+ return parser.getPipelineRanges();
571
+ }
572
+ export function getVariableRanges(text) {
573
+ const parser = new Parser(text);
574
+ parser.parseProgram();
575
+ return parser.getVariableRanges();
576
+ }
577
+ class ParseFailure extends Error {
578
+ at;
579
+ constructor(message, at) {
580
+ super(message);
581
+ this.at = at;
582
+ }
583
+ }
584
+ if (import.meta.url === `file://${process.argv[1]}`) {
585
+ const fs = await import('node:fs/promises');
586
+ const path = process.argv[2];
587
+ if (!path) {
588
+ console.error('Usage: node dist/parser.js <file.wp>');
589
+ process.exit(1);
590
+ }
591
+ const src = await fs.readFile(path, 'utf8');
592
+ const program = parseProgram(src);
593
+ console.log(JSON.stringify(program, null, 2));
594
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,106 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { resolve } from 'node:path';
3
+ import { describe, it, expect, beforeAll } from 'vitest';
4
+ import { parseProgram } from '../parser';
5
+ function loadFixture(name) {
6
+ const file = resolve(process.cwd(), name);
7
+ return readFileSync(file, 'utf8');
8
+ }
9
+ describe('parseProgram - comprehensive_test.wp', () => {
10
+ let program;
11
+ beforeAll(() => {
12
+ const src = loadFixture('comprehensive_test.wp');
13
+ program = parseProgram(src);
14
+ });
15
+ it('parses at least one route', () => {
16
+ expect(program.routes.length).toBeGreaterThan(0);
17
+ const hello = program.routes.find(r => r.path.startsWith('/hello'));
18
+ expect(hello).toBeTruthy();
19
+ expect(hello.method).toBe('GET');
20
+ // First step should be jq with config string
21
+ if (hello.pipeline.kind === 'Inline') {
22
+ const first = hello.pipeline.pipeline.steps[0];
23
+ expect(first.kind).toBe('Regular');
24
+ if (first.kind === 'Regular') {
25
+ expect(first.name).toBe('jq');
26
+ expect(typeof first.config).toBe('string');
27
+ }
28
+ }
29
+ });
30
+ it('parses configs', () => {
31
+ expect(program.configs.length).toBeGreaterThan(0);
32
+ const pg = program.configs.find(c => c.name === 'pg');
33
+ expect(pg).toBeTruthy();
34
+ expect(pg.properties.length).toBeGreaterThan(3);
35
+ const host = pg.properties.find(p => p.key === 'host');
36
+ expect(host).toBeTruthy();
37
+ expect(host.value.kind === 'EnvVar' || host.value.kind === 'String').toBe(true);
38
+ });
39
+ it('parses named pipelines and references', () => {
40
+ const np = program.pipelines.find(p => p.name === 'getTeams');
41
+ expect(np).toBeTruthy();
42
+ const route = program.routes.find(r => r.path.startsWith('/page/'));
43
+ expect(route).toBeTruthy();
44
+ expect(route.pipeline.kind).toBe('Inline');
45
+ if (route.pipeline.kind === 'Inline') {
46
+ const step = route.pipeline.pipeline.steps[0];
47
+ expect(step.kind).toBe('Regular');
48
+ if (step.kind === 'Regular') {
49
+ expect(step.name).toBe('pipeline');
50
+ expect(step.config).toBe('getTeams');
51
+ }
52
+ }
53
+ });
54
+ it('parses variables', () => {
55
+ expect(program.variables.length).toBeGreaterThan(0);
56
+ const teamsQuery = program.variables.find(v => v.name === 'teamsQuery');
57
+ expect(teamsQuery).toBeTruthy();
58
+ expect(teamsQuery.varType).toBe('pg');
59
+ expect(typeof teamsQuery.value).toBe('string');
60
+ });
61
+ it('parses describes and tests with conditions', () => {
62
+ expect(program.describes.length).toBeGreaterThan(0);
63
+ const d = program.describes.find(dd => dd.name.includes('hello'));
64
+ expect(d).toBeTruthy();
65
+ expect(d.tests.length).toBeGreaterThan(0);
66
+ const t0 = d.tests[0];
67
+ expect(t0.when).toBeTruthy();
68
+ expect(t0.conditions.length).toBeGreaterThan(0);
69
+ });
70
+ });
71
+ describe('parseProgram - focused samples', () => {
72
+ it('parses result branches', () => {
73
+ const src = `GET /test\n |> jq: \`{message: "x"}\`\n |> result\n ok(200):\n |> jq: \`{ok: true}\`\n default(500):\n |> jq: \`{ok: false}\``;
74
+ const program = parseProgram(src);
75
+ expect(program.routes.length).toBe(1);
76
+ const steps = program.routes[0].pipeline.pipeline.steps;
77
+ const res = steps.find(s => s.kind === 'Result');
78
+ expect(res).toBeTruthy();
79
+ expect(res.branches.length).toBe(2);
80
+ expect(res.branches[0].statusCode).toBe(200);
81
+ expect(res.branches[1].statusCode).toBe(500);
82
+ });
83
+ it('parses POST with body conversion pipeline', () => {
84
+ const src = `POST /users\n |> jq: \`{ name: .body.name }\``;
85
+ const program = parseProgram(src);
86
+ expect(program.routes[0].method).toBe('POST');
87
+ const first = program.routes[0].pipeline.pipeline.steps[0];
88
+ expect(first.name).toBe('jq');
89
+ });
90
+ it('parses inline and named pipeline refs', () => {
91
+ const src = `pipeline p =\n |> jq: \`{x:1}\`\n\nGET /a\n |> pipeline: p`;
92
+ const program = parseProgram(src);
93
+ const p = program.pipelines.find(pp => pp.name === 'p');
94
+ expect(p).toBeTruthy();
95
+ const route = program.routes[0];
96
+ expect(route.pipeline.kind).toBe('Inline');
97
+ if (route.pipeline.kind === 'Inline') {
98
+ const step = route.pipeline.pipeline.steps[0];
99
+ expect(step.kind).toBe('Regular');
100
+ if (step.kind === 'Regular') {
101
+ expect(step.name).toBe('pipeline');
102
+ expect(step.config).toBe('p');
103
+ }
104
+ }
105
+ });
106
+ });