mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 07:19:41 +01:00
* feat(watch): Review — Loki judges what you just did on your site and suggests what to improve Watch mode only acted when something broke. It now also notices what a demanding reviewer would (4xx, slow requests, console errors, slow loads, main-thread freezes, layout shift, pauses between steps) plus measured page checks (unnamed controls, unlabeled fields, tiny tap targets, missing alt, broken images, sideways scroll, h1/lang). The pill counts them; Review sends the session to Ask, which judges it through errors, design, engineering, process and product - every point cited from the session - and each change is one tap from a request that carries the watched steps. The trail now survives page loads within a tab, so a multi-page site reads as one visit. No model runs until Review is pressed. The report POST is now one helper (widget/send-report.ts) shared by Send and the automatic fix. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(watch): Review never blames the site for Loki's own work; plain-text answers Found by looking at the 390px render, not by the tests: - opening Loki's own Review panel scored a 0.27 layout shift against the site (Chrome reports shadow-DOM sources with a null node) - only shifts pinned on a site node count now; - the widget's own boot registered as 'the page froze 369ms' - a freeze now counts only when it overlaps the second after a tap on the site; - the review asked for **bold** lenses and the widget shows text, so clients saw literal asterisks - plain-text rubric + plainAnswer() on every reply; - '404 in 0.0s' - duration only when slow. The browser test now proves the detectors fire on real jank (a Load more that freezes and pushes content down) AND stay quiet for Loki's own panel. The 'typed values never leave the page' checks were vacuous (fill() never taps the field) - they tap the filled field now and fail under mutation. Docs: the architecture table described Watch as 'still a seam'. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
163 lines
6.5 KiB
TypeScript
163 lines
6.5 KiB
TypeScript
// Watch mode's trail and reports: privacy by construction (labels, never
|
|
// values; paths, never query strings), one report per distinct failure, and a
|
|
// report that names what broke before what led there.
|
|
// Run: npx tsx scripts/test/widget-watch.ts
|
|
import assert from "node:assert/strict";
|
|
import {
|
|
describeControl,
|
|
describeRequest,
|
|
failureSignature,
|
|
freshTrail,
|
|
isFailedRequest,
|
|
isNoiseError,
|
|
isNoticeableRequest,
|
|
nextTapStreak,
|
|
noticeCount,
|
|
pushTrail,
|
|
REVIEW_SESSION_MAX,
|
|
sessionForReview,
|
|
SLOW_REQUEST_MS,
|
|
TRAIL_MAX,
|
|
TRAIL_MAX_AGE_MS,
|
|
watchReport,
|
|
type TrailEntry,
|
|
} from "../../widget/watch-trail";
|
|
import { buildSuggestion } from "../../widget/report-payload";
|
|
|
|
let n = 0;
|
|
const check = (fn: () => void) => {
|
|
fn();
|
|
n++;
|
|
};
|
|
|
|
check(() => {
|
|
assert.equal(
|
|
describeControl({ tag: "BUTTON", text: " Save\n project " }),
|
|
"button “Save project”",
|
|
);
|
|
assert.equal(describeControl({ tag: "a", label: "Open profile" }), "link “Open profile”");
|
|
// An input is named by its placeholder — what was typed never appears.
|
|
assert.equal(
|
|
describeControl({ tag: "input", type: "email", placeholder: "Your email" }),
|
|
"email field “Your email”",
|
|
);
|
|
assert.equal(describeControl({ tag: "div", role: "button" }), "button");
|
|
});
|
|
check(() => {
|
|
assert.equal(
|
|
describeRequest("post", "https://orangecat.ch/api/projects?token=secret", 500),
|
|
"POST /api/projects → 500",
|
|
);
|
|
assert.equal(describeRequest("get", "/shop/x", null), "GET /shop/x → no response");
|
|
assert.equal(isFailedRequest(500), true);
|
|
assert.equal(isFailedRequest(null), true);
|
|
assert.equal(isFailedRequest(404), false);
|
|
});
|
|
check(() => {
|
|
let t: TrailEntry[] = [];
|
|
for (let i = 0; i < TRAIL_MAX + 5; i++) t = pushTrail(t, { at: i, kind: "tap", text: `b${i}` });
|
|
assert.equal(t.length, TRAIL_MAX);
|
|
const before = t.length;
|
|
t = pushTrail(t, { at: 99, kind: "tap", text: `b${TRAIL_MAX + 4}` });
|
|
assert.equal(t.length, before, "a repeat of the last entry is one fact");
|
|
});
|
|
check(() => {
|
|
assert.equal(isNoiseError("ResizeObserver loop limit exceeded"), true);
|
|
assert.equal(isNoiseError("Script error."), true);
|
|
assert.equal(isNoiseError("TypeError: cannot read properties of undefined"), false);
|
|
assert.equal(
|
|
failureSignature({ kind: "request", text: "GET /api/projects/123 → 500" }),
|
|
failureSignature({ kind: "request", text: "GET /api/projects/456 → 500" }),
|
|
);
|
|
});
|
|
check(() => {
|
|
const trail: TrailEntry[] = [
|
|
{ at: 1000, kind: "page", text: "/projects" },
|
|
{ at: 3000, kind: "tap", text: "button “Create”" },
|
|
];
|
|
const r = watchReport({ kind: "request", text: "POST /api/projects → 500" }, trail, 5000);
|
|
assert.match(r.message, /request failed: POST \/api\/projects → 500/);
|
|
assert.equal(r.diagnostics["step 2"], "tap button “Create” (2s ago)");
|
|
const s = buildSuggestion(r.message, r.diagnostics, 2000);
|
|
assert.ok(s.length <= 2000 && s.includes("failure: POST /api/projects → 500"));
|
|
});
|
|
|
|
check(() => {
|
|
// Three taps on one button inside the window: dead. A field, a gap, or a
|
|
// different button in between: not.
|
|
let r = nextTapStreak(null, "button “Pay”", 0);
|
|
r = nextTapStreak(r.streak, "button “Pay”", 1000);
|
|
assert.equal(r.dead, false);
|
|
r = nextTapStreak(r.streak, "button “Pay”", 2000);
|
|
assert.equal(r.dead, true);
|
|
let f = nextTapStreak(null, "email field “Your email”", 0);
|
|
f = nextTapStreak(f.streak, "email field “Your email”", 100);
|
|
f = nextTapStreak(f.streak, "email field “Your email”", 200);
|
|
assert.equal(f.dead, false, "fields are tapped to focus");
|
|
let g = nextTapStreak(null, "button “Pay”", 0);
|
|
g = nextTapStreak(g.streak, "button “Pay”", 1000);
|
|
g = nextTapStreak(g.streak, "button “Pay”", 9000);
|
|
assert.equal(g.dead, false, "outside the window it starts again");
|
|
const rep = watchReport({ kind: "dead-tap", text: "button “Pay”" }, [], 0);
|
|
assert.match(rep.message, /tapped button “Pay” three times and nothing happened/);
|
|
});
|
|
|
|
// ---- Review: what a reviewer remarks on that is not a failure ----
|
|
check(() => {
|
|
assert.equal(describeRequest("get", "/a?x=1", 200, 4230), "GET /a → 200 in 4.2s");
|
|
assert.equal(isNoticeableRequest(404, 50), true, "a 404 is worth a remark");
|
|
assert.equal(isNoticeableRequest(200, SLOW_REQUEST_MS), true, "so is a slow answer");
|
|
assert.equal(isNoticeableRequest(200, 120), false);
|
|
// 5xx and no-answer are failures (they file a fix) — never also a remark.
|
|
assert.equal(isNoticeableRequest(500, 9000), false);
|
|
assert.equal(isNoticeableRequest(null, 9000), false);
|
|
});
|
|
|
|
check(() => {
|
|
const now = 10_000_000;
|
|
const kept = freshTrail(
|
|
[
|
|
{ at: now - TRAIL_MAX_AGE_MS - 1, kind: "tap", text: "yesterday" },
|
|
{ at: now - 1000, kind: "tap", text: "button “Go”" },
|
|
{ at: now - 500, kind: "bogus", text: "x" },
|
|
{ at: "soon", kind: "tap", text: "x" },
|
|
{ at: now + 60_000, kind: "tap", text: "from the future" },
|
|
null,
|
|
],
|
|
now,
|
|
);
|
|
assert.deepEqual(kept, [{ at: now - 1000, kind: "tap", text: "button “Go”" }]);
|
|
assert.deepEqual(freshTrail("not json array", now), [], "stored garbage is an empty trail");
|
|
});
|
|
|
|
check(() => {
|
|
const t: TrailEntry[] = [
|
|
{ at: 0, kind: "page", text: "/shop" },
|
|
{ at: 1_000, kind: "tap", text: "button “Buy”" },
|
|
{ at: 1_200, kind: "notice", text: "POST /cart → 404 in 0.2s" },
|
|
{ at: 45_000, kind: "tap", text: "link “Help”" },
|
|
];
|
|
assert.equal(noticeCount(t), 1);
|
|
const s = sessionForReview(t, ["2 image(s) have no alt text"], 90_000);
|
|
assert.match(s, /page \/shop\ntap button “Buy”\nnotice POST \/cart → 404/);
|
|
assert.match(s, /\(paused 44s\)\ntap link “Help”/, "a long stop before a step is written out");
|
|
assert.match(s, /nothing for 45s, then asked for this review/);
|
|
assert.match(s, /Checks on the page they are on now:\n- 2 image\(s\) have no alt text/);
|
|
});
|
|
|
|
check(() => {
|
|
// Too long: the OLDEST steps go, the checks and the newest steps stay.
|
|
const t: TrailEntry[] = Array.from({ length: TRAIL_MAX }, (_, i) => ({
|
|
at: i,
|
|
kind: "tap" as const,
|
|
text: `button “${String(i).padStart(3, "0")} ${"x".repeat(140)}”`,
|
|
}));
|
|
const s = sessionForReview(t, ["check A"], TRAIL_MAX);
|
|
assert.ok(s.length <= REVIEW_SESSION_MAX, `capped (${s.length})`);
|
|
assert.ok(s.includes(`button “${String(TRAIL_MAX - 1).padStart(3, "0")}`), "newest kept");
|
|
assert.ok(!s.includes("button “000"), "oldest dropped");
|
|
assert.ok(s.endsWith("- check A"), "checks survive the cut");
|
|
});
|
|
|
|
console.log(`${n} widget-watch cases passed`);
|