brsh 1.3.0 → 2.0.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 (59) hide show
  1. package/.github/workflows/busybox-compat.yml +166 -0
  2. package/.github/workflows/release.yml +40 -0
  3. package/.releaserc.json +18 -0
  4. package/CHANGELOG.md +355 -0
  5. package/bin/brsh-cmd.js +78 -0
  6. package/bin/exportFileSystem.js +81 -156
  7. package/busybox-testsuite/cat.tests +49 -0
  8. package/busybox-testsuite/cp.tests +206 -0
  9. package/busybox-testsuite/grep.tests +237 -0
  10. package/busybox-testsuite/testing.sh +180 -0
  11. package/dist/brsh.css +2 -0
  12. package/dist/shell.es.js +5667 -0
  13. package/dist/shell.es.js.map +1 -0
  14. package/dist/shell.min.js +14 -219
  15. package/dist/shell.min.js.map +1 -147
  16. package/dist/terminal.es.js +6228 -0
  17. package/dist/terminal.es.js.map +1 -0
  18. package/dist/terminal.min.js +113 -18
  19. package/dist/terminal.min.js.map +1 -21
  20. package/index.js +231 -56
  21. package/lib/Command.js +12 -1
  22. package/lib/Context.js +12 -4
  23. package/lib/FileSystem.js +99 -6
  24. package/lib/NodeFileSystem.js +238 -0
  25. package/lib/Result.js +6 -0
  26. package/lib/ScriptCommand.js +193 -15
  27. package/lib/Terminal.js +192 -11
  28. package/lib/WasmCommand.js +252 -0
  29. package/lib/buildFilesystemStructure.js +58 -0
  30. package/lib/builtins/cat.js +67 -9
  31. package/lib/builtins/echo.js +36 -5
  32. package/lib/builtins/false.js +13 -0
  33. package/lib/builtins/grep.js +267 -0
  34. package/lib/builtins/index.js +9 -2
  35. package/lib/builtins/ls.js +86 -18
  36. package/lib/builtins/test.js +105 -0
  37. package/lib/builtins/true.js +10 -0
  38. package/lib/local/chmod.js +72 -0
  39. package/lib/local/cp.js +156 -0
  40. package/lib/local/index.js +9 -1
  41. package/lib/local/jsfunction.js +58 -0
  42. package/lib/local/mkdir.js +44 -0
  43. package/lib/local/mv.js +96 -0
  44. package/lib/local/pwd.js +13 -0
  45. package/lib/local/read.js +107 -0
  46. package/lib/local/rm.js +71 -0
  47. package/lib/local/which.js +2 -2
  48. package/package.json +31 -34
  49. package/test/builtins.test.js +505 -0
  50. package/test/exportFileSystem.test.js +226 -0
  51. package/test/helpers.js +35 -0
  52. package/test/local-commands.test.js +593 -0
  53. package/test/node-filesystem.test.js +133 -0
  54. package/test/scripts.test.js +277 -0
  55. package/test/shell.test.js +245 -0
  56. package/test/streaming.test.js +113 -0
  57. package/vite.config.shell.js +21 -0
  58. package/vite.config.terminal.js +27 -0
  59. package/vitest.config.js +15 -0
