Open History to see the last commit
1import type { ReactElement } from "react";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3
4import { visitElements } from "../../test/reactElementTree";
5import { reactHookHarness as hooks } from "../../test/reactHookHarness";
6
7vi.mock("react", async (importOriginal) => {
8 const actual = await importOriginal<typeof import("react")>();
9 const { reactHookHarness } = await import("../../test/reactHookHarness");
10 return {
11 ...actual,
12 useEffect: (effect: () => void | (() => void)) => effect(),
13 useState: reactHookHarness.useState,
14 };
15});
16
17import {
18 AcpRegistryAgentIcon,
19 loadCachedAcpRegistryIcon,
20 officialAcpRegistryIconUrlForAgentId,
21 resolveOfficialAcpRegistryIconUrl,
22} from "./AcpRegistryIcon";
23
24describe("ACP Registry icon cache", () => {
25 const stored = new Map<string, Response>();
26 const match = vi.fn(async (url: string) => stored.get(url)?.clone());
27 const put = vi.fn(async (url: string, response: Response) => {
28 stored.set(url, response.clone());
29 });
30 const fetchIcon = vi.fn();
31
32 beforeEach(() => {
33 hooks.reset();
34 stored.clear();
35 match.mockClear();
36 put.mockClear();
37 fetchIcon.mockReset();
38 vi.stubGlobal("caches", { open: vi.fn(async () => ({ match, put })) });
39 vi.stubGlobal("fetch", fetchIcon);
40 });
41
42 it("single-flights the first fetch and serves later reads from persistent cache", async () => {
43 const url = "https://cdn.agentclientprotocol.com/registry/icons/cache-test.svg";
44 fetchIcon.mockResolvedValue(
45 new Response("<svg/>", {
46 status: 200,
47 headers: { "content-type": "image/svg+xml" },
48 }),
49 );
50
51 const [first, concurrent] = await Promise.all([
52 loadCachedAcpRegistryIcon(url),
53 loadCachedAcpRegistryIcon(url),
54 ]);
55 const cached = await loadCachedAcpRegistryIcon(url);
56
57 expect(first.type).toBe("image/svg+xml");
58 expect(concurrent.size).toBe(first.size);
59 expect(cached.size).toBe(first.size);
60 expect(fetchIcon).toHaveBeenCalledOnce();
61 expect(put).toHaveBeenCalledOnce();
62 });
63
64 it("rejects oversized image responses before caching", async () => {
65 const url = "https://cdn.agentclientprotocol.com/registry/icons/oversized.svg";
66 fetchIcon.mockResolvedValue(
67 new Response("too large", {
68 status: 200,
69 headers: {
70 "content-length": String(513 * 1_024),
71 "content-type": "image/svg+xml",
72 },
73 }),
74 );
75
76 await expect(loadCachedAcpRegistryIcon(url)).rejects.toThrow("too large");
77 expect(put).not.toHaveBeenCalled();
78 });
79
80 it("falls back to the network when CacheStorage cannot be opened", async () => {
81 const url = "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg";
82 vi.stubGlobal("caches", {
83 open: vi.fn(async () => {
84 throw new Error("CacheStorage unavailable");
85 }),
86 });
87 fetchIcon.mockResolvedValue(
88 new Response("<svg/>", {
89 status: 200,
90 headers: { "content-type": "image/svg+xml" },
91 }),
92 );
93
94 await expect(loadCachedAcpRegistryIcon(url)).resolves.toMatchObject({
95 type: "image/svg+xml",
96 });
97 expect(fetchIcon).toHaveBeenCalledWith(url, {
98 credentials: "omit",
99 redirect: "error",
100 referrerPolicy: "no-referrer",
101 });
102 });
103
104 it("accepts only credential-free HTTPS URLs on the official CDN", () => {
105 expect(
106 resolveOfficialAcpRegistryIconUrl(
107 "https://cdn.agentclientprotocol.com/registry/icons/gemini.png",
108 ),
109 ).toBe("https://cdn.agentclientprotocol.com/registry/icons/gemini.png");
110 expect(
111 resolveOfficialAcpRegistryIconUrl("http://cdn.agentclientprotocol.com/icon.png"),
112 ).toBeNull();
113 expect(
114 resolveOfficialAcpRegistryIconUrl("https://cdn.agentclientprotocol.com.evil/icon.png"),
115 ).toBeNull();
116 expect(
117 resolveOfficialAcpRegistryIconUrl("https://user@cdn.agentclientprotocol.com/icon.png"),
118 ).toBeNull();
119 expect(
120 resolveOfficialAcpRegistryIconUrl("https://cdn.agentclientprotocol.com:8443/icon.png"),
121 ).toBeNull();
122 expect(officialAcpRegistryIconUrlForAgentId("kilo")).toBe(
123 "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg",
124 );
125 expect(officialAcpRegistryIconUrlForAgentId("../kilo")).toBeNull();
126 });
127
128 it("falls back to the raw allowlisted CDN URL when the validating fetch is blocked", async () => {
129 const url = "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg";
130 // The official CDN serves no CORS headers, so the fetch can reject even
131 // though native <img> loading works.
132 fetchIcon.mockRejectedValue(new TypeError("Failed to fetch"));
133 hooks.beginRender();
134 AcpRegistryAgentIcon({ icon: url });
135
136 await new Promise((resolve) => setTimeout(resolve, 0));
137 hooks.beginRender();
138 const tree = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
139 const image = visitElements(tree, (element) => element.type === "img");
140 expect(image?.props).toMatchObject({ src: url, referrerPolicy: "no-referrer" });
141 expect(
142 visitElements(tree, (element) => element.props["data-slot"] === "acp-icon-fallback"),
143 ).not.toBeNull();
144 });
145
146 it("renders only the validated blob object URL and keeps the fallback until load", async () => {
147 const url = "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg";
148 const objectUrl = "blob:t3/kilo-icon";
149 Object.assign(URL, {
150 createObjectURL: vi.fn(() => objectUrl),
151 revokeObjectURL: vi.fn(),
152 });
153 fetchIcon.mockResolvedValue(
154 new Response("<svg/>", {
155 status: 200,
156 headers: { "content-type": "image/svg+xml" },
157 }),
158 );
159 hooks.beginRender();
160 const validating = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
161
162 // The raw CDN URL is never rendered; only the fallback shows while the
163 // blob is fetched and validated.
164 expect(visitElements(validating, (element) => element.type === "img")).toBeNull();
165 expect(
166 visitElements(validating, (element) => element.props["data-slot"] === "acp-icon-fallback"),
167 ).not.toBeNull();
168
169 await new Promise((resolve) => setTimeout(resolve, 0));
170 hooks.beginRender();
171 const loading = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
172 const loadingImage = visitElements(loading, (element) => element.type === "img");
173
174 expect(loadingImage?.props).toMatchObject({
175 src: objectUrl,
176 alt: "",
177 decoding: "async",
178 referrerPolicy: "no-referrer",
179 });
180 expect(loadingImage?.props.className).not.toContain("dark:invert");
181 expect(loadingImage?.props.className).toContain("invisible");
182 expect(
183 visitElements(loading, (element) => element.props["data-slot"] === "acp-icon-fallback"),
184 ).not.toBeNull();
185
186 (loadingImage?.props.onLoad as (() => void) | undefined)?.();
187 hooks.beginRender();
188 const loaded = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
189 expect(
190 visitElements(loaded, (element) => element.type === "img")?.props.className,
191 ).not.toContain("invisible");
192 expect(
193 visitElements(loaded, (element) => element.props["data-slot"] === "acp-icon-fallback"),
194 ).toBeNull();
195
196 const loadedImage = visitElements(loaded, (element) => element.type === "img");
197 (loadedImage?.props.onError as (() => void) | undefined)?.();
198 hooks.beginRender();
199 const failed = AcpRegistryAgentIcon({ icon: url }) as ReactElement<Record<string, unknown>>;
200 expect(visitElements(failed, (element) => element.type === "img")).toBeNull();
201 expect(
202 visitElements(failed, (element) => element.props["data-slot"] === "acp-icon-fallback"),
203 ).not.toBeNull();
204 });
205});