brsh 1.4.0 → 2.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.
Files changed (58) hide show
  1. package/.github/workflows/busybox-compat.yml +166 -0
  2. package/.github/workflows/pages.yml +33 -0
  3. package/.github/workflows/release.yml +40 -0
  4. package/.releaserc.json +18 -0
  5. package/CHANGELOG.md +362 -0
  6. package/bin/brsh-cmd.js +78 -0
  7. package/bin/exportFileSystem.js +81 -156
  8. package/busybox-testsuite/cat.tests +49 -0
  9. package/busybox-testsuite/cp.tests +206 -0
  10. package/busybox-testsuite/grep.tests +237 -0
  11. package/busybox-testsuite/testing.sh +180 -0
  12. package/dist/brsh.css +2 -0
  13. package/dist/shell.es.js +5667 -0
  14. package/dist/shell.es.js.map +1 -0
  15. package/dist/shell.min.js +14 -222
  16. package/dist/shell.min.js.map +1 -149
  17. package/dist/terminal.es.js +6228 -0
  18. package/dist/terminal.es.js.map +1 -0
  19. package/dist/terminal.min.js +113 -18
  20. package/dist/terminal.min.js.map +1 -21
  21. package/index.js +206 -49
  22. package/lib/Context.js +12 -4
  23. package/lib/FileSystem.js +99 -6
  24. package/lib/NodeFileSystem.js +238 -0
  25. package/lib/ScriptCommand.js +193 -15
  26. package/lib/Terminal.js +188 -12
  27. package/lib/WasmCommand.js +252 -0
  28. package/lib/buildFilesystemStructure.js +58 -0
  29. package/lib/builtins/cat.js +67 -9
  30. package/lib/builtins/echo.js +36 -5
  31. package/lib/builtins/false.js +13 -0
  32. package/lib/builtins/grep.js +267 -0
  33. package/lib/builtins/index.js +9 -2
  34. package/lib/builtins/ls.js +86 -18
  35. package/lib/builtins/test.js +105 -0
  36. package/lib/builtins/true.js +10 -0
  37. package/lib/local/chmod.js +72 -0
  38. package/lib/local/cp.js +156 -0
  39. package/lib/local/index.js +8 -1
  40. package/lib/local/jsfunction.js +58 -0
  41. package/lib/local/mkdir.js +44 -0
  42. package/lib/local/mv.js +96 -0
  43. package/lib/local/pwd.js +13 -0
  44. package/lib/local/read.js +6 -4
  45. package/lib/local/rm.js +71 -0
  46. package/lib/local/which.js +2 -2
  47. package/package.json +31 -34
  48. package/test/builtins.test.js +505 -0
  49. package/test/exportFileSystem.test.js +226 -0
  50. package/test/helpers.js +35 -0
  51. package/test/local-commands.test.js +593 -0
  52. package/test/node-filesystem.test.js +133 -0
  53. package/test/scripts.test.js +277 -0
  54. package/test/shell.test.js +245 -0
  55. package/test/streaming.test.js +113 -0
  56. package/vite.config.shell.js +21 -0
  57. package/vite.config.terminal.js +27 -0
  58. package/vitest.config.js +15 -0
