import assert from 'node:assert/strict'; import { spawnSync } from 'node:child_process'; import { mkdtempSync, mkdirSync, writeFileSync, existsSync, rmSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { resolve, join } from 'node:path'; import { Terminal } from './terminal.mjs'; const shell = resolve(process.argv[2] || 'rc2'); const directory = mkdtempSync(join(tmpdir(), 'rc2-bind-context-')); const quote = text => `'${text.replaceAll("'", "''")}'`; const run = script => spawnSync(shell, ['-c', script], { encoding: 'utf8', timeout: 10000 }); const terminal = new Terminal(shell); try { const records = run("bind -read0 records <{printf 'one\\000two words\\000three\\nfour\\000\\000'}; echo $#records; for (record in $records) printf '[%s]\\n' $record"); assert.equal(records.status, 0, records.stderr); assert.equal(records.stdout, '4\n[one]\n[two words]\n[three\nfour]\n[]\n'); const malformed = run("records=kept; bind -read0 records <{printf broken}; echo $records"); assert.equal(malformed.stdout, 'kept\n'); assert.match(malformed.stderr, /NUL/); const missing = run(`records=kept; bind -read0 records ${quote(join(directory, 'missing'))}; echo $status:$records`); assert.equal(missing.stdout, '2:kept\n'); const packet = join(directory, 'records'); writeFileSync(packet, 'one\0two\0'); assert.equal(run(`bind -read0 records <${quote(packet)}; echo $records`).stdout, 'one two\n'); assert.equal(run('records=kept; bind -read0 records; echo $status:$#records').stdout, '1:0\n'); mkdirSync(join(directory, 'nested')); const history = join(directory, 'history'); const multiline = 'fn multiline {\necho body\n}'; writeFileSync(history, `echo older\n${multiline}\necho older\n`, { mode: 0o600 }); const dumped = run(`bind -history0 ${quote(history)}`); assert.equal(dumped.status, 0, dumped.stderr); assert.equal(dumped.stdout, `echo older\0${multiline}\0`); await terminal.expect('% '); await terminal.command(`cd ${quote(directory)}`); await terminal.command("fn picker { echo CONTEXT:$bind_token:$bind_prefix:$bind_root:$bind_query; bind_result=('one file' 'two;*') }; bind ctrl-t token picker"); terminal.send('echo nee tail\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x14'); await terminal.expect('CONTEXT:nee::.:' + 'nee\r\n'); await terminal.expect("% echo 'one file' 'two;*' "); terminal.send('\r'); assert.match(await terminal.expect('\r\n% '), /\r\none file two;\* tail\r\n/); terminal.send('echo --output=nested/fi\x14'); await terminal.expect('CONTEXT:nested/fi:--output=:nested:fi\r\n'); await terminal.expect("% echo '--output=one file' '--output=two;*' "); terminal.send('\r'); assert.match(await terminal.expect('\r\n% '), /\r\n--output=one file --output=two;\*\r\n/); await terminal.command("fn clear_token { bind_result='' }; bind alt-c token clear_token"); terminal.send('echo before remove after\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1b[D\x1bc'); await terminal.expect('% echo before '); terminal.send('kept\r'); assert.match(await terminal.expect('\r\n% '), /\r\nbefore kept after\r\n/); await terminal.command("fn reposition { bind_result='echo ac'; bind_cursor=6 }; bind ctrl-r replace reposition"); terminal.send('\x12'); await terminal.expect('% echo ac'); terminal.send('b\r'); assert.match(await terminal.expect('\r\n% '), /\r\nabc\r\n/); await terminal.command("fn invalid_cursor { bind_result=wrong; bind_cursor=999 }; bind ctrl-r replace invalid_cursor"); terminal.send('echo ac\x1b[D\x12'); await terminal.expect('bind: invalid cursor'); await terminal.expect('% echo ac'); terminal.send('b\r'); assert.match(await terminal.expect('\r\n% '), /\r\nabc\r\n/); await terminal.command("fn unicode_cursor { bind_result='echo é'; bind_cursor=6 }; bind ctrl-r replace unicode_cursor"); terminal.send('echo ac\x1b[D\x12'); await terminal.expect('bind: invalid cursor'); await terminal.expect('% echo ac'); terminal.send('b\r'); assert.match(await terminal.expect('\r\n% '), /\r\nabc\r\n/); await terminal.command("fn collect_picker { for (i in `{seq 1 5000}) { scratch=$i }; bind_result=$bind_token }; bind ctrl-t token collect_picker"); terminal.send('echo --output=kept\x14'); await terminal.expect("% echo '--output=kept' "); terminal.send('\r'); assert.match(await terminal.expect('\r\n% '), /\r\n--output=kept\r\n/); assert.match(await terminal.command('echo $#bind_token $#bind_prefix $#bind_root $#bind_query $#bind_line $#bind_cursor'), /\r\n0 0 0 0 0 0\r\n/); const hooks = join(directory, 'hooks.rc'); writeFileSync(hooks, "fn draft { bind_result='echo first\necho replace\necho last'; bind_cursor=16 }\nfn history_pick { echo LINE:$bind_line; bind_result=('echo chosen' 'echo another') }\nbind ctrl-f replace draft\nbind ctrl-r line history_pick\n"); await terminal.command(`. ${quote(hooks)}`); terminal.send('\x06'); await terminal.expect('% echo first'); terminal.send('\x12'); await terminal.expect('LINE:echo replace\r\n'); await terminal.expect('% echo first'); terminal.send('\r'); assert.match(await terminal.expect('\r\n% '), /\r\nfirst\r\nchosen\r\nanother\r\nlast\r\n/); await terminal.command('bind ctrl-t token picker'); const marker = join(directory, 'unsafe'); terminal.send(`echo \`{echo bad >${quote(marker)}}\x14`); await terminal.expect('\x07'); assert.equal(existsSync(marker), false); terminal.send('\x03'); await terminal.expect('^C\r\n% '); } finally { await terminal.close(); rmSync(directory, { recursive: true, force: true }); } console.log('Binding context tests passed');