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.

31.5 KB
Raw Blame
// 0.9.7 prepush-grant (Pixel): the card after retry/override, one Needs-you item per branch, superseded.
process.env.TZ = 'UTC';
// 18d: a time from another day says its day ("on Nov 10 at 9:40 AM"). These fixtures are read late on their own
// day, so the copy below stays time only; the clock still ticks. The other-day copy has its own test.
const realNow = Date.now.bind(Date);
const fixtureShift = realNow() - Date.parse('2026-11-09T23:00:00Z');
Date.now = () => realNow() - fixtureShift;

import assert from 'node:assert/strict';
import test, { afterEach } from 'node:test';
import { JSDOM } from 'jsdom';
import { loadBundled } from './bundle.mjs';

const dom = new JSDOM('<!doctype html><html><body></body></html>', { url: 'http://localhost/', pretendToBeVisual: true });
const g = globalThis;
for (const k of ['window', 'document', 'navigator', 'HTMLElement', 'Element', 'Node', 'Event', 'MouseEvent', 'KeyboardEvent', 'CustomEvent', 'FocusEvent', 'requestAnimationFrame', 'cancelAnimationFrame', 'getComputedStyle', 'localStorage', 'sessionStorage']) {
  if (!(k in dom.window)) continue;
  Object.defineProperty(g, k, { value: dom.window[k], configurable: true, writable: true });
}
g.window.matchMedia ??= () => ({ matches: false, addEventListener() {}, removeEventListener() {} });
g.IS_REACT_ACT_ENVIRONMENT = false;

const m = await loadBundled(`
  export * from './src/dev/prepush';
  export * from './src/dev/freshEyes';
  export * as fx from './src/dev/prepushFixtures';
  export { parseReview } from './src/dev/types';
  export * as dfx from './src/dev/fixtures';
  export { PrepushCard } from './src/components/dev/PrepushCard';
  export { PrepushNeedsYou, PrepushThread, threadCards, usePrepushCards } from './src/components/dev/PrepushThread';
  export { FreshEyesChip } from './src/components/dev/FreshEyesChip';
  export { DevModeError } from './src/devMode';
  export { ReviewCard, semgrepTile } from './src/components/dev/ReviewCard';
  export { renderToStaticMarkup } from 'react-dom/server.browser';
  export { createRoot } from 'react-dom/client';
  export { createElement } from 'react';
`);
const { fx } = m;
const h = m.createElement;
const html = (C, props) => m.renderToStaticMarkup(h(C, props));
const card = (raw) => {
  const c = m.parsePrepush(raw);
  assert.ok(c, 'fixture parses');
  return c;
};

const mounted = new Set();
afterEach(() => {
  for (const u of mounted) u();
  document.body.replaceChildren();
});
const settle = async () => {
  for (let i = 0; i < 20; i++) await new Promise((r) => setTimeout(r, 0));
};
const mount = async (C, props) => {
  const host = document.createElement('div');
  document.body.appendChild(host);
  const root = m.createRoot(host);
  root.render(h(C, props));
  await settle();
  const u = () => {
    if (!mounted.delete(u)) return;
    root.unmount();
    host.remove();
  };
  mounted.add(u);
  return { host, rerender: async (p) => { root.render(h(C, p)); await settle(); } };
};
const text = (el) => el.textContent.replace(/[^\S\u00a0]+/g, ' ').trim();
const button = (host, t) => [...host.querySelectorAll('button')].find((b) => text(b).startsWith(t));
const click = async (el) => {
  el.dispatchEvent(new window.MouseEvent('click', { bubbles: true }));
  await settle();
};
const type = async (el, value) => {
  const set = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, 'value').set;
  set.call(el, value);
  el.dispatchEvent(new window.Event('input', { bubbles: true }));
  await settle();
};

// ---------------------------------------------------------------------------------------------------
// 0.9.7 prepush-grant (autonomy/prepush-retry/brief.md, Frontend): the press is the approval.

const HM = (ms) => new Date(ms).toLocaleTimeString(undefined, { timeStyle: 'short' }).replace(/[\u202f ](AM|PM)/, '\u00a0$1');
const all = (...ks) => ks.map((k) => card(fx[k]));

