mirror of
https://github.com/bitbaum/chatkit.git
synced 2026-12-04 04:47:19 +01:00
On OrangeCat, the Cat's chat rail sat beside the app's Bots list: two lists taking half the screen. `hostList` drops the rail, the drawer and the Chats button; `onListChange` hands the product the rows, the load state, the open chat and the busy chats, so it can nest them under the bot in one lane. Opening and starting chats already go through the handle. busyIds is re-created only when its members change, so a host that keeps the list in state does not render in a loop. Claude-Session: https://claude.ai/code/session_019MDtmtCeJSbD69gBDGaLAE Co-authored-by: Claude <noreply@anthropic.com>
338 lines
12 KiB
JavaScript
338 lines
12 KiB
JavaScript
// What the first four products to adopt ChatApp could not do with it (0.19):
|
||
// each test is one gap a product had to work around in its own code.
|
||
// Behaviour that needs a running React (the send handle, Retry with the
|
||
// pictures, Stop dropping an unchecked half answer, StrictMode, a temporary
|
||
// chat ending) is driven in a real browser by test/browser-app.mjs.
|
||
import { test } from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import { createElement as h } from "react";
|
||
import { renderToStaticMarkup } from "react-dom/server";
|
||
import { memoryConversationStore } from "../dist/index.js";
|
||
import {
|
||
AttachMenu,
|
||
ChatApp,
|
||
ChatHistory,
|
||
ChatMessage,
|
||
ChatStarters,
|
||
ChatThread,
|
||
ChatTurnError,
|
||
Composer,
|
||
DEFAULT_CHAT_APP_LABELS,
|
||
chatTurnFailure,
|
||
starterParts,
|
||
takeDeepLink,
|
||
} from "../dist/react/index.js";
|
||
|
||
test("send handle: ChatApp takes a ref (forwardRef) and still renders on the server", () => {
|
||
const ref = { current: null };
|
||
const html = renderToStaticMarkup(
|
||
h(ChatApp, {
|
||
ref,
|
||
transport: async () => "ok",
|
||
store: memoryConversationStore(),
|
||
initialMessage: { text: "from the home box" },
|
||
deepLinkParam: "q",
|
||
}),
|
||
);
|
||
assert.match(html, /class="ck-input"/);
|
||
// Nothing is sent or filled on the server: the arrival waits for the list.
|
||
assert.doesNotMatch(html, /from the home box/);
|
||
});
|
||
|
||
test("deep link: the words and the address without them; nothing when absent", () => {
|
||
assert.deepEqual(takeDeepLink("https://x.ch/chat?q=Hello%20there&tab=2#a", "q"), {
|
||
text: "Hello there",
|
||
href: "https://x.ch/chat?tab=2#a",
|
||
});
|
||
assert.equal(takeDeepLink("https://x.ch/chat?tab=2", "q"), null);
|
||
assert.equal(takeDeepLink("not a url", "q"), null);
|
||
assert.equal(takeDeepLink("https://x.ch/?q=" + "a".repeat(5000), "q").text.length, 4000);
|
||
assert.equal(takeDeepLink("https://x.ch/?q=%20%20", "q").text, "", "blank is not a message");
|
||
});
|
||
|
||
test("failure: a ChatTurnError's words and kind travel; a raw error's never do", () => {
|
||
assert.deepEqual(
|
||
chatTurnFailure(new ChatTurnError("No model here reads pictures.", { kind: "no-vision" })),
|
||
{
|
||
message: "No model here reads pictures.",
|
||
kind: "no-vision",
|
||
},
|
||
);
|
||
assert.deepEqual(chatTurnFailure({ message: "Quota used up.", kind: "quota" }), {
|
||
message: "Quota used up.",
|
||
kind: "quota",
|
||
});
|
||
assert.deepEqual(
|
||
chatTurnFailure(new TypeError("Failed to fetch")),
|
||
{},
|
||
"a raw error is not shown",
|
||
);
|
||
assert.deepEqual(
|
||
chatTurnFailure(new TypeError("Failed to fetch"), () => "You are offline."),
|
||
{ message: "You are offline." },
|
||
);
|
||
const e = new ChatTurnError("x", { cause: new Error("inner") });
|
||
assert.ok(e instanceof Error && e.name === "ChatTurnError" && e.cause instanceof Error);
|
||
});
|
||
|
||
test("failure: each failed bubble keeps its own reason", () => {
|
||
const html = renderToStaticMarkup(
|
||
h(ChatThread, {
|
||
messages: [
|
||
{ id: "u1", role: "user", content: "one" },
|
||
{
|
||
id: "a1",
|
||
role: "assistant",
|
||
content: "",
|
||
failed: true,
|
||
failure: { message: "First reason." },
|
||
},
|
||
{ id: "u2", role: "user", content: "two" },
|
||
{
|
||
id: "a2",
|
||
role: "assistant",
|
||
content: "",
|
||
failed: true,
|
||
failure: { message: "Second reason." },
|
||
},
|
||
{ id: "u3", role: "user", content: "three" },
|
||
{ id: "a3", role: "assistant", content: "", failed: true },
|
||
],
|
||
labels: { failed: "Default failure." },
|
||
}),
|
||
);
|
||
assert.match(html, /First reason\./);
|
||
assert.match(html, /Second reason\./);
|
||
assert.match(html, /Default failure\./);
|
||
});
|
||
|
||
test("renderFooter knows which answer is the latest, and where each one is", () => {
|
||
const seen = [];
|
||
renderToStaticMarkup(
|
||
h(ChatThread, {
|
||
messages: [
|
||
{ id: "u1", role: "user", content: "a" },
|
||
{ id: "a1", role: "assistant", content: "one" },
|
||
{ id: "u2", role: "user", content: "b" },
|
||
{ id: "a2", role: "assistant", content: "two" },
|
||
],
|
||
renderFooter: (m, info) => {
|
||
if (m.role === "assistant") seen.push([m.id, info.isLatest, info.index]);
|
||
return null;
|
||
},
|
||
}),
|
||
);
|
||
assert.deepEqual(seen, [
|
||
["a1", false, 1],
|
||
["a2", true, 3],
|
||
]);
|
||
});
|
||
|
||
test("starters: a label apart from what it sends, and the language it is in", () => {
|
||
assert.deepEqual(starterParts("Plain"), { label: "Plain", prompt: "Plain" });
|
||
assert.deepEqual(starterParts({ text: "Grüezi mitenand", lang: "gsw" }), {
|
||
label: "Grüezi mitenand",
|
||
prompt: "Grüezi mitenand",
|
||
lang: "gsw",
|
||
});
|
||
assert.deepEqual(starterParts({ label: "Shorter", prompt: "Make it shorter." }), {
|
||
label: "Shorter",
|
||
prompt: "Make it shorter.",
|
||
lang: undefined,
|
||
});
|
||
const html = renderToStaticMarkup(
|
||
h(ChatStarters, { starters: ["Hi", { text: "Grüezi mitenand", lang: "gsw" }], onPick() {} }),
|
||
);
|
||
assert.match(html, /lang="gsw"[^>]*>Grüezi mitenand</);
|
||
assert.equal((html.match(/lang=/g) ?? []).length, 1, "only the starter that has one");
|
||
});
|
||
|
||
test("chat list: the product's line under the search, and its own empty state", () => {
|
||
const props = {
|
||
conversations: [],
|
||
state: "ready",
|
||
activeId: null,
|
||
busyIds: new Set(),
|
||
onOpen() {},
|
||
onNew() {},
|
||
onRename() {},
|
||
onRemove() {},
|
||
onRetry() {},
|
||
labels: DEFAULT_CHAT_APP_LABELS,
|
||
};
|
||
const html = renderToStaticMarkup(
|
||
h(ChatHistory, {
|
||
...props,
|
||
slot: h("p", null, "Only in this browser · Sign in to keep it"),
|
||
empty: h("p", null, "Ask something and it is kept here."),
|
||
}),
|
||
);
|
||
assert.match(html, /class="ck-history-slot"><p>Only in this browser/);
|
||
assert.ok(
|
||
html.indexOf("ck-history-search") < html.indexOf("ck-history-slot"),
|
||
"under the search",
|
||
);
|
||
assert.match(html, /Ask something and it is kept here/);
|
||
assert.doesNotMatch(html, /No chats yet/);
|
||
const app = renderToStaticMarkup(
|
||
h(ChatApp, {
|
||
transport: async () => "ok",
|
||
store: memoryConversationStore(),
|
||
historySlot: "Sign in to keep it",
|
||
railQuery: "all",
|
||
}),
|
||
);
|
||
// The rail is decided after mount; on the server the list is not drawn yet.
|
||
assert.match(app, /ck-app/);
|
||
});
|
||
|
||
test("attach sheet: the product's rows under Camera · Photos · Files, one anatomy", () => {
|
||
const rows = [
|
||
{ id: "things", label: "Your things", detail: "Listings and notes", onPick() {} },
|
||
{ id: "doc", label: "A document", onPick() {} },
|
||
];
|
||
const html = renderToStaticMarkup(
|
||
h(Composer, { onSend() {}, placeholder: "Ask", attach: true, attachExtras: rows }),
|
||
);
|
||
assert.match(html, /data-ck-source="files"/);
|
||
assert.equal((html.match(/class="ck-attach-extra"/g) ?? []).length, 2);
|
||
assert.match(html, /data-ck-extra="things"/);
|
||
assert.match(html, /Listings and notes/);
|
||
assert.ok(
|
||
html.indexOf("ck-attach-sources") < html.indexOf("ck-attach-extra"),
|
||
"under the sources",
|
||
);
|
||
// With attachments off, the paperclip opens a sheet of only the rows.
|
||
const only = renderToStaticMarkup(
|
||
h(Composer, { onSend() {}, placeholder: "Ask", attach: false, attachExtras: rows }),
|
||
);
|
||
assert.doesNotMatch(only, /ck-attach-source/);
|
||
assert.match(only, /ck-sheet-only/);
|
||
assert.match(only, /aria-label="Attach a screenshot or file"/);
|
||
const none = renderToStaticMarkup(
|
||
h(Composer, { onSend() {}, placeholder: "Ask", attach: false }),
|
||
);
|
||
assert.doesNotMatch(none, /Attach a screenshot/, "no rows, no attachments: no paperclip");
|
||
const menu = renderToStaticMarkup(h(AttachMenu, { onFiles() {}, rows }));
|
||
assert.match(menu, /ck-attach-extra/);
|
||
});
|
||
|
||
test("sent bubble: the product's chips beside the pictures, a link when it has one", () => {
|
||
const html = renderToStaticMarkup(
|
||
h(ChatMessage, {
|
||
message: {
|
||
id: "u",
|
||
role: "user",
|
||
content: "What about these?",
|
||
attachments: [{ kind: "text", name: "notes.txt" }],
|
||
chips: [{ label: "My bike listing", href: "/listings/1" }, { label: "Budget note" }],
|
||
},
|
||
}),
|
||
);
|
||
assert.match(html, /notes\.txt/);
|
||
assert.match(html, /<a href="\/listings\/1" class="ck-attach-file ck-turn-chip">/);
|
||
assert.match(html, /Budget note/);
|
||
});
|
||
|
||
test("attach.readFile: the app's reader decides first, declines with undefined, errors become a note", async () => {
|
||
const { readAttachmentFiles } = await import("../dist/react/index.js");
|
||
const pdf = new File(["%PDF-1.4"], "deck.pdf", { type: "application/pdf" });
|
||
const seen = [];
|
||
const readFile = async (file, ctx) => {
|
||
seen.push({ images: ctx.images, room: ctx.room, max: ctx.limits.maxFiles });
|
||
ctx.progress("Reading…");
|
||
if (file.name === "boom.pdf") throw new Error("locked");
|
||
if (file.name !== "deck.pdf") return undefined;
|
||
return {
|
||
attachments: [
|
||
{ kind: "image", name: "deck-1.jpg", mimeType: "image/jpeg", dataBase64: "AAAA" },
|
||
{ kind: "image", name: "deck-2.jpg", mimeType: "image/jpeg", dataBase64: "BBBB" },
|
||
{ kind: "text", name: "extra.txt", content: "x" },
|
||
],
|
||
note: "Pages 1–8 of 17.",
|
||
};
|
||
};
|
||
const progress = [];
|
||
const got = await readAttachmentFiles(
|
||
pdf,
|
||
{ maxFiles: 5 },
|
||
{},
|
||
{
|
||
readFile,
|
||
images: 1,
|
||
room: 2,
|
||
progress: (n) => progress.push(n),
|
||
},
|
||
);
|
||
assert.deepEqual(seen[0], { images: 1, room: 2, max: 5 });
|
||
assert.deepEqual(progress, ["Reading…"]);
|
||
assert.equal(got.note, "Pages 1–8 of 17.");
|
||
assert.equal(got.attachments.length, 2, "never more than the room left");
|
||
|
||
const boom = await readAttachmentFiles(
|
||
new File(["x"], "boom.pdf", { type: "application/pdf" }),
|
||
{},
|
||
{},
|
||
{ readFile },
|
||
);
|
||
assert.deepEqual(boom, { attachments: [], note: "Could not read boom.pdf." });
|
||
|
||
// Declined: chatkit's own reader judges it, and a PDF is still refused
|
||
// without a reader of the app's.
|
||
const declined = await readAttachmentFiles(
|
||
new File(["x"], "other.pdf", { type: "application/pdf" }),
|
||
{},
|
||
{},
|
||
{ readFile },
|
||
);
|
||
assert.equal(declined.attachments.length, 0);
|
||
assert.match(declined.note, /other\.pdf/);
|
||
const none = await readAttachmentFiles(pdf);
|
||
assert.match(none.note, /deck\.pdf/);
|
||
});
|
||
|
||
test("attach.sources: a text-only app refuses a picture with the wrongType note", async () => {
|
||
const { readAttachmentFiles } = await import("../dist/react/index.js");
|
||
const { acceptsImages } = await import("../dist/index.js");
|
||
assert.equal(acceptsImages(undefined), true);
|
||
assert.equal(acceptsImages(["camera", "files"]), true);
|
||
assert.equal(acceptsImages(["files"]), false);
|
||
const png = new File([new Uint8Array(8)], "shot.png", { type: "image/png" });
|
||
const notes = { wrongType: (n) => `${n}: text only` };
|
||
const refused = await readAttachmentFiles(png, {}, notes, { noImages: true });
|
||
assert.deepEqual(refused, { attachments: [], note: "shot.png: text only" });
|
||
// Not a picture: it takes the normal path (FileReader is the browser's; the
|
||
// browser checks cover a real text file).
|
||
const pdf = new File(["%PDF"], "a.pdf", { type: "application/pdf" });
|
||
assert.equal(
|
||
(await readAttachmentFiles(pdf, {}, notes, { noImages: true })).note,
|
||
"a.pdf: text only",
|
||
);
|
||
});
|
||
|
||
test("host list: no rail, no drawer and no Chats button when the product draws the list", () => {
|
||
const own = renderToStaticMarkup(
|
||
h(ChatApp, { transport: async () => "ok", store: memoryConversationStore() }),
|
||
);
|
||
assert.match(
|
||
own,
|
||
/aria-label="Chats"|ck-app-drawer|ck-app-rail/,
|
||
"the default keeps its own list",
|
||
);
|
||
const hosted = renderToStaticMarkup(
|
||
h(ChatApp, {
|
||
transport: async () => "ok",
|
||
store: memoryConversationStore(),
|
||
hostList: true,
|
||
onListChange: () => {},
|
||
}),
|
||
);
|
||
assert.doesNotMatch(hosted, /ck-app-rail|ck-app-drawer/);
|
||
assert.doesNotMatch(
|
||
hosted,
|
||
new RegExp(`aria-label="${DEFAULT_CHAT_APP_LABELS.openChats}"`),
|
||
"no button opens a list that is not here",
|
||
);
|
||
assert.match(hosted, /class="ck-input"/, "the chat itself is still drawn");
|
||
});
|