brsh 3.2.0 → 3.3.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.
@@ -452,3 +452,83 @@ describe('sequential script execution', () => {
452
452
  expect(output[2]).toBe('3');
453
453
  });
454
454
  });
455
+
456
+ describe('multiline quotes and line continuations in scripts', () => {
457
+ it('joins lines with unclosed double quotes into a single logical line', async () => {
458
+ const shell = await createShell({
459
+ filesystem: {
460
+ scripts: {
461
+ 'multiline-dq.sh': '#!/sh.js\necho "hello\nworld"'
462
+ }
463
+ },
464
+ permissions: execPerms('/scripts/multiline-dq.sh')
465
+ });
466
+ const { output } = await run(shell, '/scripts/multiline-dq.sh');
467
+ expect(output).toEqual(['hello', 'world']);
468
+ });
469
+
470
+ it('joins lines with unclosed single quotes into a single logical line', async () => {
471
+ const shell = await createShell({
472
+ filesystem: {
473
+ scripts: {
474
+ 'multiline-sq.sh': '#!/sh.js\necho \'first\nsecond\''
475
+ }
476
+ },
477
+ permissions: execPerms('/scripts/multiline-sq.sh')
478
+ });
479
+ const { output } = await run(shell, '/scripts/multiline-sq.sh');
480
+ expect(output).toEqual(['first', 'second']);
481
+ });
482
+
483
+ it('joins lines with trailing line continuation backslash', async () => {
484
+ const shell = await createShell({
485
+ filesystem: {
486
+ scripts: {
487
+ 'continuation.sh': '#!/sh.js\necho \\\n hello \\\n world'
488
+ }
489
+ },
490
+ permissions: execPerms('/scripts/continuation.sh')
491
+ });
492
+ const { output } = await run(shell, '/scripts/continuation.sh');
493
+ expect(output).toEqual(['hello world']);
494
+ });
495
+
496
+ it('supports line continuation in if conditions', async () => {
497
+ const shell = await createShell({
498
+ filesystem: {
499
+ scripts: {
500
+ 'if-cont.sh': '#!/sh.js\nif [ 1 = 1 ] \\\n && [ 2 = 2 ]; then\necho matched\nfi'
501
+ }
502
+ },
503
+ permissions: execPerms('/scripts/if-cont.sh')
504
+ });
505
+ const { output } = await run(shell, '/scripts/if-cont.sh');
506
+ expect(output).toEqual(['matched']);
507
+ });
508
+
509
+ it('supports negation operator in if condition: if ! command; then', async () => {
510
+ const shell = await createShell({
511
+ filesystem: {
512
+ scripts: {
513
+ 'if-not.sh': '#!/sh.js\nif ! which non_existent_binary; then\necho "not found handled"\nfi'
514
+ }
515
+ },
516
+ permissions: execPerms('/scripts/if-not.sh')
517
+ });
518
+ const { output } = await run(shell, '/scripts/if-not.sh');
519
+ expect(output).toEqual(['not found handled']);
520
+ });
521
+
522
+ it('does not execute if block when condition with ! succeeds: if ! true; then', async () => {
523
+ const shell = await createShell({
524
+ filesystem: {
525
+ scripts: {
526
+ 'if-not-true.sh': '#!/sh.js\nif ! true; then\necho unexpected\nelse\necho expected\nfi'
527
+ }
528
+ },
529
+ permissions: execPerms('/scripts/if-not-true.sh')
530
+ });
531
+ const { output } = await run(shell, '/scripts/if-not-true.sh');
532
+ expect(output).toEqual(['expected']);
533
+ });
534
+ });
@@ -604,3 +604,118 @@ describe('MOTD, profile, and initial command startup chain', () => {
604
604
  });
605
605
  });
606
606
 