test('grant and superseded parse; pushed is a known status now; a junk grant is dropped', () => {
  const r = card(fx.PP_RETRIED_GRANT);
  assert.deepEqual(r.grant, { kind: 'retry', at: fx.PP_RETRIED_GRANT.grant.at, expires_at: fx.PP_RETRIED_GRANT.grant.expires_at, used_at: null, card_id: 'pp_rg', next_card_id: null });
  assert.equal(card({ ...fx.PP_RETRIED_GRANT, grant: { ...fx.PP_RETRIED_GRANT.grant, next_card_id: 'pp_next' } }).grant.next_card_id, 'pp_next');
  assert.equal(card({ ...fx.PP_RETRIED_GRANT, grant: { kind: 'always', at: 1 } }).grant, null, 'only retry or override');
  assert.equal(card({ ...fx.PP_RETRIED_GRANT, grant: null }).grant, null);
  assert.equal(card(fx.PP_CLEAR).grant, null, 'old servers send no grant');
  assert.equal(card(fx.PP_CLEAR).folder, null);
  assert.equal(card({ ...fx.PP_CLEAR, folder: 'kwant' }).folder, 'kwant');
  const s = card(fx.PP_SUPERSEDED);
  assert.equal(s.status, 'superseded');
  assert.equal(m.phaseOf(s), 'SS');
  assert.equal(card({ ...fx.PP_SUPERSEDED, needs_you: true }).needs_you, false, 'a superseded card never needs you');
  assert.equal(m.canRetry(s), false);
  assert.equal(m.statusChip(s), null);
  assert.equal(s.superseded_by, null);
  assert.equal(card({ ...fx.PP_SUPERSEDED, superseded_by: 'pp_new' }).superseded_by, 'pp_new');
  assert.equal(card({ ...fx.PP_BLOCKED, superseded_by: 'pp_new' }).superseded_by, null, 'only a superseded card names its replacement');
  const p = card(fx.PP_PUSHED);
  assert.equal(p.status, 'pushed');
  assert.equal(m.phaseOf(p), 'P6');
  assert.equal(m.statusChip(p).text, 'Pushed');
});

test('superseded renders folded: one grey line, no checks, no buttons', () => {
  const out = html(m.PrepushCard, { card: card(fx.PP_SUPERSEDED), bot: 'Pixel', onRetry: async () => {}, onOverride: async () => {} });
  assert.match(out, /data-prepush-phase="SS"/);
  assert.match(out, /Replaced by a newer push of <code[^>]*>pixel\/inbox-empty-state<\/code>/);
  assert.doesNotMatch(out, /<button/);
  assert.doesNotMatch(out, /data-check=/);
  const linked = html(m.PrepushCard, { card: card({ ...fx.PP_SUPERSEDED, superseded_by: 'pp_new' }), bot: 'Pixel', onOpenCard: () => {} });
  assert.match(linked, /data-superseded-by="pp_new"[^>]*>See the newer push/, 'the server names the newer card: link to it');
});

test('needsYou: one item per bot, folder and branch, the newest; an older card on a pushed-again branch never shows', () => {
  const older = card({ ...fx.PP_FAILED_NEEDS_YOU, id: 'a', created_at: 1, updated_at: 1 });
  const mid = card({ ...fx.PP_NEEDS_YOU, id: 'b', created_at: 2, updated_at: 2 });
  const newest = card({ ...fx.PP_NEEDS_YOU, id: 'c', created_at: 3, updated_at: 3 });
  assert.deepEqual(m.needsYou([older, mid, newest]).map((c) => c.id), ['c'], "Oleks's four items become one");
  const clearAfter = card({ ...fx.PP_CLEAR, id: 'd', created_at: 4, updated_at: 4 });
  assert.deepEqual(m.needsYou([older, mid, newest, clearAfter]), [], 'the bot pushed again and it went clear: nothing waits');
  const otherBot = card({ ...fx.PP_NEEDS_YOU, id: 'e', kwant_id: 'k_iris', created_at: 1, updated_at: 5 });
  const otherFolder = card({ ...fx.PP_NEEDS_YOU, id: 'f', folder: 'site', created_at: 1, updated_at: 6 });
  const otherBranch = card({ ...fx.PP_NEEDS_YOU, id: 'g', branch: 'pixel/x', created_at: 1, updated_at: 7 });
  assert.deepEqual(m.needsYou([newest, otherBot, otherFolder, otherBranch]).map((c) => c.id), ['g', 'f', 'e', 'c'], 'separate targets each keep one, newest update first');
  const sup = card({ ...fx.PP_SUPERSEDED, id: 'h', created_at: 2 });
  assert.deepEqual(m.needsYou([sup, newest]).map((c) => c.id), ['c']);
  const revived = card({ ...fx.PP_NEEDS_YOU, id: 'old', created_at: 1, updated_at: 9 });
  const newer = card({ ...fx.PP_SUPERSEDED, id: 'new', created_at: 5, updated_at: 8, branch: revived.branch, kwant_id: revived.kwant_id, space_id: revived.space_id, folder: revived.folder });
  assert.deepEqual(m.needsYou([revived, newer]).map((c) => c.id), ['old'], 'a revived older card waits even though a newer card exists, when that newer one is superseded');
});

