Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AcpRegistryIcon.test.ts

111 lines3.7 KB

1import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 loadCachedAcpRegistryIcon,
5 officialAcpRegistryIconUrlForAgentId,
6 resolveOfficialAcpRegistryIconUrl,
7} from "./AcpRegistryIcon";
8​
9describe("ACP Registry icon cache", () => {
10 const stored = new Map<string, Response>();
11 const match = vi.fn(async (url: string) => stored.get(url)?.clone());
12 const put = vi.fn(async (url: string, response: Response) => {
13 stored.set(url, response.clone());
14 });
15 const fetchIcon = vi.fn();
16​
17 beforeEach(() => {
18 stored.clear();
19 match.mockClear();
20 put.mockClear();
21 fetchIcon.mockReset();
22 vi.stubGlobal("caches", { open: vi.fn(async () => ({ match, put })) });
23 vi.stubGlobal("fetch", fetchIcon);
24 });
25​
26 it("single-flights the first fetch and serves later reads from persistent cache", async () => {
27 const url = "https://cdn.agentclientprotocol.com/registry/icons/cache-test.svg";
28 fetchIcon.mockResolvedValue(
29 new Response("<svg/>", {
30 status: 200,
31 headers: { "content-type": "image/svg+xml" },
32 }),
33 );
34​
35 const [first, concurrent] = await Promise.all([
36 loadCachedAcpRegistryIcon(url),
37 loadCachedAcpRegistryIcon(url),
38 ]);
39 const cached = await loadCachedAcpRegistryIcon(url);
40​
41 expect(first.type).toBe("image/svg+xml");
42 expect(concurrent.size).toBe(first.size);
43 expect(cached.size).toBe(first.size);
44 expect(fetchIcon).toHaveBeenCalledOnce();
45 expect(put).toHaveBeenCalledOnce();
46 });
47​
48 it("rejects oversized image responses before caching", async () => {
49 const url = "https://cdn.agentclientprotocol.com/registry/icons/oversized.svg";
50 fetchIcon.mockResolvedValue(
51 new Response("too large", {
52 status: 200,
53 headers: {
54 "content-length": String(513 * 1_024),
55 "content-type": "image/svg+xml",
56 },
57 }),
58 );
59​
60 await expect(loadCachedAcpRegistryIcon(url)).rejects.toThrow("too large");
61 expect(put).not.toHaveBeenCalled();
62 });
63​
64 it("falls back to the network when CacheStorage cannot be opened", async () => {
65 const url = "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg";
66 vi.stubGlobal("caches", {
67 open: vi.fn(async () => {
68 throw new Error("CacheStorage unavailable");
69 }),
70 });
71 fetchIcon.mockResolvedValue(
72 new Response("<svg/>", {
73 status: 200,
74 headers: { "content-type": "image/svg+xml" },
75 }),
76 );
77​
78 await expect(loadCachedAcpRegistryIcon(url)).resolves.toMatchObject({
79 type: "image/svg+xml",
80 });
81 expect(fetchIcon).toHaveBeenCalledWith(url, {
82 credentials: "omit",
83 redirect: "error",
84 referrerPolicy: "no-referrer",
85 });
86 });
87​
88 it("accepts only credential-free HTTPS URLs on the official CDN", () => {
89 expect(
90 resolveOfficialAcpRegistryIconUrl(
91 "https://cdn.agentclientprotocol.com/registry/icons/gemini.png",
92 ),
93 ).toBe("https://cdn.agentclientprotocol.com/registry/icons/gemini.png");
94 expect(
95 resolveOfficialAcpRegistryIconUrl("http://cdn.agentclientprotocol.com/icon.png"),
96 ).toBeNull();
97 expect(
98 resolveOfficialAcpRegistryIconUrl("https://cdn.agentclientprotocol.com.evil/icon.png"),
99 ).toBeNull();
100 expect(
101 resolveOfficialAcpRegistryIconUrl("https://user@cdn.agentclientprotocol.com/icon.png"),
102 ).toBeNull();
103 expect(
104 resolveOfficialAcpRegistryIconUrl("https://cdn.agentclientprotocol.com:8443/icon.png"),
105 ).toBeNull();
106 expect(officialAcpRegistryIconUrlForAgentId("kilo")).toBe(
107 "https://cdn.agentclientprotocol.com/registry/v1/latest/kilo.svg",
108 );
109 expect(officialAcpRegistryIconUrlForAgentId("../kilo")).toBeNull();
110 });
111});