feat(webui): polish the GitHub star invitation

This commit is contained in:
Xubin Ren committed 2026-09-28 00:48:05 +08:00
1 parent 33dddaaa46
commit 57a4655451
15 files changed
+257 -64

No files matched your search

+78 -5
View File
@@ -1,9 +1,11 @@
import { act, cleanup, fireEvent, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { StarLink, StarPrompt } from "@/components/StarPrompt";
import { starPromptAction } from "@/lib/api";
import i18n from "@/i18n";
import { supportedLocales } from "@/i18n/config";
vi.mock("@/lib/api", () => ({ starPromptAction: vi.fn() }));
const client = {
@@ -27,8 +29,8 @@ async function enter() {
it("claims once on entry and lets users skip without permanently dismissing", async () => {
const view = render(<StarPrompt ready />);
await enter();
expect(screen.getByRole("dialog")).toHaveAccessibleName("Thanks for using nanobot");
fireEvent.click(screen.getByRole("button", { name: "Not now" }));
expect(screen.getByRole("dialog")).toHaveAccessibleName("Glad we could help.");
fireEvent.click(screen.getByRole("button", { name: "Maybe later" }));
view.rerender(<StarPrompt ready />);
await enter();
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
@@ -77,10 +79,10 @@ it("keeps a failed permanent dismissal retryable", async () => {
render(<StarPrompt ready />);
await enter();
action.mockRejectedValueOnce(new Error("offline"));
await act(async () => fireEvent.click(screen.getByRole("button", { name: "Don’t ask again" })));
await act(async () => fireEvent.click(screen.getByRole("button", { name: "Don't ask again" })));
expect(screen.getByRole("alert")).toHaveTextContent("There may be a network issue. This reminder may appear again.");
expect(screen.getByRole("dialog")).toBeVisible();
await act(async () => fireEvent.click(screen.getByRole("button", { name: "Don’t ask again" })));
await act(async () => fireEvent.click(screen.getByRole("button", { name: "Don't ask again" })));
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(action).toHaveBeenLastCalledWith(client, "dismiss");
});
@@ -106,7 +108,78 @@ it("waits for the initial connection and still attempts only once", async () =>
expect(action).not.toHaveBeenCalled();
await act(async () => statusChanged("open"));
expect(screen.getByRole("dialog")).toBeVisible();
fireEvent.click(screen.getByRole("button", { name: "Not now" }));
fireEvent.click(screen.getByRole("button", { name: "Maybe later" }));
await act(async () => statusChanged("open"));
expect(action).toHaveBeenCalledTimes(1);
});
it.each(supportedLocales.map(({ code }) => code))("localizes the complete invitation in %s", async (locale) => {
await i18n.changeLanguage(locale);
render(<StarPrompt ready />);
await enter();
expect(screen.getByRole("dialog")).toHaveAccessibleName(i18n.t("starPrompt.title"));
expect(screen.getByRole("dialog")).toHaveAccessibleDescription([
i18n.t("starPrompt.intro"), i18n.t("starPrompt.invitation"), i18n.t("starPrompt.thanks"),
].join(" "));
expect(screen.getByRole("button", { name: i18n.t("starPrompt.later") })).toBeVisible();
expect(screen.getByRole("button", { name: i18n.t("starPrompt.never") })).toBeVisible();
expect(screen.getByRole("button", { name: i18n.t("common.close"), exact: true })).toBeVisible();
expect(screen.getByRole("link", { name: i18n.t("starPrompt.action") })).toHaveAttribute("rel", "noopener noreferrer");
});
it.each(["close button", "Escape"])("restores focus after %s without permanently dismissing", async (method) => {
const user = userEvent.setup();
const view = render(<><button>Previous control</button><StarPrompt ready={false} /></>);
screen.getByRole("button", { name: "Previous control" }).focus();
view.rerender(<><button>Previous control</button><StarPrompt ready /></>);
await enter();
expect(screen.getByRole("heading", { name: "Glad we could help." })).toHaveFocus();
if (method === "Escape") await user.keyboard("{Escape}");
else await user.click(screen.getByRole("button", { name: "Close", exact: true }));
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(screen.getByRole("button", { name: "Previous control" })).toHaveFocus();
expect(action).toHaveBeenCalledTimes(1);
});
it("keeps the illustration decorative and actions usable if it fails to load", async () => {
const view = render(<StarPrompt ready={false} />);
expect(document.querySelector("img")).toBeNull();
view.rerender(<StarPrompt ready />);
await enter();
const artwork = screen.getByRole("dialog").querySelector("img")!;
expect(artwork).toHaveAttribute("alt", "");
expect(artwork).toHaveAttribute("width", "1672");
expect(artwork).toHaveAttribute("height", "941");
expect(artwork.getAttribute("src")).toContain("star-invitation");
fireEvent.error(artwork);
fireEvent.click(screen.getByRole("button", { name: "Maybe later" }));
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
});
it("keeps the GitHub action a real link and retries failed preference saves", async () => {
render(<StarPrompt ready />);
await enter();
const link = screen.getByRole("link", { name: "Star on GitHub" });
expect(link).toHaveAttribute("href", "https://github.com/HKUDS/nanobot");
expect(link).toHaveAttribute("target", "_blank");
expect(link.querySelector(".star-prompt-decoration")).toHaveAttribute("aria-hidden", "true");
action.mockRejectedValueOnce(new Error("offline"));
await act(async () => fireEvent.click(link));
expect(screen.getByRole("alert")).toBeVisible();
expect(screen.getByRole("dialog")).toBeVisible();
await act(async () => fireEvent.click(link));
expect(screen.queryByRole("dialog")).not.toBeInTheDocument();
expect(action.mock.calls.map(([, command]) => command)).toEqual(["claim", "dismiss", "dismiss"]);
});
it.each([false, true])("preserves middle-click dismissal for the GitHub link (fullWidth: %s)", async (fullWidth) => {
const saved = vi.fn();
render(<StarLink fullWidth={fullWidth} onSaved={saved} />);
const link = screen.getByRole("link");
await act(async () => fireEvent(link, new MouseEvent("auxclick", { bubbles: true, button: 2 })));
expect(action).not.toHaveBeenCalled();
await act(async () => fireEvent(link, new MouseEvent("auxclick", { bubbles: true, button: 1 })));
expect(action).toHaveBeenCalledWith(client, "dismiss");
expect(saved).toHaveBeenCalledTimes(1);
if (!fullWidth) expect(link.querySelector(".star-prompt-decoration")).toBeNull();
});