forked from TestSprite/testsprite-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.subprocess.test.ts
More file actions
1240 lines (1167 loc) · 46.7 KB
/
Copy pathcli.subprocess.test.ts
File metadata and controls
1240 lines (1167 loc) · 46.7 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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Subprocess test that builds the CLI binary and runs it as a real child
* process against an in-memory HTTP server. Catches packaging / ESM /
* shebang issues that unit tests cannot see.
*
* Per design.md §12.4 hard gate: "Subprocess test that builds the binary
* and runs `auth whoami` against the mock."
*/
import { execFileSync, spawn } from 'node:child_process';
import { existsSync, mkdtempSync, statSync } from 'node:fs';
import type { IncomingMessage, Server, ServerResponse } from 'node:http';
import { createServer } from 'node:http';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
const __dirname = dirname(fileURLToPath(import.meta.url));
const REPO_ROOT = resolve(__dirname, '..');
const BIN_PATH = join(REPO_ROOT, 'dist', 'index.js');
const ME_BODY = {
userId: 'u-subproc',
keyId: 'k-subproc',
scopes: ['read:projects', 'read:tests'],
env: 'development',
};
let server: Server;
let baseUrl: string;
let tmpHome: string;
beforeAll(async () => {
// Always rebuild — `npm run build` is fast and a stale `dist/index.js`
// would silently mask ESM/import regressions in this suite. The
// existsSync skip we used to do here let `dist` rot under
// refactors and gave false-green on `project list` once
// already.
execFileSync('npm', ['run', 'build'], { cwd: REPO_ROOT, stdio: 'pipe' });
server = createServer((req: IncomingMessage, res: ServerResponse) => {
const url = req.url ?? '/';
if (url.startsWith('/api/cli/v1/projects/')) {
const id = url.replace('/api/cli/v1/projects/', '').split('?')[0]!;
const apiKey = req.headers['x-api-key'];
if (typeof apiKey !== 'string' || apiKey === '') {
res.writeHead(401, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'AUTH_REQUIRED',
message: 'Authentication is required.',
nextAction: '',
requestId: 'req_subproc',
details: {},
},
}),
);
return;
}
if (id === 'project_subproc') {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
id: 'project_subproc',
name: 'Subproc Fixture',
type: 'frontend',
createdFrom: 'portal',
createdAt: '2026-05-01T00:00:00.000Z',
updatedAt: '2026-05-05T00:00:00.000Z',
}),
);
return;
}
res.writeHead(404, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'NOT_FOUND',
message: 'Resource not found.',
nextAction: '',
requestId: 'req_subproc',
details: { resource: 'project', id },
},
}),
);
return;
}
if (url.startsWith('/api/cli/v1/tests/')) {
const tail = url.replace('/api/cli/v1/tests/', '').split('?')[0]!;
const apiKey = req.headers['x-api-key'];
if (typeof apiKey !== 'string' || apiKey === '') {
res.writeHead(401, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'AUTH_REQUIRED',
message: 'Authentication is required.',
nextAction: '',
requestId: 'req_subproc',
details: {},
},
}),
);
return;
}
const segments = tail.split('/');
const testId = segments[0]!;
const subPath = segments[1];
if (testId === 'test_subproc' && subPath === undefined) {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
id: 'test_subproc',
projectId: 'project_subproc',
name: 'Subproc test fixture',
type: 'frontend',
createdFrom: 'portal',
status: 'failed',
createdAt: '2026-04-20T11:00:00.000Z',
updatedAt: '2026-05-05T12:34:56.000Z',
}),
);
return;
}
if (testId === 'test_subproc' && subPath === 'code') {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
testId: 'test_subproc',
language: 'typescript',
framework: 'playwright',
code: [
"import { test } from '@playwright/test';",
"test('subproc happy path', async () => {});",
'',
].join('\n'),
codeVersion: 'v3',
etag: 'sha256:subproc',
}),
);
return;
}
if (testId === 'test_subproc' && subPath === 'steps') {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
items: [
{
testId: 'test_subproc',
stepIndex: 1,
action: 'click',
description: 'Click the cart icon',
status: 'passed',
screenshotUrl: 'https://example.com/01.png',
htmlSnapshotUrl: 'https://example.com/01.html',
runIdIfAvailable: 'run_subproc',
codeVersion: 'v3',
capturedAt: '2026-05-05T12:34:55.000Z',
updatedAt: '2026-05-05T12:34:55.000Z',
},
{
testId: 'test_subproc',
stepIndex: 2,
action: 'click',
description: 'Click the submit button',
status: 'failed',
screenshotUrl: 'https://example.com/02.png',
htmlSnapshotUrl: 'https://example.com/02.html',
runIdIfAvailable: 'run_subproc',
codeVersion: 'v3',
capturedAt: '2026-05-05T12:34:56.000Z',
updatedAt: '2026-05-05T12:34:56.000Z',
},
],
nextToken: null,
}),
);
return;
}
if (testId === 'test_subproc' && subPath === 'result') {
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
testId: 'test_subproc',
status: 'failed',
startedAt: '2026-05-05T12:34:00.000Z',
finishedAt: '2026-05-05T12:34:58.000Z',
videoUrl: 'https://example.com/run_subproc.mp4',
failureAnalysisUrl: 'https://example.com/analysis.json',
snapshotId: 'snap_subproc',
runIdIfAvailable: 'run_subproc',
codeVersion: 'v3',
targetUrl: 'https://staging.example.com/checkout',
failedStepIndex: 2,
failureKind: 'assertion',
verdict: 'failed',
executionStatus: 'completed',
summary: 'Failed (assertion) on step 2: assertion error.',
}),
);
return;
}
res.writeHead(404, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'NOT_FOUND',
message: 'Resource not found.',
nextAction: '',
requestId: 'req_subproc',
details: { resource: 'test', id: tail },
},
}),
);
return;
}
if (url.startsWith('/api/cli/v1/tests')) {
const apiKey = req.headers['x-api-key'];
if (typeof apiKey !== 'string' || apiKey === '') {
res.writeHead(401, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'AUTH_REQUIRED',
message: 'Authentication is required.',
nextAction: 'Run `testsprite auth configure`.',
requestId: 'req_subproc',
details: {},
},
}),
);
return;
}
const params = new URLSearchParams(url.split('?')[1] ?? '');
const projectId = params.get('projectId');
if (!projectId) {
res.writeHead(400, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'VALIDATION_ERROR',
message: 'Invalid request.',
nextAction: 'Field `projectId` is required.',
requestId: 'req_subproc',
details: { field: 'projectId', reason: 'required' },
},
}),
);
return;
}
const items = [
{
id: 'test_subproc',
projectId: 'project_subproc',
name: 'Subproc test fixture',
type: 'frontend' as const,
createdFrom: 'portal' as const,
status: 'failed' as const,
createdAt: '2026-04-20T11:00:00.000Z',
updatedAt: '2026-05-05T12:34:56.000Z',
},
];
const typeFilter = params.get('type');
const filtered = typeFilter ? items.filter(t => t.type === typeFilter) : items;
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify({ items: filtered, nextToken: null }));
return;
}
if (url.startsWith('/api/cli/v1/projects')) {
const apiKey = req.headers['x-api-key'];
if (typeof apiKey !== 'string' || apiKey === '') {
res.writeHead(401, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'AUTH_REQUIRED',
message: 'Authentication is required.',
nextAction: 'Run `testsprite auth configure`.',
requestId: 'req_subproc',
details: {},
},
}),
);
return;
}
// Single-project listing — enough to exercise text+json paths.
res.writeHead(200, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
items: [
{
id: 'project_subproc',
name: 'Subproc Fixture',
type: 'frontend',
createdFrom: 'portal',
createdAt: '2026-05-01T00:00:00.000Z',
updatedAt: '2026-05-05T00:00:00.000Z',
},
],
nextToken: null,
}),
);
return;
}
if (url === '/api/cli/v1/me') {
const apiKey = req.headers['x-api-key'];
if (typeof apiKey !== 'string' || apiKey === '') {
res.writeHead(401, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'AUTH_REQUIRED',
message: 'Authentication is required.',
nextAction: 'Run `testsprite auth configure`.',
requestId: 'req_subproc',
details: {},
},
}),
);
return;
}
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify(ME_BODY));
return;
}
res.writeHead(404, { 'content-type': 'application/json' });
res.end(
JSON.stringify({
error: {
code: 'NOT_FOUND',
message: 'Not found.',
nextAction: '',
requestId: 'req_subproc',
details: {},
},
}),
);
});
await new Promise<void>(resolveListen => {
server.listen(0, '127.0.0.1', () => resolveListen());
});
const address = server.address();
if (typeof address !== 'object' || address === null) {
throw new Error('server.address() did not return an AddressInfo');
}
baseUrl = `http://127.0.0.1:${address.port}`;
tmpHome = mkdtempSync(join(tmpdir(), 'testsprite-subproc-'));
}, 60_000);
afterAll(async () => {
await new Promise<void>(resolveClose => server.close(() => resolveClose()));
});
interface SpawnResult {
exitCode: number;
stdout: string;
stderr: string;
}
function runCli(args: string[], envOverrides: Record<string, string> = {}): Promise<SpawnResult> {
return new Promise((resolveResult, rejectResult) => {
const child = spawn('node', [BIN_PATH, ...args], {
cwd: REPO_ROOT,
env: {
...process.env,
HOME: tmpHome,
TESTSPRITE_API_KEY: undefined,
TESTSPRITE_API_URL: undefined,
...envOverrides,
} as NodeJS.ProcessEnv,
});
let stdout = '';
let stderr = '';
child.stdout.on('data', chunk => (stdout += chunk.toString()));
child.stderr.on('data', chunk => (stderr += chunk.toString()));
child.on('error', rejectResult);
child.on('close', code => resolveResult({ exitCode: code ?? -1, stdout, stderr }));
});
}
describe('auth status subprocess (+ deprecated whoami alias)', () => {
it('prints JSON me and exits 0 against the local server', async () => {
const result = await runCli(['auth', 'status', '--output', 'json'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout);
expect(parsed).toEqual(ME_BODY);
expect(result.stderr).toBe('');
}, 30_000);
it('exits 3 with AUTH_REQUIRED when no key is configured (text mode)', async () => {
const result = await runCli(['auth', 'status'], {
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(3);
expect(result.stderr).toContain('Authentication is required.');
expect(result.stderr).toContain('testsprite setup');
}, 30_000);
it('--output json emits a parseable error envelope on AUTH_REQUIRED', async () => {
const result = await runCli(['--output', 'json', 'auth', 'status'], {
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(3);
const parsed = JSON.parse(result.stderr) as {
error: { code: string; nextAction: string; requestId: string };
};
expect(parsed.error.code).toBe('AUTH_REQUIRED');
expect(parsed.error.nextAction).toContain('testsprite setup');
}, 30_000);
it('text mode renders userId/scopes legibly', async () => {
const result = await runCli(['auth', 'status'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('userId: u-subproc');
expect(result.stdout).toContain('scopes: read:projects, read:tests');
}, 30_000);
it('--debug emits structured debug events to stderr without leaking the key', async () => {
const result = await runCli(['--debug', 'auth', 'status', '--output', 'json'], {
TESTSPRITE_API_KEY: 'sk-subproc-secret',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
expect(result.stderr).toContain('"kind":"request"');
expect(result.stderr).toContain('"kind":"response"');
expect(result.stderr).not.toContain('sk-subproc-secret');
expect(result.stderr).not.toContain('x-api-key');
}, 30_000);
it('deprecated `auth whoami` alias still works and prints a deprecation notice', async () => {
const result = await runCli(['auth', 'whoami', '--output', 'json'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
expect(JSON.parse(result.stdout)).toEqual(ME_BODY);
expect(result.stderr).toContain('[deprecated]');
expect(result.stderr).toContain('auth status');
}, 30_000);
});
describe('project list subprocess', () => {
it('--output json returns the §6.1 ProjectList shape', async () => {
const result = await runCli(['--output', 'json', 'project', 'list'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout);
expect(parsed.items).toHaveLength(1);
expect(parsed.items[0].id).toBe('project_subproc');
expect(parsed.nextToken).toBeNull();
}, 30_000);
it('text output renders a header row and the project name', async () => {
const result = await runCli(['project', 'list'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('ID');
expect(result.stdout).toContain('NAME');
expect(result.stdout).toContain('Subproc Fixture');
}, 30_000);
it('--help prints flag documentation for pagination', async () => {
const result = await runCli(['project', 'list', '--help']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('--page-size');
expect(result.stdout).toContain('--starting-token');
expect(result.stdout).toContain('--max-items');
}, 30_000);
it('--page-size 0 exits 5 (VALIDATION_ERROR), not 1 (generic)', async () => {
const result = await runCli(['--output', 'json', 'project', 'list', '--page-size', '0'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(5);
const parsed = JSON.parse(result.stderr) as { error: { code: string } };
expect(parsed.error.code).toBe('VALIDATION_ERROR');
}, 30_000);
it('--page-size 101 exits 5 (VALIDATION_ERROR) — upper-bound enforced client-side', async () => {
// Previously silently clamped to 100; now rejected at exit 5 so callers
// get fast feedback that the value is out of range (Fix 7 — B-E2E-01 wave).
const result = await runCli(['--output', 'json', 'project', 'list', '--page-size', '101'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(5);
const parsed = JSON.parse(result.stderr) as { error: { code: string } };
expect(parsed.error.code).toBe('VALIDATION_ERROR');
}, 30_000);
it('--request-timeout 30s exits 5 (VALIDATION_ERROR), not a silent fallback to 120s', async () => {
// Previously an invalid flag value resolved to `undefined` and the command
// silently ran with the default 120s deadline — the operator believed they
// had set a timeout but had not. Now the explicit flag is validated like
// every other flag.
const result = await runCli(
['--output', 'json', '--request-timeout', '30s', 'project', 'list'],
{
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
},
);
expect(result.exitCode).toBe(5);
const parsed = JSON.parse(result.stderr) as { error: { code: string; nextAction: string } };
expect(parsed.error.code).toBe('VALIDATION_ERROR');
expect(parsed.error.nextAction).toContain('request-timeout');
}, 30_000);
});
describe('malformed --endpoint-url is rejected (exit 5), not retried as a network error', () => {
// Previously: a malformed endpoint surfaced either as an opaque `Invalid URL`
// (exit 1) or, for a missing/wrong scheme, as a `fetch failed` UNAVAILABLE
// only after a full retry-and-backoff cycle. Both are misleading config
// errors. Validation throws before any fetch, so no network is hit here even
// though a (dummy) key is configured.
it('an unparseable endpoint exits 5 with a VALIDATION_ERROR naming endpoint-url', async () => {
const result = await runCli(
['--output', 'json', '--endpoint-url', 'not a url', 'project', 'list'],
{ TESTSPRITE_API_KEY: 'sk-subproc' },
);
expect(result.exitCode).toBe(5);
const parsed = JSON.parse(result.stderr) as { error: { code: string; nextAction: string } };
expect(parsed.error.code).toBe('VALIDATION_ERROR');
expect(parsed.error.nextAction).toContain('endpoint-url');
}, 30_000);
it('a non-http(s) scheme exits 5 instead of being retried as a network failure', async () => {
const result = await runCli(
['--output', 'json', '--endpoint-url', 'ftp://example.com', 'project', 'list'],
{ TESTSPRITE_API_KEY: 'sk-subproc' },
);
expect(result.exitCode).toBe(5);
const parsed = JSON.parse(result.stderr) as { error: { code: string } };
expect(parsed.error.code).toBe('VALIDATION_ERROR');
}, 30_000);
});
describe('a malformed --profile is rejected (exit 5), not silently corrupting credentials', () => {
// A profile name becomes an INI section header (`[name]`). `prod]` would
// serialise to `[prod]]`, which the parser cannot read back — `setup` would
// report success while the key silently fails to persist. The guard fires on
// any credential read/write path.
it('exits 5 with a VALIDATION_ERROR naming the profile flag', async () => {
const result = await runCli(['--output', 'json', '--profile', 'prod]', 'project', 'list'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(5);
const parsed = JSON.parse(result.stderr) as { error: { code: string; nextAction: string } };
expect(parsed.error.code).toBe('VALIDATION_ERROR');
expect(parsed.error.nextAction).toContain('profile');
}, 30_000);
});
describe('project get subprocess', () => {
it('--output json returns the §6.1 Project shape', async () => {
const result = await runCli(['--output', 'json', 'project', 'get', 'project_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout);
expect(parsed.id).toBe('project_subproc');
expect(parsed.type).toBe('frontend');
expect(parsed.createdFrom).toBe('portal');
}, 30_000);
it('text output prints the labeled fields block', async () => {
const result = await runCli(['project', 'get', 'project_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('id: project_subproc');
expect(result.stdout).toContain('createdFrom: portal');
}, 30_000);
it('exits 4 (NOT_FOUND) for an unknown project id', async () => {
const result = await runCli(['--output', 'json', 'project', 'get', 'project_does_not_exist'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(4);
const parsed = JSON.parse(result.stderr) as { error: { code: string } };
expect(parsed.error.code).toBe('NOT_FOUND');
}, 30_000);
});
describe('test list subprocess', () => {
it('--output json returns the §6.2 TestList shape', async () => {
const result = await runCli(
['--output', 'json', 'test', 'list', '--project', 'project_subproc'],
{
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
},
);
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout);
expect(parsed.items).toHaveLength(1);
expect(parsed.items[0].id).toBe('test_subproc');
expect(parsed.items[0].status).toBe('failed');
expect(parsed.nextToken).toBeNull();
}, 30_000);
it('--type frontend filter is forwarded to the facade', async () => {
const result = await runCli(
['--output', 'json', 'test', 'list', '--project', 'project_subproc', '--type', 'frontend'],
{
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
},
);
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout);
expect(parsed.items[0].type).toBe('frontend');
}, 30_000);
it('--type backend with no matching rows returns empty list', async () => {
const result = await runCli(
['--output', 'json', 'test', 'list', '--project', 'project_subproc', '--type', 'backend'],
{
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
},
);
expect(result.exitCode).toBe(0);
expect(JSON.parse(result.stdout).items).toEqual([]);
}, 30_000);
it('text output renders header + status column', async () => {
const result = await runCli(['test', 'list', '--project', 'project_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('ID');
expect(result.stdout).toContain('STATUS');
expect(result.stdout).toContain('Subproc test fixture');
expect(result.stdout).toContain('failed');
}, 30_000);
it('--help prints filter and pagination flags', async () => {
const result = await runCli(['test', 'list', '--help']);
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('--project');
expect(result.stdout).toContain('--type');
expect(result.stdout).toContain('--created-from');
expect(result.stdout).toContain('--page-size');
}, 30_000);
it('missing --project exits 5 with VALIDATION_ERROR (typed envelope)', async () => {
const result = await runCli(['--output', 'json', 'test', 'list'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
// Per the CLI error spec §2 ("missing required field" → VALIDATION_ERROR)
// and §6 (VALIDATION_ERROR → exit 5), so JSON consumers can branch on
// `error.code` instead of a generic Commander exit-1 string.
expect(result.exitCode).toBe(5);
const parsed = JSON.parse(result.stderr) as {
error: { code: string; details: { field: string } };
};
expect(parsed.error.code).toBe('VALIDATION_ERROR');
expect(parsed.error.details.field).toBe('project');
}, 30_000);
it('--type=junk exits 5 with VALIDATION_ERROR (local validation)', async () => {
const result = await runCli(
['--output', 'json', 'test', 'list', '--project', 'project_subproc', '--type', 'junk'],
{ TESTSPRITE_API_KEY: 'sk-subproc', TESTSPRITE_API_URL: baseUrl },
);
expect(result.exitCode).toBe(5);
const parsed = JSON.parse(result.stderr) as { error: { code: string } };
expect(parsed.error.code).toBe('VALIDATION_ERROR');
}, 30_000);
});
describe('test get subprocess', () => {
it('--output json returns the §6.2 Test shape', async () => {
const result = await runCli(['--output', 'json', 'test', 'get', 'test_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout);
expect(parsed.id).toBe('test_subproc');
expect(parsed.type).toBe('frontend');
expect(parsed.status).toBe('failed');
}, 30_000);
it('text output prints the labeled fields block', async () => {
const result = await runCli(['test', 'get', 'test_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('id: test_subproc');
expect(result.stdout).toContain('status: failed');
expect(result.stdout).toContain('createdFrom: portal');
}, 30_000);
it('exits 4 (NOT_FOUND) for an unknown test id', async () => {
const result = await runCli(['--output', 'json', 'test', 'get', 'test_does_not_exist'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(4);
const parsed = JSON.parse(result.stderr) as { error: { code: string } };
expect(parsed.error.code).toBe('NOT_FOUND');
}, 30_000);
});
describe('test code get subprocess', () => {
it('--output json returns the §6.3 TestCode shape', async () => {
const result = await runCli(['--output', 'json', 'test', 'code', 'get', 'test_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout) as {
testId: string;
language: string;
framework: string;
code: string;
codeVersion: string;
};
expect(parsed.testId).toBe('test_subproc');
expect(parsed.language).toBe('typescript');
expect(parsed.framework).toBe('playwright');
expect(parsed.codeVersion).toBe('v3');
}, 30_000);
it('text mode prints the inline source body without a JSON envelope', async () => {
const result = await runCli(['test', 'code', 'get', 'test_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
// The source body itself must arrive on stdout, not the wire envelope.
// Agents pipe `> file.ts` and expect a runnable file.
expect(result.stdout).toContain("import { test } from '@playwright/test';");
expect(result.stdout).not.toContain('"testId"');
expect(result.stdout).not.toContain('"codeVersion"');
}, 30_000);
it('exits 4 (NOT_FOUND) for an unknown test id', async () => {
const result = await runCli(
['--output', 'json', 'test', 'code', 'get', 'test_does_not_exist'],
{ TESTSPRITE_API_KEY: 'sk-subproc', TESTSPRITE_API_URL: baseUrl },
);
expect(result.exitCode).toBe(4);
const parsed = JSON.parse(result.stderr) as { error: { code: string } };
expect(parsed.error.code).toBe('NOT_FOUND');
}, 30_000);
});
describe('test steps subprocess', () => {
it('--output json returns the §6.4 TestStepList shape', async () => {
const result = await runCli(['--output', 'json', 'test', 'steps', 'test_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout) as {
items: Array<{ stepIndex: number; status: string | null; runIdIfAvailable: string | null }>;
nextToken: string | null;
};
expect(parsed.items).toHaveLength(2);
expect(parsed.items[0]!.stepIndex).toBe(1);
expect(parsed.items[1]!.status).toBe('failed');
// §6.4 atomicity: every step in one response shares runIdIfAvailable.
const runIds = new Set(parsed.items.map(s => s.runIdIfAvailable));
expect(runIds.size).toBe(1);
expect(parsed.nextToken).toBeNull();
}, 30_000);
it('text mode renders the step table and shared run metadata', async () => {
const result = await runCli(['test', 'steps', 'test_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
expect(result.stdout).toContain('INDEX');
expect(result.stdout).toContain('ACTION');
expect(result.stdout).toContain('Click the cart icon');
expect(result.stdout).toContain('Click the submit button');
expect(result.stdout).toContain('runId: run_subproc');
expect(result.stdout).toContain('codeVersion: v3');
}, 30_000);
});
describe('test result subprocess', () => {
it('--output json returns the §6.5 LatestResult shape with correlation block', async () => {
const result = await runCli(['--output', 'json', 'test', 'result', 'test_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout) as {
status: string;
snapshotId: string;
runIdIfAvailable: string | null;
codeVersion: string | null;
failedStepIndex: number | null;
failureKind: string | null;
};
expect(parsed.status).toBe('failed');
// §6.5: every correlation field is present (not omitted) so agents
// can detect drift between code/result/steps.
expect(parsed.snapshotId).toBe('snap_subproc');
expect(parsed.runIdIfAvailable).toBe('run_subproc');
expect(parsed.codeVersion).toBe('v3');
expect(parsed.failedStepIndex).toBe(2);
expect(parsed.failureKind).toBe('assertion');
}, 30_000);
it('text mode highlights failureKind + failedStepIndex above timestamps', async () => {
const result = await runCli(['test', 'result', 'test_subproc'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const lines = result.stdout.split('\n');
const kindLine = lines.findIndex(l => l.startsWith('failureKind'));
const startedLine = lines.findIndex(l => l.startsWith('startedAt'));
expect(kindLine).toBeGreaterThanOrEqual(0);
expect(kindLine).toBeLessThan(startedLine);
expect(result.stdout).toContain('failureKind: assertion');
expect(result.stdout).toContain('failedStepIndex: 2');
expect(result.stdout).toContain('verdict: failed');
expect(result.stdout).toContain('summary: Failed (assertion) on step 2');
}, 30_000);
});
describe('auth remove subprocess', () => {
it('removes the profile file entry and exits 0', async () => {
// First configure a profile (via the consolidated `setup` path)
const configureResult = await runCli(['setup', '--from-env', '--no-agent'], {
TESTSPRITE_API_KEY: 'sk-subproc',
TESTSPRITE_API_URL: baseUrl,
});
expect(configureResult.exitCode).toBe(0);
const removeResult = await runCli(['auth', 'remove']);
expect(removeResult.exitCode).toBe(0);
expect(removeResult.stdout).toContain('Removed credentials');
}, 30_000);
});
describe('setup --from-env subprocess', () => {
it('writes the credentials file with mode 0600', async () => {
const result = await runCli(['setup', '--from-env', '--no-agent'], {
TESTSPRITE_API_KEY: 'sk-mode-test',
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(0);
const credentialsPath = join(tmpHome, '.testsprite', 'credentials');
expect(existsSync(credentialsPath)).toBe(true);
expect(statSync(credentialsPath).mode & 0o777).toBe(0o600);
}, 30_000);
it('exits 5 with VALIDATION_ERROR when --from-env is set without TESTSPRITE_API_KEY', async () => {
// Explicitly do not pass TESTSPRITE_API_KEY
const result = await runCli(['setup', '--from-env', '--no-agent'], {
TESTSPRITE_API_URL: baseUrl,
});
expect(result.exitCode).toBe(5);
expect(result.stderr).toContain('TESTSPRITE_API_KEY');
}, 30_000);
});
/**
* P6 dry-run smoke. Each command must run end-to-end without an API
* key, without network access (the local server is up but we never
* point at it), and without writing to disk. Stdout is the canned
* sample envelope; stderr carries the dry-run banner and any
* "would write" annotations.
*/
describe('--dry-run subprocess smoke', () => {
it('project list --dry-run returns canned ProjectList without auth', async () => {
const result = await runCli(['project', 'list', '--dry-run', '--output', 'json']);
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout) as { items: unknown[]; nextToken: null };
expect(parsed.items.length).toBeGreaterThan(0);
expect(parsed.nextToken).toBeNull();
expect(result.stderr).toContain('[dry-run] sample response');
}, 30_000);
it('project get --dry-run returns canned Project without auth', async () => {
const result = await runCli([
'project',
'get',
'proj_anything',
'--dry-run',
'--output',
'json',
]);
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout) as { id: string };
expect(parsed.id).toBeTruthy();
}, 30_000);
it('test list --dry-run returns canned TestList', async () => {
const result = await runCli([
'test',
'list',
'--project',
'proj_anything',
'--dry-run',
'--output',
'json',
]);
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout) as { items: unknown[] };
expect(parsed.items.length).toBeGreaterThan(0);
}, 30_000);
it('test failure get --dry-run --out <dir> does NOT create the directory', async () => {
const targetDir = join(tmpHome, 'dryrun-bundle-' + Date.now());
expect(existsSync(targetDir)).toBe(false);
const result = await runCli([
'test',
'failure',
'get',
'test_anything',
'--dry-run',
'--out',
targetDir,
'--output',
'json',
]);
expect(result.exitCode).toBe(0);
expect(existsSync(targetDir)).toBe(false);
expect(result.stderr).toContain('[dry-run] would write bundle to');
expect(result.stderr).toContain(targetDir);
const parsed = JSON.parse(result.stdout) as { ok: boolean; dryRun: boolean };
expect(parsed.ok).toBe(true);
expect(parsed.dryRun).toBe(true);
}, 30_000);
it('test failure get --dry-run --out <dir> text mode does not claim the bundle was written', async () => {
// Stdout is the success contract — automation may parse it. The
// real-mode renderer says "Bundle written to ..."; in dry-run that
// would be a lie since the directory is never created. Codex flagged
// this as a P2 in the first review of P6 piece-2; this test guards
// against the regression.
const targetDir = join(tmpHome, 'dryrun-bundle-text-' + Date.now());
const result = await runCli([
'test',
'failure',
'get',
'test_anything',
'--dry-run',
'--out',
targetDir,
]);
expect(result.exitCode).toBe(0);
expect(existsSync(targetDir)).toBe(false);
expect(result.stdout).toMatch(/^\(dry-run\) would write bundle to /);
expect(result.stdout).not.toContain('Bundle written to');
}, 30_000);
it('test code get --dry-run --out <file> does NOT create the file', async () => {
const targetFile = join(tmpHome, 'dryrun-code-' + Date.now() + '.ts');
expect(existsSync(targetFile)).toBe(false);
const result = await runCli([
'test',
'code',
'get',
'test_anything',
'--dry-run',
'--out',
targetFile,
'--output',
'json',
]);
expect(result.exitCode).toBe(0);
expect(existsSync(targetFile)).toBe(false);
expect(result.stderr).toContain('[dry-run] would write code body');
expect(result.stderr).toContain(targetFile);
}, 30_000);
it('auth configure --dry-run does NOT prompt and does NOT write credentials', async () => {
// No TESTSPRITE_API_KEY in env; if dry-run actually called the prompt
// path the subprocess would block waiting on stdin and the test would
// time out. The fact that it exits 0 within the timeout proves it