Files
piik/tests/channel-chat-export.test.ts
T
AAA c55205f92d feat(profiles): represent unnamed members across room surfaces
Use an empty stored name for unnamed members and one localized formatter for guest labels and same-list duplicates. Full member IDs retain authority; archived empty names remain distinct from unknown live profiles. Reuse the profile editor for an owner-controlled optional nickname prompt and remember dismissal per site and member. The current private Domain 55 phase carries the setting; existing stored headers default it off. Core, Browser, SDK and archive controls passed, including an actual two-browser hosted-room skip and reentry check.
2026-11-27 04:08:31 -05:00

55 lines
3.0 KiB
TypeScript

import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { beforeEach, expect, test, vi } from "vitest";
import { RoomChatExport } from "../src/client/channel/components/ChannelRoomControls";
import type { ChannelMessage } from "../src/client/channel/protocol";
import { downloadBlob } from "../src/client/lib/download";
import { en } from "../src/client/locales/en";
const actions = vi.hoisted(() => [] as { icon: string; label: string; disabled?: boolean; onClick?: () => void }[]);
vi.mock("../src/client/channel/components/ChannelAction", () => ({ ChannelAction: (props: typeof actions[number]) => {
actions.push(props);
return createElement("button", { disabled: props.disabled }, props.label);
} }));
vi.mock("../src/client/ui/copy", () => ({ isCopyKey: () => false, say: (key: keyof typeof en) => en[key],
useCopy: () => ({ lang: "en", t: (key: keyof typeof en) => en[key] }) }));
vi.mock("../src/client/lib/download", () => ({ downloadBlob: vi.fn() }));
beforeEach(() => { actions.length = 0; vi.clearAllMocks(); });
const message = (seq: number, text: string): ChannelMessage => ({
id: `private-message-${seq}`, userId: "private-user", seq, time: seq * 1000, text, attachments: [],
});
function render(messages: ChannelMessage[]) {
renderToStaticMarkup(createElement(RoomChatExport, {
messages, users: [{ id: "private-user", name: "朋友\nFriend" }],
}));
return actions.find(action => action.icon === "save")!;
}
test("exports the loaded window as readable UTF-8 text without fetching history, files or identifiers", async () => {
const action = render([message(7, "中文 👋\nSecond line"), { ...message(8, ""), attachments: [{
id: "private-file", name: "secret.txt", size: 1, mediaType: "application/octet-stream", sha256: "a".repeat(64),
}] }, { ...message(9, "Last"), userId: "departed-user", reactions: { revision: 1, counts: { heart: 1 } } }]);
expect(action.label).toBe("Export chat text (TXT)");
expect(action.disabled).toBe(false);
action.onClick!();
const [blob, filename] = vi.mocked(downloadBlob).mock.calls[0];
expect(blob.type).toBe("text/plain;charset=utf-8");
expect(filename).toMatch(/^piik-chat-.*\.txt$/);
expect(new Uint8Array(await blob.arrayBuffer()).slice(0, 3)).toEqual(new Uint8Array([239, 187, 191]));
expect(await blob.text()).toBe("Piik chat\r\n\r\n[1970-01-01T00:00:07.000Z] 朋友\nFriend\r\n中文 👋\nSecond line\r\n\r\n[1970-01-01T00:00:09.000Z] User\r\nLast\r\n");
});
test.each([{ messages: [] }, { messages: [message(1, "")] }])("disables export when the loaded window has no text: $messages", ({ messages }) => {
expect(render(messages).disabled).toBe(true);
expect(downloadBlob).not.toHaveBeenCalled();
});
test("copied unnamed authors retain their own label in the transcript", async () => {
const archived = { ...message(1, "Copied"), archivedAuthorName: "" };
render([archived]).onClick!();
const [blob] = vi.mocked(downloadBlob).mock.calls[0];
expect(await blob.text()).toMatch(/Guest #[2-9A-HJ-NP-Z]{4}\r\nCopied/);
});