Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

12.8 KB
Raw Blame
import assert from 'node:assert/strict';
import test from 'node:test';
import { loadBundled } from './bundle.mjs';

// The sound effects module reads its mute flag on load.
globalThis.localStorage ??= { getItem: () => null, setItem: () => {} };

const m = await loadBundled(`
  export * from './src/computerMode';
  export { ComputerModeControl, ComputerModeChip, LocalBrowserNote, OpenWindowButton, SignInLine } from './src/components/ComputerModeSwitch';
  export { renderToStaticMarkup } from 'react-dom/server.browser';
  export { createElement } from 'react';
`);

const settings = (mode, local = true, vm = true, reasons = {}, window = {}, isolates = ['browser', 'kwnt_shell']) => ({
  mode,
  available: { local, vm },
  reasons: { local: null, vm: null, ...reasons },
  window: { visible: false, available: false, reason: null, ...window },
  isolates,
  claudeVm: null,
});
const api = (state, extra = {}) => ({ state, select: () => {}, dismissError: () => {}, setWindow: () => {}, windowPending: false, ...extra });
const render = (C, state) => m.renderToStaticMarkup(m.createElement(C, { api: api(state) }));
const ready = (s) => ({ ...m.initialSwitchState, settings: s });

test('parseComputerSettings accepts the contract and rejects anything else', () => {
  const ok = m.parseComputerSettings({ mode: 'vm', available: { local: false, vm: true }, reasons: { local: 'No Chrome found', vm: null } });
  assert.deepEqual(ok, settings('vm', false, true, { local: 'No Chrome found' }));
  assert.equal(m.parseComputerSettings({ mode: 'VM', available: {} }), null);
  assert.equal(m.parseComputerSettings({ mode: 'local' }), null);
  assert.equal(m.parseComputerSettings(null), null);
  // Missing reasons and non-boolean availability are tolerated, read as false/null.
  assert.deepEqual(m.parseComputerSettings({ mode: 'local', available: { local: 1, vm: true } }), settings('local', false, true));
});

