feat(webui): refine mobile message actions and hover timestamps

This commit is contained in:
Xubin Ren committed 2026-09-24 15:17:05 +08:00
1 parent 4693bba81f
commit 7aac9355ec
10 files changed
+334 -73

No files matched your search

+28
View File
@@ -4,6 +4,7 @@ import { setAppLanguage } from "@/i18n";
import {
fmtDateTime,
formatMessageEndTime,
formatMessageHoverTime,
formatTurnLatency,
relativeTime,
} from "@/lib/format";
@@ -112,4 +113,31 @@ describe("localized format helpers", () => {
expect(minutePlus).toContain("m");
expect(minutePlus).toContain("s");
});
it("uses a short 24-hour clock today and localized month/day for every other date", () => {
const now = new Date(2026, 8, 24, 16, 0);
expect(formatMessageHoverTime(new Date(2026, 8, 24, 14, 32).getTime(), "zh-CN", now)).toBe("14:32");
expect(formatMessageHoverTime(new Date(2026, 8, 24, 0, 5).getTime(), "en", now)).toBe("00:05");
expect(formatMessageHoverTime(new Date(2026, 8, 23, 14, 32).getTime(), "zh-CN", now)).toBe("9/23");
expect(formatMessageHoverTime(new Date(2026, 8, 23).getTime(), "fr", now)).toBe(
new Intl.DateTimeFormat("fr", { month: "numeric", day: "numeric" }).format(new Date(2026, 8, 23)),
);
expect(formatMessageHoverTime(new Date(2025, 8, 22).getTime(), "zh-CN", now)).toBe("9/22");
expect(formatMessageHoverTime(new Date(2026, 8, 25).getTime(), "zh-CN", now)).toBe("9/25");
});
it.each([
[new Date(2026, 0, 1, 0, 30), new Date(2025, 11, 31, 23, 55)],
[new Date(2026, 2, 1, 0, 30), new Date(2026, 1, 28, 23, 55)],
[new Date(2026, 2, 9, 0, 30), new Date(2026, 2, 8, 0, 15)],
[new Date(2026, 10, 2, 0, 30), new Date(2026, 10, 1, 23, 55)],
])("compares local calendar days across month/year and DST boundaries (%s)", (now, previousDay) => {
expect(formatMessageHoverTime(previousDay.getTime(), "en", now)).toBe(
new Intl.DateTimeFormat("en", { month: "numeric", day: "numeric" }).format(previousDay),
);
});
it.each([null, undefined, NaN, Infinity, 9e15])("omits invalid hover time %s", (value) => {
expect(formatMessageHoverTime(value)).toBe("");
});
});
@@ -0,0 +1,56 @@
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { ThreadMessages } from "@/components/thread/ThreadMessages";
import { fmtDateTime } from "@/lib/format";
vi.mock("@/components/MarkdownText", () => ({ MarkdownText: ({ content }: { content: string }) => <p>{content}</p> }));
afterEach(() => { cleanup(); vi.useRealTimers(); });
describe("desktop hover timestamp", () => {
it("uses user creation / assistant completion time and exposes the full date on keyboard focus", async () => {
vi.useFakeTimers({ toFake: ["Date"] });
vi.setSystemTime(new Date(2026, 8, 24, 16));
const createdAt = new Date(2026, 8, 23, 12).getTime();
const completedAt = new Date(2026, 8, 24, 14, 32).getTime();
const { container } = render(<ThreadMessages messages={[
{ id: "u", role: "user", content: "Question", createdAt },
{ id: "a", role: "assistant", content: "Answer", createdAt, completedAt },
]} />);
const times = container.querySelectorAll<HTMLTimeElement>("[data-message-hover-time]");
expect(times).toHaveLength(2);
expect(times[0]).toHaveTextContent("9/23");
expect(times[1]).toHaveTextContent("14:32");
expect(times[1]).toHaveAttribute("datetime", new Date(completedAt).toISOString());
expect(times[1]).toHaveAttribute("aria-label", fmtDateTime(completedAt));
fireEvent.focus(times[1]);
expect(await screen.findByRole("tooltip")).toHaveTextContent(fmtDateTime(completedAt));
expect(times[1].closest("[data-thread-display-unit]")).toHaveAttribute("data-context-block-active");
});
it("refreshes the short date when hovering again after midnight", () => {
vi.useFakeTimers({ toFake: ["Date"] });
const createdAt = new Date(2026, 11, 31, 23, 30).getTime();
vi.setSystemTime(new Date(2026, 11, 31, 23, 50));
const { container } = render(<ThreadMessages messages={[{ id: "a", role: "assistant", content: "Answer", createdAt }]} />);
const time = container.querySelector("[data-message-hover-time]")!;
const row = time.closest("[data-thread-display-unit]")!;
fireEvent.pointerEnter(row, { pointerType: "mouse" });
expect(time).toHaveTextContent("23:30");
fireEvent.pointerLeave(row, { pointerType: "mouse", relatedTarget: document.body });
vi.setSystemTime(new Date(2027, 0, 1, 0, 10));
fireEvent.pointerEnter(row, { pointerType: "mouse" });
expect(time).toHaveTextContent("12/31");
});
it("falls back to creation time and omits invalid timestamps", () => {
vi.useFakeTimers({ toFake: ["Date"] });
vi.setSystemTime(new Date(2026, 8, 24, 16));
const { container } = render(<ThreadMessages messages={[
{ id: "a", role: "assistant", content: "Answer", createdAt: new Date(2026, 8, 22).getTime(), completedAt: NaN },
{ id: "b", role: "assistant", content: "Unknown", createdAt: NaN },
]} />);
const times = container.querySelectorAll("[data-message-hover-time]");
expect(times).toHaveLength(1);
expect(times[0]).toHaveTextContent("9/22");
});
});
+71 -1
View File
@@ -7,7 +7,7 @@ import { useMessageWebLinks } from "@/components/MessageLinksMenu";
import { copyTextToClipboard } from "@/lib/clipboard";
vi.mock("@/lib/clipboard", () => ({ copyTextToClipboard: vi.fn(async () => true) }));
afterEach(() => { cleanup(); vi.mocked(copyTextToClipboard).mockReset().mockResolvedValue(true); });
afterEach(() => { cleanup(); vi.unstubAllGlobals(); vi.mocked(copyTextToClipboard).mockReset().mockResolvedValue(true); });
function view(content: string, openPreview?: (url: string) => void) {
return <WebPreviewContext.Provider value={openPreview}>
@@ -22,6 +22,76 @@ async function openLinks() {
}
describe("message link actions", () => {
function mobileViewport() {
const listeners = new Set<() => void>();
let narrow = true;
vi.stubGlobal("matchMedia", vi.fn((query: string) => ({
get matches() { return query === "(max-width: 767px)" && narrow; },
media: query,
addEventListener: (_event: string, listener: () => void) => listeners.add(listener),
removeEventListener: (_event: string, listener: () => void) => listeners.delete(listener),
})));
return (value: boolean) => act(() => { narrow = value; listeners.forEach(listener => listener()); });
}
it("puts mobile actions after the reply and opens one bottom sheet with working back, copy and preview", async () => {
mobileViewport();
const preview = vi.fn();
const content = "[Website](https://example.com/demo)";
const { container } = render(view(content, preview));
const link = await screen.findByRole("link", { name: /Website/ });
const footer = container.querySelector("[data-message-mobile-actions]")!;
expect(link.compareDocumentPosition(footer) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
expect(container.querySelector(".message-block-menu-trigger")).not.toBeInTheDocument();
expect(container.querySelector("[data-message-hover-time]")).not.toBeInTheDocument();
fireEvent.click(within(footer as HTMLElement).getByRole("button", { name: "Copy" }));
await waitFor(() => expect(copyTextToClipboard).toHaveBeenCalledWith(content));
const menu = await openLinks();
expect(menu).toHaveAttribute("data-message-mobile-sheet");
expect(screen.getAllByRole("dialog")).toHaveLength(1);
fireEvent.click(within(menu).getByRole("button", { name: "Back" }));
expect(within(menu).queryByRole("button", { name: "Copy" })).not.toBeInTheDocument();
fireEvent.click(within(menu).getByRole("button", { name: "Close" }));
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
expect(screen.getByRole("button", { name: "Message actions" })).toHaveFocus();
await openLinks();
fireEvent.click(screen.getByRole("button", { name: "Preview website" }));
expect(preview).toHaveBeenCalledWith("https://example.com/demo");
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
});
it("restores the desktop menu on resize without duplicating actions", async () => {
const resize = mobileViewport();
const { container } = render(view("[Website](https://example.com/demo)"));
await screen.findByRole("link", { name: /Website/ });
await openLinks();
resize(false);
await waitFor(() => expect(container.querySelector("[data-message-mobile-actions]")).not.toBeInTheDocument());
expect(screen.queryByRole("dialog")).not.toHaveAttribute("data-message-mobile-sheet");
expect(screen.getAllByRole("button", { name: "Message actions" })).toHaveLength(1);
// Resizing may close the modal as focus is returned; either way its trigger stays usable.
if (!screen.queryByRole("dialog")) fireEvent.click(screen.getByRole("button", { name: "Message actions" }));
expect(screen.getAllByRole("dialog")).toHaveLength(1);
});
it("keeps user actions outside the bubble and exposes a labelled mobile fork action", async () => {
mobileViewport();
const fork = vi.fn();
const { container } = render(<ThreadMessages onForkFromMessage={fork} messages={[
{ id: "user", role: "user", content: "A prompt", createdAt: 1 },
{ id: "reply", role: "assistant", content: "A reply", createdAt: 2 },
]} />);
await screen.findByText("A reply");
expect(container.querySelector("[data-user-text-bubble] [data-message-block-menu-trigger]")).toBeNull();
expect(container.querySelectorAll("[data-message-mobile-actions]")).toHaveLength(2);
fireEvent.click(screen.getAllByRole("button", { name: "Message actions" })[1]);
const menu = screen.getByRole("dialog");
expect(within(menu).getByRole("button", { name: "Fork" })).toHaveTextContent("Fork");
fireEvent.click(within(menu).getByRole("button", { name: "Fork" }));
expect(fork).toHaveBeenCalledWith(1);
await waitFor(() => expect(screen.queryByRole("dialog")).not.toBeInTheDocument());
});
it("collects rendered links, deduplicates destinations and ignores code, images and file references", async () => {
render(view("[First](https://example.com/) and [Again](https://example.com).\n\n[Second](https://example.org/demo)\n\nhttps://example.net/plain\n\n`https://code.invalid/inline`\n\n```text\nhttps://code.invalid/block\n```\n\n![Image](https://image.invalid/pic.png)\n\n[notes.txt](notes.txt)"));
await screen.findByRole("link", { name: "First" });