607
+ describe('negation operator (!)', () => {
608
+ let shell;
609
+ beforeEach(async () => { shell = await createShell(); });
610
+
611
+ it('inverts successful exit code 0 to 1 for ! true', async () => {
612
+ const { exitCode } = await run(shell, '! true');
613
+ expect(exitCode).toBe(1);
614
+ });
615
+
616
+ it('inverts failure exit code to 0 for ! false', async () => {
617
+ const { exitCode } = await run(shell, '! false');
618
+ expect(exitCode).toBe(0);
619
+ });
620
+
621
+ it('inverts non-zero exit code of missing command for ! which wllama', async () => {
622
+ const { exitCode } = await run(shell, '! which wllama');
623
+ expect(exitCode).toBe(0);
624
+ });
625
+
626
+ it('inverts exit code in conditional chains with &&', async () => {
627
+ const { output, exitCode } = await run(shell, '! false && echo "negated success"');
628
+ expect(output).toContain('negated success');
629
+ expect(exitCode).toBe(0);
630
+ });
631
+
632
+ it('inverts exit code in conditional chains with ||', async () => {
633
+ const { output, exitCode } = await run(shell, '! true || echo "fallback"');
634
+ expect(output).toContain('fallback');
635
+ expect(exitCode).toBe(0);
636
+ });
637
+
638
+ it('inverts exit code of a pipeline with leading !', async () => {
639
+ const { exitCode } = await run(shell, '! true | false');
640
+ expect(exitCode).toBe(0);
641
+ });
642
+
643
+ it('inverts exit code of a pipeline stage', async () => {
644
+ const { exitCode } = await run(shell, 'false | ! true');
645
+ expect(exitCode).toBe(1);
646
+ });
647
+ });
648
+
649
+ describe('stream redirection syntax', () => {
650
+ let shell;
651
+ beforeEach(async () => {
652
+ shell = await createShell({
653
+ filesystem: {
654
+ home: {}
655
+ }
656
+ });
657
+ });
658
+
659
+ it('redirects stderr to stdout with 2>&1', async () => {
660
+ const { output, errors } = await run(shell, 'cat /non_existent 2>&1');
661
+ expect(errors).toHaveLength(0);
662
+ expect(output.length).toBeGreaterThan(0);
663
+ expect(output[0]).toContain('No such file or directory');
664
+ });
665
+
666
+ it('pipes stderr into next stage when using 2>&1 |', async () => {
667
+ const { output } = await run(shell, 'cat /non_existent 2>&1 | cat');
668
+ expect(output.length).toBeGreaterThan(0);
669
+ expect(output[0]).toContain('No such file or directory');
670
+ });
671
+
672
+ it('discards stderr when redirected to /dev/null with 2> /dev/null', async () => {
673
+ const { output, errors, exitCode } = await run(shell, 'cat /non_existent 2> /dev/null');
674
+ expect(errors).toHaveLength(0);
675
+ expect(output).toHaveLength(0);
676
+ expect(exitCode).not.toBe(0);
677
+ });
678
+
679
+ it('strips 2>&1 from positional arguments (argv) so command does not receive it as an argument', async () => {
680
+ // which rejects with "which requires a single argument" if 2>&1 is not stripped
681
+ const { output, errors } = await run(shell, 'which non_existent 2>&1');
682
+ expect(output.join(' ')).not.toContain('which requires a single argument');
683
+ expect(errors.join(' ')).not.toContain('which requires a single argument');
684
+ });
685
+
686
+ it('redirects stderr to a file with 2>', async () => {
687
+ await run(shell, 'cat /non_existent 2> /home/err.txt');
688
+ const content = shell.context.fs.getFileByPath('/home/err.txt');
689
+ expect(content).toContain('No such file or directory');
690
+ });
691
+
692
+ it('appends stderr to a file with 2>>', async () => {
693
+ shell.context.fs.setFileByPath('/home/err.txt', 'existing', true);
694
+ await run(shell, 'cat /non_existent 2>> /home/err.txt');
695
+ const content = shell.context.fs.getFileByPath('/home/err.txt');
696
+ expect(content).toContain('existing\n');
697
+ expect(content).toContain('No such file or directory');
698
+ });
699
+
700
+ it('redirects stdout with 1> and 1>>', async () => {
701
+ await run(shell, 'echo first 1> /home/out.txt');
702
+ await run(shell, 'echo second 1>> /home/out.txt');
703
+ expect(shell.context.fs.getFileByPath('/home/out.txt')).toBe('first\nsecond');
704
+ });
705
+
706
+ it('redirects both stdout and stderr to a file with &>', async () => {
707
+ await run(shell, 'echo test &> /home/combined.txt');
708
+ expect(shell.context.fs.getFileByPath('/home/combined.txt')).toBe('test');
709
+ });
710
+
711
+ it('redirects both stdout and stderr to a file with > file 2>&1', async () => {
712
+ await run(shell, 'echo msg > /home/both.txt 2>&1');
713
+ expect(shell.context.fs.getFileByPath('/home/both.txt')).toBe('msg');
714
+ });
715
+
716
+ it('preserves quoted redirection tokens as literal arguments', async () => {
717
+ const { output } = await run(shell, 'echo "2>&1"');
718
+ expect(output).toEqual(['2>&1']);
719
+ });
720
+ });
721
+
@@ -882,5 +882,131 @@ describe('Output Typing Animation Suppression', () => {
882
882
  expect(el.children).not.toContain(input);
883
883
  expect(term._mobileInputElement).toBeNull();
884
884
  });
