Open History to see the last commit
1import { act } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
4
5import { AcpRegistryAgentIcon } from "./AcpRegistryIcon";
6
7const url = "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg";
8const fetchIcon = vi.fn<typeof fetch>();
9let root: Root;
10let container: HTMLDivElement;
11
12beforeEach(() => {
13 vi.stubGlobal("caches", undefined);
14 vi.stubGlobal("fetch", fetchIcon);
15 fetchIcon.mockReset();
16 container = document.createElement("div");
17 document.body.append(container);
18 root = createRoot(container);
19});
20afterEach(async () => {
21 await act(async () => root.unmount());
22 container.remove();
23 vi.unstubAllGlobals();
24});
25
26const image = () => container.querySelector("img");
27const fallback = () => container.querySelector('[data-slot="acp-icon-fallback"]');
28
29it("falls back to the raw allowlisted CDN URL when the validating fetch is blocked", async () => {
30 // The official CDN serves no CORS headers, so the fetch can reject even
31 // though native <img> loading works.
32 fetchIcon.mockRejectedValue(new TypeError("Failed to fetch"));
33 await act(async () => root.render(<AcpRegistryAgentIcon icon={url} />));
34
35 await vi.waitFor(() => expect(image()?.getAttribute("src")).toBe(url));
36 expect(image()?.referrerPolicy).toBe("no-referrer");
37 expect(fallback()).not.toBeNull();
38});
39
40function serve(body: string) {
41 fetchIcon.mockResolvedValue(
42 new Response(body, { status: 200, headers: { "content-type": "image/svg+xml" } }),
43 );
44}
45
46it("swaps the fallback for the validated blob once the image loads", async () => {
47 serve('<svg xmlns="http://www.w3.org/2000/svg" width="8" height="8"/>');
48 await act(async () => root.render(<AcpRegistryAgentIcon icon={url} />));
49
50 await vi.waitFor(() => expect(fallback()).toBeNull());
51 expect(image()?.src).toMatch(/^blob:/);
52 expect(image()?.checkVisibility()).toBe(true);
53});
54
55it("keeps the fallback and drops the image when the validated blob fails to decode", async () => {
56 serve("not an svg");
57 await act(async () => root.render(<AcpRegistryAgentIcon icon={url} />));
58
59 await vi.waitFor(() => expect(fetchIcon).toHaveBeenCalledOnce());
60 await vi.waitFor(() => expect(image()).toBeNull());
61 expect(fallback()).not.toBeNull();
62});
63
64it("never renders an icon from outside the official CDN", async () => {
65 await act(async () =>
66 root.render(<AcpRegistryAgentIcon icon="https://cdn.agentclientprotocol.com.evil/icon.svg" />),
67 );
68 expect(fetchIcon).not.toHaveBeenCalled();
69 expect(image()).toBeNull();
70 expect(fallback()).not.toBeNull();
71});