feat(webui): polish tabbed previews and image presentation

This commit is contained in:
Xubin Ren committed 2026-09-24 15:17:05 +08:00
1 parent fccf415283
commit b555164a17
41 files changed
+1459 -621

No files matched your search

+27 -17
View File
@@ -16,22 +16,28 @@ describe("File actions", () => {
it("resolves paths only when opened and uses the same menu for both copies", async () => {
const user = userEvent.setup();
const resolveMetadata = vi.fn().mockResolvedValue(details);
const openPreview = vi.fn();
render(<FileActionsProvider value={{ resolveMetadata, openPreview }}>
render(<FileActionsProvider value={{ resolveMetadata }}>
<FileReferenceChip path="notes.md:12" onOpen={vi.fn()} />
</FileActionsProvider>);
expect(resolveMetadata).not.toHaveBeenCalled();
await user.click(screen.getByRole("button", { name: "File actions for notes.md:12" }));
expect(screen.queryByRole("button", { name: /File actions/ })).not.toBeInTheDocument();
fireEvent.contextMenu(screen.getByRole("button", { name: "notes.md:12" }), { clientX: 100, clientY: 200 });
await waitFor(() => expect(screen.getByRole("menuitem", { name: "Copy absolute path" })).not.toHaveAttribute("data-disabled"));
expect(resolveMetadata).toHaveBeenCalledWith("notes.md:12");
expect(screen.queryByRole("menuitem", { name: "Preview" })).not.toBeInTheDocument();
expect(screen.queryByText("Paths refer to the gateway machine.")).not.toBeInTheDocument();
expect(screen.getAllByRole("menuitem")).toHaveLength(2);
await user.click(screen.getByRole("menuitem", { name: "Copy absolute path" }));
expect(copyTextToClipboard).toHaveBeenLastCalledWith(details.path);
expect(await screen.findByText("Copied")).toBeVisible();
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(screen.getByRole("button", { name: "notes.md:12" })).toHaveFocus();
fireEvent.contextMenu(screen.getByRole("button", { name: "notes.md:12" }));
await waitFor(() => expect(screen.getByRole("menuitem", { name: "Copy relative path" })).not.toHaveAttribute("data-disabled"));
await user.click(screen.getByRole("menuitem", { name: "Copy relative path" }));
expect(copyTextToClipboard).toHaveBeenLastCalledWith("notes.md");
await user.click(screen.getByRole("menuitem", { name: "Preview" }));
expect(openPreview).toHaveBeenCalledWith("notes.md:12");
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
await user.keyboard("{Escape}");
await waitFor(() => expect(screen.queryByRole("menu")).not.toBeInTheDocument());
expect(screen.getByRole("button", { name: "notes.md:12" })).toHaveFocus();
});
it("keeps primary click working and supports context menu and Shift+F10", async () => {
@@ -43,17 +49,21 @@ describe("File actions", () => {
expect(onOpen).toHaveBeenCalledTimes(1);
expect(onOpen).toHaveBeenCalledWith("src/app.ts");
fireEvent.contextMenu(reference);
expect(await screen.findByRole("menuitem", { name: "Preview" })).toBeVisible();
expect(await screen.findByRole("menuitem", { name: "Copy reference" })).toBeVisible();
await user.keyboard("{Escape}");
fireEvent.keyDown(reference, { key: "F10", shiftKey: true });
expect(await screen.findByRole("menuitem", { name: "Copy reference" })).toBeVisible();
expect(onOpen).toHaveBeenCalledTimes(1);
await user.keyboard("{Escape}");
fireEvent.keyDown(reference, { key: "ContextMenu" });
expect(await screen.findByRole("menuitem", { name: "Copy reference" })).toBeVisible();
expect(onOpen).toHaveBeenCalledTimes(1);
});
it("does not invent a relative path for files outside the project", async () => {
const user = userEvent.setup();
render(<FileActions path="/media/image.png" metadata={{ path: "/media/image.png", relative_path: null }} />);
await user.click(screen.getByRole("button", { name: "File actions for image.png" }));
render(<FileActions path="/media/image.png" metadata={{ path: "/media/image.png", relative_path: null }}><button>image.png</button></FileActions>);
fireEvent.contextMenu(screen.getByRole("button", { name: "image.png" }));
expect(screen.getByRole("menuitem", { name: "Copy relative path" })).toHaveAttribute("data-disabled");
expect(screen.queryByRole("menuitem", { name: "Preview" })).not.toBeInTheDocument();
await user.click(screen.getByRole("menuitem", { name: "Copy absolute path" }));
@@ -63,10 +73,10 @@ describe("File actions", () => {
it("retains the original reference if resolution fails and reports clipboard errors", async () => {
const user = userEvent.setup();
vi.mocked(copyTextToClipboard).mockResolvedValue(false);
render(<FileActionsProvider value={{ resolveMetadata: vi.fn().mockRejectedValue(new Error("404")), openPreview: vi.fn() }}>
render(<FileActionsProvider value={{ resolveMetadata: vi.fn().mockRejectedValue(new Error("404")) }}>
<FileReferenceChip path="missing.txt" onOpen={vi.fn()} />
</FileActionsProvider>);
await user.click(screen.getByRole("button", { name: "File actions for missing.txt" }));
fireEvent.contextMenu(screen.getByRole("button", { name: "missing.txt" }));
expect(await screen.findByText(/Could not resolve this file/)).toBeVisible();
expect(screen.getByRole("menuitem", { name: "Copy absolute path" })).toHaveAttribute("data-disabled");
await user.click(screen.getByRole("menuitem", { name: "Copy reference" }));
@@ -77,11 +87,11 @@ describe("File actions", () => {
it("ignores late metadata from a previous session", async () => {
const user = userEvent.setup();
let resolveOld!: (value: FileReferenceMetadata) => void;
const previous = { resolveMetadata: () => new Promise<FileReferenceMetadata>((resolve) => { resolveOld = resolve; }), openPreview: vi.fn() };
const current = { resolveMetadata: vi.fn().mockResolvedValue({ path: "/new/notes.md", relative_path: "notes.md" }), openPreview: vi.fn() };
const previous = { resolveMetadata: () => new Promise<FileReferenceMetadata>((resolve) => { resolveOld = resolve; }) };
const current = { resolveMetadata: vi.fn().mockResolvedValue({ path: "/new/notes.md", relative_path: "notes.md" }) };
const view = (value: typeof current | typeof previous) => <FileActionsProvider value={value}><FileReferenceChip path="notes.md" onOpen={vi.fn()} /></FileActionsProvider>;
const { rerender } = render(view(previous));
await user.click(screen.getByRole("button", { name: "File actions for notes.md" }));
fireEvent.contextMenu(screen.getByRole("button", { name: "notes.md" }));
rerender(view(current));
await waitFor(() => expect(current.resolveMetadata).toHaveBeenCalled());
await act(async () => resolveOld(details));
@@ -91,10 +101,10 @@ describe("File actions", () => {
it("degrades to the original reference on an older gateway response", async () => {
const user = userEvent.setup();
render(<FileActionsProvider value={{ resolveMetadata: vi.fn().mockResolvedValue({ path: "/workspace/notes.md", content: "old preview response" }), openPreview: vi.fn() }}>
render(<FileActionsProvider value={{ resolveMetadata: vi.fn().mockResolvedValue({ path: "/workspace/notes.md", content: "old preview response" }) }}>
<FileReferenceChip path="notes.md" onOpen={vi.fn()} />
</FileActionsProvider>);
await user.click(screen.getByRole("button", { name: "File actions for notes.md" }));
fireEvent.contextMenu(screen.getByRole("button", { name: "notes.md" }));
expect(await screen.findByText(/Could not resolve this file/)).toBeVisible();
expect(screen.getByRole("menuitem", { name: "Copy absolute path" })).toHaveAttribute("data-disabled");
await user.click(screen.getByRole("menuitem", { name: "Copy reference" }));