-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathwasm-commands.test.ts
More file actions
837 lines (718 loc) · 28.4 KB
/
wasm-commands.test.ts
File metadata and controls
837 lines (718 loc) · 28.4 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
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { existsSync } from "node:fs";
import { join } from "node:path";
import { AgentOs } from "../src/index.js";
import curlPackage from "@rivet-dev/agent-os-curl";
import slackCliPackage from "@rivet-dev/agent-os-slack-cli";
import {
REGISTRY_SOFTWARE,
registrySkipReason,
} from "./helpers/registry-commands.js";
/**
* Comprehensive tests for WASM command packages.
* Each section tests a specific registry package end-to-end.
*/
describe.skipIf(registrySkipReason)("WASM command packages", () => {
let vm: AgentOs;
beforeEach(async () => {
vm = await AgentOs.create({ software: REGISTRY_SOFTWARE });
});
afterEach(async () => {
await vm.dispose();
});
// ── coreutils: shell ──────────────────────────────────────────────
describe("sh (coreutils)", () => {
test("variables and arithmetic", async () => {
const r = await vm.exec('X=42; echo $((X + 8))');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("50");
});
test("for loop", async () => {
const r = await vm.exec('for i in 1 2 3; do echo $i; done');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("1\n2\n3");
});
test("command substitution", async () => {
const r = await vm.exec('echo "count: $(echo hello | wc -c)"');
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("count:");
});
test("pipe chain with sort", async () => {
const r = await vm.exec('printf "c\\nb\\na\\n" | sort');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("a\nb\nc");
});
test("redirect to file and read back", async () => {
const r = await vm.exec(
'echo "hello" > /tmp/redir.txt && cat /tmp/redir.txt',
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("hello");
});
test("heredoc", async () => {
const r = await vm.exec(`cat <<'EOF'
line1
line2
EOF`);
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("line1");
expect(r.stdout).toContain("line2");
});
test("exit code propagation", async () => {
const r = await vm.exec("false");
expect(r.exitCode).toBe(1);
});
test("subshell isolation", async () => {
const r = await vm.exec('(X=inner); echo "${X:-outer}"');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("outer");
});
});
// ── coreutils: file operations ────────────────────────────────────
describe("file operations (coreutils)", () => {
test("cp and cat", async () => {
await vm.exec("echo data > /tmp/orig.txt");
const r = await vm.exec(
"cp /tmp/orig.txt /tmp/copy.txt && cat /tmp/copy.txt",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("data");
});
test("mv renames file", async () => {
await vm.exec("echo moved > /tmp/mv-src.txt");
const r = await vm.exec(
"mv /tmp/mv-src.txt /tmp/mv-dst.txt && cat /tmp/mv-dst.txt",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("moved");
});
test("rm removes file", async () => {
await vm.exec("echo x > /tmp/rm-me.txt");
const r = await vm.exec(
"rm /tmp/rm-me.txt && test ! -f /tmp/rm-me.txt && echo ok",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("ok");
});
test("mkdir -p and rmdir", async () => {
const r = await vm.exec(
"mkdir -p /tmp/a/b/c && test -d /tmp/a/b/c && echo ok",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("ok");
});
test("ln -s creates symlink", async () => {
await vm.exec("echo linked > /tmp/ln-target.txt");
const r = await vm.exec(
"ln -s /tmp/ln-target.txt /tmp/ln-link.txt && cat /tmp/ln-link.txt",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("linked");
});
test("ls lists files", async () => {
await vm.exec("mkdir -p /tmp/ls-test && echo a > /tmp/ls-test/a.txt && echo b > /tmp/ls-test/b.txt");
const r = await vm.exec("ls /tmp/ls-test/");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("a.txt");
expect(r.stdout).toContain("b.txt");
});
test("touch creates file", async () => {
const r = await vm.exec(
"touch /tmp/touched.txt && test -f /tmp/touched.txt && echo ok",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("ok");
});
test("head and tail", async () => {
await vm.exec('printf "1\\n2\\n3\\n4\\n5\\n" > /tmp/lines.txt');
const head = await vm.exec("head -2 /tmp/lines.txt");
expect(head.stdout.trim()).toBe("1\n2");
const tail = await vm.exec("tail -2 /tmp/lines.txt");
expect(tail.stdout.trim()).toBe("4\n5");
});
test("wc counts lines", async () => {
await vm.exec('printf "hello world\\nfoo bar\\n" > /tmp/wc.txt');
const r = await vm.exec("wc -l /tmp/wc.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("2");
});
test("sort and uniq", async () => {
await vm.exec('printf "b\\na\\nb\\nc\\na\\n" > /tmp/dup.txt');
const r = await vm.exec("sort /tmp/dup.txt | uniq");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("a\nb\nc");
});
test("cut extracts fields", async () => {
await vm.exec('printf "a,b,c\\n1,2,3\\n" > /tmp/csv.txt');
const r = await vm.exec("cut -d, -f2 /tmp/csv.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("b\n2");
});
test("tr translates characters", async () => {
const r = await vm.exec("echo hello | tr 'a-z' 'A-Z'");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("HELLO");
});
test("tee writes to file and stdout", async () => {
const r = await vm.exec(
"echo data | tee /tmp/tee-out.txt && cat /tmp/tee-out.txt",
);
expect(r.exitCode).toBe(0);
const lines = r.stdout.trim().split("\n");
expect(lines[0]).toBe("data");
expect(lines[1]).toBe("data");
});
test("basename and dirname", async () => {
const bn = await vm.exec("basename /foo/bar/baz.txt");
expect(bn.stdout.trim()).toBe("baz.txt");
const dn = await vm.exec("dirname /foo/bar/baz.txt");
expect(dn.stdout.trim()).toBe("/foo/bar");
});
test("base64 encode and decode", async () => {
const r = await vm.exec("echo -n 'hello' | base64 | base64 -d");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("hello");
});
test("sha256sum computes hash", async () => {
await vm.exec('printf "test\\n" > /tmp/hash.txt');
const r = await vm.exec("sha256sum /tmp/hash.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain(
"f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2",
);
});
test("env prints environment", async () => {
const r = await vm.exec("env", { env: { FOO: "bar" } });
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("FOO=bar");
});
test("which resolves virtual PATH commands", async () => {
const bash = await vm.exec("which bash");
expect(bash.exitCode).toBe(0);
expect(bash.stdout.trim()).toBe("/bin/bash");
const rg = await vm.exec("which rg");
expect(rg.exitCode).toBe(0);
expect(rg.stdout.trim()).toBe("/bin/rg");
const missing = await vm.exec("which definitely-not-a-command");
expect(missing.exitCode).toBe(1);
expect(missing.stdout.trim()).toBe("");
});
test("xu executes as a registered PATH command", async () => {
const r = await vm.exec("xu hello-agent-os");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("xu-ok:hello-agent-os");
});
test("test command conditionals", async () => {
await vm.exec("echo yes > /tmp/exists.txt");
const r = await vm.exec(
'test -f /tmp/exists.txt && echo "exists" || echo "missing"',
);
expect(r.stdout.trim()).toBe("exists");
});
test("date outputs something", async () => {
const r = await vm.exec("date +%Y");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toMatch(/^\d{4}$/);
});
test("seq generates sequence", async () => {
const r = await vm.exec("seq 1 5");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("1\n2\n3\n4\n5");
});
test("expr arithmetic", async () => {
const r = await vm.exec("expr 6 '*' 7");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("42");
});
});
// ── grep ──────────────────────────────────────────────────────────
describe("grep", () => {
test("basic pattern match", async () => {
await vm.exec('printf "apple\\nbanana\\ncherry\\napricot\\n" > /tmp/grep.txt');
const r = await vm.exec("grep ap /tmp/grep.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("apple");
expect(r.stdout).toContain("apricot");
expect(r.stdout).not.toContain("banana");
});
test("grep -c counts matches", async () => {
await vm.exec('printf "a\\nb\\na\\nc\\na\\n" > /tmp/gc.txt');
const r = await vm.exec("grep -c a /tmp/gc.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("3");
});
test("grep -i case insensitive", async () => {
await vm.exec('printf "Hello\\nworld\\nHELLO\\n" > /tmp/gi.txt');
const r = await vm.exec("grep -i hello /tmp/gi.txt");
expect(r.exitCode).toBe(0);
const lines = r.stdout.trim().split("\n");
expect(lines).toHaveLength(2);
});
test("grep -v inverts match", async () => {
await vm.exec('printf "yes\\nno\\nyes\\n" > /tmp/gv.txt');
const r = await vm.exec("grep -v yes /tmp/gv.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("no");
});
test("grep with regex", async () => {
await vm.exec('printf "foo123\\nbar456\\nbaz\\n" > /tmp/gre.txt');
const r = await vm.exec("grep '[0-9]' /tmp/gre.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("foo123");
expect(r.stdout).toContain("bar456");
expect(r.stdout).not.toContain("baz");
});
test("egrep extended regex", async () => {
await vm.exec('printf "cat\\ndog\\nbird\\n" > /tmp/eg.txt');
const r = await vm.exec("egrep 'cat|dog' /tmp/eg.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("cat");
expect(r.stdout).toContain("dog");
expect(r.stdout).not.toContain("bird");
});
test("grep pipe from stdin", async () => {
const r = await vm.exec("echo 'hello world' | grep world");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("world");
});
});
// ── sed ───────────────────────────────────────────────────────────
describe("sed", () => {
test("substitute first occurrence", async () => {
const r = await vm.exec("echo 'hello world' | sed 's/world/earth/'");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("hello earth");
});
test("global substitution", async () => {
const r = await vm.exec("echo 'aaa' | sed 's/a/b/g'");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("bbb");
});
test("delete lines matching pattern", async () => {
await vm.exec('printf "keep\\ndelete-me\\nkeep\\n" > /tmp/sed.txt');
const r = await vm.exec("sed '/delete/d' /tmp/sed.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("keep\nkeep");
});
test("print specific line with -n and p", async () => {
await vm.exec('printf "a\\nb\\nc\\n" > /tmp/sedp.txt');
const r = await vm.exec("sed -n '2p' /tmp/sedp.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("b");
});
});
// ── gawk ──────────────────────────────────────────────────────────
describe("awk (gawk)", () => {
test("print specific field", async () => {
await vm.exec('printf "a b c\\n1 2 3\\n" > /tmp/awk.txt');
const r = await vm.exec("awk '{print $2}' /tmp/awk.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("b\n2");
});
test("field separator", async () => {
const r = await vm.exec("echo 'a:b:c' | awk -F: '{print $2}'");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("b");
});
test("sum column", async () => {
await vm.exec('printf "10\\n20\\n30\\n" > /tmp/sum.txt');
const r = await vm.exec(
"awk '{s+=$1} END {print s}' /tmp/sum.txt",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("60");
});
test("pattern matching", async () => {
await vm.exec('printf "ERR foo\\nINFO bar\\nERR baz\\n" > /tmp/awkg.txt');
const r = await vm.exec("awk '/ERR/ {print $2}' /tmp/awkg.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("foo\nbaz");
});
});
// ── findutils ─────────────────────────────────────────────────────
describe("find and xargs (findutils)", () => {
test("find files by name", async () => {
await vm.exec("mkdir -p /tmp/findtest/sub && echo a > /tmp/findtest/a.txt && echo b > /tmp/findtest/b.log && echo c > /tmp/findtest/sub/c.txt");
const r = await vm.exec("find /tmp/findtest -name '*.txt'");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("a.txt");
expect(r.stdout).toContain("c.txt");
expect(r.stdout).not.toContain("b.log");
});
test("find with -type d", async () => {
await vm.exec("mkdir -p /tmp/finddir/sub1 /tmp/finddir/sub2 && echo f > /tmp/finddir/file.txt");
const r = await vm.exec(
"find /tmp/finddir -mindepth 1 -type d",
);
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("sub1");
expect(r.stdout).toContain("sub2");
});
test("xargs passes args to command", async () => {
await vm.exec('printf "hello\\nworld\\n" > /tmp/xargs.txt');
const r = await vm.exec("cat /tmp/xargs.txt | xargs echo");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("hello world");
});
});
// ── diffutils ─────────────────────────────────────────────────────
describe("diff (diffutils)", () => {
test("diff identical files returns 0", async () => {
await vm.exec("echo same > /tmp/d1.txt && echo same > /tmp/d2.txt");
const r = await vm.exec("diff /tmp/d1.txt /tmp/d2.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout).toBe("");
});
test("diff shows differences", async () => {
await vm.exec("echo old > /tmp/d3.txt && echo new > /tmp/d4.txt");
const r = await vm.exec("diff /tmp/d3.txt /tmp/d4.txt");
// Our diff outputs the diff but may not set exit code 1.
expect(r.stdout).toContain("old");
expect(r.stdout).toContain("new");
});
});
// ── tar ───────────────────────────────────────────────────────────
describe("tar", () => {
test("create and extract archive", async () => {
await vm.exec("mkdir -p /tmp/tardir && echo file-a > /tmp/tardir/a.txt && echo file-b > /tmp/tardir/b.txt");
const create = await vm.exec("tar cf /tmp/test.tar -C /tmp tardir");
expect(create.exitCode).toBe(0);
const extract = await vm.exec(
"mkdir -p /tmp/extracted && tar xf /tmp/test.tar -C /tmp/extracted && cat /tmp/extracted/tardir/a.txt",
);
expect(extract.exitCode).toBe(0);
expect(extract.stdout.trim()).toBe("file-a");
});
test("list archive contents", async () => {
await vm.exec("mkdir -p /tmp/tarlist && echo x > /tmp/tarlist/x.txt");
await vm.exec("tar cf /tmp/list.tar -C /tmp tarlist");
const r = await vm.exec("tar tf /tmp/list.tar");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("x.txt");
});
});
// ── gzip ──────────────────────────────────────────────────────────
describe("gzip", () => {
test("compress and decompress", async () => {
await vm.exec('echo "compress me please" > /tmp/gz.txt');
const comp = await vm.exec(
"gzip /tmp/gz.txt && test -f /tmp/gz.txt.gz && echo ok",
);
expect(comp.exitCode).toBe(0);
expect(comp.stdout.trim()).toBe("ok");
const decomp = await vm.exec(
"gunzip /tmp/gz.txt.gz && cat /tmp/gz.txt",
);
expect(decomp.exitCode).toBe(0);
expect(decomp.stdout.trim()).toBe("compress me please");
});
test("zcat reads compressed file without extracting", async () => {
await vm.exec('echo "zcat-data" > /tmp/zc.txt');
await vm.exec("gzip /tmp/zc.txt");
const r = await vm.exec("zcat /tmp/zc.txt.gz");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("zcat-data");
});
});
// ── jq ────────────────────────────────────────────────────────────
describe("jq", () => {
test("extract field from JSON", async () => {
const r = await vm.exec('echo \'{"name":"test","version":42}\' | jq .name');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe('"test"');
});
test("extract number", async () => {
const r = await vm.exec('echo \'{"x":99}\' | jq .x');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("99");
});
test("filter array", async () => {
const r = await vm.exec('echo \'[{"a":1},{"a":2},{"a":3}]\' | jq ".[].a"');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("1\n2\n3");
});
test("construct new JSON", async () => {
const r = await vm.exec(
'echo \'{"a":1,"b":2}\' | jq \'{sum: (.a + .b)}\'',
);
expect(r.exitCode).toBe(0);
const parsed = JSON.parse(r.stdout.trim());
expect(parsed).toEqual({ sum: 3 });
});
});
// ── ripgrep ───────────────────────────────────────────────────────
describe("rg (ripgrep)", () => {
test("search files recursively", async () => {
await vm.exec("mkdir -p /tmp/rgdir/sub && echo 'needle here' > /tmp/rgdir/a.txt && echo nothing > /tmp/rgdir/b.txt && echo 'another needle' > /tmp/rgdir/sub/c.txt");
const r = await vm.exec("rg needle /tmp/rgdir/");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("a.txt");
expect(r.stdout).toContain("c.txt");
expect(r.stdout).not.toContain("b.txt");
});
test("rg -l lists matching files only", async () => {
await vm.exec("mkdir -p /tmp/rgl && echo match > /tmp/rgl/x.txt && echo no > /tmp/rgl/y.txt");
const r = await vm.exec("rg -l match /tmp/rgl/");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("x.txt");
expect(r.stdout).not.toContain("y.txt");
});
});
// ── fd ─────────────────────────────────────────────────────────────
describe("fd (fd-find)", () => {
test("find files by pattern", async () => {
await vm.exec("mkdir -p /tmp/fdtest/sub && echo ts > /tmp/fdtest/hello.ts && echo js > /tmp/fdtest/world.js && echo ts > /tmp/fdtest/sub/foo.ts");
const r = await vm.exec("fd '\\.ts$' /tmp/fdtest/");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("hello.ts");
expect(r.stdout).toContain("foo.ts");
expect(r.stdout).not.toContain("world.js");
});
});
// ── tree ──────────────────────────────────────────────────────────
describe("tree", () => {
test("displays directory structure", async () => {
await vm.exec("mkdir -p /tmp/treedir/sub && echo a > /tmp/treedir/a.txt && echo b > /tmp/treedir/sub/b.txt");
const r = await vm.exec("tree /tmp/treedir");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("a.txt");
expect(r.stdout).toContain("sub");
expect(r.stdout).toContain("b.txt");
});
});
// ── yq ────────────────────────────────────────────────────────────
describe("yq", () => {
test("extract field from YAML via pipe", async () => {
const r = await vm.exec("echo 'name: test' | yq '.name'");
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("test");
});
});
// ── curl (requires C build) ───────────────────────────────────────
describe("curl", () => {
const hasCurl = existsSync(join(curlPackage.commandDir, "curl"));
const CURL_SCRIPT = `
const http = require("http");
const server = http.createServer((req, res) => {
if (req.method === "POST") {
let body = "";
req.on("data", (chunk) => body += chunk);
req.on("end", () => {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ echo: body }));
});
} else {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("hello from server");
}
});
server.listen(0, "0.0.0.0", () => {
console.log("PORT:" + server.address().port);
});
`;
const CURL_KEEPALIVE_SCRIPT = `
const net = require("net");
const server = net.createServer((socket) => {
socket.once("data", () => {
const body = "hello from keepalive";
socket.write(
"HTTP/1.1 200 OK\\r\\n" +
"Content-Type: text/plain\\r\\n" +
"Content-Length: " + Buffer.byteLength(body) + "\\r\\n" +
"Connection: keep-alive\\r\\n" +
"Keep-Alive: timeout=60\\r\\n" +
"\\r\\n" +
body,
);
// Intentionally leave the socket open so curl's shutdown path performs
// a non-blocking drain read instead of waiting for EOF.
});
});
server.listen(0, "0.0.0.0", () => {
console.log("PORT:" + server.address().port);
});
`;
async function startServer(
testVm: AgentOs,
script = CURL_SCRIPT,
): Promise<{ pid: number; port: number }> {
await testVm.writeFile("/tmp/curl-server.js", script);
let resolvePort: (port: number) => void;
const portPromise = new Promise<number>((r) => {
resolvePort = r;
});
const { pid } = testVm.spawn("node", ["/tmp/curl-server.js"], {
onStdout: (data: Uint8Array) => {
const text = new TextDecoder().decode(data);
const match = text.match(/PORT:(\d+)/);
if (match) resolvePort(Number(match[1]));
},
});
const port = await portPromise;
return { pid, port };
}
test("curl GET request", async () => {
expect(hasCurl).toBe(true);
const { pid, port } = await startServer(vm);
try {
const r = await vm.exec(`curl -s http://localhost:${port}/`);
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("hello from server");
} finally {
vm.killProcess(pid);
}
});
test("curl POST with data", async () => {
expect(hasCurl).toBe(true);
const { pid, port } = await startServer(vm);
try {
const r = await vm.exec(
`curl -s -X POST -d 'test-body' http://localhost:${port}/`,
);
expect(r.exitCode).toBe(0);
const json = JSON.parse(r.stdout);
expect(json.echo).toBe("test-body");
} finally {
vm.killProcess(pid);
}
});
test.skipIf(!hasCurl)(
"curl exits promptly after a keep-alive response",
async () => {
const { pid, port } = await startServer(vm, CURL_KEEPALIVE_SCRIPT);
try {
const startedAt = Date.now();
const r = await vm.exec(`curl -s http://localhost:${port}/`);
const elapsedMs = Date.now() - startedAt;
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("hello from keepalive");
expect(r.stderr).not.toContain("i/o error");
expect(elapsedMs).toBeLessThan(8000);
} finally {
vm.killProcess(pid);
}
},
15000,
);
});
// ── file permissions (Bug 1 regression tests) ──────────────────────
describe("file permissions", () => {
test("stat shows correct file mode", async () => {
await vm.exec("echo test > /tmp/perm.txt");
const r = await vm.exec('stat -c "%a" /tmp/perm.txt');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("644");
});
test("stat shows correct directory mode", async () => {
await vm.exec("mkdir -p /tmp/perm-dir");
const r = await vm.exec('stat -c "%a" /tmp/perm-dir');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("755");
});
test("chmod changes file mode", async () => {
await vm.exec("echo test > /tmp/chmod-test.txt");
const r1 = await vm.exec('chmod 755 /tmp/chmod-test.txt');
expect(r1.exitCode).toBe(0);
const r = await vm.exec('stat -c "%a" /tmp/chmod-test.txt');
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("755");
});
test("ls -la shows correct permissions", async () => {
await vm.exec("echo test > /tmp/ls-perm.txt");
const r = await vm.exec("ls -la /tmp/ls-perm.txt");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("rw-r--r--");
});
});
// ── exit codes (Bug 5 regression tests) ─────────────────────────
describe("exit codes", () => {
test("grep returns 1 on no match", async () => {
await vm.exec('echo "hello" > /tmp/ec.txt');
const r = await vm.exec("grep nonexistent /tmp/ec.txt");
expect(r.exitCode).toBe(1);
});
test("diff returns 1 on different files", async () => {
await vm.exec('echo "a" > /tmp/d1.txt && echo "b" > /tmp/d2.txt');
const r = await vm.exec("diff /tmp/d1.txt /tmp/d2.txt");
expect(r.exitCode).toBe(1);
});
test("false returns 1", async () => {
const r = await vm.exec("false");
expect(r.exitCode).toBe(1);
});
test("test -f on missing file returns 1", async () => {
const r = await vm.exec("test -f /tmp/nonexistent-file");
expect(r.exitCode).toBe(1);
});
});
// ── complex pipelines ─────────────────────────────────────────────
describe("cross-package pipelines", () => {
test("find | grep | wc pipeline", async () => {
await vm.exec("mkdir -p /tmp/pipe && echo x > /tmp/pipe/a.txt && echo x > /tmp/pipe/b.log && echo x > /tmp/pipe/c.txt");
const r = await vm.exec(
"find /tmp/pipe -name '*.txt' | grep txt | wc -l",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("2");
});
test("awk + sort pipeline", async () => {
await vm.exec('printf "alice 90\\nbob 70\\ncharlie 85\\n" > /tmp/scores.txt');
const r = await vm.exec(
"sort -k2 -rn /tmp/scores.txt | head -1 | awk '{print $1}'",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("alice");
});
test("tar + gzip round trip", async () => {
await vm.exec("mkdir -p /tmp/tgz && echo round-trip-data > /tmp/tgz/data.txt");
const create = await vm.exec(
"tar cf - -C /tmp tgz | gzip > /tmp/archive.tar.gz",
);
expect(create.exitCode).toBe(0);
const extract = await vm.exec(
"mkdir -p /tmp/tgz-out && zcat /tmp/archive.tar.gz | tar xf - -C /tmp/tgz-out && cat /tmp/tgz-out/tgz/data.txt",
);
expect(extract.exitCode).toBe(0);
expect(extract.stdout.trim()).toBe("round-trip-data");
});
test("sed + grep text processing chain", async () => {
await vm.exec('printf "ERROR: disk full\\nINFO: ok\\nERROR: timeout\\nINFO: done\\n" > /tmp/chain.txt');
const r = await vm.exec(
"grep ERROR /tmp/chain.txt | sed 's/ERROR: //'",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("disk full\ntimeout");
});
test("jq + awk data transformation", async () => {
await vm.exec('printf \'[{"price":10},{"price":20}]\\n\' > /tmp/items.json');
const r = await vm.exec(
"cat /tmp/items.json | jq '.[].price' | awk '{s+=$1} END {print s}'",
);
expect(r.exitCode).toBe(0);
expect(r.stdout.trim()).toBe("30");
});
});
});
// ── slack-cli (Go WASM, separate build) ──────────────────────────────
const slackCliSkipReason = existsSync(slackCliPackage.commandDir)
? false
: "slack-cli WASM binary not available (run: cd registry && make build-wasm-go copy-wasm)";
describe.skipIf(slackCliSkipReason)("slack-cli", () => {
let vm: AgentOs;
beforeEach(async () => {
vm = await AgentOs.create({ software: [...REGISTRY_SOFTWARE, slackCliPackage] });
});
afterEach(async () => {
await vm.dispose();
});
test("slack version prints version info", async () => {
const r = await vm.exec("slack version");
expect(r.exitCode).toBe(0);
expect(r.stdout).toMatch(/slack/i);
});
test("slack help prints usage", async () => {
const r = await vm.exec("slack help");
expect(r.exitCode).toBe(0);
expect(r.stdout).toContain("slack");
});
});