Files
chatkit/test/messenger-render.test.js
T
CatoandClaude e690b2254a Messenger: people talking to people, and to the agents among them (0.29.0) (#82)
* Messenger: people talking to people, and to the agents among them (0.26.0)

OrangeCat's direct messages were built in the app, so what was wrong with
them could not be fixed for the next product: an agent's answer in a
conversation between two people looked like the other person's, nothing
said an agent was taking part, top bar and a two-row box took half a
phone, and nothing could be attached. This is the messenger as a package.

@bitbaum/chatkit/messenger: Messenger (the whole screen) and its parts
useMessenger, ConversationList, MessageThread, MessageAttachments. The app
implements MessengerTransport; the package has no database, websocket or
storage. styles/messenger.css is the embedded sheet plus the Messenger's
section, without ChatApp.

- An agent is told from a person by the shape of its face, an Agent mark
  with whom it answered for, and an outlined bubble; the top bar says it
  is in the conversation; naming it shows that it is thinking, and says
  so if it never answers.
- Files: Camera, Photos, Videos, Files, checked against the attachment
  policy before an upload starts, photos shrunk in the browser, progress
  and Try again per file, a full-screen viewer, a film drawn as its still.
- The thread: runs, day lines, a New line, ticks from readState, a failed
  message kept with Try again, earlier messages loaded above with the
  reader's place kept, one bubble per message however often it arrives.
- Composer: layout="inline", attachmentsAlone, attach.take, attach.accept,
  a "videos" source, files dropped onto the box. Defaults unchanged.

The state is one pure function (messengerReducer), tested in Node.
test/browser-messenger.mjs drives demo/messenger.html at 390px, 320px
dark and 1280px. It found two defects before any product did: a read mark
that cleared the badge without telling the server, and a page of history
that pulled the reader to the end of the thread.

threadkit 0.1.2 (attachment policy, mention matcher, readState) is merged
and tagged but not on npm, so those rules are carried word for word in
src/thread-rules.ts with threadkit's own tests; the file says when it goes.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmsmMXvM981mcszxEA6

* test(messenger): wait for the opened conversation before reading its thread

Side by side, the conversation that was open can still be on screen with a
"New" line of its own, so the check read the old thread on a slow runner.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmsmMXvM981mcszxEA6

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-12 16:26:03 +01:00

306 lines
10 KiB
JavaScript

// The Messenger renders on the server, and its markup says what the screen
// must: who is an agent, whose message is whose, what was read. Rendered to a
// string; what needs a running browser is in test/browser-messenger.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 {
ConversationList,
MessageThread,
Messenger,
DEFAULT_MESSENGER_LABELS,
messengerLabels,
} from "../dist/react/messenger.js";
import { Composer } from "../dist/react/index.js";
import { AttachMenu } from "../dist/react/attach.js";
import { ATTACH_SOURCE_INPUT, MESSAGE_FILES_ACCEPT, acceptFamilies } from "../dist/index.js";
const START = new Date("2026-11-01T00:00:00Z");
const person = (actorId, name, extra = {}) => ({
actorId,
kind: "human",
name,
joinedAt: START,
visibleFrom: "thread-start",
...extra,
});
const CAT = { id: "cat", name: "Cat", mention: "@cat" };
const catSeat = {
actorId: "cat",
kind: "ai",
name: "Cat",
joinedAt: START,
visibleFrom: "thread-start",
};
const msg = (id, authorId, body, at, extra = {}) => ({
id,
threadId: "c1",
authorId,
body,
createdAt: new Date(at),
...extra,
});
const withCat = {
id: "c1",
createdAt: START,
participants: [
person("me", "Georgy"),
person("maya", "Maya Keller", { lastReadAt: new Date("2026-11-10T10:05:00Z") }),
catSeat,
],
};
const dm = {
id: "c1",
createdAt: START,
participants: [person("me", "Georgy"), person("maya", "Maya Keller")],
};
const thread = (conversation, messages, extra = {}) =>
renderToStaticMarkup(
h(MessageThread, {
conversation,
viewerId: "me",
messages,
agents: [CAT],
timeZone: "UTC",
locale: "en-GB",
...extra,
}),
);
test("an agent's message carries its name, the Agent mark and whom it answered for", () => {
const html = thread(withCat, [
msg("1", "me", "@cat who is right?", "2026-11-10T10:00:00Z"),
msg("2", "cat", "Maya is.", "2026-11-10T10:00:05Z", { onBehalfOf: "me" }),
]);
assert.match(html, /ck-msgr-from-agent/);
assert.match(
html,
/class="ck-speaker ck-speaker-agent">Cat<span class="ck-speaker-kind">Agent<\/span>/,
);
assert.match(html, /for Georgy/);
assert.match(html, /ck-msgr-avatar-agent/, "an agent's face has its own shape");
});
test("a person is never marked as an agent, and is named once an agent is in the room", () => {
const html = thread(withCat, [msg("1", "maya", "Penguins are fish.", "2026-11-10T10:00:00Z")]);
assert.match(html, /class="ck-speaker">Maya Keller<\/span>/);
assert.doesNotMatch(html, /ck-speaker-kind/);
assert.doesNotMatch(html, /ck-msgr-from-agent/);
});
test("between two people nobody needs naming", () => {
const html = thread(dm, [msg("1", "maya", "hello", "2026-11-10T10:00:00Z")]);
assert.doesNotMatch(html, /ck-speaker/);
});
test("an agent's words are markdown; a person's are exactly what was typed", () => {
const html = thread(withCat, [
msg("1", "cat", "They are **birds**.", "2026-11-10T10:00:00Z"),
msg("2", "maya", "They are **fish** <b>really</b>", "2026-11-10T10:10:00Z"),
]);
assert.match(html, /<strong>birds<\/strong>/);
assert.match(html, /They are \*\*fish\*\* &lt;b&gt;really&lt;\/b&gt;/);
});
test("a person's address is a link that opens elsewhere; a script address is not a link", () => {
const html = thread(dm, [
msg("1", "maya", "see https://example.org/x and javascript:alert(1)", "2026-11-10T10:00:00Z"),
]);
assert.match(html, /<a[^>]*href="https:\/\/example\.org\/x"/);
assert.doesNotMatch(html, /href="javascript:/);
});
test("the viewer's message says how far it got: sending, sent, read, not sent", () => {
const read = thread(withCat, [msg("1", "me", "hi", "2026-11-10T10:00:00Z")]);
assert.match(
read,
/aria-label="Read"/,
"the only other PERSON has read it; the agent is not waited for",
);
const sent = thread(withCat, [msg("1", "me", "hi", "2026-11-10T11:00:00Z")]);
assert.match(sent, /aria-label="Sent"/);
const sending = thread(dm, [
msg("t", "me", "hi", "2026-11-10T11:00:00Z", { clientId: "t", delivery: "sending" }),
]);
assert.match(sending, /aria-label="Sending"/);
const failed = thread(
dm,
[msg("t", "me", "hi", "2026-11-10T11:00:00Z", { clientId: "t", delivery: "failed" })],
{
onRetry: () => {},
onDiscard: () => {},
},
);
assert.match(failed, /Not sent\./);
assert.match(failed, />Try again<\/button>/);
assert.match(failed, />Delete<\/button>/);
});
test("pictures are tiles with their size known, a film is its still, a file is a download", () => {
const html = thread(dm, [
msg("1", "maya", "", "2026-11-10T10:00:00Z", {
attachments: [
{
id: "a",
kind: "image",
name: "cat.jpg",
mimeType: "image/jpeg",
bytes: 1000,
width: 1200,
height: 900,
url: "/a.jpg",
},
],
}),
msg("2", "maya", "", "2026-11-10T10:10:00Z", {
attachments: [
{
id: "b",
kind: "video",
name: "hike.mp4",
mimeType: "video/mp4",
bytes: 9_000_000,
durationMs: 42_000,
url: "/b.mp4",
posterUrl: "/b.jpg",
},
{
id: "c",
kind: "file",
name: "route.pdf",
mimeType: "application/pdf",
bytes: 184_320,
url: "/c.pdf",
},
],
}),
]);
assert.match(html, /<img src="\/a\.jpg"[^>]*width="288"[^>]*height="216"/);
assert.match(html, /<img src="\/b\.jpg"/, "the film's still is drawn");
assert.doesNotMatch(html, /<video/, "no film is loaded to draw a tile");
assert.match(html, /0:42/);
assert.match(html, /<a class="ck-msgr-file" href="\/c\.pdf" download="route\.pdf"/);
assert.match(html, /180 KB/);
});
test("an agent that was asked is shown thinking, and then as not having answered", () => {
const thinking = thread(withCat, [msg("1", "me", "@cat ?", "2026-11-10T10:00:00Z")], {
awaited: [{ agentId: "cat", state: "thinking" }],
});
assert.match(thinking, /Cat is thinking…/);
const quiet = thread(withCat, [msg("1", "me", "@cat ?", "2026-11-10T10:00:00Z")], {
awaited: [{ agentId: "cat", state: "quiet" }],
onDismissAgent: () => {},
});
assert.match(quiet, /Cat has not answered\. Ask again in a moment\./);
assert.match(quiet, />Dismiss<\/button>/);
});
test("an empty conversation says so, and how to bring the agent in", () => {
const html = thread(dm, []);
assert.match(html, /No messages yet\. Say hello\./);
assert.match(html, /Type @cat to bring Cat in\. Everyone here sees its answer\./);
});
test("the list: one row anatomy, an unread count a screen reader can hear, the agent on the face", () => {
const html = renderToStaticMarkup(
h(ConversationList, {
viewerId: "me",
agents: [CAT],
selectedId: "c1",
onSelect: () => {},
onNew: () => {},
timeZone: "UTC",
locale: "en-GB",
conversations: [
{
...withCat,
last: { body: "Maya is.", authorId: "cat", createdAt: new Date("2026-11-10T10:00:00Z") },
unread: 2,
},
],
}),
);
assert.match(html, /<h1 class="ck-msgr-list-title">Messages<\/h1>/);
assert.match(html, /aria-current="true"/);
assert.match(html, /Maya Keller/);
assert.match(html, /aria-label="2 unread"/);
assert.match(html, /ck-msgr-row-agent/);
assert.match(html, />New<\/button>/);
});
test("the whole Messenger renders without a browser", () => {
const transport = {
conversations: async () => [],
messages: async () => ({ messages: [], before: null }),
send: async () => {
throw new Error("not in this test");
},
};
const html = renderToStaticMarkup(h(Messenger, { viewer: { id: "me" }, transport }));
assert.match(html, /class="ck-msgr" data-pane="list"/);
assert.match(html, /Messages/);
assert.doesNotMatch(html, />New<\/button>/, "no people search, no New");
});
test("labels: a product overrides one word of a group and keeps the rest", () => {
const labels = messengerLabels({ title: "Nachrichten", preview: { photo: "Foto" } });
assert.equal(labels.title, "Nachrichten");
assert.equal(labels.preview.photo, "Foto");
assert.equal(labels.preview.video, DEFAULT_MESSENGER_LABELS.preview.video);
});
test("the inline composer is the same box with one more class; the default is unchanged", () => {
// Where the microphone sits needs a browser that has one: see
// test/browser-messenger.mjs.
const html = renderToStaticMarkup(
h(Composer, { onSend: () => {}, placeholder: "Message", layout: "inline" }),
);
assert.match(html, /class="ck-composer ck-composer-inline"/);
assert.match(html, /<textarea[^>]*class="ck-input"/);
const stacked = renderToStaticMarkup(h(Composer, { onSend: () => {}, placeholder: "Ask" }));
assert.match(stacked, /class="ck-composer"/);
assert.doesNotMatch(stacked, /ck-composer-inline/);
});
test("videos are their own source, and every source still asks for one family", () => {
const html = renderToStaticMarkup(
h(AttachMenu, {
onFiles: () => {},
sources: ["camera", "photos", "videos", "files"],
accept: { files: MESSAGE_FILES_ACCEPT },
}),
);
assert.match(
html,
/data-ck-source="videos"[^>]*accept="video\/\*"|accept="video\/\*"[^>]*data-ck-source="videos"/,
);
assert.match(html, />Videos</);
assert.deepEqual(acceptFamilies(ATTACH_SOURCE_INPUT.videos.accept), ["video"]);
assert.deepEqual(acceptFamilies(MESSAGE_FILES_ACCEPT), ["document"]);
assert.ok(
html.includes(MESSAGE_FILES_ACCEPT),
"the Files source asks for a messenger's documents",
);
});
test("an app that set its attach labels before Videos existed still gets a word for it", () => {
const html = renderToStaticMarkup(
h(AttachMenu, {
onFiles: () => {},
sources: ["videos"],
rows: [{ id: "x", label: "Extra", onPick: () => {} }],
labels: {
attach: "Anhängen",
title: "Hinzufügen",
close: "Schliessen",
sources: { camera: "Kamera", photos: "Fotos", files: "Dateien" },
},
}),
);
assert.match(html, />Videos</);
});