86 lines
6 KiB
JavaScript
86 lines
6 KiB
JavaScript
// RemoteHost logic without a browser: consent, deny by default, payload re-building, snapshots, claude.send through the panel.
|
|
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node ide/tests/remote-host.test.mjs
|
|
import { register } from 'node:module';
|
|
register('data:text/javascript,' + encodeURIComponent(`
|
|
export async function resolve(s, c, n) {
|
|
if (s === './backend.js') return { url: 'data:text/javascript,export const backend = globalThis.__backend;', shortCircuit: true };
|
|
if (s === './ui.js') return { url: 'data:text/javascript,' + encodeURIComponent('const e=()=>({});export const dialog=(o)=>globalThis.__dialog(o);export const el=e;export const notify=()=>{};export const prefs={get:(k,d)=>d,set(){}};export const t=(k,v={})=>String(k).replace(/\\\\{(\\\\w+)\\\\}/g,(m,n)=>n in v?v[n]:m);'), shortCircuit: true };
|
|
return n(s, c);
|
|
}`), import.meta.url);
|
|
|
|
const calls = [];
|
|
const handlers = new Map();
|
|
globalThis.__backend = {
|
|
kind: 'native',
|
|
async invoke(name, p) { calls.push([name, p]); return name === 'terminal.open' ? { id: 7 } : { ok: true }; },
|
|
on(ev, fn) { handlers.set(ev, fn); return () => handlers.delete(ev); },
|
|
};
|
|
let allow = true;
|
|
globalThis.__dialog = (o) => { setTimeout(() => o.buttons[allow ? 1 : 0].action(), 0); return {}; };
|
|
|
|
const { RemoteHost } = await import('../ui/js/remote-host.js');
|
|
let fail = 0;
|
|
const ok = (c, m) => { if (!c) { console.error('FAIL: ' + m); fail++; } };
|
|
const sent = [];
|
|
const channel = { send: (o) => { sent.push(o); return Promise.resolve(); }, close() {} };
|
|
const typed = [];
|
|
const conv = { key: 'claude:p', items: [{ role: 'user', text: 'hi' }], input: { value: 'draft' }, rcTab: { ended: false }, async send() { typed.push(this.input.value); this.input.value = ''; } };
|
|
const app = { project: { name: 'demo', root: '/home/x/demo' }, projectKey: '/home/x/demo', claude: { convs: [conv], current: () => conv }, terminal: { terms: [{ id: 3, label: 'sh', term: { cols: 100, rows: 30 } }] } };
|
|
const host = new RemoteHost(app);
|
|
host.active = true; host.scope = 'terminal'; host.mode = 'server'; host.expires = Date.now() / 1000 + 3600;
|
|
for (const n of ['terminal.output', 'terminal.exit', 'claude.event', 'claude.exit', 'run.output']) host.cleanups.push(globalThis.__backend.on(n, (p) => host.onIdeEvent(n, p)));
|
|
const wait = (ms = 30) => new Promise((r) => setTimeout(r, ms));
|
|
const take = () => sent.splice(0);
|
|
|
|
host.onMessage('relay:1', channel, 1, { t: 'invoke', id: 1, name: 'terminal.write', payload: {} });
|
|
await wait(); ok(!take().length, 'nothing is answered before hello');
|
|
|
|
host.onMessage('relay:1', channel, 1, { t: 'hello', v: 1, name: 'Pixel <8>', want: 'all' });
|
|
await wait(); let m = take();
|
|
ok(m[0]?.t === 'welcome' && m[0].scope === 'terminal', 'welcome with the shared scope (terminal share cannot be widened to all)');
|
|
ok(m[0].panels.length === 1 && m[0].panels[0].id === 'terminal', 'panels of the scope');
|
|
ok(m.some((x) => x.t === 'snap' && x.panel === 'terminal' && x.id === 3), 'terminal snapshot');
|
|
ok(host.devices.get('relay:1').name === 'Pixel 8', 'name cleaned');
|
|
|
|
host.onMessage('relay:1', channel, 1, { t: 'invoke', id: 2, name: 'file.read', payload: { path: 'a' } });
|
|
host.onMessage('relay:1', channel, 1, { t: 'invoke', id: 3, name: 'claude.send', payload: { prompt: 'x' } });
|
|
host.onMessage('relay:1', channel, 1, { t: 'invoke', id: 4, name: 'terminal.open', payload: { cols: 99999, rows: 1, shell: '/bin/evil' } });
|
|
host.onMessage('relay:1', channel, 1, { t: 'invoke', id: 5, name: 'terminal.write', payload: { id: '7', data: 'ls\r', extra: 1 } });
|
|
await wait(); m = take();
|
|
ok(m.find((x) => x.id === 2).error === 'denied' && m.find((x) => x.id === 3).error === 'denied', 'deny by default (out of scope)');
|
|
ok(calls.some(([n, p]) => n === 'terminal.open' && p.cols === 400 && p.rows === 2 && !('shell' in p)), 'terminal.open payload re-built');
|
|
ok(calls.some(([n, p]) => n === 'terminal.write' && p.id === 7 && p.data === 'ls\r' && !('extra' in p)), 'terminal.write payload re-built');
|
|
|
|
handlers.get('terminal.output')({ id: 3, data: btoa('hello') });
|
|
handlers.get('claude.event')({ conv: 'x' });
|
|
handlers.get('run.output')({ id: 1 });
|
|
await wait(); m = take();
|
|
ok(m.filter((x) => x.t === 'event').map((x) => x.name).join() === 'terminal.output', 'only the scope\'s events are forwarded');
|
|
host.onMessage('relay:1', channel, 1, { t: 'invoke', id: 6, name: 'snap', payload: { panel: 'terminal' } });
|
|
await wait(); m = take();
|
|
ok(atob(m.find((x) => x.t === 'snap').data) === 'hello', 'ring buffer in the snapshot');
|
|
|
|
// claude scope: claude.send goes through the panel (typed into the Remote Control tab by the panel), the draft is given back
|
|
host.scope = 'all';
|
|
host.onMessage('relay:2', channel, 2, { t: 'hello', v: 1, name: 'PC', want: 'all' });
|
|
await wait(); take();
|
|
host.onMessage('relay:2', channel, 2, { t: 'invoke', id: 1, name: 'claude.send', payload: { prompt: 'do it' } });
|
|
host.onMessage('relay:2', channel, 2, { t: 'invoke', id: 2, name: 'claude.send', payload: { prompt: '/rc' } });
|
|
host.onMessage('relay:2', channel, 2, { t: 'invoke', id: 3, name: 'file.read', payload: { path: '../../etc/passwd' } });
|
|
host.onMessage('relay:2', channel, 2, { t: 'invoke', id: 4, name: 'file.write', payload: { path: 'a', content: 'x' } });
|
|
await wait(); m = take();
|
|
ok(typed[0] === 'do it' && conv.input.value === 'draft', 'claude.send through the panel, draft restored');
|
|
ok(m.find((x) => x.id === 1).result.via === 'terminal', 'reports the terminal route');
|
|
ok(m.find((x) => x.id === 2).error === 'denied' && m.find((x) => x.id === 3).error === 'denied' && m.find((x) => x.id === 4).error === 'denied', '/rc, .. and file.write refused');
|
|
|
|
// refusal
|
|
allow = false;
|
|
host.onMessage('relay:3', channel, 3, { t: 'hello', v: 1, name: 'Bad', want: 'claude' });
|
|
await wait(400); m = take();
|
|
ok(m.some((x) => x.t === 'denied' && x.reason === 'refused') && !host.devices.has('relay:3'), 'Deny answers refused');
|
|
|
|
// revoke + stop
|
|
host.revoke('relay:2'); await wait(); ok(take().some((x) => x.t === 'denied' && x.reason === 'revoked'), 'revoke');
|
|
await host.stop(); ok(!host.active && handlers.size === 0, 'stop clears the listeners');
|
|
if (fail) process.exit(1);
|
|
console.log('remote-host: ok');
|