Files
piik/tests/channel-chat-export.test.ts
T
AAA 64e6281eae feat(i18n): replace visual mode with more languages
Use one text catalog across the 3.0 launcher, rooms and shared controls. Add Traditional Chinese, Japanese and Korean, expose them through the third language control, and remove the separate visual-mode state and rendering paths. Preserve ordinary icons, illustrations and motion.
2026-11-14 10:40:23 -04:00

48 lines
2.7 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,
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();
});