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.

Tests: the last three load flakes wait for the work, not the clock (22e) (#717)

Oleksandr Ivanchenko1d330d275829
diff --git a/frontend/tests/devAutodetect.test.mjs b/frontend/tests/devAutodetect.test.mjs
index 3d800655..8c577c11 100644
--- a/frontend/tests/devAutodetect.test.mjs
+++ b/frontend/tests/devAutodetect.test.mjs
@@ -5,6 +5,7 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import { JSDOM } from 'jsdom';
import { loadBundled } from './bundle.mjs';
+import { holdTimers } from './heldTimers.mjs';
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost/s/spc_demo', pretendToBeVisual: true });
const g = globalThis;
@@ -431,23 +432,32 @@ test('F3 DOM: the toast goes after its time, paused while focus is inside; on a
document.body.appendChild(form);
Object.defineProperty(window, 'innerHeight', { value: 844, configurable: true });
let closed = 0;
- const st = m.parseDevMode(server.state);
- const host = document.createElement('div');
- document.body.appendChild(host);
- const root = createRoot(host);
- root.render(h(m.DevOnToast, { state: st, phone: true, onOpenDesk: noop, onTypeRepo: noop, onUndo: async () => {}, onClose: () => closed++, timeout: 40 }));
- await settle();
- const wrap = document.querySelector('[data-dev-on-toast]');
- assert.equal(wrap.style.bottom, '152px', '844 − 700 + 8: above the chat box, never over Send');
- const undo = wrap.querySelector('[data-dev-on-toast-undo]');
- undo.focus();
- await new Promise((r) => setTimeout(r, 120));
- assert.equal(closed, 0, 'held while focus is inside');
- undo.blur();
- await new Promise((r) => setTimeout(r, 120));
- assert.equal(closed, 1, 'closes once its time is up');
- root.unmount();
- form.remove();
+ // The toast's 40 ms timer is held and fired by the test, so a slow settle() can't close it before focus
+ // is inside (22e). Its time being up is clock.run().
+ const clock = holdTimers((ms) => ms === 40);
+ try {
+ const st = m.parseDevMode(server.state);
+ const host = document.createElement('div');
+ document.body.appendChild(host);
+ const root = createRoot(host);
+ root.render(h(m.DevOnToast, { state: st, phone: true, onOpenDesk: noop, onTypeRepo: noop, onUndo: async () => {}, onClose: () => closed++, timeout: 40 }));
+ await settle();
+ const wrap = document.querySelector('[data-dev-on-toast]');
+ assert.equal(wrap.style.bottom, '152px', '844 − 700 + 8: above the chat box, never over Send');
+ const undo = wrap.querySelector('[data-dev-on-toast-undo]');
+ undo.focus();
+ await settle();
+ clock.run();
+ assert.equal(closed, 0, 'held while focus is inside');
+ undo.blur();
+ await settle();
+ clock.run();
+ assert.equal(closed, 1, 'closes once its time is up');
+ root.unmount();
+ } finally {
+ clock.restore();
+ form.remove();
+ }
});
test('F8 DOM: the off note’s Turn back on turns Dev mode on in one press and closes the note', async () => {
@@ -786,19 +796,28 @@ test('K1 DOM: Tab from the switch goes straight to the toast; Shift+Tab goes bac
test('K1 DOM: the toast stays while keyboard focus or the pointer is on the switch, and goes after its time once they leave', async () => {
let closed = 0;
- const { root, sw } = await withSwitch({ onClose: () => closed++, timeout: 40 });
- sw.focus();
- sw.dispatchEvent(new window.FocusEvent('focus'));
- await new Promise((r) => setTimeout(r, 120));
- assert.equal(closed, 0, 'held while focus is on the switch');
- sw.blur();
- sw.dispatchEvent(new window.Event('pointerenter'));
- await new Promise((r) => setTimeout(r, 120));
- assert.equal(closed, 0, 'held while the pointer is over the switch');
- sw.dispatchEvent(new window.Event('pointerleave'));
- await new Promise((r) => setTimeout(r, 120));
- assert.equal(closed, 1);
- root.unmount();
+ // Same as F3: the 40 ms timer is held, so withSwitch()'s settle() can't outlast it under load (22e).
+ const clock = holdTimers((ms) => ms === 40);
+ try {
+ const { root, sw } = await withSwitch({ onClose: () => closed++, timeout: 40 });
+ sw.focus();
+ sw.dispatchEvent(new window.FocusEvent('focus'));
+ await settle();
+ clock.run();
+ assert.equal(closed, 0, 'held while focus is on the switch');
+ sw.blur();
+ sw.dispatchEvent(new window.Event('pointerenter'));
+ await settle();
+ clock.run();
+ assert.equal(closed, 0, 'held while the pointer is over the switch');
+ sw.dispatchEvent(new window.Event('pointerleave'));
+ await settle();
+ clock.run();
+ assert.equal(closed, 1);
+ root.unmount();
+ } finally {
+ clock.restore();
+ }
});
test('K1 DOM: Dismiss from the keyboard puts focus back on the switch', async () => {
diff --git a/frontend/tests/dragDropPane.test.mjs b/frontend/tests/dragDropPane.test.mjs
index 85ffd4a7..6da215d2 100644
--- a/frontend/tests/dragDropPane.test.mjs
+++ b/frontend/tests/dragDropPane.test.mjs
@@ -7,6 +7,7 @@ import assert from 'node:assert/strict';
import test from 'node:test';
import { JSDOM } from 'jsdom';
import { loadBundled } from './bundle.mjs';
+import { holdTimers } from './heldTimers.mjs';
import { readFileSync } from 'node:fs';
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost/', pretendToBeVisual: true });
@@ -162,9 +163,17 @@ test('a file drag over the header or the message list lights up the chat; off th
await fire(side, 'dragenter', two());
await fire(list, 'dragleave', two());
assert.equal(phase(v.host), 'armed');
- // Out of the window: the layer fades and goes.
- await fire(side, 'dragleave', two(), OUT);
- assert.equal(phase(v.host), 'leaving');
+ // Out of the window: the layer fades and goes. The fade's timer is held until the test has seen it,
+ // so a slow tick() can't end it first (22e).
+ const fade = holdTimers((ms) => ms === 100 || ms === 120);
+ try {
+ await fire(side, 'dragleave', two(), OUT);
+ assert.equal(phase(v.host), 'leaving');
+ assert.ok(fade.pending().length > 0 && fade.pending().every((ms) => ms < 150), 'the fade is timed to end by 150 ms');
+ } finally {
+ fade.run();
+ fade.restore();
+ }
await wait(150);
assert.equal(layer(v.host), null);
} finally {
@@ -182,7 +191,14 @@ test('a drop on the message list lands in the tray, the cursor goes to the field
const files = [mkFile('one.png'), mkFile('two.pdf', 'application/pdf'), mkFile('three.md', 'text/markdown')];
await fire(list, 'dragenter', dataTransfer(files));
assert.equal(v.host.querySelector('[data-drop-summary]')?.textContent, '3 files · 1 image, 1 PDF, 1 text file');
- const drop = await fire(layer(v.host), 'drop', dataTransfer(files));
+ // The landing's 160 ms timer is held until the test has seen the landing phase (22e).
+ const landing = holdTimers((ms) => ms === 160);
+ let drop;
+ try {
+ drop = await fire(layer(v.host), 'drop', dataTransfer(files));
+ } finally {
+ landing.restore();
+ }
assert.equal(drop.defaultPrevented, true, 'the browser does not open the file');
assert.equal(chips(v.host), 3, 'three files in the tray');
assert.equal(sent.length, 3, 'all three upload as with the paperclip');
@@ -192,6 +208,8 @@ test('a drop on the message list lands in the tray, the cursor goes to the field
assert.equal(v.host.querySelectorAll('li[data-fresh]').length, 3, 'the new chips come in tinted');
const delays = [...v.host.querySelectorAll('li[data-fresh]')].map((li) => li.style.getPropertyValue('--kw-chip-delay'));
assert.deepEqual(delays, ['40ms', '70ms', '100ms']);
+ assert.deepEqual(landing.pending(), [160], 'the landing is timed to end by 160 ms');
+ landing.run();
await wait(200);
assert.equal(layer(v.host), null, 'gone by 160 ms');
await wait(700);
diff --git a/frontend/tests/heldTimers.mjs b/frontend/tests/heldTimers.mjs
new file mode 100644
index 00000000..11ba190e
--- /dev/null
+++ b/frontend/tests/heldTimers.mjs
@@ -0,0 +1,42 @@
+// Holds the product timers a test is about, so the test fires them when it is ready instead of racing
+// wall-clock time (22e). `holdTimers((ms) => ms === 40)` swaps the global setTimeout/clearTimeout: a
+// timer whose delay matches is kept, not scheduled; every other timer (React's, the test's own waits)
+// runs as normal. `pending()` lists the held delays, `run()` fires the held timers once, and
+// `restore()` puts the real functions back (call it in a `finally`).
+export const holdTimers = (match) => {
+ const realSet = globalThis.setTimeout;
+ const realClear = globalThis.clearTimeout;
+ const held = new Map();
+ let next = 0;
+ globalThis.setTimeout = (fn, ms = 0, ...args) => {
+ if (!match(ms)) return realSet(fn, ms, ...args);
+ const id = { held: (next += 1) };
+ held.set(id, { fn, ms, args });
+ return id;
+ };
+ globalThis.clearTimeout = (id) => {
+ if (id && typeof id === 'object' && 'held' in id) held.delete(id);
+ else realClear(id);
+ };
+ return {
+ pending: () => [...held.values()].map((t) => t.ms),
+ run: () => {
+ const due = [...held.values()];
+ held.clear();
+ for (const t of due) t.fn(...t.args);
+ return due.length;
+ },
+ restore: () => {
+ globalThis.setTimeout = realSet;
+ globalThis.clearTimeout = realClear;
+ },
+ };
+};
+
+// Waits until `ok()` is true, yielding to timers and React between checks; gives up after `ms` of
+// wall time so the test's own assertion reports what went wrong. It reads performance.now(), because
+// some files freeze Date.now (ideaJar does).
+export const until = async (ok, ms = 3000) => {
+ const end = performance.now() + ms;
+ while (!ok() && performance.now() < end) await new Promise((r) => setTimeout(r, 5));
+};
diff --git a/frontend/tests/ideaJar.test.mjs b/frontend/tests/ideaJar.test.mjs
index 4bda5897..56573340 100644
--- a/frontend/tests/ideaJar.test.mjs
+++ b/frontend/tests/ideaJar.test.mjs
@@ -7,6 +7,7 @@ import test, { afterEach } from 'node:test';
import { readFileSync } from 'node:fs';
import { JSDOM } from 'jsdom';
import { loadBundled } from './bundle.mjs';
+import { until } from './heldTimers.mjs';
const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost/', pretendToBeVisual: true });
const g = globalThis;
@@ -258,7 +259,8 @@ test('Look now: Asking… while it waits, then the slip moves to Being peeked at
assert.equal(asking.getAttribute('aria-disabled'), 'true');
assert.equal(asking.disabled, false, 'busy stays focusable (M3)');
d.resolve();
- await settle();
+ // The 202 → render chain is real work; wait for it, not a fixed tick count (22e).
+ await until(() => sectionOfCard(host, ID.fresh) === 'peeking');
assert.equal(sectionOfCard(host, ID.fresh), 'peeking');
assert.match(text(card(host, ID.fresh).querySelector('[data-peeking-text]')), /^Pixel is peeking/, 'kwant_name from the 202 names the bot');
});
@@ -488,6 +490,8 @@ test('dropping an idea: POST, then it shows in the jar and the live region says
const input = host.querySelector('[data-quick-add] input');
await typeInto(input, ' A faster way to find old chats ');
await submit(host.querySelector('[data-quick-add]'));
+ // The POST → render chain is real work; wait for the card, not a fixed tick count (22e).
+ await until(() => card(host, 'new-1'));
assert.deepEqual(calls.find((c) => c.method === 'POST').body, { space_id: 'spc_default', text: 'A faster way to find old chats' });
assert.ok(card(host, 'new-1'));
assert.equal(input.value, '');