885
+
886
+ it('disables autocapitalize, autocorrect, autocomplete, and spellcheck on mobile input', () => {
887
+ const el = document.createElement('div');
888
+ const term = new Terminal({
889
+ el,
890
+ Shell: false,
891
+ mobileInput: 'click'
892
+ });
893
+
894
+ const input = term._mobileInputElement;
895
+ expect(input.autocapitalize).toBe('none');
896
+ expect(input.autocorrect).toBe('off');
897
+ expect(input.autocomplete).toBe('off');
898
+ expect(input.spellcheck).toBe('false');
899
+ expect(input.enterkeyhint).toBe('go');
900
+ term.destroy();
901
+ });
902
+
903
+ it('handles mobile character typing when characters are prepended due to zero caret', () => {
904
+ const el = document.createElement('div');
905
+ const term = new Terminal({
906
+ el,
907
+ Shell: false,
908
+ mobileInput: 'click'
909
+ });
910
+
911
+ const input = term._mobileInputElement;
912
+ const received = [];
913
+ term.onData((data) => received.push(data));
914
+
915
+ // Type 'h'
916
+ input.value = 'h';
917
+ input.dispatchEvent('input');
918
+
919
+ // Type 'e' (prepended at offset 0 due to zero-size input caret, value becomes 'eh')
920
+ input.value = 'eh';
921
+ input.dispatchEvent('input');
922
+
923
+ // Type 'l' (prepended at offset 0, value becomes 'leh')
924
+ input.value = 'leh';
925
+ input.dispatchEvent('input');
926
+
927
+ // Type 'p' (prepended at offset 0, value becomes 'pleh')
928
+ input.value = 'pleh';
929
+ input.dispatchEvent('input');
930
+
931
+ expect(received).toEqual(['h', 'e', 'l', 'p']);
932
+ term.destroy();
933
+ });
934
+
935
+ it('handles mobile typing via inputType insertText and e.data', () => {
936
+ const el = document.createElement('div');
937
+ const term = new Terminal({
938
+ el,
939
+ Shell: false,
940
+ mobileInput: 'click'
941
+ });
942
+
943
+ const input = term._mobileInputElement;
944
+ const received = [];
945
+ term.onData((data) => received.push(data));
946
+
947
+ input.value = 'a';
948
+ input.dispatchEvent('input', { inputType: 'insertText', data: 'a' });
949
+
950
+ input.value = 'ab';
951
+ input.dispatchEvent('input', { inputType: 'insertText', data: 'b' });
952
+
953
+ input.value = 'abc';
954
+ input.dispatchEvent('input', { inputType: 'insertText', data: 'c' });
955
+
956
+ expect(received).toEqual(['a', 'b', 'c']);
957
+ term.destroy();
958
+ });
959
+
960
+ it('handles mobile backspace and Enter', () => {
961
+ const el = document.createElement('div');
962
+ const term = new Terminal({
963
+ el,
964
+ Shell: false,
965
+ mobileInput: 'click'
966
+ });
967
+
968
+ const input = term._mobileInputElement;
969
+ const received = [];
970
+ term.onData((data) => received.push(data));
971
+
972
+ // Type 'hi'
973
+ input.value = 'hi';
974
+ input.dispatchEvent('input', { inputType: 'insertText', data: 'hi' });
975
+
976
+ // Backspace via inputType
977
+ input.value = 'h';
978
+ input.dispatchEvent('input', { inputType: 'deleteContentBackward' });
979
+
980
+ // Enter via insertLineBreak
981
+ input.dispatchEvent('input', { inputType: 'insertLineBreak' });
982
+
983
+ expect(received).toEqual(['hi', '\x7f', '\r']);
984
+ expect(input.value).toBe('');
985
+ expect(term._mobileInputBuffer).toBe('');
986
+
987
+ term.destroy();
988
+ });
989
+
990
+ it('handles Android keyup with Unidentified via diff', () => {
991
+ const el = document.createElement('div');
992
+ const term = new Terminal({
993
+ el,
994
+ Shell: false,
995
+ mobileInput: 'click'
996
+ });
997
+
998
+ const input = term._mobileInputElement;
999
+ const received = [];
1000
+ term.onData((data) => received.push(data));
1001
+
1002
+ input.value = 'x';
1003
+ input.dispatchEvent('keyup', { key: 'Unidentified' });
1004
+
1005
+ input.value = 'yx'; // prepended 'y'
1006
+ input.dispatchEvent('keyup', { key: 'Unidentified' });
1007
+
1008
+ expect(received).toEqual(['x', 'y']);
1009
+ term.destroy();
1010
+ });
885
1011
  });
