brsh 3.10.0 → 3.10.1

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.
@@ -1140,5 +1140,153 @@ describe('Output Typing Animation Suppression', () => {
1140
1140
 
1141
1141
  term.destroy();
1142
1142
  });
1143
+
1144
+ it('measures initial dimensions before attaching cli and subtracts CSS padding', () => {
1145
+ const el = document.createElement('div');
1146
+ el.clientWidth = 1020;
1147
+ el.clientHeight = 520;
1148
+ globalThis.window.getComputedStyle = () => ({
1149
+ paddingLeft: '10px',
1150
+ paddingRight: '10px',
1151
+ paddingTop: '14px',
1152
+ paddingBottom: '14px'
1153
+ });
1154
+ const origCreate = globalThis.document.createElement;
1155
+ globalThis.document.createElement = (tag) => {
1156
+ const node = origCreate(tag);
1157
+ if (tag === 'span') {
1158
+ node.getBoundingClientRect = () => ({ left: 0, top: 0, width: 8, height: 18 });
1159
+ }
1160
+ return node;
1161
+ };
1162
+
1163
+ try {
1164
+ const sizedShell = new Shell({ cwd: '/root' });
1165
+ const term = new Terminal({
1166
+ el,
1167
+ cli: sizedShell
1168
+ });
1169
+
1170
+ // (1020 - 20) / 8 = 125 cols; (520 - 28) / 18 = 27 rows
1171
+ expect(term.cols).toBe(125);
1172
+ expect(term.rows).toBe(27);
1173
+ expect(sizedShell.stdout.columns).toBe(125);
1174
+ expect(sizedShell.stdout.rows).toBe(27);
1175
+ expect(sizedShell.context.getVar('COLUMNS')).toBe('125');
1176
+ expect(sizedShell.context.getVar('LINES')).toBe('27');
1177
+
1178
+ term.destroy();
1179
+ } finally {
1180
+ globalThis.document.createElement = origCreate;
1181
+ delete globalThis.window.getComputedStyle;
1182
+ }
1183
+ });
1184
+
1185
+ it('does not double-emit Enter when mobile input keydown bubbles to window', () => {
1186
+ const el = document.createElement('div');
1187
+ const term = new Terminal({
1188
+ el,
1189
+ Shell: false,
1190
+ mobileInput: 'click'
1191
+ });
1192
+
1193
+ const received = [];
1194
+ term.onData((d) => received.push(d));
1195
+
1196
+ const enterEvent = {
1197
+ key: 'Enter',
1198
+ defaultPrevented: false,
1199
+ preventDefault() { this.defaultPrevented = true; }
1200
+ };
1201
+ // Dispatch on hidden <input>, then simulate DOM bubbling to window
1202
+ term._mobileInputElement.dispatchEvent('keydown', enterEvent);
1203
+ globalThis.window.dispatchEvent('keydown', enterEvent);
1204
+
1205
+ // IME composing events on window should also be ignored
1206
+ globalThis.window.dispatchEvent('keydown', { key: 'a', isComposing: true });
1207
+ globalThis.window.dispatchEvent('keydown', { key: 'a', keyCode: 229 });
1208
+
1209
+ expect(received).toEqual(['\r']);
1210
+ term.destroy();
1211
+ });
1212
+
1213
+ it('supports legacy/alias key names and ignores bare lock/unidentified keys', () => {
1214
+ const el = document.createElement('div');
1215
+ const term = new Terminal({
1216
+ el,
1217
+ Shell: false,
1218
+ cols: 80,
1219
+ rows: 24
1220
+ });
1221
+
1222
+ const received = [];
1223
+ term.onData((d) => received.push(d));
1224
+
1225
+ term.sendKey('Space');
1226
+ term.sendKey('Spacebar');
1227
+ term.sendKey('Return');
1228
+ term.sendKey('Esc');
1229
+ term.sendKey('Up');
1230
+ term.sendKey('Down');
1231
+ term.sendKey('Right');
1232
+ term.sendKey('Left');
1233
+ term.sendKey('Del');
1234
+ term.sendKey('F1');
1235
+ term.sendKey('F12');
1236
+ term.sendKey('NumLock');
1237
+ term.sendKey('ScrollLock');
1238
+ term.sendKey('CapsLock');
1239
+ term.sendKey('Dead');
1240
+ term.sendKey('Unidentified');
1241
+
1242
+ expect(received).toEqual([
1243
+ ' ', ' ', '\r', '\x1b',
1244
+ '\x1b[A', '\x1b[B', '\x1b[C', '\x1b[D',
1245
+ '\x1b[3~', '\x1bOP', '\x1b[24~'
1246
+ ]);
1247
+ term.destroy();
1248
+ });
1249
+
1250
+ it('passes Ctrl+C (^C) to raw ReplCommand to clear current input instead of killing REPL', async () => {
1251
+ class LineClearRepl extends Shell.ReplCommand {
1252
+ static get name() { return 'clearrepl'; }
1253
+ shouldBeInteractive() { return true; }
1254
+ getReplTitle() { return 'LineClear REPL'; }
1255
+ getReplPrompt() { return 'lc> '; }
1256
+ async evaluateReplLine(line) {
1257
+ return { output: [`ok:${line}`] };
1258
+ }
1259
+ }
1260
+
1261
+ const replShell = new Shell({ filesystem: { root: {} }, cwd: '/root', hostname: 'brsh' });
1262
+ const ready = waitForReady(replShell);
1263
+ const term = new Terminal({
1264
+ el: document.createElement('div'),
1265
+ cli: replShell,
1266
+ cols: 60,
1267
+ rows: 15
1268
+ });
1269
+ await ready;
1270
+
1271
+ replShell.context.setCommand(LineClearRepl);
1272
+ const replDone = waitForReady(replShell);
1273
+ replShell.onCommand('clearrepl');
1274
+ await new Promise(r => setTimeout(r, 30));
1275
+
1276
+ for (const ch of 'draft') term.sendKey(ch);
1277
+ // Send Ctrl+C while input is non-empty -> clears input line, keeps REPL alive in alt screen
1278
+ term.sendKey('c', { ctrl: true });
1279
+ await new Promise(r => setTimeout(r, 20));
1280
+
1281
+ expect(term.screen.isAltScreen).toBe(true);
1282
+ expect(term.getScreenText()).toContain('lc> draft^C');
1283
+
1284
+ // Exit cleanly with Ctrl+D
1285
+ term.sendKey('d', { ctrl: true });
1286
+ await replDone;
1287
+ expect(replShell.lastCode).toBe(0);
1288
+ term.destroy();
1289
+ });
1143
1290
  });
