// The components render on the server (Next RSC/SSR) and carry the parts the // standard requires. Rendered to a string: a chat that cannot even render // without a browser is a chat that breaks every Next page it is put on. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { createElement as h } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { Composer, ChatThread, ChatStarters, Markdown } from "../dist/react/index.js"; import { AttachMenu, ATTACH_SOURCE_INPUT, shrinkImage } from "../dist/react/attach.js"; test("composer: textarea, send, and 16px guaranteed by the stylesheet", () => { const html = renderToStaticMarkup(h(Composer, { onSend: () => {}, placeholder: "Ask" })); assert.match(html, /]*class="ck-input"/); assert.match(html, /aria-label="Send"/); const css = readFileSync(new URL("../styles.css", import.meta.url), "utf8"); assert.match( css, /\.ck-input\s*\{[^}]*font-size:\s*max\(16px/, "input must be >= 16px (iOS zoom)", ); assert.match(css, /\.ck-md\s*\{[^}]*font-size:\s*var\(--ck-text\)/); assert.match(css, /--ck-text:\s*1rem/); }); test("composer: the paperclip carries one input per source, not one mixed input", () => { const html = renderToStaticMarkup( h(Composer, { onSend: () => {}, placeholder: "Ask", attach: true }), ); assert.match(html, /]*data-ck-source="camera"[^>]*>/); assert.match( html, /]*accept="image\/\*"[^>]*capture="environment"|capture="environment"[^>]*accept="image\/\*"/, ); assert.match(html, /]*data-ck-source="photos"/); const files = /]*data-ck-source="files"[^>]*>/.exec(html)?.[0] ?? ""; assert.ok(files, "a Files input"); assert.doesNotMatch(files, /accept=/, "Files accepts anything: the real document browser"); assert.match(html, /aria-haspopup="dialog"/); }); test("composer: an attachment the APP holds can be sent with no words", () => { const send = (props) => renderToStaticMarkup(h(Composer, { onSend: () => {}, placeholder: "Ask", ...props })); const sendButton = (html) => /]*aria-label="Send"[^>]*>/.exec(html)?.[0] ?? ""; // Nothing typed, nothing held: nothing to send. assert.match(sendButton(send({ attachmentOnlyText: "See attached" })), /disabled/); // The app holds one (a reference, a staged photo): it is a message. assert.doesNotMatch( sendButton(send({ attachmentOnlyText: "See attached", heldAttachments: 1 })), /disabled/, ); // Held, but the app never said what such a message means: still not one. assert.match(sendButton(send({ heldAttachments: 1 })), /disabled/); }); test("composer: labels are translatable (heidi is not in English)", () => { const html = renderToStaticMarkup( h(Composer, { onSend: () => {}, placeholder: "Frag", labels: { send: "Senden" } }), ); assert.match(html, /aria-label="Senden"/); }); test("thread: names who speaks once two agents answer, and offers retry on the last answer", () => { const html = renderToStaticMarkup( h(ChatThread, { messages: [ { id: "1", role: "user", content: "I want to earn" }, { id: "2", role: "assistant", content: "Use **OrangeCat**.", speaker: { name: "Cat", id: "cat" }, }, { id: "3", role: "assistant", content: "I can build it.", speaker: { name: "Loki", id: "loki" }, }, ], onRetry: () => {}, }), ); assert.match(html, /ck-speaker">CatLokiOrangeCat<\/strong>/); assert.equal((html.match(/Try again/g) ?? []).length, 1, "retry only on the last answer"); assert.match(html, /ck-from-cat/); }); test("thread: a failed turn says so, with retry — never silence", () => { const html = renderToStaticMarkup( h(ChatThread, { messages: [ { id: "1", role: "user", content: "hi" }, { id: "2", role: "assistant", content: "", failed: true }, ], onRetry: () => {}, }), ); assert.match(html, /ck-failed/); assert.match(html, /ck-turn-pinned/, "a failed turn never hides its Retry behind a hover"); assert.match(html, /Try again/); }); test("thread: empty shows the starters, live shows stop", () => { const empty = renderToStaticMarkup( h(ChatThread, { messages: [], empty: h(ChatStarters, { starters: ["What can you build?"], onPick: () => {} }), }), ); assert.match(empty, /What can you build\?/); const live = renderToStaticMarkup( h(ChatThread, { messages: [{ id: "1", role: "user", content: "x" }], live: {}, onStop: () => {}, }), ); assert.match(live, /ck-stop/); }); test("markdown never renders model text as HTML", () => { const html = renderToStaticMarkup(h(Markdown, { text: " **ok**" })); assert.ok(!html.includes(" { assert.equal(typeof shrinkImage, "function"); assert.ok(ATTACH_SOURCE_INPUT.files); const html = renderToStaticMarkup( h(AttachMenu, { onFiles: () => {}, sources: ["camera", "photos"], trigger: "Add photo" }), ); assert.match(html, /data-ck-source="camera"/); assert.match(html, /data-ck-source="photos"/); assert.doesNotMatch(html, /data-ck-source="files"/, "an image uploader offers no Files"); assert.match(html, /class="ck-attach-trigger"[^>]*>Add photo