Files
t3code/apps/web/src/components/settings/AcpRegistryIcon.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

207 lines
7.6 KiB
TypeScript

import type { ReactElement } from "react";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
import { visitElements } from "../../test/reactElementTree";
import { reactHookHarness as hooks } from "../../test/reactHookHarness";
vi.mock("react", async (importOriginal) => {
const actual = await importOriginal<typeof import("react")>();
const { reactHookHarness } = await import("../../test/reactHookHarness");
return {
...actual,
useEffect: (effect: () => void | (() => void)) => effect(),
useState: reactHookHarness.useState,
useId: () => "acp-icon-test-filter",
};
});
import {
AcpRegistryAgentIcon,
loadCachedAcpRegistryIcon,
officialAcpRegistryIconUrlForAgentId,
resolveOfficialAcpRegistryIconUrl,
} from "./AcpRegistryIcon";
describe("ACP Registry icon cache", () => {
const stored = new Map<string, Response>();
const match = vi.fn(async (url: string) => stored.get(url)?.clone());
const put = vi.fn(async (url: string, response: Response) => {
stored.set(url, response.clone());
});
const fetchIcon = vi.fn();
beforeEach(() => {
hooks.reset();
stored.clear();
match.mockClear();
put.mockClear();
fetchIcon.mockReset();
vi.stubGlobal("caches", { open: vi.fn(async () => ({ match, put })) });
vi.stubGlobal("fetch", fetchIcon);
});
it("single-flights the first fetch and serves later reads from persistent cache", async () => {
const url = "https://cdn.agentclientprotocol.com/registry/icons/cache-test.svg";
fetchIcon.mockResolvedValue(
new Response("<svg/>", {
status: 200,
headers: { "content-type": "image/svg+xml" },
}),
);
const [first, concurrent] = await Promise.all([
loadCachedAcpRegistryIcon(url),
loadCachedAcpRegistryIcon(url),
]);
const cached = await loadCachedAcpRegistryIcon(url);
expect(first.type).toBe("image/svg+xml");
expect(concurrent.size).toBe(first.size);
expect(cached.size).toBe(first.size);
expect(fetchIcon).toHaveBeenCalledOnce();
expect(put).toHaveBeenCalledOnce();
});
it("rejects oversized image responses before caching", async () => {
const url = "https://cdn.agentclientprotocol.com/registry/icons/oversized.svg";
fetchIcon.mockResolvedValue(
new Response("too large", {
status: 200,
headers: {
"content-length": String(513 * 1_024),
"content-type": "image/svg+xml",
},
}),
);
await expect(loadCachedAcpRegistryIcon(url)).rejects.toThrow("too large");
expect(put).not.toHaveBeenCalled();
});
it("falls back to the network when CacheStorage cannot be opened", async () => {
const url = "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg";
vi.stubGlobal("caches", {
open: vi.fn(async () => {
throw new Error("CacheStorage unavailable");
}),
});
fetchIcon.mockResolvedValue(
new Response("<svg/>", {
status: 200,
headers: { "content-type": "image/svg+xml" },
}),
);
await expect(loadCachedAcpRegistryIcon(url)).resolves.toMatchObject({
type: "image/svg+xml",
});
expect(fetchIcon).toHaveBeenCalledWith(url, {
credentials: "omit",
redirect: "error",
referrerPolicy: "no-referrer",
});
});
it("accepts only credential-free HTTPS URLs on the official CDN", () => {
expect(
resolveOfficialAcpRegistryIconUrl(
"https://cdn.agentclientprotocol.com/registry/icons/gemini.png",
),
).toBe("https://cdn.agentclientprotocol.com/registry/icons/gemini.png");
expect(
resolveOfficialAcpRegistryIconUrl("http://cdn.agentclientprotocol.com/icon.png"),
).toBeNull();
expect(
resolveOfficialAcpRegistryIconUrl("https://cdn.agentclientprotocol.com.evil/icon.png"),
).toBeNull();
expect(
resolveOfficialAcpRegistryIconUrl("https://[email protected]/icon.png"),
).toBeNull();
expect(
resolveOfficialAcpRegistryIconUrl("https://cdn.agentclientprotocol.com:8443/icon.png"),
).toBeNull();
expect(officialAcpRegistryIconUrlForAgentId("kilo")).toBe(
"https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg",
);
expect(officialAcpRegistryIconUrlForAgentId("../kilo")).toBeNull();
});
it("falls back to the raw allowlisted CDN URL when the validating fetch is blocked", async () => {
const url = "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg";
// The official CDN serves no CORS headers, so the fetch can reject even
// though native <img> loading works.
fetchIcon.mockRejectedValue(new TypeError("Failed to fetch"));
hooks.beginRender();
AcpRegistryAgentIcon({ icon: url });
await new Promise((resolve) => setTimeout(resolve, 0));
hooks.beginRender();
const tree = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
const image = visitElements(tree, (element) => element.type === "img");
expect(image?.props).toMatchObject({ src: url, referrerPolicy: "no-referrer" });
expect(
visitElements(tree, (element) => element.props["data-slot"] === "acp-icon-fallback"),
).not.toBeNull();
});
it("renders only the validated blob object URL and keeps the fallback until load", async () => {
const url = "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg";
const objectUrl = "blob:t3/kilo-icon";
Object.assign(URL, {
createObjectURL: vi.fn(() => objectUrl),
revokeObjectURL: vi.fn(),
});
fetchIcon.mockResolvedValue(
new Response("<svg/>", {
status: 200,
headers: { "content-type": "image/svg+xml" },
}),
);
hooks.beginRender();
const validating = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
// The raw CDN URL is never rendered; only the fallback shows while the
// blob is fetched and validated.
expect(visitElements(validating, (element) => element.type === "img")).toBeNull();
expect(
visitElements(validating, (element) => element.props["data-slot"] === "acp-icon-fallback"),
).not.toBeNull();
await new Promise((resolve) => setTimeout(resolve, 0));
hooks.beginRender();
const loading = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
const loadingImage = visitElements(loading, (element) => element.type === "img");
expect(loadingImage?.props).toMatchObject({
src: objectUrl,
alt: "",
decoding: "async",
referrerPolicy: "no-referrer",
});
expect(loadingImage?.props.className).not.toContain("dark:invert");
expect(loadingImage?.props.className).toContain("invisible");
expect(
visitElements(loading, (element) => element.props["data-slot"] === "acp-icon-fallback"),
).not.toBeNull();
(loadingImage?.props.onLoad as (() => void) | undefined)?.();
hooks.beginRender();
const loaded = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
expect(
visitElements(loaded, (element) => element.type === "img")?.props.className,
).not.toContain("invisible");
expect(
visitElements(loaded, (element) => element.props["data-slot"] === "acp-icon-fallback"),
).toBeNull();
const loadedImage = visitElements(loaded, (element) => element.type === "img");
(loadedImage?.props.onError as (() => void) | undefined)?.();
hooks.beginRender();
const failed = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
expect(visitElements(failed, (element) => element.type === "img")).toBeNull();
expect(
visitElements(failed, (element) => element.props["data-slot"] === "acp-icon-fallback"),
).not.toBeNull();
});
});