1144
1291
  });
1292
+
@@ -187,5 +187,81 @@ describe('Running .wasm files directly as CLI tools in base shell', () => {
187
187
  const res = await run(shell, 'devtool');
188
188
  expect(res.exitCode).toBe(0);
189
189
  });
190
+
191
+ it('passes live stdout COLUMNS and LINES to WASI environ_get over stale context vars', async () => {
192
+ // WASI binary that reads environ_sizes_get + environ_get and writes the environment block to stdout
193
+ const ENV_DUMP_WASM_BYTES = Buffer.from(
194
+ 'AGFzbQEAAAABEgNgAn9/AX9gBH9/f38Bf2AAAAJzAxZ3YXNpX3NuYXBzaG90X3ByZXZpZXcxEWVudmlyb25fc2l6ZXNfZ2V0AAAWd2FzaV9zbmFwc2hvdF9wcmV2aWV3MQtlbnZpcm9uX2dldAAAFndhc2lfc25hcHNob3RfcHJldmlldzEIZmRfd3JpdGUAAQMCAQIFAwEAAQcTAgZtZW1vcnkCAAZfc3RhcnQAAwowAS4AQQBBBBAAGkEQQYAEEAEaQQhBgAQ2AgBBDEEEKAIANgIAQQFBCEEBQQAQAhoL',
195
+ 'base64'
196
+ );
197
+
198
+ const shell = new Shell({
199
+ filesystem: {
200
+ bin: { 'envdump.wasm': ENV_DUMP_WASM_BYTES }
201
+ },
202
+ permissions: {
203
+ '/bin/envdump.wasm': 0o755
204
+ }
205
+ });
206
+
207
+ // Simulate stale 80x24 context vars while live stream dimensions are 132x42
208
+ shell.context.setVar('COLUMNS', '80');
209
+ shell.context.setVar('LINES', '24');
210
+ shell.stdout.columns = 132;
211
+ shell.stdout.rows = 42;
212
+ shell.stdin.columns = 132;
213
+ shell.stdin.rows = 42;
214
+
215
+ const res = await run(shell, 'envdump');
216
+ expect(res.exitCode).toBe(0);
217
+ const rawEnv = res.output.join('\n').replace(/\0/g, '\n');
218
+ expect(rawEnv).toContain('COLUMNS=132');
219
+ expect(rawEnv).toContain('LINES=42');
220
+ expect(rawEnv).not.toContain('COLUMNS=80');
221
+ expect(rawEnv).not.toContain('LINES=24');
222
+ });
223
+
224
+ it('delivers stdin via hasCustomStdinListener and distinguishes poll_oneoff timeout from FD_READ', async () => {
225
+ // Asyncify WASI binary polling FD_READ (type=1) + CLOCK 35ms (type=0) and printing "<nevents>:<event0.type>\n"
226
+ const ASYNC_POLL_WASM_BYTES = Buffer.from(
227
+ 'AGFzbQEAAAABFARgBH9/f38Bf2ABfwBgAABgAAF/AkgCFndhc2lfc25hcHNob3RfcHJldmlldzELcG9sbF9vbmVvZmYAABZ3YXNpX3NuYXBzaG90X3ByZXZpZXcxCGZkX3dyaXRlAAADBwYBAgECAwIFAwEAAgYGAX8BQQALB4YBBwZtZW1vcnkCABVhc3luY2lmeV9zdGFydF91bndpbmQAAhRhc3luY2lmeV9zdG9wX3Vud2luZAADFWFzeW5jaWZ5X3N0YXJ0X3Jld2luZAAEFGFzeW5jaWZ5X3N0b3BfcmV3aW5kAAUSYXN5bmNpZnlfZ2V0X3N0YXRlAAYGX3N0YXJ0AAcK1AEGBgBBASQACwYAQQAkAAsGAEECJAALBgBBACQACwQAIwALsAEAQcAAQgs3AwBByABBAToAAEHQAEEANgIAQfAAQhY3AwBB+ABBADoAAEGAAUEBNgIAQYgBQsCd2BA3AwBBmAFBADsBAEHKAUEJOgAAQcAAQcABQQJBmAIQABojAEEBRgRADwtBrAJBMEGYAigCAGo6AABBrQJBOjoAAEGuAkEwQcoBLQAAajoAAEGvAkEKOgAAQcACQawCNgIAQcQCQQQ2AgBBAUHAAkEBQcgCEAEaCw==',
228
+ 'base64'
229
+ );
230
+
231
+ const shell = new Shell({
232
+ filesystem: {
233
+ bin: { 'asyncpoll.wasm': ASYNC_POLL_WASM_BYTES }
234
+ },
235
+ permissions: {
236
+ '/bin/asyncpoll.wasm': 0o755
237
+ }
238
+ });
239
+
240
+ // 1. No input sent -> clock timeout fires ONLY clock event ("1:0"), NOT false FD_READ
241
+ const rawOut1 = [];
242
+ const onRaw1 = (chunk) => rawOut1.push(String(chunk));
243
+ shell.stdout.on('data', onRaw1);
244
+ const timeoutRes = await run(shell, 'asyncpoll');
245
+ shell.stdout.off('data', onRaw1);
246
+ expect(timeoutRes.exitCode).toBe(0);
247
+ const text1 = rawOut1.join('') + timeoutRes.output.join('');
248
+ expect(text1).toContain('1:0');
249
+ expect(text1).not.toContain('1:1');
250
+
251
+ // 2. Input written while WASM is polling -> delivered via hasCustomStdinListener, wakes FD_READ ("1:1")
252
+ const rawOut2 = [];
253
+ const onRaw2 = (chunk) => rawOut2.push(String(chunk));
254
+ shell.stdout.on('data', onRaw2);
255
+ const cmdPromise = shell.onCommand('asyncpoll');
256
+ await new Promise(r => setTimeout(r, 10));
257
+ shell.write('k');
258
+ await cmdPromise;
259
+ shell.stdout.off('data', onRaw2);
260
+ expect(shell.lastCode).toBe(0);
261
+ const text2 = rawOut2.join('');
262
+ expect(text2).toContain('1:1');
263
+ expect(text2).not.toContain('1:0');
264
+ });
190
265
  });
