Files
loki/scripts/test/widget-chat-css.ts
T
CatoandClaude 58ee6e8c78 feat(widget): the conversation drawn by the shared chat (chatkit), on Loki's own widget first (#1162)
The widget's chat was the fleet's own hand-rolled chat; with
data-fc-chat="kit" it is now drawn by @bitbaum/chatkit, the thread and
composer Loki's own chat renders, over the same conversation model.

- widget/chat/: a separate lazy chunk (public/widget-chat.js, ~24 KB
  gzipped, Preact standing in for React), fetched on first open, carrying
  the tag's nonce. chatkit's CSS is rewritten for the shadow root (:root ->
  :host, rem -> px) and coloured from the boot theme.
- widget/conversation-kit.ts maps the model's snapshot to what chatkit
  draws; Fix this, the changes, the confirmation card, the About chips and
  Continue in Loki go into chatkit's slots as the widget's own nodes.
- A chunk that cannot load leaves the panel's own view: no dead end.
- /widget-chat.js is public in the auth proxy, like /widget.js.
- Tests: widget-kit-browser (34 checks: ask, card, screenshot, fallback,
  phone width, host hotkeys, weight budget), widget-chat-css, and
  widget-self-contained now guards the chunk/panel seam.


Claude-Session: https://claude.ai/code/session_01BWXjqiHm8jFta4FXuc7Tck

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-12 05:13:25 -07:00

65 lines
2.7 KiB
TypeScript

// The shared chat's stylesheet inside the widget's shadow root, and its
// colours from the widget's theme (widget/chat/shadow-css.ts, chat/theme.ts).
//
// chatkit's CSS assumes it owns the page: defaults on `:root`, which never
// matches in a shadow root, and sizes in rem, which is the HOST's root size.
// The browser test (widget-kit-browser.ts) shows the result on a 10px-root
// page; this pins the transform itself, on the real stylesheet.
//
// Run: npx tsx scripts/test/widget-chat-css.ts
import { readFileSync } from "node:fs";
import { createRequire } from "node:module";
import { PALETTE } from "../../src/lib/palette";
import { shadowChatkitCss } from "../../widget/chat/shadow-css";
import { chatkitVars } from "../../widget/chat/theme";
let pass = 0;
let fail = 0;
function ok(cond: boolean, label: string) {
if (cond) pass++;
else {
fail++;
console.error(`✗ ${label}`);
}
}
ok(
shadowChatkitCss(".a { padding: 0.375rem 1.25rem; gap: -0.5rem; }") ===
".a { padding: 6px 20px; gap: -8px; }",
"rem becomes px at the widget's 16px, signs and fractions kept",
);
ok(shadowChatkitCss(".a { width: 2remx; }").includes("2remx"), "only whole rem units change");
ok(
shadowChatkitCss(":where(:root) { --ck-bg: red; }") === ":where(:host) { --ck-bg: red; }",
"the defaults move from :root to the shadow host",
);
const require = createRequire(import.meta.url);
const real = readFileSync(require.resolve("@bitbaum/chatkit/styles.css"), "utf8");
ok(/\drem\b/.test(real) && real.includes(":where(:root)"), "the real stylesheet has both to fix");
const fixed = shadowChatkitCss(real);
ok(!/\drem\b/.test(fixed), "no rem is left in the shipped chat CSS");
ok(
!fixed.replace(/\/\*[\s\S]*?\*\//g, "").includes(":root"),
"no :root rule is left in the shipped chat CSS",
);
// Every variable chatkit reads is set from the widget's theme, so nothing
// falls through to a host page's own tokens.
const vars = chatkitVars(PALETTE.widget);
const read = new Set([...real.matchAll(/var\((--ck-[a-z0-9-]+)/g)].map((m) => m[1]));
const declaredByKit = new Set([...real.matchAll(/(--ck-[a-z0-9-]+)\s*:/g)].map((m) => m[1]));
const colours = [...read].filter((v) =>
/bg|raised|overlay|fg|muted|line|accent|danger|warning/.test(v),
);
const unset = colours.filter((v) => !vars.includes(`${v}:`));
ok(unset.length === 0, `every colour chatkit reads comes from the widget theme (${unset})`);
ok(
[...read].every((v) => vars.includes(`${v}:`) || declaredByKit.has(v)),
"every other variable has chatkit's own default",
);
ok(vars.startsWith(":host {") && !vars.includes("undefined"), "the theme sets them on the host");
console.log(`${pass} passed, ${fail} failed`);
process.exit(fail ? 1 : 0);