Files
chatkit/test/browser.mjs
T
CatoandClaude Opus 5.5 ed759a8516 feat: chatkit v0.1 — one chat for every product (#1)
* feat: chatkit v0.1 — one chat for every product

The fleet had 13 chats in 10 repos, each written fresh, each missing what
another had fixed. This extracts loki /loki (composer, thread, messages,
markdown, attachments) and adds heidi's microphone and orangecat's keyboard fix:

- Composer: auto-growing 16px textarea, Enter/Shift+Enter/IME, Send and Stop
  in one slot, attachments with paste, mic always present, every label
  translatable, app-specific controls in slots.
- useDictation: browser recogniser first; missing or SILENT (4 s watchdog,
  bounded permission wait) -> record and transcribe on the server; a dead
  recogniser remembered 30 days; every failure shown in words.
- ChatThread/ChatMessage: stick-to-bottom + jump button, speaker names once two
  agents speak, Copy on answers, Retry on the last one, failed turns visible.
- Markdown with no HTML path; javascript: links stay text.
- styles.css: --ck-* variables defaulting to @bitbaum/design-tokens.
- verify = format, lint, types, build, 18 unit/render tests and 21 browser
  checks (390px + desktop, light + dark, a sentence through a fake mic past a
  silent recogniser). Browser checks mutation-tested (14px input, 60 s
  watchdog) — both fail as they should.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(styles): --ck-* defaults at zero specificity on :root, so an app's own values win

Declared on the chat's elements they overrode any app setting --ck-* at the
root — the opposite of what the README promised. Found preparing loki, whose
colour variables are full OKLCH colours under the design-token names and so
must map them. New browser check: a :root --ck-accent reaches the chat.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-24 23:14:53 -07:00

144 lines
5.6 KiB
JavaScript

// The standard, checked in a real browser — the part no unit test can see.
//
// pnpm run demo && node test/browser.mjs
//
// Each check is something that shipped broken in at least one fleet chat:
// - text and input at 16px (below it iOS zooms the page on focus)
// - a microphone in every composer
// - every control at least 44px (a thumb has to land on it)
// - no horizontal scroll at 390px, light and dark
// - the mic still works where the browser's recogniser is silent: headless
// Chromium exposes webkitSpeechRecognition with no speech service behind it
// — exactly heidi's measured failure — so the press must fall back to
// recording, post the audio, and put the words in the box.
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
import { extname, join, normalize } from "node:path";
import { fileURLToPath } from "node:url";
import { chromium } from "playwright";
const root = fileURLToPath(new URL("..", import.meta.url));
const types = {
".html": "text/html",
".js": "text/javascript",
".css": "text/css",
".woff2": "font/woff2",
};
const server = createServer(async (req, res) => {
const path = normalize(join(root, decodeURIComponent(new URL(req.url, "http://x").pathname)));
if (!path.startsWith(root)) return res.writeHead(403).end();
try {
const body = await readFile(path);
res
.writeHead(200, { "content-type": types[extname(path)] ?? "application/octet-stream" })
.end(body);
} catch {
res.writeHead(404).end();
}
}).listen(0);
const base = `http://localhost:${server.address().port}/demo/index.html`;
let failures = 0;
const check = (ok, what) => {
console.log(`${ok ? "PASS" : "FAIL"} ${what}`);
if (!ok) failures++;
};
const browser = await chromium.launch({
args: ["--use-fake-device-for-media-stream", "--use-fake-ui-for-media-stream"],
});
try {
for (const [name, width, height, dark] of [
["desktop light", 1280, 800, false],
["desktop dark", 1280, 800, true],
["phone dark", 390, 844, true],
]) {
const page = await browser.newPage({ viewport: { width, height } });
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.goto(base);
if (dark) await page.evaluate(() => document.documentElement.classList.add("dark"));
await page.waitForSelector(".ck-input");
const m = await page.evaluate(() => {
const px = (sel) =>
[...document.querySelectorAll(sel)].map((e) => parseFloat(getComputedStyle(e).fontSize));
return {
overflow: document.documentElement.scrollWidth - window.innerWidth,
input: Math.min(...px(".ck-input")),
text: Math.min(...px(".ck-md, .ck-bubble")),
composers: document.querySelectorAll(".ck-composer").length,
mics: document.querySelectorAll(".ck-composer .ck-mic").length,
small: [
...document.querySelectorAll(".ck-icon-btn, .ck-send, .ck-starter, .ck-voice-confirm"),
]
.map((e) => e.getBoundingClientRect())
.filter((r) => r.width > 0 && (r.width < 44 || r.height < 44)).length,
};
});
check(m.overflow <= 0, `${name}: no horizontal scroll (${m.overflow}px over)`);
check(m.input >= 16, `${name}: input text >= 16px (${m.input}px)`);
check(m.text >= 16, `${name}: message text >= 16px (${m.text}px)`);
check(
m.composers > 0 && m.mics === m.composers,
`${name}: a mic in every composer (${m.mics}/${m.composers})`,
);
check(m.small === 0, `${name}: every control >= 44px (${m.small} smaller)`);
check(errors.length === 0, `${name}: no page errors ${errors.join(" | ")}`);
await page.close();
}
// An app's own variables win: a --ck-* set on :root must reach the chat
// (the defaults are declared at zero specificity for exactly this).
{
const page = await browser.newPage();
await page.goto(base);
await page.waitForSelector("#conversation .ck-link");
const bg = await page.evaluate(() => {
document.documentElement.style.setProperty("--ck-accent", "rgb(0, 128, 0)");
return getComputedStyle(document.querySelector("#conversation .ck-link")).textDecorationColor;
});
check(bg === "rgb(0, 128, 0)", `an app's :root --ck-accent reaches the chat (${bg})`);
await page.close();
}
const context = await browser.newContext({ permissions: ["microphone"] });
const page = await context.newPage();
let posted = 0;
await page.route("**/api/transcribe", async (route) => {
posted = route.request().postDataBuffer()?.length ?? 0;
await route.fulfill({
contentType: "application/json",
body: JSON.stringify({ text: "hello from the mic" }),
});
});
await page.goto(base);
const box = page.locator("#empty");
await box.locator(".ck-mic").click();
check(await box.locator(".ck-voice-bar").isVisible(), "mic: pressing it shows the recording bar");
// Past the silent-recogniser watchdog (4 s): it must now be recording.
await page.waitForTimeout(5500);
await box.locator(".ck-voice-confirm").click();
await page
.waitForFunction(() => document.querySelector("#empty .ck-input")?.value !== "", null, {
timeout: 5000,
})
.catch(() => {});
check(
posted > 0,
`mic: a silent recogniser falls back to recording and posts the audio (${posted} bytes)`,
);
check(
(await box.locator(".ck-input").inputValue()) === "hello from the mic",
"mic: the spoken sentence lands in the input",
);
await context.close();
} finally {
await browser.close();
server.close();
}
if (failures) {
console.log(`\n${failures} browser check(s) failed`);
process.exit(1);
}
console.log("\nall browser checks passed");