191
266
 
267
+
package/test/wbt.test.js CHANGED
@@ -1796,8 +1796,118 @@ describe('WBT filesystem installer architecture (installer, not runtime)', () =>
1796
1796
  const tmpKeys = Object.keys(tmpDir);
1797
1797
  expect(tmpKeys.some(k => k.startsWith('.wbt-run-'))).toBe(false);
1798
1798
  });
1799
- });
1800
1799
 
1800
+ it('persists interactive: true in /usr/lib/<pkg>/package.json so standalone WasmCommand detects interactivity immediately', async () => {
1801
+ await run(shell, 'wbt install nano');
1802
+ await run(shell, 'wbt install doom');
1803
+
1804
+ const nanoPkgJson = JSON.parse(shell.context.fs.getFileByPath('/usr/lib/nano/package.json'));
1805
+ const doomPkgJson = JSON.parse(shell.context.fs.getFileByPath('/usr/lib/doom/package.json'));
1806
+ expect(nanoPkgJson.interactive).toBe(true);
1807
+ expect(doomPkgJson.interactive).toBe(true);
1808
+
1809
+ const WasmCommand = shell.constructor.WasmCommand;
1810
+ const doomCmd = new WasmCommand('/usr/lib/doom/doom.wasm', [], shell.context);
1811
+ expect(doomCmd.interactive).toBe(true);
1812
+ expect(doomCmd.captureInput).toBe(true);
1813
+ });
1814
+
1815
+ it('does not duplicate stdin keystrokes and forwards live stdout resizes during wbt run', async () => {
1816
+ if (!NANO_WASM_BYTES) return;
1817
+ shell.setTerminalSize(110, 36);
1818
+
1819
+ const cmdPromise = shell.onCommand('wbt run nano /wbt-run-note.txt');
1820
+ await new Promise(r => setTimeout(r, 80));
1821
+
1822
+ // Resize terminal mid-run -> forwarded to ephemeral wbt run subcommand without error
1823
+ shell.setTerminalSize(124, 40);
1824
+ await new Promise(r => setTimeout(r, 20));
1825
+
1826
+ for (const ch of 'Single echo!') {
1827
+ shell.write(ch);
1828
+ await new Promise(r => setTimeout(r, 6));
1829
+ }
1830
+ await new Promise(r => setTimeout(r, 30));
1831
+
1832
+ // Save: Ctrl+O, Enter, Exit: Ctrl+X
1833
+ shell.write('\x0f');
1834
+ await new Promise(r => setTimeout(r, 30));
1835
+ shell.write('\r');
1836
+ await new Promise(r => setTimeout(r, 30));
1837
+ shell.write('\x18');
1838
+
1839
+ await cmdPromise;
1840
+ expect(shell.lastCode).toBe(0);
1841
+ const saved = shell.context.fs.getFileByPath('/wbt-run-note.txt');
1842
+ expect(saved).toContain('Single echo!');
1843
+ expect(saved).not.toContain('SSiinnggllee');
1844
+ });
1801
1845
 
