fix(webui): cache tool catalogs across settings navigation (#6552)
* fix(webui): cache tool catalogs across settings navigation * refactor(webui): remove unreachable catalog invalidation condition
This commit is contained in:
1 parent
62c2ed4e7e
commit
8632940a99
15 files changed
+424
-31
No files matched your search
@@ -1,7 +1,10 @@
|
||||
import { act, fireEvent, screen } from "@testing-library/react";
|
||||
import { act, cleanup, fireEvent, screen } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { CliAppInfo, McpPresetInfo, NanobotFeatureInfo } from "@/lib/types";
|
||||
import type { NanobotClient } from "@/lib/nanobot-client";
|
||||
import { invalidateToolsCatalog, TOOLS_CATALOG_TTL_MS } from "@/lib/tools-catalog-cache";
|
||||
import { CLI_APPS_REFRESH_RETRY_MS } from "@/components/settings/system/AppsSettings";
|
||||
import {
|
||||
installSettingsViewTestHooks,
|
||||
jsonResponse,
|
||||
@@ -37,6 +40,90 @@ const channel: NanobotFeatureInfo = {
|
||||
describe("catalog loading", () => {
|
||||
installSettingsViewTestHooks();
|
||||
|
||||
it("reuses both catalogs after leaving settings and keeps expired rows visible during refresh", async () => {
|
||||
const client = { status: "open", onStatus: () => () => {} } as unknown as NanobotClient;
|
||||
const cli = deferred<Response>();
|
||||
const mcp = deferred<Response>();
|
||||
let refresh = false;
|
||||
const fetch = vi.fn((input: RequestInfo | URL) => {
|
||||
const url = String(input);
|
||||
if (url === "/api/settings/cli-apps") return refresh ? cli.promise : Promise.resolve(jsonResponse({ apps: [app], installed_count: 1 }));
|
||||
if (url === "/api/settings/mcp-presets") return refresh ? mcp.promise : Promise.resolve(jsonResponse({ presets: [plugin], installed_count: 1 }));
|
||||
return Promise.resolve(jsonResponse(settingsPayload()));
|
||||
});
|
||||
vi.stubGlobal("fetch", fetch);
|
||||
renderSettingsView({ client, initialSettings: settingsPayload() });
|
||||
await act(async () => {});
|
||||
cleanup();
|
||||
fetch.mockClear();
|
||||
renderSettingsView({ client, initialSettings: settingsPayload() });
|
||||
expect(screen.getByRole("heading", { name: "AnyGen" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: "Computer Use" })).toBeInTheDocument();
|
||||
expect(screen.queryByRole("status", { name: "Loading apps…" })).not.toBeInTheDocument();
|
||||
expect(fetch.mock.calls.filter(([url]) => String(url).match(/cli-apps|mcp-presets/))).toHaveLength(0);
|
||||
cleanup();
|
||||
refresh = true;
|
||||
const now = vi.spyOn(Date, "now").mockReturnValue(Date.now() + TOOLS_CATALOG_TTL_MS + 1);
|
||||
try {
|
||||
renderSettingsView({ client, initialSettings: settingsPayload() });
|
||||
expect(screen.getByRole("heading", { name: "AnyGen" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: "Computer Use" })).toBeInTheDocument();
|
||||
expect(screen.queryByRole("status", { name: "Loading apps…" })).not.toBeInTheDocument();
|
||||
expect(fetch.mock.calls.filter(([url]) => String(url).match(/cli-apps|mcp-presets/))).toHaveLength(2);
|
||||
await act(async () => {
|
||||
cli.resolve(jsonResponse({ apps: [{ ...app, display_name: "Updated app" }], installed_count: 1 }));
|
||||
mcp.resolve(jsonResponse({ presets: [{ ...plugin, display_name: "Updated plugin" }], installed_count: 1 }));
|
||||
});
|
||||
expect(screen.getByRole("heading", { name: "Updated app" })).toBeInTheDocument();
|
||||
expect(screen.getByRole("heading", { name: "Updated plugin" })).toBeInTheDocument();
|
||||
} finally {
|
||||
now.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it("refreshes an invalidated initial read immediately and ignores its superseded response", async () => {
|
||||
const client = { status: "open", onStatus: () => () => {} } as unknown as NanobotClient;
|
||||
const old = deferred<Response>();
|
||||
let reads = 0;
|
||||
vi.stubGlobal("fetch", vi.fn((input: RequestInfo | URL) => {
|
||||
if (String(input) === "/api/settings") return Promise.resolve(jsonResponse(settingsPayload()));
|
||||
if (String(input) === "/api/settings/cli-apps") {
|
||||
reads++;
|
||||
return reads === 1 ? old.promise : Promise.resolve(jsonResponse({ apps: [app], installed_count: 1 }));
|
||||
}
|
||||
return Promise.resolve(jsonResponse({ presets: [], installed_count: 0 }));
|
||||
}));
|
||||
renderSettingsView({ client, initialSettings: settingsPayload() });
|
||||
await act(async () => { invalidateToolsCatalog(client); });
|
||||
expect(reads).toBe(2);
|
||||
expect(screen.getByRole("heading", { name: "AnyGen" })).toBeInTheDocument();
|
||||
await act(async () => { old.resolve(jsonResponse({ apps: [], installed_count: 0 })); });
|
||||
expect(screen.getByRole("heading", { name: "AnyGen" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("resumes a pending CLI catalog refresh after remount", async () => {
|
||||
vi.useFakeTimers();
|
||||
const client = { status: "open", onStatus: () => () => {} } as unknown as NanobotClient;
|
||||
let reads = 0;
|
||||
vi.stubGlobal("fetch", vi.fn(async (input: RequestInfo | URL) => {
|
||||
if (String(input) === "/api/settings") return jsonResponse(settingsPayload());
|
||||
if (String(input) === "/api/settings/cli-apps") {
|
||||
reads++;
|
||||
return jsonResponse({ apps: [{ ...app, display_name: reads === 1 ? "AnyGen" : "Refreshed app" }], installed_count: 1, catalog_refresh_pending: reads === 1 });
|
||||
}
|
||||
return jsonResponse({ presets: [], installed_count: 0 });
|
||||
}));
|
||||
renderSettingsView({ client, initialSettings: settingsPayload() });
|
||||
await act(async () => {});
|
||||
cleanup();
|
||||
renderSettingsView({ client, initialSettings: settingsPayload() });
|
||||
expect(screen.getByRole("heading", { name: "AnyGen" })).toBeInTheDocument();
|
||||
expect(reads).toBe(1);
|
||||
await act(async () => { await vi.advanceTimersByTimeAsync(CLI_APPS_REFRESH_RETRY_MS); });
|
||||
expect(reads).toBe(2);
|
||||
expect(screen.getByRole("heading", { name: "Refreshed app" })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("keeps loaded Apps rows visible while waiting for Agent Plugins", async () => {
|
||||
const cli = deferred<Response>();
|
||||
const mcp = deferred<Response>();
|
||||
|
||||
@@ -10,6 +10,7 @@ import { createSystemSettingsActions } from "@/components/settings/system/create
|
||||
import { useSystemSettingsState } from "@/components/settings/system/useSystemSettingsState";
|
||||
import i18n from "@/i18n";
|
||||
import { NanobotClient } from "@/lib/nanobot-client";
|
||||
import { getToolsCatalog } from "@/lib/tools-catalog-cache";
|
||||
import type { McpPresetInfo, McpPresetsPayload } from "@/lib/types";
|
||||
import {
|
||||
installSettingsViewTestHooks,
|
||||
@@ -490,7 +491,7 @@ function renderMcpActions() {
|
||||
const client = new NanobotClient({ url: "ws://example.test" });
|
||||
client.requestMutation = requestMutationMock;
|
||||
return renderHook(() => {
|
||||
const state = useSystemSettingsState();
|
||||
const state = useSystemSettingsState(getToolsCatalog(client, "tok"));
|
||||
const actions = createSystemSettingsActions({
|
||||
state,
|
||||
featureCatalog: [],
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { act, fireEvent, screen, waitFor, within } from "@testing-library/react";
|
||||
import { act, cleanup, fireEvent, screen, waitFor, within } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import i18n from "@/i18n";
|
||||
@@ -423,11 +423,17 @@ describe("SettingsView Apps catalog", () => {
|
||||
});
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
renderSettingsView({ initialSection: "apps" });
|
||||
const client = { requestMutation: requestMutationMock, status: "open", onStatus: () => () => {} } as never;
|
||||
renderSettingsView({ client, initialSection: "apps" });
|
||||
fireEvent.click(await screen.findByRole("button", { name: "MCP" }));
|
||||
|
||||
expect(await screen.findByRole("button", { name: "Xmind" }))
|
||||
.toHaveAccessibleDescription("Connecting…");
|
||||
cleanup();
|
||||
renderSettingsView({ client, initialSection: "apps", initialSettings: settingsPayload() });
|
||||
fireEvent.click(screen.getByRole("button", { name: "MCP" }));
|
||||
expect(screen.getByRole("button", { name: "Xmind" })).toHaveAccessibleDescription("Connecting…");
|
||||
expect(mcpPresetRequests).toBe(1);
|
||||
expect(await screen.findByRole(
|
||||
"button",
|
||||
{ name: "Manage Xmind" },
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { act, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import { act, cleanup, fireEvent, render, screen, waitFor, within } from "@testing-library/react";
|
||||
import { expect, it, vi } from "vitest";
|
||||
import { installedMcpPresetsFromPayload } from "@/lib/mcp-preset-events";
|
||||
import { SkillsCatalogSettings } from "@/components/settings/SkillsCatalogSettings";
|
||||
@@ -562,7 +562,8 @@ describe("Settings system domains", () => {
|
||||
},
|
||||
});
|
||||
|
||||
renderSettingsView();
|
||||
const client = { requestMutation: requestMutationMock, status: "open", onStatus: () => () => {} } as never;
|
||||
renderSettingsView({ client });
|
||||
|
||||
expect(await screen.findByText("AnyGen")).toBeInTheDocument();
|
||||
const uninstall = screen.getByRole("button", { name: "Uninstall app" });
|
||||
@@ -579,7 +580,12 @@ describe("Settings system domains", () => {
|
||||
expect(await screen.findByText("Uninstalled CLI for AnyGen.")).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "Dismiss" }));
|
||||
|
||||
cleanup();
|
||||
fetchMock.mockClear();
|
||||
renderSettingsView({ client, initialSettings: settingsPayload() });
|
||||
expect(screen.getByText("AnyGen")).toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: "Uninstall app" })).not.toBeInTheDocument();
|
||||
expect(fetchMock.mock.calls.filter(([url]) => String(url).includes("cli-apps"))).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("keeps runtime dependencies out of Apps and explains chat mentions", async () => {
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { getToolsCatalog, invalidateToolsCatalog, TOOLS_CATALOG_TTL_MS } from "@/lib/tools-catalog-cache";
|
||||
import type { CliAppsPayload, McpPresetsPayload } from "@/lib/types";
|
||||
|
||||
const cli: CliAppsPayload = { apps: [], installed_count: 0 };
|
||||
const mcp: McpPresetsPayload = { presets: [], installed_count: 0 };
|
||||
const response = (payload: unknown) => new Response(JSON.stringify(payload), { headers: { "content-type": "application/json" } });
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
const promise = new Promise<T>(res => { resolve = res; });
|
||||
return { promise, resolve };
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("tools catalog cache", () => {
|
||||
it("isolates host clients and changed credentials, including late responses", async () => {
|
||||
const client = {};
|
||||
const old = getToolsCatalog(client, "old-token");
|
||||
const pending = deferred<Response>();
|
||||
vi.stubGlobal("fetch", vi.fn(() => pending.promise));
|
||||
const loading = old.loadCliApps();
|
||||
const next = getToolsCatalog(client, "new-token");
|
||||
const otherHost = getToolsCatalog({}, "old-token");
|
||||
pending.resolve(response(cli));
|
||||
await loading;
|
||||
expect(old.getSnapshot().cliApps).toEqual(cli);
|
||||
expect(next.getSnapshot().cliApps).toBeNull();
|
||||
expect(otherHost.getSnapshot().cliApps).toBeNull();
|
||||
expect(getToolsCatalog(client, "old-token").getSnapshot().cliApps).toBeNull();
|
||||
expect(vi.mocked(fetch).mock.calls[0][1]?.headers).toEqual({ Authorization: "Bearer old-token" });
|
||||
});
|
||||
|
||||
it("deduplicates in-flight reads and does not overwrite mutation payloads with older GETs", async () => {
|
||||
const client = {};
|
||||
const cache = getToolsCatalog(client, "token");
|
||||
const cliRead = deferred<Response>();
|
||||
const mcpRead = deferred<Response>();
|
||||
const fetch = vi.fn((url: string) => url.endsWith("cli-apps") ? cliRead.promise : mcpRead.promise);
|
||||
vi.stubGlobal("fetch", fetch);
|
||||
const first = cache.loadCliApps();
|
||||
expect(cache.loadCliApps()).toBe(first);
|
||||
const second = cache.loadMcpPresets();
|
||||
const changedCli = { ...cli, installed_count: 1 };
|
||||
const changedMcp = { ...mcp, installed_count: 1 };
|
||||
cache.setCliApps(changedCli);
|
||||
cache.setMcpPresets(changedMcp);
|
||||
cliRead.resolve(response(cli));
|
||||
mcpRead.resolve(response(mcp));
|
||||
await Promise.all([first, second]);
|
||||
expect(fetch).toHaveBeenCalledTimes(2);
|
||||
expect(getToolsCatalog(client, "token").getSnapshot()).toMatchObject({ cliApps: changedCli, mcpPresets: changedMcp });
|
||||
});
|
||||
|
||||
it("retains expired and invalidated data, but refreshes it rather than treating it as fresh", async () => {
|
||||
const client = {};
|
||||
const cache = getToolsCatalog(client, "token");
|
||||
cache.setCliApps(cli);
|
||||
cache.setMcpPresets(mcp);
|
||||
expect(cache.cliFresh()).toBe(true);
|
||||
expect(cache.mcpFresh()).toBe(true);
|
||||
vi.spyOn(Date, "now").mockReturnValue(Date.now() + TOOLS_CATALOG_TTL_MS);
|
||||
expect(cache.cliFresh()).toBe(false);
|
||||
expect(cache.mcpFresh()).toBe(false);
|
||||
cache.setCliApps(cli);
|
||||
cache.setMcpPresets(mcp);
|
||||
invalidateToolsCatalog(client);
|
||||
expect(cache.cliFresh()).toBe(false);
|
||||
expect(cache.mcpFresh()).toBe(false);
|
||||
expect(cache.getSnapshot()).toMatchObject({ cliApps: cli, mcpPresets: mcp });
|
||||
vi.stubGlobal("fetch", vi.fn(async (url: string) => response(url.endsWith("cli-apps") ? cli : mcp)));
|
||||
await Promise.all([cache.loadCliApps(), cache.loadMcpPresets()]);
|
||||
expect(cache.cliFresh()).toBe(true);
|
||||
expect(cache.mcpFresh()).toBe(true);
|
||||
});
|
||||
|
||||
it("does not surface an older GET failure after a mutation has succeeded", async () => {
|
||||
const cache = getToolsCatalog({}, "token");
|
||||
let reject!: (reason: Error) => void;
|
||||
vi.stubGlobal("fetch", vi.fn(() => new Promise<Response>((_, fail) => { reject = fail; })));
|
||||
const loading = cache.loadMcpPresets();
|
||||
cache.setMcpPresets(mcp);
|
||||
reject(new Error("old read failed"));
|
||||
await expect(loading).resolves.toBeUndefined();
|
||||
expect(cache.getSnapshot().mcpPresets).toEqual(mcp);
|
||||
});
|
||||
|
||||
it("leaves failures retryable and preserves stale payloads on a refresh failure", async () => {
|
||||
const cache = getToolsCatalog({}, "token");
|
||||
vi.stubGlobal("fetch", vi.fn().mockRejectedValueOnce(new Error("offline")).mockResolvedValueOnce(response(cli)).mockRejectedValueOnce(new Error("offline")));
|
||||
await expect(cache.loadCliApps()).rejects.toThrow("offline");
|
||||
expect(cache.getSnapshot().cliApps).toBeNull();
|
||||
expect(cache.cliFresh()).toBe(false);
|
||||
await cache.loadCliApps();
|
||||
cache.invalidate();
|
||||
await expect(cache.loadCliApps()).rejects.toThrow("offline");
|
||||
expect(cache.getSnapshot().cliApps).toEqual(cli);
|
||||
expect(cache.cliFresh()).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user