feat(webui): add catalog loading skeletons (#6092)

* feat(webui): add catalog loading skeletons

* feat(webui): add MCP tool loading skeleton

* fix(webui): make skill discovery skeletons visible

* refactor(webui): share skeleton status markup
This commit is contained in:
chengyongru authored and GitHub committed 2026-10-07 21:28:53 +08:00
1 parent 7cb396788c
commit f24969d282
25 files changed
+538 -99

No files matched your search

+1 -1
View File
@@ -1490,7 +1490,7 @@ describe("App layout", () => {
screen.queryByRole("button", { name: "Open details for custom-skill" }),
).not.toBeInTheDocument();
});
expect(screen.getByText("No matching skills.")).toBeInTheDocument();
expect(screen.getByText("No skills are available.")).toBeInTheDocument();
});
it("discovers and installs a skill from skills.sh", async () => {
+124
View File
@@ -0,0 +1,124 @@
import { act, fireEvent, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import type { CliAppInfo, McpPresetInfo, NanobotFeatureInfo } from "@/lib/types";
import {
installSettingsViewTestHooks,
jsonResponse,
renderSettingsView,
settingsPayload,
} from "@/tests/settings-test-utils";
function deferred<T>() {
let resolve!: (value: T) => void;
const promise = new Promise<T>((res) => { resolve = res; });
return { promise, resolve };
}
const app: CliAppInfo = {
name: "anygen", display_name: "AnyGen", category: "generation",
description: "Generate documents.", requires: "ANYGEN_API_KEY", source: "harness",
entry_point: "cli-anything-anygen", install_supported: true, installed: true,
available: true, status: "installed", skill_installed: true,
};
const plugin: McpPresetInfo = {
name: "plugin-computer-use", display_name: "Computer Use", category: "Plugin",
description: "Control the desktop.", requires: "accessibility", transport: "stdio",
install_supported: false, installed: true, configured: true, enabled: false,
available: false, status: "disabled", required_fields: [], source: "agent-plugin",
docs_url: "", note: "", connection_summary: "",
};
const channel: NanobotFeatureInfo = {
name: "discord", display_name: "Discord", type: "channel", installed: true,
configured: false, enabled: false, ready: false, status: "not_enabled",
install_supported: true, requires_restart: false,
};
describe("catalog loading", () => {
installSettingsViewTestHooks();
it("keeps loaded Apps rows visible while waiting for Agent Plugins", async () => {
const cli = deferred<Response>();
const mcp = deferred<Response>();
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL) => {
const url = String(input);
if (url === "/api/settings/cli-apps") return cli.promise;
if (url === "/api/settings/mcp-presets") return mcp.promise;
return Promise.resolve(jsonResponse(settingsPayload()));
}));
renderSettingsView({ initialSettings: settingsPayload() });
expect(screen.getByRole("status", { name: "Loading apps…" })).toHaveAttribute("aria-busy", "true");
await act(async () => { cli.resolve(jsonResponse({ apps: [app], installed_count: 1 })); });
expect(screen.getByRole("heading", { name: "AnyGen" })).toBeInTheDocument();
expect(screen.getByRole("status", { name: "Loading apps…" })).toBeInTheDocument();
await act(async () => { mcp.resolve(jsonResponse({ presets: [plugin], installed_count: 1 })); });
expect(screen.getByRole("heading", { name: "Computer Use" })).toBeInTheDocument();
expect(screen.queryByRole("status", { name: "Loading apps…" })).not.toBeInTheDocument();
});
it("finishes an empty MCP category independently but waits for Apps when searching", async () => {
const cli = deferred<Response>();
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL) => {
const url = String(input);
if (url === "/api/settings/cli-apps") return cli.promise;
if (url === "/api/settings/mcp-presets") {
return Promise.resolve(jsonResponse({ presets: [], installed_count: 0 }));
}
return Promise.resolve(jsonResponse(settingsPayload()));
}));
renderSettingsView({ initialSettings: settingsPayload() });
await act(async () => {});
expect(screen.getByRole("status", { name: "Loading apps…" })).toBeInTheDocument();
expect(screen.queryByText("No apps available.")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "MCP" }));
expect(screen.getByText("No MCP tools available.")).toBeInTheDocument();
expect(screen.queryByRole("status", { name: "Loading apps…" })).not.toBeInTheDocument();
fireEvent.change(screen.getByPlaceholderText("Search tools"), { target: { value: "AnyGen" } });
expect(screen.getByRole("status", { name: "Loading apps…" })).toBeInTheDocument();
expect(screen.queryByText("No tools match your search.")).not.toBeInTheDocument();
await act(async () => { cli.resolve(jsonResponse({ apps: [app], installed_count: 1 })); });
expect(screen.getByRole("heading", { name: "AnyGen" })).toBeInTheDocument();
expect(screen.queryByRole("status", { name: "Loading apps…" })).not.toBeInTheDocument();
});
it.each(["apps", "channels"] as const)("reports a failed %s read without claiming an empty catalog", async (section) => {
vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => {
const url = String(input);
if (url === "/api/settings") return jsonResponse(settingsPayload());
if (url === "/api/settings/mcp-presets") return jsonResponse({ presets: [], installed_count: 0 });
throw new Error("Catalog offline");
}));
renderSettingsView({ initialSection: section, initialSettings: settingsPayload() });
expect(await screen.findByText("Catalog offline")).toBeInTheDocument();
expect(screen.queryByRole("status", { name: /Loading (apps|channels)/ })).not.toBeInTheDocument();
expect(screen.queryByText("No apps available.")).not.toBeInTheDocument();
expect(screen.queryByText("No channels match this filter.")).not.toBeInTheDocument();
});
it("shows Channels placeholders until the first response and preserves rows on a focus refresh", async () => {
const initial = deferred<Response>();
const refresh = deferred<Response>();
const fetchFeatures = vi.fn().mockReturnValueOnce(initial.promise).mockReturnValue(refresh.promise);
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL) => (
String(input) === "/api/settings/nanobot-features"
? fetchFeatures() : Promise.resolve(jsonResponse(settingsPayload()))
)));
renderSettingsView({ initialSection: "channels", initialSettings: settingsPayload() });
expect(screen.getByRole("status", { name: "Loading channels…" })).toHaveAttribute("aria-busy", "true");
fireEvent.change(screen.getByRole("searchbox", { name: "Search channels" }), {
target: { value: "discord" },
});
await act(async () => { initial.resolve(jsonResponse({ features: [channel], enabled_count: 0 })); });
expect(screen.getByRole("button", { name: "View Discord settings" })).toBeInTheDocument();
expect(screen.queryByRole("status", { name: "Loading channels…" })).not.toBeInTheDocument();
act(() => { window.dispatchEvent(new Event("focus")); });
expect(fetchFeatures).toHaveBeenCalledTimes(2);
expect(screen.getByRole("button", { name: "View Discord settings" })).toBeInTheDocument();
expect(screen.queryByRole("status", { name: "Loading channels…" })).not.toBeInTheDocument();
await act(async () => { refresh.resolve(jsonResponse({ features: [], enabled_count: 0 })); });
expect(screen.getByText("No channels match this filter.")).toBeInTheDocument();
});
});
+61 -3
View File
@@ -98,6 +98,56 @@ describe("McpManagementDialog", () => {
fireEvent.click(within(dialog).getByRole("button", { name: "Reload tools" }));
expect(onAction).toHaveBeenCalledTimes(2);
});
it("shows a skeleton for the first inspection and keeps tools and draft selections during reinspection", () => {
const emptyPreset = { ...connectedPreset, tool_count: 0, tool_names: [] };
const { updateDialog } = renderDialog({
initialTab: "tools",
preset: emptyPreset,
actionKey: "test:docs",
});
const dialog = screen.getByRole("dialog", { name: "Docs MCP" });
expect(within(dialog).getByRole("status", { name: "Loading tools…" })).toHaveAttribute("aria-busy", "true");
expect(within(dialog).queryByRole("textbox", { name: "Search tools" })).not.toBeInTheDocument();
expect(within(dialog).queryByText("No tools available")).not.toBeInTheDocument();
updateDialog(connectedPreset);
expect(within(dialog).queryByRole("status", { name: "Loading tools…" })).not.toBeInTheDocument();
const searchDocs = within(dialog).getByRole("checkbox", { name: /search_docs/ });
fireEvent.click(searchDocs);
expect(searchDocs).not.toBeChecked();
updateDialog({ ...connectedPreset, tool_names: [] }, "test:docs");
expect(within(dialog).queryByRole("status", { name: "Loading tools…" })).not.toBeInTheDocument();
expect(within(dialog).getByRole("checkbox", { name: /search_docs/ })).not.toBeChecked();
expect(within(dialog).getByRole("checkbox", { name: /write_note/ })).toBeChecked();
expect(within(dialog).getByText("2 enabled")).toBeInTheDocument();
expect(within(dialog).getByRole("button", { name: "Save changes" })).toBeEnabled();
});
it.each([
{ error: undefined, message: "No tools available" },
{ error: "Inspection failed", message: "Inspection failed" },
])("offers recovery when inspection finishes with $message", ({ error, message }) => {
const onAction = vi.fn();
const emptyPreset = { ...connectedPreset, tool_count: 0, tool_names: [], error };
const { updateDialog } = renderDialog({
initialTab: "tools",
preset: emptyPreset,
actionKey: "test:docs",
onAction,
});
const dialog = screen.getByRole("dialog", { name: "Docs MCP" });
expect(within(dialog).getByRole("status", { name: "Loading tools…" })).toBeInTheDocument();
updateDialog(emptyPreset);
expect(within(dialog).queryByRole("status", { name: "Loading tools…" })).not.toBeInTheDocument();
expect(within(dialog).getByText(message)).toBeInTheDocument();
if (error) expect(within(dialog).getByRole("alert")).toHaveTextContent(error);
fireEvent.click(within(dialog).getByRole("button", { name: "Reload tools" }));
expect(onAction).toHaveBeenLastCalledWith("test", "docs");
});
});
function renderDialog({
@@ -105,19 +155,21 @@ function renderDialog({
onAction = vi.fn(),
onToolsChange = vi.fn(),
preset = connectedPreset,
actionKey = null,
}: {
initialTab: McpManagementTab;
onAction?: ReturnType<typeof vi.fn>;
onToolsChange?: ReturnType<typeof vi.fn>;
preset?: McpPresetInfo;
actionKey?: string | null;
}) {
function Harness() {
function Harness({ preset, actionKey }: { preset: McpPresetInfo; actionKey: string | null }) {
const [tab, setTab] = useState(initialTab);
return (
<McpManagementDialog
preset={preset}
values={{}}
actionKey={null}
actionKey={actionKey}
statusLabel="Connected"
statusTone="success"
tab={tab}
@@ -131,5 +183,11 @@ function renderDialog({
/>
);
}
return render(<Harness />);
const view = render(<Harness preset={preset} actionKey={actionKey} />);
return {
...view,
updateDialog: (nextPreset: McpPresetInfo, nextActionKey: string | null = null) => {
view.rerender(<Harness preset={nextPreset} actionKey={nextActionKey} />);
},
};
}
+107 -12
View File
@@ -2,6 +2,7 @@ import { act, fireEvent, render, renderHook, screen } from "@testing-library/rea
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { SkillsMarketplace } from "@/components/settings/SkillsMarketplace";
import { SkillsCatalogSettings } from "@/components/settings/SkillsCatalogSettings";
import { ThreadComposer } from "@/components/thread/ThreadComposer";
import {
fetchSkills,
@@ -38,6 +39,52 @@ function marketplace(token: string) {
}
describe("useSkills", () => {
it("shows loading, retries a failed first read, and preserves an empty catalog during refresh", async () => {
let rejectInitial!: (error: Error) => void;
let resolveRetry!: (value: Awaited<ReturnType<typeof fetchSkills>>) => void;
let resolveRefresh!: (value: Awaited<ReturnType<typeof fetchSkills>>) => void;
vi.mocked(fetchSkills).mockReset()
.mockImplementationOnce(() => new Promise((_resolve, reject) => { rejectInitial = reject; }))
.mockImplementationOnce(() => new Promise((resolve) => { resolveRetry = resolve; }))
.mockImplementationOnce(() => new Promise((resolve) => { resolveRefresh = resolve; }));
const getToken = () => "tok";
function Catalog() {
const state = useSkills(getToken);
return <SkillsCatalogSettings {...state} />;
}
render(<ClientProvider client={client} token="tok"><Catalog /></ClientProvider>);
expect(screen.getByRole("status", { name: "Loading skills…" })).toHaveAttribute("aria-busy", "true");
fireEvent.change(screen.getByRole("textbox", { name: "Search installed skills" }), {
target: { value: "cron" },
});
await act(async () => { rejectInitial(new Error("Offline")); });
expect(screen.getByRole("alert")).toHaveTextContent("Could not load skills.");
expect(screen.queryByText("No skills are available.")).not.toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
expect(screen.getByRole("status", { name: "Loading skills…" })).toBeInTheDocument();
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
await act(async () => { resolveRetry({ skills: [] }); });
expect(screen.getByText("No skills are available.")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "All 0" })).toBeInTheDocument();
act(() => { requestSkillsRefresh(); });
expect(screen.getByText("No skills are available.")).toBeInTheDocument();
expect(screen.queryByRole("status", { name: "Loading skills…" })).not.toBeInTheDocument();
await act(async () => {
resolveRefresh({ skills: [{
name: "weather", description: "Get the weather.", source: "builtin", available: true,
}] });
});
expect(screen.getByText("No matching skills.")).toBeInTheDocument();
fireEvent.change(screen.getByRole("textbox", { name: "Search installed skills" }), {
target: { value: "" },
});
expect(screen.getByRole("button", { name: "Open details for weather" })).toBeInTheDocument();
expect(fetchSkills).toHaveBeenCalledTimes(3);
});
it("discovers skills added after page load when opening the $ menu, not on each keystroke", async () => {
const installed = {
name: "simple-pr-review",
@@ -50,7 +97,7 @@ describe("useSkills", () => {
.mockResolvedValue({ skills: [installed] });
const getToken = () => "tok";
function Composer() {
const skills = useSkills(getToken);
const { skills } = useSkills(getToken);
return <ThreadComposer onSend={vi.fn()} skills={skills} />;
}
render(<Composer />);
@@ -105,7 +152,7 @@ describe("useSkills", () => {
});
// The old response was captured before installation; it cannot satisfy the new opens.
expect(fetchSkills).toHaveBeenCalledTimes(2);
expect(result.current).toEqual([installed]);
expect(result.current.skills).toEqual([installed]);
unmount();
requestSkillsRefresh();
@@ -126,19 +173,19 @@ describe("useSkills", () => {
const getToken = () => "tok";
const { result } = renderHook(() => useSkills(getToken));
await act(async () => {});
expect(result.current).toEqual([installed]);
expect(result.current.skills).toEqual([installed]);
await act(async () => {
requestSkillsRefresh();
});
expect(fetchSkills).toHaveBeenCalledTimes(2);
expect(result.current).toEqual([installed]);
expect(result.current).toEqual({ skills: [installed], loading: false, error: false });
await act(async () => {
requestSkillsRefresh();
});
expect(fetchSkills).toHaveBeenCalledTimes(3);
expect(result.current).toEqual([]);
expect(result.current).toEqual({ skills: [], loading: false, error: false });
});
it.each([false, true])("does not start a queued refresh after unmount (failure: %s)", async (fails) => {
@@ -162,11 +209,11 @@ describe("useSkills", () => {
expect(fetchSkills).toHaveBeenCalledTimes(1);
});
it("does not let an older request overwrite a newer skill event", async () => {
let resolveSkills!: (value: Awaited<ReturnType<typeof fetchSkills>>) => void;
it.each([false, true])("does not let an older request overwrite a newer skill event (failure: %s)", async (fails) => {
let settle!: () => void;
vi.mocked(fetchSkills).mockReset().mockImplementationOnce(
() => new Promise((resolve) => {
resolveSkills = resolve;
() => new Promise((resolve, reject) => {
settle = () => fails ? reject(new Error("Offline")) : resolve({ skills: [] });
}),
);
const installed = {
@@ -184,13 +231,13 @@ describe("useSkills", () => {
detail: { skills: [installed] },
}));
});
expect(result.current).toEqual([installed]);
expect(result.current).toEqual({ skills: [installed], loading: false, error: false });
await act(async () => {
resolveSkills({ skills: [] });
settle();
});
expect(result.current).toEqual([installed]);
expect(result.current).toEqual({ skills: [installed], loading: false, error: false });
});
});
@@ -217,6 +264,54 @@ describe("SkillsMarketplace", () => {
vi.useRealTimers();
});
it("shows discovery skeletons while trending or the first search is pending and keeps loaded search results", async () => {
const skill = {
id: "skillhub:react-testing", skill_id: "react-testing", name: "React Testing",
source: "react-testing", provider: "skillhub" as const, installs: 42,
url: "https://skillhub.cn/react-testing", installed: false, install_supported: true,
metric: "installs_total" as const, rank: 1,
};
let resolveTrending!: (value: Awaited<ReturnType<typeof fetchTrendingMarketplaceSkills>>) => void;
let resolveSearch!: (value: Awaited<ReturnType<typeof searchMarketplaceSkills>>) => void;
let resolveNextSearch!: (value: Awaited<ReturnType<typeof searchMarketplaceSkills>>) => void;
vi.mocked(fetchTrendingMarketplaceSkills).mockImplementationOnce(
() => new Promise((resolve) => { resolveTrending = resolve; }),
);
vi.mocked(searchMarketplaceSkills)
.mockImplementationOnce(() => new Promise((resolve) => { resolveSearch = resolve; }))
.mockImplementationOnce(() => new Promise((resolve) => { resolveNextSearch = resolve; }));
render(marketplace("tok"));
expect(screen.getByRole("status", { name: "Loading skills…" })).toHaveAttribute("aria-busy", "true");
await act(async () => {
resolveTrending({ period: "mixed", provider: "all", install_supported: true, skills: [skill] });
});
expect(screen.getByRole("button", { name: "Install React Testing" })).toBeInTheDocument();
fireEvent.change(screen.getByRole("textbox", { name: "Search skills" }), {
target: { value: "React" },
});
expect(screen.getByRole("status", { name: "Loading skills…" })).toHaveAttribute("aria-busy", "true");
expect(searchMarketplaceSkills).not.toHaveBeenCalled();
await act(async () => { await vi.advanceTimersByTimeAsync(300); });
expect(searchMarketplaceSkills).toHaveBeenCalledWith("tok", "React");
expect(screen.getByRole("status", { name: "Loading skills…" })).toBeInTheDocument();
await act(async () => {
resolveSearch({ query: "React", provider: "all", install_supported: true, skills: [skill] });
});
fireEvent.change(screen.getByRole("textbox", { name: "Search skills" }), {
target: { value: "Vue" },
});
await act(async () => { await vi.advanceTimersByTimeAsync(300); });
expect(screen.getByRole("button", { name: "Install React Testing" })).toBeInTheDocument();
expect(screen.queryByRole("status", { name: "Loading skills…" })).not.toBeInTheDocument();
await act(async () => {
resolveNextSearch({ query: "Vue", provider: "all", install_supported: true, skills: [] });
});
expect(screen.getByText("No skills found for “Vue”.")).toBeInTheDocument();
});
it("keeps loaded marketplace data stable when the auth token rotates", async () => {
const { rerender } = render(marketplace("tok-old"));