1846
+ it('extends standard Command rather than LocalCommand for both Wbt and GenericWasmCommand', () => {
1847
+ const Command = require('../lib/Command.js');
1848
+ const LocalCommand = require('../lib/local/LocalCommand.js');
1849
+ const GenericCmd = Wbt.createGenericWasmCommand({ name: 'demo-tool' });
1802
1850
 
1851
+ expect(Wbt.prototype instanceof Command).toBe(true);
1852
+ expect(Wbt.prototype instanceof LocalCommand).toBe(false);
1853
+ expect(GenericCmd.prototype instanceof Command).toBe(true);
1854
+ expect(GenericCmd.prototype instanceof LocalCommand).toBe(false);
1855
+ });
1856
+
1857
+ it('works across separate built ESM bundles (dist/shell.es.js + dist/wbt.es.js) and runs installed binaries without wbt at runtime', async () => {
1858
+ const distShellPath = path.resolve(import.meta.dirname, '../dist/shell.es.js');
1859
+ const distWbtPath = path.resolve(import.meta.dirname, '../dist/wbt.es.js');
1860
+ if (!fs.existsSync(distShellPath) || !fs.existsSync(distWbtPath)) return;
1861
+
1862
+ const ShellMod = await import(distShellPath);
1863
+ const WbtMod = await import(distWbtPath);
1864
+ const BundledShell = ShellMod.default?.default ?? ShellMod.default ?? ShellMod;
1865
+ const BundledWbt = WbtMod.default?.default ?? WbtMod.default ?? WbtMod;
1866
+
1867
+ const ConfiguredBundledWbt = BundledWbt.configure({
1868
+ fetch: async (url) => {
1869
+ if (url.endsWith('freedoom')) {
1870
+ return {
1871
+ ok: true,
1872
+ status: 200,
1873
+ text: async () => '#!/bin/sh\necho "freedoom-bundle-ok:$1"\n'
1874
+ };
1875
+ }
1876
+ return {
1877
+ ok: true,
1878
+ status: 200,
1879
+ arrayBuffer: async () => MINIMAL_WASM_BYTES.buffer.slice(0)
1880
+ };
1881
+ }
1882
+ });
1883
+
1884
+ const s1 = new BundledShell({ filesystem: {}, interactive: false });
1885
+ s1.context.setCommand(ConfiguredBundledWbt);
1886
+
1887
+ const listRes = await run(s1, 'wbt list');
1888
+ expect(listRes.exitCode).toBe(0);
1889
+ expect(listRes.output.join('\n')).toContain('doom/wasm');
1890
+
1891
+ // Ephemeral script execution via `wbt run` with readonlyContext across bundles
1892
+ const runRes = await run(s1, 'wbt run freedoom hello');
1893
+ expect(runRes.exitCode).toBe(0);
1894
+ expect(runRes.output.join('\n')).toContain('freedoom-bundle-ok:hello');
1895
+
1896
+ // Install into filesystem via `wbt install`, serialize to .brfs, and boot a fresh Shell WITHOUT Wbt
1897
+ const installRes = await run(s1, 'wbt install doom');
1898
+ expect(installRes.exitCode).toBe(0);
1899
+
1900
+ const containerBytes = s1.context.fs.toBinaryContainer();
1901
+ const s2 = await BundledShell.fromContainer(containerBytes, { interactive: false });
1902
+ // Ensure Wbt is NOT registered in s2
1903
+ expect(s2.context.getCommand('wbt')).toBeNull();
1904
+
1905
+ const nativeRes = await run(s2, 'freedoom from-container');
1906
+ expect(nativeRes.exitCode).toBe(0);
1907
+ expect(nativeRes.output.join('\n')).toContain('freedoom-bundle-ok:from-container');
1908
+
1909
+ const wasmRes = await run(s2, 'doom');
1910
+ expect(wasmRes.exitCode).toBe(0);
1911
+ });
1912
+ });
1803
1913