@@ -0,0 +1,133 @@
1
+ import { describe, it, expect, beforeEach, afterEach } from 'vitest';
2
+ import { createShell, run } from './helpers.js';
3
+ import fs from 'fs';
4
+ import path from 'path';
5
+ import os from 'os';
6
+
7
+ describe('useRealFilesystem', () => {
8
+ let tmpDir;
9
+ let shell;
10
+
11
+ beforeEach(async () => {
12
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'brsh-test-'));
13
+ fs.writeFileSync(path.join(tmpDir, 'hello.txt'), 'Hello World\nFoo Bar');
14
+ fs.writeFileSync(path.join(tmpDir, 'other.txt'), 'other content');
15
+ fs.mkdirSync(path.join(tmpDir, 'subdir'));
16
+ fs.writeFileSync(path.join(tmpDir, 'subdir', 'nested.txt'), 'nested content');
17
+
18
+ shell = await createShell({ useRealFilesystem: true, cwd: tmpDir });
19
+ });
20
+
21
+ afterEach(() => {
22
+ fs.rmSync(tmpDir, { recursive: true, force: true });
23
+ });
24
+
25
+ it('ls lists real files in cwd', async () => {
26
+ const { output } = await run(shell, 'ls');
27
+ expect(output.join(' ')).toMatch(/hello\.txt/);
28
+ expect(output.join(' ')).toMatch(/other\.txt/);
29
+ expect(output.join(' ')).toMatch(/subdir/);
30
+ });
31
+
32
+ it('ls lists files at an explicit real path', async () => {
33
+ const { output } = await run(shell, `ls ${tmpDir}/subdir`);
34
+ expect(output.join(' ')).toMatch(/nested\.txt/);
35
+ });
36
+
37
+ it('cat reads real file content', async () => {
38
+ const { output } = await run(shell, `cat ${tmpDir}/hello.txt`);
39
+ expect(output).toContain('Hello World');
40
+ expect(output).toContain('Foo Bar');
41
+ });
42
+
43
+ it('grep searches a real file', async () => {
44
+ const { output, exitCode } = await run(shell, `grep Foo ${tmpDir}/hello.txt`);
45
+ expect(output).toContain('Foo Bar');
46
+ expect(exitCode).toBe(0);
47
+ });
48
+
49
+ it('grep -r searches recursively through real dirs', async () => {
50
+ const { output } = await run(shell, `grep -r nested ${tmpDir}`);
51
+ expect(output.some(l => l.includes('nested content'))).toBe(true);
52
+ });
53
+
54
+ it('mkdir creates a real directory', async () => {
55
+ await run(shell, `mkdir ${tmpDir}/newdir`);
56
+ expect(fs.existsSync(path.join(tmpDir, 'newdir'))).toBe(true);
57
+ expect(fs.statSync(path.join(tmpDir, 'newdir')).isDirectory()).toBe(true);
58
+ });
59
+
60
+ it('mkdir -p creates nested real directories', async () => {
61
+ await run(shell, `mkdir -p ${tmpDir}/a/b/c`);
62
+ expect(fs.existsSync(path.join(tmpDir, 'a', 'b', 'c'))).toBe(true);
63
+ });
64
+
65
+ it('rm deletes a real file', async () => {
66
+ await run(shell, `rm ${tmpDir}/other.txt`);
67
+ expect(fs.existsSync(path.join(tmpDir, 'other.txt'))).toBe(false);
68
+ });
69
+
70
+ it('rm -r deletes a real directory tree', async () => {
71
+ await run(shell, `rm -r ${tmpDir}/subdir`);
72
+ expect(fs.existsSync(path.join(tmpDir, 'subdir'))).toBe(false);
73
+ });
74
+
75
+ it('cp copies a real file', async () => {
76
+ await run(shell, `cp ${tmpDir}/hello.txt ${tmpDir}/hello-copy.txt`);
77
+ expect(fs.existsSync(path.join(tmpDir, 'hello-copy.txt'))).toBe(true);
78
+ expect(fs.readFileSync(path.join(tmpDir, 'hello-copy.txt'), 'utf8')).toBe('Hello World\nFoo Bar');
79
+ });
80
+
81
+ it('cp -r copies a real directory tree', async () => {
82
+ await run(shell, `cp -r ${tmpDir}/subdir ${tmpDir}/subdir-copy`);
83
+ expect(fs.existsSync(path.join(tmpDir, 'subdir-copy', 'nested.txt'))).toBe(true);
84
+ expect(fs.readFileSync(path.join(tmpDir, 'subdir-copy', 'nested.txt'), 'utf8')).toBe('nested content');
85
+ });
86
+
87
+ it('mv renames a real file', async () => {
88
+ await run(shell, `mv ${tmpDir}/other.txt ${tmpDir}/renamed.txt`);
89
+ expect(fs.existsSync(path.join(tmpDir, 'other.txt'))).toBe(false);
90
+ expect(fs.readFileSync(path.join(tmpDir, 'renamed.txt'), 'utf8')).toBe('other content');
91
+ });
92
+
93
+ it('ls -l shows real file permissions', async () => {
94
+ const { output } = await run(shell, `ls -l ${tmpDir}`);
95
+ expect(output.some(l => l.includes('hello.txt') && l.match(/^-[rwx-]{9}/))).toBe(true);
96
+ });
97
+
98
+ it('ls -l uses real file size', async () => {
99
+ const realSize = fs.statSync(path.join(tmpDir, 'hello.txt')).size;
100
+ const { output } = await run(shell, `ls -l ${tmpDir}`);
101
+ const line = output.find(l => l.includes('hello.txt'));
102
+ expect(line).toContain(String(realSize));
103
+ });
104
+
105
+ it('chmod changes real file permissions', async () => {
106
+ await run(shell, `chmod 755 ${tmpDir}/hello.txt`);
107
+ expect(fs.statSync(path.join(tmpDir, 'hello.txt')).mode & 0o777).toBe(0o755);
108
+ });
109
+
110
+ it('pwd shows the real cwd', async () => {
111
+ const { output } = await run(shell, 'pwd');
112
+ expect(output[0]).toBe(tmpDir);
113
+ });
114
+
115
+ it('cd changes the real cwd', async () => {
116
+ await shell.onCommand(`cd ${tmpDir}/subdir`);
117
+ const { output } = await run(shell, 'pwd');
118
+ expect(output[0]).toBe(path.join(tmpDir, 'subdir'));
119
+ });
120
+
121
+ it('echo and other builtins still work', async () => {
122
+ const { output } = await run(shell, 'echo hello world');
123
+ expect(output).toContain('hello world');
124
+ });
125
+
126
+ it('scripts run against the real filesystem', async () => {
127
+ const scriptPath = path.join(tmpDir, 'check.sh');
128
+ fs.writeFileSync(scriptPath, `#!/sh.js\ncat ${tmpDir}/hello.txt`);
129
+ fs.chmodSync(scriptPath, 0o755);
130
+ const { output } = await run(shell, scriptPath);
131
+ expect(output).toContain('Hello World');
132
+ });
133
+ });
@@ -0,0 +1,277 @@
1
+ import { createRequire } from 'module';
2
+ import { describe, it, expect, beforeEach } from 'vitest';
3
+ import { createShell, run } from './helpers.js';
4
+
5
+ // Use createRequire so these CJS modules share the same module cache as
6
+ // index.js's internal require() calls — ensuring instanceof checks work.
7
+ const require = createRequire(import.meta.url);
8
+ const Shell = require('../index.js');
9
+ const Command = require('../lib/Command.js');
10
+
11
+ // Returns a permissions map granting execute (0o755) to the given virtual paths.
12
+ function execPerms(...paths) {
13
+ return Object.fromEntries(paths.map(p => [p, 0o755]));
14
+ }
15
+
16
+ // ---------------------------------------------------------------------------
17
+ // JS application from filesystem
18
+ // ---------------------------------------------------------------------------
19
+
20
+ class HelloApp extends Command {
21
+ get name() { return 'hello-app'; }
22
+ run() { return Promise.resolve('Hello from JS app'); }
23
+ }
24
+
25
+ class GreeterApp extends Command {
26
+ get name() { return 'greeter'; }
27
+ run() {
28
+ const name = this.arguments[0] || 'World';
29
+ return Promise.resolve(`Hello, ${name}!`);
30
+ }
31
+ }
32
+
33
+ class FailingApp extends Command {
34
+ get name() { return 'failing-app'; }
35
+ run() { return Promise.reject(new Error('App crashed')); }
36
+ }
37
+
38
+ describe('JS application from filesystem', () => {
39
+ let shell;
40
+
41
+ beforeEach(async () => {
42
+ shell = await createShell({
43
+ filesystem: {
44
+ apps: {
45
+ hello: HelloApp,
46
+ greeter: GreeterApp,
47
+ failing: FailingApp
48
+ }
49
+ }
50
+ });
51
+ });
52
+
53
+ it('runs a Command class stored at an absolute path', async () => {
54
+ const { output } = await run(shell, '/apps/hello');
55
+ expect(output).toContain('Hello from JS app');
56
+ });
57
+
58
+ it('runs a Command class stored at a relative path (./)', async () => {
59
+ const { output } = await run(shell, './apps/hello');
60
+ expect(output).toContain('Hello from JS app');
61
+ });
62
+
63
+ it('passes arguments to the application', async () => {
64
+ const { output } = await run(shell, './apps/greeter Alice');
65
+ expect(output).toContain('Hello, Alice!');
66
+ });
67
+
68
+ it('surfaces an application error as stderr', async () => {
69
+ const { errors, exitCode } = await run(shell, './apps/failing');
70
+ expect(errors.length).toBeGreaterThan(0);
71
+ expect(exitCode).toBeGreaterThan(0);
72
+ });
73
+
74
+ it('errors when the path does not exist', async () => {
75
+ const { errors, exitCode } = await run(shell, './apps/ghost');
76
+ expect(errors.length).toBeGreaterThan(0);
77
+ expect(exitCode).toBeGreaterThan(0);
78
+ });
79
+
80
+ it('errors when the path targets a directory', async () => {
81
+ const { errors, exitCode } = await run(shell, './apps');
82
+ expect(errors.length).toBeGreaterThan(0);
83
+ expect(exitCode).toBeGreaterThan(0);
84
+ });
85
+ });
86
+
87
+ // ---------------------------------------------------------------------------
88
+ // Shell scripts
89
+ // ---------------------------------------------------------------------------
90
+
91
+ describe('shell script execution', () => {
92
+ let shell;
93
+
94
+ beforeEach(async () => {
95
+ shell = await createShell({
96
+ filesystem: {
97
+ scripts: {
98
+ 'greet.sh': '#!/sh.js\necho "Hello Script"',
99
+ 'multi.sh': '#!/sh.js\necho line1\necho line2',
100
+ 'nointerp.sh': 'this has no shebang'
101
+ }
102
+ },
103
+ permissions: execPerms('/scripts/greet.sh', '/scripts/multi.sh')
104
+ });
105
+ });
106
+
107
+ it('runs a single-command script via absolute path', async () => {
108
+ const { output } = await run(shell, '/scripts/greet.sh');
109
+ expect(output).toContain('Hello Script');
110
+ });
111
+
112
+ it('runs a script via relative path (./)', async () => {
113
+ const { output } = await run(shell, './scripts/greet.sh');
114
+ expect(output).toContain('Hello Script');
115
+ });
116
+
117
+ it('executes multiple commands in a script', async () => {
118
+ const { output } = await run(shell, '/scripts/multi.sh');
119
+ expect(output).toContain('line1');
120
+ expect(output).toContain('line2');
121
+ });
122
+
123
+ it('errors when file has no interpreter shebang', async () => {
124
+ const { exitCode } = await run(shell, '/scripts/nointerp.sh');
125
+ expect(exitCode).toBeGreaterThan(0);
126
+ });
127
+
128
+ it('errors when script file does not exist', async () => {
129
+ const { errors, exitCode } = await run(shell, '/scripts/ghost.sh');
130
+ expect(errors.length).toBeGreaterThan(0);
131
+ expect(exitCode).toBeGreaterThan(0);
132
+ });
133
+ });
134
+
135
+ // ---------------------------------------------------------------------------
136
+ // Conditional execution via &&
137
+ // ---------------------------------------------------------------------------
138
+
139
+ describe('conditional execution', () => {
140
+ let shell;
141
+ beforeEach(async () => { shell = await createShell(); });
142
+
143
+ it('runs both commands when first succeeds (&&)', async () => {
144
+ const { output } = await run(shell, 'echo a && echo b');
145
+ expect(output).toContain('a');
146
+ expect(output).toContain('b');
147
+ });
148
+
149
+ it('stops chain after first failure (&&)', async () => {
150
+ const { output } = await run(shell, 'notacommand && echo should_not_run');
151
+ expect(output).not.toContain('should_not_run');
152
+ });
153
+ });
154
+
155
+ // ---------------------------------------------------------------------------
156
+ // Control flow: if / else / elif / while / for
157
+ // ---------------------------------------------------------------------------
158
+
159
+ describe('if / else / elif', () => {
160
+ let shell;
161
+
162
+ beforeEach(async () => {
163
+ shell = await createShell({
164
+ filesystem: {
165
+ scripts: {
166
+ 'if-then.sh': '#!/sh.js\nif true; then\necho yes\nfi',
167
+ 'if-else.sh': '#!/sh.js\nif false; then\necho yes\nelse\necho no\nfi',
168
+ 'if-elif.sh': '#!/sh.js\nexport V=b\nif [ "$V" = "a" ]; then\necho isa\nelif [ "$V" = "b" ]; then\necho isb\nelse\necho isc\nfi',
169
+ 'if-nested.sh': '#!/sh.js\nif true; then\nif true; then\necho inner\nfi\nfi',
170
+ 'if-var.sh': '#!/sh.js\nexport X=hello\nif [ "$X" = "hello" ]; then\necho matched\nfi',
171
+ }
172
+ },
173
+ permissions: execPerms(
174
+ '/scripts/if-then.sh', '/scripts/if-else.sh', '/scripts/if-elif.sh',
175
+ '/scripts/if-nested.sh', '/scripts/if-var.sh'
176
+ )
177
+ });
178
+ });
179
+
180
+ it('runs then-branch when condition succeeds', async () => {
181
+ const { output } = await run(shell, '/scripts/if-then.sh');
182
+ expect(output).toContain('yes');
183
+ });
184
+
185
+ it('runs else-branch when condition fails', async () => {
186
+ const { output } = await run(shell, '/scripts/if-else.sh');
187
+ expect(output).not.toContain('yes');
188
+ expect(output).toContain('no');
189
+ });
190
+
191
+ it('runs the matching elif branch', async () => {
192
+ const { output } = await run(shell, '/scripts/if-elif.sh');
193
+ expect(output).toContain('isb');
194
+ expect(output).not.toContain('isa');
195
+ expect(output).not.toContain('isc');
196
+ });
197
+
198
+ it('supports nested if blocks', async () => {
199
+ const { output } = await run(shell, '/scripts/if-nested.sh');
200
+ expect(output).toContain('inner');
201
+ });
202
+
203
+ it('uses [ ] test to check a variable', async () => {
204
+ const { output } = await run(shell, '/scripts/if-var.sh');
205
+ expect(output).toContain('matched');
206
+ });
207
+ });
208
+
209
+ describe('while loop', () => {
210
+ let shell;
211
+
212
+ beforeEach(async () => {
213
+ shell = await createShell({
214
+ filesystem: {
215
+ scripts: {
216
+ 'while-count.sh': '#!/sh.js\nexport I=0\nwhile [ "$I" != "3" ]; do\nexport I=$(($I + 1))\ndone\necho $I',
217
+ 'while-skip.sh': '#!/sh.js\nif false; then\nwhile true; do\necho infinite\ndone\nfi\necho done',
218
+ }
219
+ },
220
+ permissions: execPerms('/scripts/while-count.sh', '/scripts/while-skip.sh')
221
+ });
222
+ });
223
+
224
+ it('loops until condition becomes false', async () => {
225
+ const { output } = await run(shell, '/scripts/while-count.sh');
226
+ expect(output).toContain('3');
227
+ });
228
+
229
+ it('skips a while block when inside a false if branch', async () => {
230
+ const { output } = await run(shell, '/scripts/while-skip.sh');
231
+ expect(output).toContain('done');
232
+ expect(output).not.toContain('infinite');
233
+ });
234
+ });
235
+
236
+ describe('for loop', () => {
237
+ let shell;
238
+
239
+ beforeEach(async () => {
240
+ shell = await createShell({
241
+ filesystem: {
242
+ scripts: {
243
+ 'for-list.sh': '#!/sh.js\nfor item in apple banana cherry; do\necho $item\ndone',
244
+ 'for-var.sh': '#!/sh.js\nexport ITEMS="x y z"\nfor i in $ITEMS; do\necho $i\ndone',
245
+ 'for-nested.sh': '#!/sh.js\nfor a in 1 2; do\nfor b in x y; do\necho $a$b\ndone\ndone',
246
+ }
247
+ },
248
+ permissions: execPerms('/scripts/for-list.sh', '/scripts/for-var.sh', '/scripts/for-nested.sh')
249
+ });
250
+ });
251
+
252
+ it('iterates over a literal list', async () => {
253
+ const { output } = await run(shell, '/scripts/for-list.sh');
254
+ expect(output).toContain('apple');
255
+ expect(output).toContain('banana');
256
+ expect(output).toContain('cherry');
257
+ });
258
+
259
+ it('iterates in order', async () => {
260
+ const { output } = await run(shell, '/scripts/for-list.sh');
261
+ expect(output.indexOf('apple')).toBeLessThan(output.indexOf('banana'));
262
+ expect(output.indexOf('banana')).toBeLessThan(output.indexOf('cherry'));
263
+ });
264
+
265
+ it('expands a variable to build the list', async () => {
266
+ const { output } = await run(shell, '/scripts/for-var.sh');
267
+ expect(output).toContain('x');
268
+ expect(output).toContain('y');
269
+ expect(output).toContain('z');
270
+ });
271
+
272
+ it('supports nested for loops', async () => {
273
+ const { output } = await run(shell, '/scripts/for-nested.sh');
274
+ expect(output).toContain('1x');
275
+ expect(output).toContain('2y');
276
+ });
277
+ });
@@ -0,0 +1,245 @@
1
+ import { createRequire } from 'module';
2
+ import { describe, it, expect, beforeEach } from 'vitest';
3
+ import { createShell, run } from './helpers.js';
4
+
5
+ const require = createRequire(import.meta.url);
6
+ const Shell = require('../index.js');
7
+
8
+ describe('Shell core behaviour', () => {
9
+ let shell;
10
+
11
+ beforeEach(async () => {
12
+ shell = await createShell();
13
+ });
14
+
15
+ it('emits STATUS_READY after construction', () => {
16
+ expect(shell).toBeDefined();
17
+ });
18
+
19
+ it('exposes status constants', () => {
20
+ expect(Shell.STATUS_READY).toBe('READY');
21
+ expect(Shell.STATUS_WORKING).toBe('WORKING');
22
+ expect(Shell.STATUS_DESTROYED).toBe('DESTROYED');
23
+ });
24
+
25
+ it('sets lastCode to 0 initially', () => {
26
+ expect(shell.lastCode).toBe(0);
27
+ });
28
+
29
+ it('emits STATUS_WORKING then STATUS_READY during a command', async () => {
30
+ const statuses = [];
31
+ shell.on('status', (s) => statuses.push(s));
32
+ await shell.onCommand('echo hello');
33
+ expect(statuses).toContain(Shell.STATUS_WORKING);
34
+ expect(statuses[statuses.length - 1]).toBe(Shell.STATUS_READY);
35
+ });
36
+
37
+ it('strips comments from input', async () => {
38
+ const { output } = await run(shell, 'echo hello # this is a comment');
39
+ expect(output).toContain('hello');
40
+ });
41
+
42
+ it('substitutes $VARIABLE in arguments', async () => {
43
+ await shell.onCommand('export GREETING=hello');
44
+ const { output } = await run(shell, 'echo $GREETING');
45
+ expect(output).toContain('hello');
46
+ });
47
+
48
+ it('substitutes ${VARIABLE} in arguments', async () => {
49
+ await shell.onCommand('export NAME=world');
50
+ const { output } = await run(shell, 'echo ${NAME}');
51
+ expect(output).toContain('world');
52
+ });
53
+
54
+ it('handles single-quoted strings as one argument', async () => {
55
+ const { output } = await run(shell, "echo 'hello world'");
56
+ expect(output).toContain('hello world');
57
+ });
58
+
59
+ it('handles double-quoted strings as one argument', async () => {
60
+ const { output } = await run(shell, 'echo "hello world"');
61
+ expect(output).toContain('hello world');
62
+ });
63
+
64
+ it('emits stdErr and sets lastCode != 0 for unknown commands', async () => {
65
+ const { errors, exitCode } = await run(shell, 'notacommand');
66
+ expect(errors.length).toBeGreaterThan(0);
67
+ expect(exitCode).toBeGreaterThan(0);
68
+ });
69
+
70
+ it('short-circuits && chain on non-zero exit code', async () => {
71
+ const { output, errors } = await run(shell, 'notacommand && echo should_not_print');
72
+ expect(output).not.toContain('should_not_print');
73
+ expect(errors.length).toBeGreaterThan(0);
74
+ });
75
+
76
+ it('runs both sides of && when first command succeeds', async () => {
77
+ const { output } = await run(shell, 'echo first && echo second');
78
+ expect(output).toContain('first');
79
+ expect(output).toContain('second');
80
+ });
81
+
82
+ it('ignores empty commands gracefully', async () => {
83
+ const result = await run(shell, '');
84
+ expect(result.exitCode).toBe(0);
85
+ });
86
+
87
+ it('returns the prompt string', () => {
88
+ const prompt = shell.getPrompt();
89
+ expect(typeof prompt).toBe('string');
90
+ expect(prompt.length).toBeGreaterThan(0);
91
+ });
92
+
93
+ it('emits clear event when clear() is called', () => {
94
+ return new Promise((resolve) => {
95
+ shell.once('clear', resolve);
96
+ shell.clear();
97
+ });
98
+ });
99
+
100
+ it('destroys shell with exit event', () => {
101
+ return new Promise((resolve) => {
102
+ shell.once('exit', (code) => {
103
+ expect(code).toBe(0);
104
+ resolve();
105
+ });
106
+ shell.destroy();
107
+ });
108
+ });
109
+
110
+ it('does not double-destroy', () => {
111
+ shell.destroy();
112
+ expect(() => shell.destroy()).not.toThrow();
113
+ });
114
+
115
+ it('exposes the Command class', () => {
116
+ expect(Shell.Command).toBeDefined();
117
+ });
118
+ });
119
+
120
+ describe('|| and ; operators', () => {
121
+ let shell;
122
+ beforeEach(async () => { shell = await createShell(); });
123
+
124
+ it('runs right side of || when left fails', async () => {
125
+ const { output } = await run(shell, 'notacommand || echo fallback');
126
+ expect(output).toContain('fallback');
127
+ });
128
+
129
+ it('skips right side of || when left succeeds', async () => {
130
+ const { output } = await run(shell, 'echo first || echo should_not_run');
131
+ expect(output).toContain('first');
132
+ expect(output).not.toContain('should_not_run');
133
+ });
134
+
135
+ it('|| resets the && chain allowing further commands to run', async () => {
136
+ const { output } = await run(shell, 'notacommand && echo skip || echo recover');
137
+ expect(output).not.toContain('skip');
138
+ expect(output).toContain('recover');
139
+ });
140
+
141
+ it('; always runs the next command regardless of exit code', async () => {
142
+ const { output } = await run(shell, 'echo a ; echo b');
143
+ expect(output).toContain('a');
144
+ expect(output).toContain('b');
145
+ });
146
+
147
+ it('; continues after a failing command', async () => {
148
+ const { output } = await run(shell, 'notacommand ; echo after');
149
+ expect(output).toContain('after');
150
+ });
151
+
152
+ it('chains multiple ; separated commands', async () => {
153
+ const { output } = await run(shell, 'echo x ; echo y ; echo z');
154
+ expect(output).toContain('x');
155
+ expect(output).toContain('y');
156
+ expect(output).toContain('z');
157
+ });
158
+ });
159
+
160
+ describe('command substitution $()', () => {
161
+ let shell;
162
+ beforeEach(async () => { shell = await createShell(); });
163
+
164
+ it('substitutes the output of a subcommand into an argument', async () => {
165
+ const { output } = await run(shell, 'echo $(echo hello)');
166
+ expect(output).toContain('hello');
167
+ });
168
+
169
+ it('substitutes into a variable assignment', async () => {
170
+ await shell.onCommand('export RESULT=$(echo captured)');
171
+ expect(shell.context.getVar('RESULT')).toBe('captured');
172
+ });
173
+ });
174
+
175
+ describe('arithmetic substitution $(( ))', () => {
176
+ let shell;
177
+ beforeEach(async () => { shell = await createShell(); });
178
+
179
+ it('evaluates simple addition', async () => {
180
+ const { output } = await run(shell, 'echo $((2 + 3))');
181
+ expect(output).toContain('5');
182
+ });
183
+
184
+ it('evaluates multiplication', async () => {
185
+ const { output } = await run(shell, 'echo $((4 * 5))');
186
+ expect(output).toContain('20');
187
+ });
188
+
189
+ it('substitutes variables inside arithmetic', async () => {
190
+ await shell.onCommand('export X=7');
191
+ const { output } = await run(shell, 'echo $(($X + 3))');
192
+ expect(output).toContain('10');
193
+ });
194
+
195
+ it('stores arithmetic result in a variable', async () => {
196
+ await shell.onCommand('export TOTAL=$((10 - 4))');
197
+ expect(shell.context.getVar('TOTAL')).toBe('6');
198
+ });
199
+ });
200
+
201
+ describe('unclosed quote error', () => {
202
+ let shell;
203
+ beforeEach(async () => { shell = await createShell(); });
204
+
205
+ it('emits stdErr and sets non-zero exit code for unclosed single quote', async () => {
206
+ const { errors, exitCode } = await run(shell, "echo 'unclosed");
207
+ expect(errors.length).toBeGreaterThan(0);
208
+ expect(exitCode).toBeGreaterThan(0);
209
+ });
210
+
211
+ it('emits stdErr and sets non-zero exit code for unclosed double quote', async () => {
212
+ const { errors, exitCode } = await run(shell, 'echo "unclosed');
213
+ expect(errors.length).toBeGreaterThan(0);
214
+ expect(exitCode).toBeGreaterThan(0);
215
+ });
216
+ });
217
+
218
+ describe('tabCompletion', () => {
219
+ it('returns matching built-in commands from PATH', async () => {
220
+ const shell = await createShell();
221
+ const result = shell.tabCompletion('ec');
222
+ expect(result).not.toBeNull();
223
+ expect(result.options).toContain('echo');
224
+ });
225
+
226
+ it('returns null when nothing matches', async () => {
227
+ const shell = await createShell();
228
+ const result = shell.tabCompletion('zzznothing');
229
+ expect(result).toBeNull();
230
+ });
231
+
232
+ it('returns filesystem paths when given a path prefix', async () => {
233
+ const shell = await createShell({ filesystem: { home: { user: {} } } });
234
+ const result = shell.tabCompletion('/ho');
235
+ expect(result).not.toBeNull();
236
+ expect(result.options.some(o => o.startsWith('home'))).toBe(true);
237
+ });
238
+
239
+ it('appends / to directory completions', async () => {
240
+ const shell = await createShell({ filesystem: { mydir: {} } });
241
+ const result = shell.tabCompletion('/my');
242
+ expect(result).not.toBeNull();
243
+ expect(result.options).toContain('mydir/');
244
+ });
245
+ });