@@ -0,0 +1,593 @@
1
+ import { describe, it, expect, beforeEach } from 'vitest';
2
+ import { createShell, run } from './helpers.js';
3
+
4
+ describe('export', () => {
5
+ let shell;
6
+ beforeEach(async () => { shell = await createShell(); });
7
+
8
+ it('sets an environment variable', async () => {
9
+ await shell.onCommand('export MYVAR=hello');
10
+ expect(shell.context.getVar('MYVAR')).toBe('hello');
11
+ });
12
+
13
+ it('allows reading the variable via echo', async () => {
14
+ await shell.onCommand('export GREETING=hi');
15
+ const { output } = await run(shell, 'echo $GREETING');
16
+ expect(output).toContain('hi');
17
+ });
18
+
19
+ it('errors without an argument', async () => {
20
+ const { exitCode } = await run(shell, 'export');
21
+ expect(exitCode).toBeGreaterThan(0);
22
+ });
23
+ });
24
+
25
+ describe('alias', () => {
26
+ let shell;
27
+ beforeEach(async () => { shell = await createShell(); });
28
+
29
+ it('creates an alias that executes the mapped command', async () => {
30
+ // Use quotes so the full "echo hello" is treated as the alias value
31
+ await shell.onCommand("alias 'greet=echo hello'");
32
+ const { output } = await run(shell, 'greet');
33
+ expect(output).toContain('hello');
34
+ });
35
+
36
+ it('errors without a key=value pair', async () => {
37
+ const { exitCode } = await run(shell, 'alias noequals');
38
+ expect(exitCode).toBeGreaterThan(0);
39
+ });
40
+ });
41
+
42
+ describe('cd', () => {
43
+ let shell;
44
+ beforeEach(async () => {
45
+ shell = await createShell({
46
+ filesystem: { home: { user: {} } }
47
+ });
48
+ });
49
+
50
+ it('changes to an absolute directory', async () => {
51
+ await shell.onCommand('cd /home');
52
+ expect(shell.context.fs.cwd).toBe('/home/');
53
+ });
54
+
55
+ it('changes to a nested directory', async () => {
56
+ await shell.onCommand('cd /home/user');
57
+ expect(shell.context.fs.cwd).toBe('/home/user/');
58
+ });
59
+
60
+ it('navigates up with ..', async () => {
61
+ await shell.onCommand('cd /home/user');
62
+ await shell.onCommand('cd ..');
63
+ expect(shell.context.fs.cwd).toBe('/home/');
64
+ });
65
+
66
+ it('errors on a non-existent directory', async () => {
67
+ const { errors, exitCode } = await run(shell, 'cd /does/not/exist');
68
+ expect(errors.length).toBeGreaterThan(0);
69
+ expect(exitCode).toBeGreaterThan(0);
70
+ });
71
+
72
+ it('errors when target is a file not a directory', async () => {
73
+ const s = await createShell({ filesystem: { home: { 'file.txt': 'data' } } });
74
+ const { errors, exitCode } = await run(s, 'cd /home/file.txt');
75
+ expect(errors.length).toBeGreaterThan(0);
76
+ expect(exitCode).toBeGreaterThan(0);
77
+ });
78
+
79
+ it('errors without an argument', async () => {
80
+ const { exitCode } = await run(shell, 'cd');
81
+ expect(exitCode).toBeGreaterThan(0);
82
+ });
83
+ });
84
+
85
+ describe('clear', () => {
86
+ let shell;
87
+ beforeEach(async () => { shell = await createShell(); });
88
+
89
+ it('emits a clear event', () => {
90
+ return new Promise((resolve) => {
91
+ shell.once('clear', resolve);
92
+ shell.onCommand('clear');
93
+ });
94
+ });
95
+
96
+ it('errors with unexpected arguments', async () => {
97
+ const { exitCode } = await run(shell, 'clear extra');
98
+ expect(exitCode).toBeGreaterThan(0);
99
+ });
100
+ });
101
+
102
+ describe('exit', () => {
103
+ it('destroys the shell and emits exit with code 0', async () => {
104
+ const shell = await createShell();
105
+ return new Promise((resolve) => {
106
+ shell.once('exit', (code) => {
107
+ expect(code).toBe(0);
108
+ resolve();
109
+ });
110
+ shell.onCommand('exit');
111
+ });
112
+ });
113
+
114
+ it('exits with a specific code', async () => {
115
+ const shell = await createShell();
116
+ return new Promise((resolve) => {
117
+ shell.once('exit', (code) => {
118
+ expect(code).toBe(42);
119
+ resolve();
120
+ });
121
+ shell.onCommand('exit 42');
122
+ });
123
+ });
124
+ });
125
+
126
+ describe('which', () => {
127
+ let shell;
128
+ beforeEach(async () => { shell = await createShell(); });
129
+
130
+ it('finds a built-in command and returns exit code 0', async () => {
131
+ const { exitCode } = await run(shell, 'which echo');
132
+ expect(exitCode).toBe(0);
133
+ });
134
+
135
+ it('outputs the full path for a found command', async () => {
136
+ const { output } = await run(shell, 'which echo');
137
+ expect(output.join(' ')).toMatch(/\/bin\/echo/);
138
+ });
139
+
140
+ it('returns non-zero for an unknown command', async () => {
141
+ const { exitCode } = await run(shell, 'which nothinghere');
142
+ expect(exitCode).toBeGreaterThan(0);
143
+ });
144
+
145
+ it('errors without an argument', async () => {
146
+ const { exitCode } = await run(shell, 'which');
147
+ expect(exitCode).toBeGreaterThan(0);
148
+ });
149
+ });
150
+
151
+ describe('read', () => {
152
+ let shell;
153
+ beforeEach(async () => { shell = await createShell(); });
154
+
155
+ it('streams a prompt before waiting for input', () => {
156
+ return new Promise((resolve, reject) => {
157
+ const tid = setTimeout(() => reject(new Error('prompt not received')), 500);
158
+ shell.once('stdErr', (line) => {
159
+ clearTimeout(tid);
160
+ expect(line).toBe('enter value: ');
161
+ shell.onInput('a');
162
+ shell.onInput('b');
163
+ shell.onInput('c');
164
+ resolve();
165
+ });
166
+ shell.onCommand('read -p "enter value: " -n 3 INPUT');
167
+ });
168
+ });
169
+
170
+ it('stores received characters in the named variable', async () => {
171
+ const p = shell.onCommand('read -n 1 -t 1 CHAR');
172
+ // _runCommand is async so drain all microtasks before sending input
173
+ await new Promise(resolve => setImmediate(resolve));
174
+ shell.onInput('x');
175
+ await p;
176
+ expect(shell.context.getVar('CHAR')).toBe('x');
177
+ });
178
+
179
+ it('shows help with -h', async () => {
180
+ const { output } = await run(shell, 'read -h');
181
+ expect(output.join(' ')).toMatch(/usage/i);
182
+ });
183
+ });
184
+
185
+ describe('source', () => {
186
+ it('executes a script and merges exported variables into the current context', async () => {
187
+ const shell = await createShell({
188
+ filesystem: {
189
+ scripts: { 'setup.sh': '#!/sh.js\nexport LOADED=yes' }
190
+ }
191
+ });
192
+ await shell.onCommand('source /scripts/setup.sh');
193
+ expect(shell.context.getVar('LOADED')).toBe('yes');
194
+ });
195
+
196
+ it('sourced aliases are available in the current context', async () => {
197
+ const shell = await createShell({
198
+ filesystem: {
199
+ scripts: { 'aliases.sh': "#!/sh.js\nalias 'greet=echo hi'" }
200
+ }
201
+ });
202
+ await shell.onCommand('source /scripts/aliases.sh');
203
+ const { output } = await run(shell, 'greet');
204
+ expect(output).toContain('hi');
205
+ });
206
+
207
+ it('errors on a non-existent file', async () => {
208
+ const shell = await createShell();
209
+ const { errors, exitCode } = await run(shell, 'source /nope.sh');
210
+ expect(exitCode).toBeGreaterThan(0);
211
+ expect(errors.length).toBeGreaterThan(0);
212
+ });
213
+
214
+ it('errors when the target has no shebang', async () => {
215
+ const shell = await createShell({
216
+ filesystem: {
217
+ scripts: { 'plain.sh': 'just text, no shebang' }
218
+ }
219
+ });
220
+ const { exitCode } = await run(shell, 'source /scripts/plain.sh');
221
+ expect(exitCode).toBeGreaterThan(0);
222
+ });
223
+ });
224
+
225
+ describe('chmod', () => {
226
+ let shell;
227
+ beforeEach(async () => {
228
+ shell = await createShell({
229
+ filesystem: {
230
+ home: {
231
+ 'script.sh': '#!/bin/sh\necho hello',
232
+ 'readme.txt': 'just text'
233
+ }
234
+ },
235
+ cwd: '/home'
236
+ });
237
+ });
238
+
239
+ it('sets octal mode on a file', async () => {
240
+ await shell.onCommand('chmod 755 /home/script.sh');
241
+ expect(shell.context.fs.getMode('/home/script.sh')).toBe(0o755);
242
+ });
243
+
244
+ it('sets a 3-digit octal mode', async () => {
245
+ await shell.onCommand('chmod 644 /home/script.sh');
246
+ expect(shell.context.fs.getMode('/home/script.sh')).toBe(0o644);
247
+ });
248
+
249
+ it('adds execute bit with +x', async () => {
250
+ await shell.onCommand('chmod 644 /home/script.sh');
251
+ await shell.onCommand('chmod +x /home/script.sh');
252
+ const mode = shell.context.fs.getMode('/home/script.sh');
253
+ expect(mode & 0o100).toBeTruthy();
254
+ });
255
+
256
+ it('removes execute bit with -x', async () => {
257
+ await shell.onCommand('chmod 755 /home/script.sh');
258
+ await shell.onCommand('chmod -x /home/script.sh');
259
+ const mode = shell.context.fs.getMode('/home/script.sh');
260
+ expect(mode & 0o111).toBe(0);
261
+ });
262
+
263
+ it('adds read bit with u+r', async () => {
264
+ await shell.onCommand('chmod 000 /home/readme.txt');
265
+ await shell.onCommand('chmod u+r /home/readme.txt');
266
+ const mode = shell.context.fs.getMode('/home/readme.txt');
267
+ expect(mode & 0o400).toBeTruthy();
268
+ });
269
+
270
+ it('errors on invalid mode', async () => {
271
+ const { errors, exitCode } = await run(shell, 'chmod badmode /home/script.sh');
272
+ expect(exitCode).toBeGreaterThan(0);
273
+ expect(errors.join(' ')).toMatch(/invalid mode/i);
274
+ });
275
+
276
+ it('errors when file does not exist', async () => {
277
+ const { errors, exitCode } = await run(shell, 'chmod 755 /home/nope.sh');
278
+ expect(exitCode).toBeGreaterThan(0);
279
+ expect(errors.join(' ')).toMatch(/No such file/i);
280
+ });
281
+
282
+ it('errors with no arguments', async () => {
283
+ const { errors, exitCode } = await run(shell, 'chmod');
284
+ expect(exitCode).toBeGreaterThan(0);
285
+ expect(errors.join(' ')).toMatch(/usage/i);
286
+ });
287
+
288
+ it('allows running a script after chmod +x', async () => {
289
+ const { output, exitCode } = await run(shell, './script.sh');
290
+ expect(exitCode).toBeGreaterThan(0);
291
+
292
+ await shell.onCommand('chmod +x /home/script.sh');
293
+ const { output: out2, exitCode: code2 } = await run(shell, './script.sh');
294
+ expect(code2).toBe(0);
295
+ expect(out2).toContain('hello');
296
+ });
297
+
298
+ it('blocks execution of a script without execute permission', async () => {
299
+ const { errors, exitCode } = await run(shell, './script.sh');
300
+ expect(exitCode).toBeGreaterThan(0);
301
+ expect(errors.join(' ')).toMatch(/permission denied/i);
302
+ });
303
+ });
304
+
305
+ describe('pwd', () => {
306
+ it('prints the current working directory', async () => {
307
+ const shell = await createShell({ cwd: '/home' });
308
+ const { output } = await run(shell, 'pwd');
309
+ expect(output).toContain('/home');
310
+ });
311
+
312
+ it('prints / at root', async () => {
313
+ const shell = await createShell({ cwd: '/' });
314
+ const { output } = await run(shell, 'pwd');
315
+ expect(output).toContain('/');
316
+ });
317
+
318
+ it('reflects cd changes', async () => {
319
+ const shell = await createShell({
320
+ filesystem: { home: { user: {} } },
321
+ cwd: '/'
322
+ });
323
+ await shell.onCommand('cd /home/user');
324
+ const { output } = await run(shell, 'pwd');
325
+ expect(output).toContain('/home/user');
326
+ });
327
+ });
328
+
329
+ describe('mkdir', () => {
330
+ let shell;
331
+ beforeEach(async () => {
332
+ shell = await createShell({ filesystem: { home: {} }, cwd: '/home' });
333
+ });
334
+
335
+ it('creates a new directory', async () => {
336
+ await run(shell, 'mkdir /home/newdir');
337
+ const dir = shell.context.fs.getFileByPath('/home/newdir');
338
+ expect(dir).toBeDefined();
339
+ expect(dir.constructor).toBe(Object);
340
+ });
341
+
342
+ it('-p creates parent directories', async () => {
343
+ await run(shell, 'mkdir -p /home/a/b/c');
344
+ const dir = shell.context.fs.getFileByPath('/home/a/b/c');
345
+ expect(dir).toBeDefined();
346
+ });
347
+
348
+ it('-p does not error if directory already exists', async () => {
349
+ const { exitCode } = await run(shell, 'mkdir -p /home');
350
+ expect(exitCode).toBe(0);
351
+ });
352
+
353
+ it('errors if directory exists without -p', async () => {
354
+ const { exitCode, errors } = await run(shell, 'mkdir /home');
355
+ expect(exitCode).toBeGreaterThan(0);
356
+ expect(errors.join(' ')).toMatch(/File exists/i);
357
+ });
358
+
359
+ it('errors with no arguments', async () => {
360
+ const { exitCode } = await run(shell, 'mkdir');
361
+ expect(exitCode).toBeGreaterThan(0);
362
+ });
363
+ });
364
+
365
+ describe('rm', () => {
366
+ let shell;
367
+ beforeEach(async () => {
368
+ shell = await createShell({
369
+ filesystem: {
370
+ home: {
371
+ 'file.txt': 'hello',
372
+ subdir: { 'inner.txt': 'inner' }
373
+ }
374
+ }
375
+ });
376
+ });
377
+
378
+ it('removes a file', async () => {
379
+ await run(shell, 'rm /home/file.txt');
380
+ expect(shell.context.fs.getFileByPath('/home/file.txt')).toBeNull();
381
+ });
382
+
383
+ it('errors when removing a directory without -r', async () => {
384
+ const { exitCode, errors } = await run(shell, 'rm /home/subdir');
385
+ expect(exitCode).toBeGreaterThan(0);
386
+ expect(errors.join(' ')).toMatch(/Is a directory/i);
387
+ });
388
+
389
+ it('-r removes a directory recursively', async () => {
390
+ await run(shell, 'rm -r /home/subdir');
391
+ expect(shell.context.fs.getFileByPath('/home/subdir')).toBeNull();
392
+ });
393
+
394
+ it('-f does not error on missing file', async () => {
395
+ const { exitCode } = await run(shell, 'rm -f /home/nope.txt');
396
+ expect(exitCode).toBe(0);
397
+ });
398
+
399
+ it('errors on missing file without -f', async () => {
400
+ const { exitCode } = await run(shell, 'rm /home/nope.txt');
401
+ expect(exitCode).toBeGreaterThan(0);
402
+ });
403
+
404
+ it('fires onChange after deleting a file', async () => {
405
+ const changes = [];
406
+ const shell2 = await createShell({
407
+ filesystem: { home: { 'file.txt': 'hello' } },
408
+ onFsChange: (fs) => changes.push(Object.keys(fs.home || {}))
409
+ });
410
+ await run(shell2, 'rm /home/file.txt');
411
+ // Wait for microtask to fire
412
+ await new Promise(resolve => setTimeout(resolve, 10));
413
+ expect(changes.length).toBeGreaterThan(0);
414
+ expect(changes[changes.length - 1]).not.toContain('file.txt');
415
+ });
416
+ });
417
+
418
+ describe('mv', () => {
419
+ let shell;
420
+ beforeEach(async () => {
421
+ shell = await createShell({
422
+ filesystem: { home: { 'file.txt': 'hello', dest: {} } }
423
+ });
424
+ });
425
+
426
+ it('renames a file', async () => {
427
+ await run(shell, 'mv /home/file.txt /home/renamed.txt');
428
+ expect(shell.context.fs.getFileByPath('/home/renamed.txt')).toBe('hello');
429
+ expect(shell.context.fs.getFileByPath('/home/file.txt')).toBeNull();
430
+ });
431
+
432
+ it('moves a file into a directory', async () => {
433
+ await run(shell, 'mv /home/file.txt /home/dest');
434
+ expect(shell.context.fs.getFileByPath('/home/dest/file.txt')).toBe('hello');
435
+ expect(shell.context.fs.getFileByPath('/home/file.txt')).toBeNull();
436
+ });
437
+
438
+ it('errors on missing source', async () => {
439
+ const { exitCode } = await run(shell, 'mv /home/nope.txt /home/dest');
440
+ expect(exitCode).toBeGreaterThan(0);
441
+ });
442
+ });
443
+
444
+ describe('cp', () => {
445
+ let shell;
446
+ beforeEach(async () => {
447
+ shell = await createShell({
448
+ filesystem: {
449
+ home: {
450
+ 'file.txt': 'hello',
451
+ subdir: { 'inner.txt': 'inner' },
452
+ dest: {}
453
+ }
454
+ }
455
+ });
456
+ });
457
+
458
+ it('copies a file', async () => {
459
+ await run(shell, 'cp /home/file.txt /home/copy.txt');
460
+ expect(shell.context.fs.getFileByPath('/home/copy.txt')).toBe('hello');
461
+ expect(shell.context.fs.getFileByPath('/home/file.txt')).toBe('hello');
462
+ });
463
+
464
+ it('copies a file into a directory', async () => {
465
+ await run(shell, 'cp /home/file.txt /home/dest');
466
+ expect(shell.context.fs.getFileByPath('/home/dest/file.txt')).toBe('hello');
467
+ });
468
+
469
+ it('-r copies a directory recursively', async () => {
470
+ await run(shell, 'cp -r /home/subdir /home/subdircopy');
471
+ expect(shell.context.fs.getFileByPath('/home/subdircopy/inner.txt')).toBe('inner');
472
+ });
473
+
474
+ it('errors when copying directory without -r', async () => {
475
+ const { exitCode, errors } = await run(shell, 'cp /home/subdir /home/dest2');
476
+ expect(exitCode).toBeGreaterThan(0);
477
+ expect(errors.join(' ')).toMatch(/omitting directory/i);
478
+ });
479
+
480
+ it('errors on missing source', async () => {
481
+ const { exitCode } = await run(shell, 'cp /home/nope.txt /home/dest');
482
+ expect(exitCode).toBeGreaterThan(0);
483
+ });
484
+ });
485
+
486
+ describe('onFsChange callback', () => {
487
+ it('fires when a file is created via mkdir', async () => {
488
+ const calls = [];
489
+ const shell = await createShell({
490
+ filesystem: { home: {} },
491
+ onFsChange: (fs) => calls.push(fs)
492
+ });
493
+ await run(shell, 'mkdir /home/newdir');
494
+ await new Promise(resolve => setTimeout(resolve, 10));
495
+ expect(calls.length).toBeGreaterThan(0);
496
+ const last = calls[calls.length - 1];
497
+ expect(last.home.newdir).toBeDefined();
498
+ });
499
+
500
+ it('fires with updated permissions after chmod', async () => {
501
+ const calls = [];
502
+ const shell = await createShell({
503
+ filesystem: { home: { 'f.txt': 'x' } },
504
+ onFsChange: (fs, perms) => calls.push(perms)
505
+ });
506
+ await run(shell, 'chmod 755 /home/f.txt');
507
+ await new Promise(resolve => setTimeout(resolve, 10));
508
+ expect(calls.length).toBeGreaterThan(0);
509
+ expect(calls[calls.length - 1]['/home/f.txt']).toBe(0o755);
510
+ });
511
+
512
+ it('does not fire in subshells (command substitution)', async () => {
513
+ const calls = [];
514
+ const shell = await createShell({
515
+ onFsChange: () => calls.push(1)
516
+ });
517
+ // Command substitution runs in a subshell — should not trigger parent callback
518
+ const beforeLen = calls.length;
519
+ await run(shell, 'echo $(echo hello)');
520
+ await new Promise(resolve => setTimeout(resolve, 10));
521
+ expect(calls.length).toBe(beforeLen);
522
+ });
523
+ });
524
+
525
+ describe('jsfunction', () => {
526
+ let shell;
527
+ beforeEach(async () => { shell = await createShell(); });
528
+
529
+ it('defines and calls a no-arg function', async () => {
530
+ await shell.onCommand("jsfunction hi 'args => \"hello world\"'");
531
+ const { output, exitCode } = await run(shell, 'hi');
532
+ expect(output).toContain('hello world');
533
+ expect(exitCode).toBe(0);
534
+ });
535
+
536
+ it('passes arguments to the function', async () => {
537
+ await shell.onCommand("jsfunction greet 'args => \"Hello \" + args[0]'");
538
+ const { output } = await run(shell, 'greet Alice');
539
+ expect(output).toContain('Hello Alice');
540
+ });
541
+
542
+ it('returning undefined produces no output and exits 0', async () => {
543
+ await shell.onCommand("jsfunction noop 'args => undefined'");
544
+ const { output, exitCode } = await run(shell, 'noop');
545
+ expect(output.length).toBe(0);
546
+ expect(exitCode).toBe(0);
547
+ });
548
+
549
+ it('invalid JS body gives stderr and exit code 1', async () => {
550
+ const { errors, exitCode } = await run(shell, 'jsfunction bad "{{{"');
551
+ expect(errors.length).toBeGreaterThan(0);
552
+ expect(exitCode).toBe(1);
553
+ });
554
+
555
+ it('non-function expression gives stderr and exit code 1', async () => {
556
+ const { errors, exitCode } = await run(shell, 'jsfunction notfn \'"hello"\'');
557
+ expect(errors.length).toBeGreaterThan(0);
558
+ expect(exitCode).toBe(1);
559
+ });
560
+
561
+ it('runtime error in function gives stderr and exit code 1', async () => {
562
+ await shell.onCommand("jsfunction boom 'args => { throw new Error(\"boom\"); }'");
563
+ const { errors, exitCode } = await run(shell, 'boom');
564
+ expect(errors.join(' ')).toMatch(/boom/);
565
+ expect(exitCode).toBe(1);
566
+ });
567
+
568
+ it('missing arguments gives usage error', async () => {
569
+ const { errors, exitCode } = await run(shell, 'jsfunction');
570
+ expect(errors.length).toBeGreaterThan(0);
571
+ expect(exitCode).toBe(1);
572
+ });
573
+
574
+ it('async function return value is captured', async () => {
575
+ await shell.onCommand("jsfunction asynchi 'args => Promise.resolve(\"async result\")'");
576
+ const { output, exitCode } = await run(shell, 'asynchi');
577
+ expect(output).toContain('async result');
578
+ expect(exitCode).toBe(0);
579
+ });
580
+
581
+ it('can be defined and used within a script', async () => {
582
+ const scriptShell = await createShell({
583
+ filesystem: {
584
+ scripts: {
585
+ 'fn.sh': '#!/sh.js\njsfunction double \'args => String(Number(args[0]) * 2)\'\necho $(double 5)'
586
+ }
587
+ },
588
+ permissions: { '/scripts/fn.sh': 0o755 }
589
+ });
590
+ const { output } = await run(scriptShell, '/scripts/fn.sh');
591
+ expect(output).toContain('10');
592
+ });
593
+ });