886
1012
  });
package/test/wbt.test.js CHANGED
@@ -440,14 +440,28 @@ function createMockWllama() {
440
440
  return new Uint8Array([0x47, 0x47, 0x55, 0x46, 0x03, 0x00, 0x00, 0x00]);
441
441
  }
442
442
 
443
- async createChatCompletion(opts) {
444
- const messages = opts.messages || [];
443
+ createChatCompletion(opts) {
444
+ const messages = (opts && opts.messages) || [];
445
445
  const sysMsg = messages.find(m => m.role === 'system');
446
446
  const userMsgs = messages.filter(m => m.role === 'user').map(m => m.content);
447
447
  const prefix = sysMsg ? `[sys:${sysMsg.content}] ` : '';
448
448
  const gpuTag = this._loadParams.n_gpu_layers === 0 ? 'CPU' : 'WebGPU';
449
449
  const reply = `${prefix}Reply(${gpuTag},turns=${userMsgs.length}): ${userMsgs[userMsgs.length - 1] || ''}`;
450
- return {
450
+
451
+ if (opts && opts.stream) {
452
+ return (async function* () {
453
+ const parts = reply.split(' ');
454
+ for (let i = 0; i < parts.length; i++) {
455
+ const token = (i === 0 ? '' : ' ') + parts[i];
456
+ yield {
457
+ id: 'chatcmpl-wllama',
458
+ choices: [{ delta: { content: token } }]
459
+ };
460
+ }
461
+ })();
462
+ }
463
+
464
+ return Promise.resolve({
451
465
  id: 'chatcmpl-wllama',
452
466
  object: 'chat.completion',
453
467
  choices: [
@@ -457,21 +471,35 @@ function createMockWllama() {
457
471
  finish_reason: 'stop'
458
472
  }
459
473
  ]
460
- };
474
+ });
461
475
  }
462
476
 
463
- async createCompletion(opts) {
464
- return {
477
+ createCompletion(opts) {
478
+ const reply = `${opts.prompt} [completed]`;
479
+ if (opts && opts.stream) {
480
+ return (async function* () {
481
+ const parts = reply.split(' ');
482
+ for (let i = 0; i < parts.length; i++) {
483
+ const token = (i === 0 ? '' : ' ') + parts[i];
484
+ yield {
485
+ id: 'cmpl-wllama',
486
+ choices: [{ text: token }]
487
+ };
488
+ }
489
+ })();
490
+ }
491
+
492
+ return Promise.resolve({
465
493
  id: 'cmpl-wllama',
466
494
  object: 'text_completion',
467
495
  choices: [
468
496
  {
469
497
  index: 0,
470
- text: `${opts.prompt} [completed]`,
498
+ text: reply,
471
499
  finish_reason: 'stop'
472
500
  }
473
501
  ]
474
- };
502
+ });
475
503
  }
476
504
 
477
505
  async createEmbedding(opts) {
@@ -1108,6 +1136,62 @@ describe('WBT Wllama v3 WASM & WebGPU package (`wllama`, `llama`, `llama-cli`)',
1108
1136
  expect(rawRes.output.join('\n')).toContain('Explain WebGPU in one sentence. [completed]');
1109
1137
  });
1110
1138
 
1139
+ it('streams tokens in real time as they are generated and supports --no-stream', async () => {
1140
+ const streamedTokens = [];
1141
+ const rawChunks = [];
1142
+ const onToken = (token) => streamedTokens.push(token);
1143
+ const onRaw = (chunk) => rawChunks.push(chunk);
1144
+
1145
+ shell.on('token', onToken);
1146
+ shell.on('stdOutRaw', onRaw);
1147
+
1148
+ const res = await run(
1149
+ shell,
1150
+ 'wllama -m /home/tinyllama.gguf -p "Stream me"'
1151
+ );
1152
+
1153
+ shell.removeListener('token', onToken);
1154
+ shell.removeListener('stdOutRaw', onRaw);
1155
+
1156
+ expect(res.exitCode).toBe(0);
1157
+ expect(res.output.join('\n')).toContain('Reply(WebGPU,turns=1): Stream me');
1158
+ // Verified tokens were streamed incrementally
1159
+ expect(streamedTokens.length).toBeGreaterThan(1);
1160
+ expect(streamedTokens.join('')).toContain('Stream me');
1161
+ expect(rawChunks.length).toBeGreaterThan(1);
1162
+
1163
+ // Test with --no-stream flag
1164
+ const noStreamTokens = [];
1165
+ const onNoStreamToken = (token) => noStreamTokens.push(token);
1166
+ shell.on('token', onNoStreamToken);
1167
+
1168
+ const noStreamRes = await run(
1169
+ shell,
1170
+ 'wllama -m /home/tinyllama.gguf --no-stream -p "No stream"'
1171
+ );
1172
+
1173
+ shell.removeListener('token', onNoStreamToken);
1174
+
1175
+ expect(noStreamRes.exitCode).toBe(0);
1176
+ expect(noStreamRes.output.join('\n')).toContain('Reply(WebGPU,turns=1): No stream');
1177
+ expect(noStreamTokens.length).toBe(0);
1178
+
1179
+ // Test status indication (loading model, running inference)
1180
+ const statusUpdates = [];
1181
+ const onStatus = (chunk) => statusUpdates.push(chunk);
1182
+ shell.stderr.on('data', onStatus);
1183
+
1184
+ await run(
1185
+ shell,
1186
+ 'wllama -m /home/tinyllama.gguf -p "Status test"'
1187
+ );
1188
+
1189
+ shell.stderr.removeListener('data', onStatus);
1190
+ const combinedStatus = statusUpdates.join('');
1191
+ expect(combinedStatus).toMatch(/Loading model|Loaded model/);
1192
+ expect(combinedStatus).toContain('Running inference');
1193
+ });
1194
+
1111
1195
  it('supports Hugging Face model loading (--hf / hf:), embeddings (wllama embed), info, and bench', async () => {
1112
1196
  const hfRes = await run(
1113
1197
  shell,