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.

9.8 KB
Raw Blame
// The sign-in screen: the one-time link up front, the token folded away under "Other setups", steps that work
// without kwnt on the PATH.
import assert from 'node:assert/strict';
import test from 'node:test';
import { loadBundled } from './bundle.mjs';

const m = await loadBundled(`
  import React from 'react';
  import { renderToStaticMarkup } from 'react-dom/server.browser';
  import { HINT_WRONG_TOKEN, OWN_TOKEN_HINT, OtherSetups, TokenForm, WRONG_TOKEN, isLoopback, selectForRetry, tokenFromPaste, tunnelCommand, wrongTokenError } from './src/components/AuthGate';
  import { LINK_SPENT, signinHintOf } from './src/auth';
  export { HINT_WRONG_TOKEN, LINK_SPENT, OWN_TOKEN_HINT, WRONG_TOKEN, isLoopback, selectForRetry, signinHintOf, tokenFromPaste, tunnelCommand, wrongTokenError };
  export const form = (notice = null, extra = {}) => renderToStaticMarkup(React.createElement(TokenForm, { notice, onSignedIn: () => {}, ...extra }));
  export const other = (loc, docker = false) => renderToStaticMarkup(React.createElement(OtherSetups, { loc, docker }));
`);

const text = (html) => html.replace(/<[^>]+>/g, ' ').replace(/&#x27;|&#39;/g, "'").replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/\s+/g, ' ');
const http = (hostname, port = '8799') => ({ protocol: 'http:', hostname, port });

test('the default view: the one-time link and how to make one, the token box folded into "Other setups"', () => {
  const html = m.form();
  const t = text(html);
  assert.match(t, /^ ?Sign in /);
  assert.match(t, /Open the one-time sign-in link kwant bots printed when it started, or scan its QR code with your phone/);
  assert.match(t, /Used or lost it\? Make a new one on the machine running kwant bots: kwnt signin-link/);
  assert.match(html, /<details class="[^"]*">/, 'closed by default');
  // The token stays as the fallback, inside the fold, after the link
  const fold = html.slice(html.indexOf('<details'));
  assert.match(fold, /Paste a sign-in link or the access token/);
  assert.match(fold, /id="kwnt-token"/);
  assert.match(text(fold), /cat ~\/\.kwnt\/api-token/);
  assert.ok(t.indexOf('kwnt signin-link') < t.indexOf('Other setups'));
  assert.match(t, /Stays signed in for a year\. Anyone with a sign-in link or the token controls this server\./);
  assert.ok(!/Where does kwant bots run|On a server|type="radio"/.test(html));
  assert.ok(!t.includes(m.OWN_TOKEN_HINT.slice(0, 30)), 'the own-token hint only shows with a wrong token');
});

test('a used or expired link: the notice says so and the other ways to sign in are still there', () => {
  const t = text(m.form(m.LINK_SPENT));
  assert.match(t, /This sign-in link was already used or has expired\. Each link works once, for 15 minutes\. Sign in another way below\./);
  assert.match(t, /kwnt signin-link/);
  assert.match(t, /Other setups/);
});

test('other setups: Docker, systemd and a source build, so it works without kwnt on the PATH', () => {
  const t = text(m.other(http('vps.example.com')));
  assert.match(t, /docker compose exec kwnt kwnt token --raw/);
  assert.match(t, /sudo cat \/var\/lib\/kwnt\/api-token/);
  assert.match(t, /\.\/target\/release\/kwnt token --raw/);
});

test('on loopback (Docker) the Docker commands lead: the link up front, the token first in other setups', () => {
  const t = text(m.other(http('127.0.0.1'), true));
  assert.ok(t.indexOf('docker compose exec kwnt kwnt token --raw') < t.indexOf('cat ~/.kwnt/api-token'));
  const t2 = text(m.other(http('vps.example.com')));
  assert.ok(t2.indexOf('cat ~/.kwnt/api-token') < t2.indexOf('docker compose'));
});

test('SSH tunnel: on plain http at a network address with a port, using that port', () => {
  const t = text(m.other(http('vps.example.com', '9000')));
  assert.match(t, /ssh -N -L 9000:127\.0\.0\.1:9000 user@vps\.example\.com/);
  assert.match(t, /http:\/\/127\.0\.0\.1:9000/);
  assert.equal(m.tunnelCommand(http('2001:db8::1')).command, 'ssh -N -L 8799:127.0.0.1:8799 user@[2001:db8::1]');
});

test('SSH tunnel: left out behind https, without a port, and on loopback', () => {
  for (const loc of [
    { protocol: 'https:', hostname: 'kwant.jumpin.games', port: '' },
    { protocol: 'https:', hostname: 'vps.example.com', port: '8443' },
    http('vps.example.com', ''),
    http('127.0.0.1'),
    http('localhost'),
  ]) {
    assert.equal(m.tunnelCommand(loc), null, JSON.stringify(loc));
    assert.ok(!text(m.other(loc)).includes('ssh -N'), JSON.stringify(loc));
  }
});

test('loopback hosts', () => {
  for (const h of ['127.0.0.1', 'localhost', '[::1]', '::1']) assert.ok(m.isLoopback(h), h);
  for (const h of ['vps.example.com', '10.0.0.5', 'mac.local']) assert.ok(!m.isLoopback(h), h);
});

test('pasting the whole sign-in link works as well as the token', () => {
  assert.equal(m.tokenFromPaste('  abc123def456abc123  '), 'abc123def456abc123');
  assert.equal(m.tokenFromPaste('http://1.2.3.4:8799/#kwnt_token=abc123def456abc123'), 'abc123def456abc123');
  assert.equal(m.tokenFromPaste('http://<this-host>:8799/#kwnt_token=tok'), 'tok');
});

test('a token from another computer: the error says where it has to come from', () => {
  assert.match(m.WRONG_TOKEN, /machine running kwant bots, as the user running it/);
  assert.match(m.OWN_TOKEN_HINT, /one copied from another computer won't work here/);
});

test('after a wrong token the box is focused and selected, so the next paste replaces it', () => {
  const calls = [];
  m.selectForRetry({ focus: () => calls.push('focus'), select: () => calls.push('select') });
  assert.deepEqual(calls, ['focus', 'select']);
  assert.doesNotThrow(() => m.selectForRetry(null));
});

// 24f: an app store (Umbrel, CasaOS, Coolify, Render) sets the token and says where it shows it.
const UMBREL = 'Use the password Umbrel shows on kwnt\'s app page. <b>not html</b>';

test('a store hint leads: its words, then the token box open, before the device code and the link', () => {
  const html = m.form(null, { signinHint: UMBREL, deviceLogin: true });
  const t = text(html);
  assert.match(html, /data-testid="kwnt-signin-hint"/);
  assert.match(t, /Use the password Umbrel shows on kwnt's app page\. <b>not html<\/b>/, 'shown as text, never as markup');
  assert.ok(!html.includes('<b>not html</b>'));
  const fold = html.indexOf('<details');
  const box = html.indexOf('id="kwnt-token"');
  assert.ok(box > 0 && box < fold, 'the token box is outside the fold');
  assert.equal(html.split('id="kwnt-token"').length, 2, 'one token box');
  assert.ok(html.indexOf('kwnt-signin-hint') < box);
  assert.ok(t.indexOf('Umbrel shows') < t.indexOf('kwnt signin-link'));
  // The fold still has the shell ways in, for people who have one
  assert.match(text(html.slice(fold)), /cat ~\/\.kwnt\/api-token|docker compose exec kwnt kwnt token --raw/);
});

test('with a hint, the device button and the link command are not above Other setups', () => {
  for (const deviceLogin of [true, false]) {
    const html = m.form(null, { signinHint: UMBREL, deviceLogin });
    const fold = html.indexOf('<details');
    const above = text(html.slice(0, fold));
    const inside = text(html.slice(fold));
    assert.ok(!/Sign in from another device|signin-link|one-time sign-in link/.test(above), `nothing above the fold (deviceLogin ${deviceLogin}): ${above}`);
    // In the fold: the device code first, then the link starting with "Open", then the shell token commands
    assert.match(inside, /Open the one-time sign-in link/);
    assert.ok(!/Or open/.test(inside));
    assert.match(inside, /kwnt signin-link/);
    assert.ok(inside.indexOf('one-time sign-in link') < inside.indexOf('Get the access token'));
    if (deviceLogin) assert.ok(inside.indexOf('Sign in from another device') < inside.indexOf('one-time sign-in link'));
    else assert.ok(!inside.includes('Sign in from another device'));
    // hint -> token box -> Other setups -> footer
    const order = ['kwnt-signin-hint', 'id="kwnt-token"', '<details', 'Stays signed in for a year'].map((k) => html.indexOf(k));
    assert.ok(order.every((i, n) => i >= 0 && (n === 0 || i > order[n - 1])), String(order));
  }
});

test('with a hint, a wrong token points back to the hint; without one the error is unchanged', () => {
  assert.equal(m.HINT_WRONG_TOKEN, "That isn't this install's token. Copy it again from the place named above.");
  assert.equal(m.wrongTokenError(UMBREL), m.HINT_WRONG_TOKEN);
  assert.equal(m.wrongTokenError(null), m.WRONG_TOKEN);
});

test('the token box and Continue are 44 px tall on a phone, unchanged from sm up', () => {
  const html = m.form(null, { signinHint: UMBREL });
  assert.match(html, /<input id="kwnt-token"[^>]*class="[^"]*min-h-\[44px\] sm:min-h-0/);
  assert.match(html, /<button type="submit"[^>]*class="[^"]*min-h-\[44px\] sm:min-h-0/);
});

test('without a hint the screen is unchanged: the device code and the link lead, "Or open" after the button', () => {
  const t = text(m.form(null, { deviceLogin: true }));
  assert.ok(t.indexOf('Sign in from another device') < t.indexOf('Or open the one-time sign-in link'));
  assert.ok(t.indexOf('kwnt signin-link') < t.indexOf('Other setups'));
});

test('without a hint the screen is unchanged: the box stays folded', () => {
  for (const hint of [undefined, null, '']) {
    const html = m.form(null, { signinHint: hint });
    assert.ok(!html.includes('kwnt-signin-hint'), String(hint));
    assert.ok(html.indexOf('id="kwnt-token"') > html.indexOf('<details'), String(hint));
  }
});

test('the hint from the status answer: text only, trimmed, null when missing or odd', () => {
  assert.equal(m.signinHintOf({ signin_hint: '  Copy KWNT_API_TOKEN from Render. ' }), 'Copy KWNT_API_TOKEN from Render.');
  assert.equal(m.signinHintOf({ signin_hint: null }), null);
  assert.equal(m.signinHintOf({}), null, 'a server without a hint leaves the field out');
  assert.equal(m.signinHintOf({}), null);
  assert.equal(m.signinHintOf({ signin_hint: '   ' }), null);
  assert.equal(m.signinHintOf({ signin_hint: 42 }), null);
});