test('needsYou: a card the person just pressed leaves at once, and comes back if the press failed', async () => {
  const c = card(fx.PP_NEEDS_YOU);
  assert.deepEqual(m.needsYou([c], new Set(['pp_needs'])), []);
  const { host, rerender } = await mount(m.PrepushNeedsYou, { cards: [c], names: { k_pixel: 'Pixel' }, onOpen: () => {}, acted: new Set() });
  assert.equal(host.querySelectorAll('[data-needs-prepush-item]').length, 1);
  // The branch tells two items apart, so it goes under the line at every width (Mira FIX 5): one chip, never hidden.
  const branches = host.querySelectorAll('[data-needs-prepush-item] code');
  assert.equal(branches.length, 1, 'no second, clipped chip beside the line');
  const branch = host.querySelector('[data-needs-branch]');
  assert.equal(branch?.textContent, c.branch);
  assert.equal(branch.getAttribute('title'), c.branch, 'the full name on hover when it is truncated');
  assert.doesNotMatch(branch.className, /hidden/);
  await rerender({ cards: [c], names: { k_pixel: 'Pixel' }, onOpen: () => {}, acted: new Set(['pp_needs']) });
  assert.equal(host.querySelector('[data-needs-prepush]'), null, 'gone before the server answers');
  await rerender({ cards: [c], names: { k_pixel: 'Pixel' }, onOpen: () => {}, acted: new Set() });
  assert.equal(host.querySelectorAll('[data-needs-prepush-item]').length, 1, 'back when the call failed');
});

test('usePrepushCards: setActed marks and unmarks a card', async () => {
  let state;
  const api = { list: async () => [card(fx.PP_NEEDS_YOU)], get: async () => card(fx.PP_NEEDS_YOU) };
  const Probe = () => {
    state = m.usePrepushCards('s_demo', true, api);
    return null;
  };
  await mount(Probe, {});
  assert.equal(state.acted.size, 0);
  state.setActed('pp_needs', true);
  await settle();
  assert.ok(state.acted.has('pp_needs'));
  state.setActed('pp_needs', false);
  await settle();
  assert.equal(state.acted.size, 0);
});

test('followUp: the new card the retry led to, by the grant\'s card id, else the newest later card of the same target', () => {
  const r = card(fx.PP_RETRIED_GRANT);
  assert.equal(m.followUp(r, [r]), null);
  assert.equal(m.followUp(r, all('PP_RETRIED_GRANT', 'PP_NEXT_CHECKING', 'PP_NEEDS_YOU')).id, 'pp_next');
  const noId = card({ ...fx.PP_NEXT_CHECKING, grant: null });
  assert.equal(m.followUp(r, [r, noId]).id, 'pp_next', 'old server: matched by branch and time');
  assert.equal(m.followUp(r, [r, card({ ...fx.PP_NEXT_CHECKING, branch: 'other' })]), null, 'another branch is another story');
  const named = card({ ...fx.PP_RETRIED_GRANT, grant: { ...fx.PP_RETRIED_GRANT.grant, next_card_id: 'pp_named' } });
  const newer = card({ ...fx.PP_NEXT_CHECKING, id: 'pp_newer', created_at: fx.PP_NEXT_CHECKING.created_at + 60000 });
  assert.equal(m.followUp(named, [named, newer, card({ ...fx.PP_NEXT_CLEAR, id: 'pp_named' })]).id, 'pp_named', "the server's next_card_id wins");
  assert.equal(m.followUp(r, [r, card({ ...fx.PP_NEXT_CHECKING, grant: { ...fx.PP_NEXT_CHECKING.grant, card_id: 'someone_else' } })]), null);
  assert.equal(m.pressedBefore(card(fx.PP_NEXT_CHECKING), all('PP_RETRIED_GRANT', 'PP_NEXT_CHECKING')).id, 'pp_rg');
  assert.equal(m.pressedBefore(card(fx.PP_CLEAR), [card(fx.PP_CLEAR)]), null);
});