test('unavailableReason uses the server reason, with a fallback', () => {
  assert.equal(m.unavailableReason(settings('local', true, false, { vm: 'Docker is not installed' }), 'vm'), 'Docker is not installed');
  assert.match(m.unavailableReason(settings('local', true, false), 'vm'), /VM \(browser and built-in shell isolated\) isn't available/);
  assert.equal(m.unavailableReason(settings('local'), 'vm'), null);
});

test('a failed PUT reverts the control and keeps the error', () => {
  let s = m.switchReducer(m.initialSwitchState, { type: 'loaded', settings: settings('local') });
  s = m.switchReducer(s, { type: 'start', mode: 'vm' });
  assert.equal(m.shownMode(s), 'vm');
  s = m.switchReducer(s, { type: 'failed', error: 'Docker is not running' });
  assert.equal(m.shownMode(s), 'local');
  assert.equal(s.error, 'Docker is not running');
  s = m.switchReducer(s, { type: 'start', mode: 'vm' });
  assert.equal(s.error, null, 'a new attempt clears the old error');
  s = m.switchReducer(s, { type: 'saved', settings: settings('vm') });
  assert.equal(m.shownMode(s), 'vm');
  assert.equal(s.pending, null);
});

test('a refetch while saving does not override the pending choice', () => {
  let s = m.switchReducer(ready(settings('local')), { type: 'start', mode: 'vm' });
  s = m.switchReducer(s, { type: 'loaded', settings: settings('local') });
  assert.equal(m.shownMode(s), 'vm');
});

test('saveComputerMode surfaces the 400 error body and network failures', async () => {
  const realFetch = globalThis.fetch;
  try {
    globalThis.fetch = async () => new Response(JSON.stringify({ error: 'Docker is not installed' }), { status: 400 });
    assert.deepEqual(await m.saveComputerMode('vm'), { ok: false, error: 'Docker is not installed' });
    globalThis.fetch = async () => new Response('oops', { status: 500 });
    assert.deepEqual(await m.saveComputerMode('vm'), { ok: false, error: 'Switching failed (HTTP 500).' });
    globalThis.fetch = async () => { throw new TypeError('offline'); };
    assert.deepEqual(await m.saveComputerMode('vm'), { ok: false, error: 'Could not reach the server.' });
    let body;
    globalThis.fetch = async (_u, init) => { body = init.body; return new Response(JSON.stringify(settings('vm')), { status: 200 }); };
    assert.deepEqual(await m.saveComputerMode('vm'), { ok: true, settings: settings('vm') });
    assert.equal(body, '{"mode":"vm"}');
    globalThis.fetch = async () => new Response('', { status: 404 });
    assert.equal(await m.fetchComputerSettings(), null, 'older servers without the route hide the switch');
  } finally {
    globalThis.fetch = realFetch;
  }
});

test('control: both available', () => {
  const html = render(m.ComputerModeControl, ready(settings('local')));
  assert.match(html, /role="radiogroup"/);
  assert.match(html, /aria-checked="true"[^>]*>.*Local computer/);
  assert.match(html, /Uses your Chrome and your files\.</);
  assert.doesNotMatch(html, /Commands ask/);
  assert.match(html, /VM \(browser and built-in shell isolated\)/);
  assert.match(html, /The browser and built-in shell run in Docker containers, away from your files\./);
  assert.match(html, /Claude, Codex and Grok still run on this computer, with their own file and shell tools\./);
  assert.doesNotMatch(html, /VM \(isolated\)/);
  assert.doesNotMatch(html, /disabled=""/);
  assert.doesNotMatch(html, /Unavailable/);
});

test('isolates: parsed from the server, honest default for older servers', () => {
  const base = { mode: 'vm', available: { local: true, vm: true }, reasons: {} };
  assert.deepEqual(m.parseComputerSettings({ ...base, isolates: ['browser', 'kwnt_shell', 7] }).isolates, ['browser', 'kwnt_shell']);
  assert.deepEqual(m.parseComputerSettings(base).isolates, ['browser', 'kwnt_shell']);
  assert.deepEqual(m.parseComputerSettings({ ...base, isolates: 'all' }).isolates, ['browser', 'kwnt_shell']);
});

test('VM copy follows what the server says it isolates', () => {
  const now = m.vmCopy(['browser', 'kwnt_shell']);
  assert.equal(now.label, 'VM (browser and built-in shell isolated)');
  assert.match(now.note, /Claude, Codex and Grok still run on this computer/);
  // compose stack: only the browser is isolated, and the card says the built-in shell isn't there
  const browserOnly = m.vmCopy(['browser']);
  assert.equal(browserOnly.label, 'VM (browser isolated)');
  assert.equal(browserOnly.hint, 'The browser runs in a Docker container, away from your files.');
  assert.match(browserOnly.note, /built-in shell isn't available in this setup/);
  assert.doesNotMatch(`${browserOnly.label} ${browserOnly.hint}`, /built-in shell/);
  const bo = render(m.ComputerModeControl, ready(settings('vm', false, true, {}, {}, ['browser'])));
  assert.match(bo, /VM \(browser isolated\)/);
  assert.doesNotMatch(bo, /browser and built-in shell/);
  const full = m.vmCopy(['browser', 'kwnt_shell', 'cli_agents']);
  assert.equal(full.label, 'VM (isolated)');
  assert.equal(full.note, null);
  const html = render(m.ComputerModeControl, ready(settings('vm', true, true, {}, {}, ['browser', 'kwnt_shell', 'cli_agents'])));
  assert.match(html, /VM \(isolated\)/);
  assert.doesNotMatch(html, /still run on this computer/);
  assert.equal(m.modeLabel('vm'), 'VM (browser and built-in shell isolated)');
});

test('control: one unavailable is disabled and says why', () => {
  const html = render(m.ComputerModeControl, ready(settings('local', true, false, { vm: 'Docker is not installed' })));
  assert.match(html, /aria-disabled="true"[^>]*disabled=""[^>]*>.*VM \(browser and built-in shell isolated\)/);
  assert.match(html, /Unavailable: Docker is not installed/);
});

test('control: PUT failed shows the error inline on the saved mode', () => {
  const html = render(m.ComputerModeControl, { settings: settings('local'), pending: null, error: 'Docker is not running' });
  assert.match(html, /role="alert"[^>]*>.*Docker is not running/);
  assert.match(html, /aria-checked="true"[^>]*>.*Local computer/);
});

test('control and chip render nothing before the setting loads', () => {
  assert.equal(render(m.ComputerModeControl, m.initialSwitchState), '');
  assert.equal(render(m.ComputerModeChip, m.initialSwitchState), '');
});

test('chip names the current mode and offers the other one', () => {
  const vm = render(m.ComputerModeChip, ready(settings('vm')));
  assert.match(vm, />VM</);
  assert.match(vm, /Click to switch to Local computer/);
  const blocked = render(m.ComputerModeChip, ready(settings('local', true, false, { vm: 'Docker is not installed' })));
  assert.match(blocked, /disabled=""/);
  assert.match(blocked, /VM \(browser and built-in shell isolated\) is unavailable: Docker is not installed/);
});

test('local note text', () => {
  assert.match(m.renderToStaticMarkup(m.createElement(m.LocalBrowserNote)), /Your bots&#x27;? Chrome is open as a window on your computer\./);
});

test('the window flag parses, defaults to the in-app view and only counts in local mode', () => {
  const parsed = m.parseComputerSettings({
    mode: 'local', available: { local: true, vm: false }, reasons: {},
    window: { visible: true, available: false, reason: 'no display' },
  });
  assert.deepEqual(parsed.window, { visible: true, available: false, reason: 'no display' });
  assert.equal(m.showsLocalWindow(parsed), true);
  // older servers have no window field: local streams in the app
  const old = m.parseComputerSettings({ mode: 'local', available: { local: true, vm: true } });
  assert.deepEqual(old.window, { visible: false, available: false, reason: null });
  assert.equal(m.showsLocalWindow(old), false);
  assert.equal(m.showsLocalWindow({ ...parsed, mode: 'vm' }), false);
  assert.equal(m.showsLocalWindow(null), false);
});

test('open-as-window button: local in-app only, disabled with the reason when no display', () => {
  const btn = (s, extra) => m.renderToStaticMarkup(m.createElement(m.OpenWindowButton, { api: api(ready(s), extra), label: true }));
  assert.equal(btn(settings('vm', true, true, {}, { available: true })), '');
  assert.equal(btn(settings('local', true, true, {}, { visible: true, available: true })), '');
  const ok = btn(settings('local', true, true, {}, { available: true }));
  assert.match(ok, /Open as window/);
  assert.doesNotMatch(ok, /disabled=""/);
  const blocked = btn(settings('local', true, true, {}, { reason: 'no display' }));
  assert.match(blocked, /disabled=""/);
  assert.match(blocked, /Can&#x27;t open a window: no display/);
  assert.match(btn(settings('local', true, true, {}, { available: true }), { windowPending: true }), /aria-busy="true"/);
});

test('the note offers to bring the browser back, and saveLocalWindow sends the flag', async () => {
  let asked = null;
  const note = m.renderToStaticMarkup(m.createElement(m.LocalBrowserNote, { api: api(ready(settings('local')), { setWindow: (v) => (asked = v) }) }));
  assert.match(note, /Show it here instead/);
  const realFetch = globalThis.fetch;
  try {
    let call;
    globalThis.fetch = async (url, init) => { call = { url, init }; return new Response(JSON.stringify(settings('local', true, true, {}, { visible: true, available: true })), { status: 200 }); };
    const r = await m.saveLocalWindow(true);
    assert.equal(r.ok, true);
    assert.equal(r.settings.window.visible, true);
    assert.equal(call.url, '/api/settings/computer/window');
    assert.equal(call.init.method, 'PUT');
    assert.equal(call.init.body, '{"visible":true}');
    globalThis.fetch = async () => new Response(JSON.stringify({ error: 'no display' }), { status: 400 });
    assert.deepEqual(await m.saveLocalWindow(true), { ok: false, error: 'no display' });
    globalThis.fetch = async () => new Response('', { status: 502 });
    assert.deepEqual(await m.saveLocalWindow(false), { ok: false, error: 'Could not bring the browser back (HTTP 502).' });
  } finally {
    globalThis.fetch = realFetch;
  }
  assert.equal(asked, null, 'rendering alone does not switch');
});

test('a sign-in Google refuses gets one line, and on the desktop one button that opens Chrome until signed in', async () => {
  const line = (signIn) => m.renderToStaticMarkup(m.createElement(m.SignInLine, { signIn, api: api(ready(settings('local'))) }));
  assert.equal(line(null), '');
  const desktop = line({ text: 'Google only allows sign-in in a normal Chrome window.', action: 'Sign in in Chrome' });
  assert.match(desktop, /Google only allows sign-in in a normal Chrome window\./);
  assert.match(desktop, /<button[^>]*>.*Sign in in Chrome<\/button>/);
  const server = line({ text: "Google doesn't allow sign-in from a browser on a server. Sign in on this site with email instead, or connect Google in Apps &amp; accounts.", action: null });
  assert.doesNotMatch(server, /<button/);
  const realFetch = globalThis.fetch;
  try {
    let body;
    globalThis.fetch = async (_url, init) => { body = init.body; return new Response(JSON.stringify(settings('local', true, true, {}, { visible: true, available: true })), { status: 200 }); };
    assert.equal((await m.saveLocalWindow(true, true)).ok, true);
    assert.equal(body, '{"visible":true,"until_signed_in":true}');
  } finally {
    globalThis.fetch = realFetch;
  }
});