test('story after a retry: fixing → checking the new push → Pushed · HH:MM, or blocked again', () => {
  const r = card(fx.PP_RETRIED_GRANT);
  const at = fx.PP_RETRIED_GRANT.grant.at;
  const s0 = m.storyOf(r, [r], 'Quant', null, at + 1000);
  assert.equal(s0.step, 'fixing');
  assert.equal(s0.lead, 'You asked Quant to fix this and push again. That was your OK: once the checks pass, its next push of pixel/inbox-empty-state goes without asking you.');
  assert.equal(s0.now, 'Quant is fixing it…');
  const s1 = m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_CHECKING'), 'Quant', null, at);
  assert.equal(s1.step, 'checking');
  assert.equal(s1.now, "Checking Quant's new push…");
  assert.equal(s1.next.id, 'pp_next');
  assert.equal(m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_CLEAR'), 'Quant', null, at).step, 'pushing');
  const s3 = m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_PUSHED'), 'Quant', null, at);
  assert.equal(s3.step, 'pushed');
  assert.equal(s3.now, `Pushed · ${HM(fx.PP_NEXT_PUSHED.grant.used_at)}`);
  const s4 = m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_BLOCKED'), 'Quant', null, at);
  assert.equal(s4.step, 'blocked');
  assert.equal(s4.now, 'Blocked again: 1 finding. Nothing was pushed.');
  const sx = m.storyOf(r, [r], 'Quant', null, fx.PP_RETRIED_GRANT.grant.expires_at + 1);
  assert.equal(sx.step, 'expired', 'a window that ran out says so');
  assert.equal(sx.lead, 'You asked Quant to fix this and push again.', 'the lead stops promising (Mira FIX 7)');
  assert.doesNotMatch(sx.lead, /without asking/);
  assert.equal(sx.now, "No fixed push came within 2 hours, so your OK ran out. Quant's next push asks you as usual.");
  const replacedNext = card({ ...fx.PP_NEXT_BLOCKED, status: 'superseded', needs_you: false });
  const rp = m.storyOf(r, [r, replacedNext], 'Quant', null, at);
  assert.equal(rp.step, 'replaced');
  assert.equal(rp.now, 'Quant pushed again, so a newer check replaced this one.', 'says what replaced what (Mira FIX 6)');
  const outR = html(m.PrepushCard, { card: r, bot: 'Quant', story: rp, onOpenCard: () => {} });
  assert.match(outR, /See the newer check/);
  const old = card({ ...fx.PP_RETRIED_GRANT, grant: null });
  assert.match(m.storyOf(old, [old], 'Quant').lead, /is checked from try 1\.$/, 'without a grant the card promises nothing it can\'t keep');
  assert.equal(m.storyOf(card(fx.PP_NEEDS_YOU), [], 'Quant'), null, 'only pressed cards tell the story');
});

test('story after Push anyway: pushing → Pushed · HH:MM', () => {
  const o = card(fx.PP_OVERRIDDEN_GRANT);
  const s = m.storyOf(o, [o], 'Quant', null, fx.PP_OVERRIDDEN_GRANT.grant.at);
  assert.equal(s.step, 'pushing');
  assert.equal(s.lead, `You let this push go at ${HM(fx.PP_OVERRIDDEN_GRANT.override.at)} with 1 finding open. That was your OK: Quant's push of this commit goes without asking you.`);
  assert.equal(s.lead, m.overriddenLine(o, 'Quant'), 'one source for the override line');
  assert.equal(s.now, 'Quant can push it now.');
  // Mira FIX 2: an override grant runs out too; the card says so instead of "can push it now" for ever.
  const sx = m.storyOf(o, [o], 'Quant', null, fx.PP_OVERRIDDEN_GRANT.grant.expires_at);
  assert.equal(sx.step, 'expired');
  assert.equal(sx.lead, `You let this push go at ${HM(fx.PP_OVERRIDDEN_GRANT.override.at)} with 1 finding open.`, 'an expired override drops the promise from the lead');
  assert.doesNotMatch(sx.lead, /without asking/);
  const outX = html(m.PrepushCard, { card: o, bot: 'Quant', story: sx });
  assert.doesNotMatch(outX, /without asking/, 'the expired override card promises nothing anywhere');
  assert.equal(sx.now, "Quant didn't push within 2 hours, so your OK ran out. Its push asks you as usual.");
  const p = card(fx.PP_PUSHED);
  const sp = m.storyOf(p, [p], 'Quant');
  assert.equal(sp.step, 'pushed');
  assert.equal(sp.now, `Pushed · ${HM(fx.PP_PUSHED.grant.used_at)}`);
});

test('a floor or payment approval the push still needs gets one line with a link; an ordinary approval does not', () => {
  const r = card(fx.PP_RETRIED_GRANT);
  const after = new Date(fx.PP_RETRIED_GRANT.grant.at + 60_000).toISOString();
  const floor = { id: 'ap1', tool_name: 'Bash', kwant_id: 'k_pixel', asked_at: after, details: { prod_floor: { kind: 'push_main', why: 'This push goes to main, a watched branch.' } } };
  const pay = { id: 'ap2', tool_name: 'stripe', kwant_id: 'k_pixel', asked_at: after, details: { payment_reason: 'It charges a card' } };
  const plain = { id: 'ap3', tool_name: 'Bash', kwant_id: 'k_pixel', asked_at: after, details: { command: 'git push' } };
  const otherBot = { ...floor, id: 'ap4', kwant_id: 'k_iris' };
  const before = { ...floor, id: 'ap5', asked_at: new Date(fx.PP_RETRIED_GRANT.grant.at - 600_000).toISOString() };
  assert.deepEqual(m.pushApproval(r, [plain, otherBot, before, floor]), { id: 'ap1', reason: 'This push goes to main, a watched branch' });
  assert.deepEqual(m.pushApproval(r, [pay]), { id: 'ap2', reason: 'It charges a card' });
  const bare = { ...floor, id: 'ap6', details: { prod_floor: { kind: 'push_main' } } };
  assert.deepEqual(m.pushApproval(r, [bare]), { id: 'ap6', reason: null }, 'no made-up reason');
  assert.equal(m.pushApproval(r, [plain]), null);
  assert.equal(m.pushApproval(card(fx.PP_NEEDS_YOU), [floor]), null, 'only after a press');
  assert.equal(m.pushApproval(card(fx.PP_PUSHED), [floor]), null, 'a push that went needs nothing');
  const s = m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_CLEAR'), 'Quant', { id: 'ap1', reason: 'This push goes to main' }, fx.PP_RETRIED_GRANT.grant.at + 600_000);
  assert.equal(s.lead, 'You asked Quant to fix this and push again.', 'an asking push promises nothing in the lead (round 3)');
  assert.equal(s.step, 'asks');
  assert.equal(s.now, "Quant's push still needs your OK. This push goes to main. Pushes like this always ask, every time.");
  assert.equal(m.asksLine('Quant', { id: 'x', reason: null }), "Quant's push still needs your OK. Pushes like this always ask, every time.");
});

test('the retried card shows the new state at once and links to the new card', async () => {
  const r = card(fx.PP_RETRIED_GRANT);
  const out0 = html(m.PrepushCard, { card: r, bot: 'Quant', story: m.storyOf(r, [r], 'Quant', null, fx.PP_RETRIED_GRANT.grant.at) });
  assert.match(out0, /data-prepush-phase="RT"/);
  assert.match(out0, /You asked Quant to fix this and push again\. That was your OK: once the checks pass, its next push of <code[^>]*>pixel\/inbox-empty-state<\/code> goes without asking you\./);
  assert.match(out0, /data-story="fixing"/);
  assert.match(out0, /role="status"[^>]*>.*Quant is fixing it…/);
  const opened = [];
  const cards = all('PP_RETRIED_GRANT', 'PP_NEXT_BLOCKED');
  const { host } = await mount(m.PrepushCard, { card: r, bot: 'Quant', story: m.storyOf(r, cards, 'Quant'), onOpenCard: (id) => opened.push(id) });
  assert.match(text(host), /Blocked again: 1 finding\. Nothing was pushed\./);
  await click(button(host, 'See what it found'));
  assert.deepEqual(opened, ['pp_next']);
});

test('the story line follows the new push live, and a floor approval line opens that approval', async () => {
  const r = card(fx.PP_RETRIED_GRANT);
  const at = fx.PP_RETRIED_GRANT.grant.at;
  const props = (cards, approval = null) => ({ card: r, bot: 'Quant', story: m.storyOf(r, cards, 'Quant', approval, at), onOpenCard: () => {}, onOpenApproval: (id) => openedA.push(id) });
  const openedA = [];
  const { host, rerender } = await mount(m.PrepushCard, props([r]));
  const now = () => text(host.querySelector('[data-story-now]'));
  assert.equal(now(), 'Quant is fixing it…');
  await rerender(props(all('PP_RETRIED_GRANT', 'PP_NEXT_CHECKING')));
  assert.equal(now(), "Checking Quant's new push…");
  assert.ok(host.querySelector('[data-story-next="pp_next"]'), 'links to the new card');
  await rerender(props(all('PP_RETRIED_GRANT', 'PP_NEXT_CLEAR'), { id: 'ap1', reason: 'This push goes to main' }));
  const line = host.querySelector('[data-story-approval="ap1"]');
  assert.ok(line);
  assert.equal(text(line).replace(/Open it.*$/, '').trim(), "Quant's push still needs your OK. This push goes to main. Pushes like this always ask, every time.");
  assert.equal(line.querySelector('.dv-led')?.getAttribute('data-s'), 'warn', 'amber light in the amber box, not the running violet');
  // Said once: the amber line is the only status line, not a second "needs your OK." above it.
  assert.equal((text(host).match(/needs your OK/g) ?? []).length, 1);
  assert.equal(host.querySelectorAll('[role="status"]').length, 1);
  assert.equal(line.getAttribute('role'), 'status');
  await click(button(line, 'Open it'));
  assert.deepEqual(openedA, ['ap1']);
  await rerender(props(all('PP_RETRIED_GRANT', 'PP_NEXT_PUSHED')));
  assert.equal(now(), `Pushed · ${HM(fx.PP_NEXT_PUSHED.grant.used_at)}`);
  assert.equal(host.querySelector('[data-story-approval]'), null);
});

test('the new card links back to the request, and once its push went it folds to "Pushed · HH:MM"', async () => {
  const cards = all('PP_RETRIED_GRANT', 'PP_NEXT_CHECKING');
  const next = cards[1];
  const opened = [];
  const { host } = await mount(m.PrepushCard, { card: next, bot: 'Quant', after: cards[0], onOpenCard: (id) => opened.push(id) });
  assert.match(text(host.querySelector('[data-after-retry="pp_rg"]')), new RegExp(`After you asked for a fix at ${HM(fx.PP_RETRIED_GRANT.grant.at)}\\.`));
  await click(button(host, 'See your request'));
  assert.deepEqual(opened, ['pp_rg']);
  const out = html(m.PrepushCard, { card: card(fx.PP_NEXT_PUSHED), bot: 'Quant', after: cards[0] });
  assert.match(out, /data-prepush-phase="P2b"/);
  assert.match(out, new RegExp(`Pushed · ${HM(fx.PP_NEXT_PUSHED.grant.used_at)}`));
  assert.match(out, /· your retry was the OK, so it didn&#x27;t ask again\.|· your retry was the OK, so it didn't ask again\./);
  assert.doesNotMatch(out, /no second OK/);
});

test('Push anyway: the card follows its one push to "Pushed · HH:MM"', () => {
  const o = card(fx.PP_OVERRIDDEN_GRANT);
  const out = html(m.PrepushCard, { card: o, bot: 'Quant', story: m.storyOf(o, [o], 'Quant', null, fx.PP_OVERRIDDEN_GRANT.grant.at) });
  assert.match(out, /data-story="pushing"/);
  assert.match(out, /Quant can push it now\./);
  const p = card(fx.PP_PUSHED);
  const outP = html(m.PrepushCard, { card: p, bot: 'Quant', story: m.storyOf(p, [p], 'Quant') });
  assert.match(outP, /data-story="pushed"/);
  assert.match(outP, new RegExp(`Pushed · ${HM(fx.PP_PUSHED.grant.used_at)}`));
  assert.doesNotMatch(outP, /<button[^>]*>(Push anyway…|Ask Quant)/);
});

test('PrepushThread wires the story: the retried card follows the new card from the whole list', async () => {
  // The thread reads the real clock, so keep the retry's window open for this test (round 3 expires late pushes).
  const open = card({ ...fx.PP_RETRIED_GRANT, grant: { ...fx.PP_RETRIED_GRANT.grant, expires_at: Date.now() + 3_600_000 } });
  const every = [open, card(fx.PP_NEXT_CHECKING)];
  const { host } = await mount(m.PrepushThread, { cards: every, all: every, bot: 'Quant', live: new Set(), approvals: [] });
  assert.equal(text(host.querySelector('#prepush-pp_rg [data-story-now]')), "Checking Quant's new push…");
  assert.ok(host.querySelector('#prepush-pp_next [data-after-retry="pp_rg"]'));
});

test('the chat messages to the bot say its next push needs no second OK (Mira FIX 8)', () => {
  const c = card(fx.PP_NEEDS_YOU);
  assert.equal(
    m.retryMessage(c),
    `Fix what the pre-push check found on ${c.branch}, commit, and push again. I've given my OK for that push, so once the checks pass it goes without asking me.`,
  );
  assert.equal(
    m.overrideMessage(c),
    `I let your push of ${c.branch} go anyway, for this commit only. That was my OK, so run the same push again without asking me.`,
  );
});

test('the new card after an override push names Push anyway as the OK (Mira FIX 4)', () => {
  const p = card({ ...fx.PP_NEXT_PUSHED, grant: { ...fx.PP_NEXT_PUSHED.grant, kind: 'override' } });
  const out = html(m.PrepushCard, { card: p, bot: 'Quant' });
  assert.match(out, /your Push anyway was the OK, so it didn(&#x27;|')t ask again\./);
});

test('a long story line keeps its light beside the text, so it never sits alone on a row on a phone', () => {
  const r = card(fx.PP_RETRIED_GRANT);
  const out = html(m.PrepushCard, { card: r, bot: 'Quant', story: m.storyOf(r, [r], 'Quant', null, fx.PP_RETRIED_GRANT.grant.expires_at + 1) });
  assert.match(out, /<span data-story-text="true" class="flex min-w-0 items-start gap-2"><span aria-hidden="true" data-s="off" class="dv-led mt-\[5px\] shrink-0 dv-led-still"><\/span><span data-story-now/);
});

// ---------------------------------------------------------------------------------------------
// Round 3 (Rigel QA, Nov 13): floor after a retry, a late push after the window, the replaced chain.

const floorAfter = (c, extra = 60_000) => ({
  id: 'ap_floor', tool_name: 'Bash', kwant_id: c.kwant_id, asked_at: new Date(c.grant.at + extra).toISOString(),
  details: { command: 'git push origin HEAD:main', prod_floor: { kind: 'push_main', why: 'git push sends new commits to main' } },
});

test('round 3: a production-floor push after a retry makes no card, and the retried card shows the amber line with Open it', async () => {
  const r = card(fx.PP_RETRIED_GRANT);
  const at = fx.PP_RETRIED_GRANT.grant.at + 120_000;
  const ap = m.pushApproval(r, [floorAfter(r)]);
  assert.deepEqual(ap, { id: 'ap_floor', reason: 'git push sends new commits to main' });
  const s = m.storyOf(r, [r], 'Quant', ap, at);
  assert.equal(s.step, 'asks', 'not "fixing": the floor approval is the follow-up');
  assert.equal(s.now, "Quant's push still needs your OK. Git push sends new commits to main. Pushes like this always ask, every time.");
  assert.equal(s.lead, 'You asked Quant to fix this and push again.');
  assert.doesNotMatch(s.lead, /without asking/);
  const sc = m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_CHECKING'), 'Quant', ap, at);
  assert.equal(sc.step, 'asks', 'a check still running does not hide the floor approval');
  assert.equal(m.storyOf(r, [r], 'Quant', null, at).step, 'fixing', 'no approval: still fixing');
  const opened = [];
  const { host } = await mount(m.PrepushCard, { card: r, bot: 'Quant', story: s, onOpenCard: () => {}, onOpenApproval: (id) => opened.push(id) });
  assert.doesNotMatch(text(host), /without asking/);
  assert.equal(host.querySelector('[data-story="asks"] .dv-led')?.getAttribute('data-s'), 'warn', 'amber light, not a spinner');
  await click(button(host, 'Open it'));
  assert.deepEqual(opened, ['ap_floor']);
  // An override with a floor approval waiting: the lead drops its promise too.
  const o = card(fx.PP_OVERRIDDEN_GRANT);
  const so = m.storyOf(o, [o], 'Quant', ap, fx.PP_OVERRIDDEN_GRANT.grant.at);
  assert.equal(so.step, 'asks');
  assert.equal(so.lead, `You let this push go at ${HM(fx.PP_OVERRIDDEN_GRANT.override.at)} with 1 finding open.`);
});

test('round 3: the asks line capitalises the reason it is given', () => {
  assert.equal(m.asksLine('Quant', { id: 'x', reason: 'a stripe CLI command that changes something' }),
    "Quant's push still needs your OK. A stripe CLI command that changes something. Pushes like this always ask, every time.");
});

test('round 3: an unused grant past its window is expired even when a later push made a card, and links the approval', async () => {
  const r = card(fx.PP_RETRIED_GRANT);
  const late = fx.PP_RETRIED_GRANT.grant.expires_at + 60_000;
  for (const k of ['PP_NEXT_CLEAR', 'PP_NEXT_CHECKING']) {
    const s = m.storyOf(r, all('PP_RETRIED_GRANT', k), 'Quant', null, late);
    assert.equal(s.step, 'expired', `${k} after the window: not "pushing"`);
    assert.equal(s.lead, 'You asked Quant to fix this and push again.');
    assert.doesNotMatch(s.lead, /without asking/);
    assert.equal(s.now, "No fixed push came within 2 hours, so your OK ran out. Quant's next push asks you as usual.");
  }
  assert.equal(m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_BLOCKED'), 'Quant', null, late).step, 'blocked', 'blocked again still says so');
  assert.equal(m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_PUSHED'), 'Quant', null, late).step, 'pushed', 'a used grant never expires');
  const ap = m.pushApproval(r, [floorAfter(r, 125 * 60_000)]);
  const s = m.storyOf(r, all('PP_RETRIED_GRANT', 'PP_NEXT_CLEAR'), 'Quant', ap, late);
  assert.equal(s.step, 'expired');
  assert.equal(s.approval.id, 'ap_floor');
  const opened = [];
  const { host } = await mount(m.PrepushCard, { card: r, bot: 'Quant', story: s, onOpenCard: () => {}, onOpenApproval: (id) => opened.push(id) });
  assert.match(text(host), /your OK ran out/);
  assert.doesNotMatch(text(host), /without asking|is pushing/);
  await click(button(host, 'Open it'));
  assert.deepEqual(opened, ['ap_floor']);
});

test('round 3: "See the newer check" follows superseded_by to the live card, at most 10 hops, and stops on a cycle', () => {
  const r = card({ ...fx.PP_RETRIED_GRANT, grant: { ...fx.PP_RETRIED_GRANT.grant, next_card_id: 'pp_next' } });
  const mid = card({ ...fx.PP_NEXT_BLOCKED, status: 'superseded', needs_you: false, superseded_by: 'pp_live' });
  const live = card({ ...fx.PP_NEXT_BLOCKED, id: 'pp_live', created_at: (fx.PP_NEXT_BLOCKED.created_at ?? 0) + 1 });
  const s = m.storyOf(r, [r, mid, live], 'Quant', null, fx.PP_RETRIED_GRANT.grant.at + 600_000);
  assert.equal(s.step, 'replaced');
  assert.equal(s.next.id, 'pp_live', 'the link lands on the live card, not on one that is itself replaced');
  const sup = (id, to) => card({ ...fx.PP_SUPERSEDED, id, superseded_by: to });
  const a = sup('a', 'b');
  assert.equal(m.liveCard(a, [a, sup('b', 'c'), sup('c', 'a')]).id, 'c', 'a cycle stops at the last new card');
  const chain = Array.from({ length: 15 }, (_, i) => sup(`n${i}`, `n${i + 1}`));
  assert.equal(m.liveCard(chain[0], chain).id, 'n10', 'at most 10 hops');
  assert.equal(m.liveCard(sup('x', 'gone'), []).id, 'x', 'a missing card keeps the last one found');
});

test('round 3: in the thread, a "newer" link lands on the live card at the end of the chain', async () => {
  const r = card({ ...fx.PP_RETRIED_GRANT, grant: { ...fx.PP_RETRIED_GRANT.grant, next_card_id: 'pp_next', expires_at: Date.now() + 3_600_000 } });
  const mid = card({ ...fx.PP_NEXT_BLOCKED, status: 'superseded', needs_you: false, superseded_by: 'pp_mid' });
  const mid2 = card({ ...fx.PP_NEXT_BLOCKED, id: 'pp_mid', status: 'superseded', needs_you: false, superseded_by: 'pp_live', created_at: (fx.PP_NEXT_BLOCKED.created_at ?? 0) + 1 });
  const live = card({ ...fx.PP_NEXT_BLOCKED, id: 'pp_live', created_at: (fx.PP_NEXT_BLOCKED.created_at ?? 0) + 2 });
  const every = [r, mid, mid2, live];
  const { host } = await mount(m.PrepushThread, { cards: every, all: every, bot: 'Quant', live: new Set(), approvals: [] });
  await click(button(host.querySelector('#prepush-pp_rg'), 'See the newer check'));
  assert.ok(host.querySelector('#prepush-pp_live').classList.contains('kw-prepush-flash'), 'the live card rings');
  assert.ok(!host.querySelector('#prepush-pp_mid').classList.contains('kw-prepush-flash'), 'not a card that is itself replaced');
  host.querySelector('#prepush-pp_live').classList.remove('kw-prepush-flash');
  const ss = host.querySelector('#prepush-pp_next [data-superseded-by]');
  if (ss) { await click(ss); assert.ok(host.querySelector('#prepush-pp_live').classList.contains('kw-prepush-flash'), 'the Replaced card\'s own link too'); }
});

// 18d (clock says day): the override and retry sentences say "on Nov 9 at 2:09 PM" when read on another day,
// "at 2:09 PM" on the same day; P2b's tail carries the day too. Never "at Nov 9, 2:09 PM".
test('18d: prepush sentences with "at" say the day when it was not today', () => {
  const SAME = Date.UTC(2026, 10, 9, 23, 0);
  const NEXT = Date.UTC(2026, 10, 10, 9, 0);
  const NEXT_YEAR = Date.UTC(2027, 0, 4, 9, 0);
  const over = m.parsePrepush(m.fx.PP_OVERRIDDEN);
  assert.equal(m.overriddenHead(over, SAME), 'You let this push go at 2:09\u00a0PM with 1 finding open.');
  assert.equal(m.overriddenHead(over, NEXT), 'You let this push go on Nov 9 at 2:09\u00a0PM with 1 finding open.');
  assert.equal(m.overriddenHead(over, NEXT_YEAR), 'You let this push go on Nov 9, 2026 at 2:09\u00a0PM with 1 finding open.');
  const next = m.parsePrepush(m.fx.PP_NEXT_CHECKING);
  assert.equal(m.afterRetryLine(next, SAME), 'After you asked for a fix at 2:10\u00a0PM.');
  assert.equal(m.afterRetryLine(next, NEXT), 'After you asked for a fix on Nov 9 at 2:10\u00a0PM.');
  const clear = m.parsePrepush(m.fx.PP_CLEAR);
  assert.equal(m.clearedParts(clear, SAME).after, ' after 5 checks passed · 2:02\u00a0PM');
  assert.equal(m.clearedParts(clear, NEXT).after, ' after 5 checks passed · Nov 9, 2:02\u00a0PM');
  assert.equal(m.overriddenHead({ ...over, override: null }, NEXT), 'You let this push go with 1 finding open.');
});