Open History to see the last commit
1import { RegistryContext } from "@effect/atom-react";
2import { EnvironmentId } from "@t3tools/contracts";
3import { Atom, AtomRegistry } from "effect/reactivity";
4import { expect, it, vi } from "vite-plus/test";
5
6import { render } from "../../test/render";
7import { ProjectFavicon, type ProjectFaviconProject } from "./ProjectFavicon";
8import "../index.css";
9
10const state = vi.hoisted(() => ({
11 favicon: undefined as Atom.Atom<string | null> | undefined,
12 faviconByCwd: new Map<string, Atom.Atom<string | null>>(),
13 refresh: (() => {}) as () => void,
14}));
15vi.mock(import("../state/assets"), () => ({
16 projectFaviconUrlAtom: (target: { readonly cwd: string }) => {
17 const byCwd = state.faviconByCwd.get(target.cwd);
18 if (byCwd) return byCwd;
19 if (!state.favicon) throw new Error("Favicon resource not configured");
20 return state.favicon;
21 },
22 refreshProjectFaviconUrl: () => state.refresh(),
23}));
24
25function setup() {
26 state.refresh = () => {};
27 const activeResources = new Set<string>();
28 state.favicon = Atom.make((get) => {
29 activeResources.add("favicon");
30 get.addFinalizer(() => activeResources.delete("favicon"));
31 return null;
32 });
33 return { activeResources };
34}
35
36it("only activates the favicon resource after removing a custom icon", async () => {
37 const { activeResources } = setup();
38 const registry = AtomRegistry.make();
39 const project: ProjectFaviconProject = {
40 environmentId: EnvironmentId.make("test"),
41 workspaceRoot: "/project",
42 title: "Project",
43 faviconPath: "icon.png",
44 projectIcon: { kind: "monogram", text: "PR", color: "blue" },
45 };
46 const view = (projectIcon: ProjectFaviconProject["projectIcon"]) => (
47 <RegistryContext value={registry}>
48 <ProjectFavicon project={{ ...project, projectIcon }} />
49 </RegistryContext>
50 );
51 try {
52 await using screen = await render(view(project.projectIcon));
53 expect(activeResources.size).toBe(0);
54 await screen.rerender(view({ kind: "emoji", emoji: "🌱" }));
55 expect(activeResources.size).toBe(0);
56 await screen.rerender(view({ kind: "lucide", name: "leaf", color: "green" }));
57 expect(activeResources.size).toBe(0);
58 await screen.rerender(view(null));
59 expect(activeResources.size).toBe(1);
60 await screen.rerender(view(project.projectIcon));
61 await screen.rerender(view(null));
62 expect(activeResources.size).toBe(1);
63 } finally {
64 registry.dispose();
65 }
66});
67
68it("asks for a new URL when a remote icon's URL stops loading", async () => {
69 const registry = AtomRegistry.make();
70 const favicon = Atom.make<string | null>("http://127.0.0.1:9/api/assets/expired/favicon.svg");
71 const svg =
72 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"><rect width="1" height="1"/></svg>';
73 const freshUrl = URL.createObjectURL(new Blob([svg], { type: "image/svg+xml" }));
74 state.favicon = favicon;
75 state.refresh = () => registry.set(favicon, freshUrl);
76 const project: ProjectFaviconProject = {
77 environmentId: EnvironmentId.make("test"),
78 workspaceRoot: "/project",
79 title: "Project",
80 faviconPath: null,
81 projectIcon: null,
82 };
83 try {
84 await using screen = await render(
85 <RegistryContext value={registry}>
86 <ProjectFavicon project={project} />
87 </RegistryContext>,
88 );
89 await expect
90 .poll(() => screen.container.querySelector("img:not(.hidden)")?.getAttribute("src"))
91 .toBe(freshUrl);
92 } finally {
93 URL.revokeObjectURL(freshUrl);
94 registry.dispose();
95 }
96});
97
98it("asks for a new URL only once when the new URL fails too", async () => {
99 const registry = AtomRegistry.make();
100 const favicon = Atom.make<string | null>("http://127.0.0.1:9/api/assets/expired/favicon.svg");
101 const brokenUrl = "http://127.0.0.1:9/api/assets/broken/favicon.svg";
102 let refreshes = 0;
103 state.favicon = favicon;
104 state.refresh = () => {
105 refreshes += 1;
106 registry.set(favicon, brokenUrl);
107 };
108 const project: ProjectFaviconProject = {
109 environmentId: EnvironmentId.make("test"),
110 workspaceRoot: "/project",
111 title: "Project",
112 faviconPath: null,
113 projectIcon: null,
114 };
115 try {
116 await using screen = await render(
117 <RegistryContext value={registry}>
118 <ProjectFavicon project={project} />
119 </RegistryContext>,
120 );
121 await vi.waitFor(() => {
122 const image = screen.container.querySelector<HTMLImageElement>(`img[src="${brokenUrl}"]`);
123 expect(image?.complete).toBe(true);
124 });
125 expect(refreshes).toBe(1);
126 } finally {
127 registry.dispose();
128 }
129});
130
131it("shows the next project's icon when a group's own project has none", async () => {
132 const registry = AtomRegistry.make();
133 const svg =
134 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"><rect width="1" height="1"/></svg>';
135 const appIcon = `data:image/svg+xml,${encodeURIComponent(svg)}`;
136 state.faviconByCwd = new Map([
137 ["/repo", Atom.make<string | null>(null)],
138 ["/repo/apps/web", Atom.make<string | null>(appIcon)],
139 ["/repo/apps/docs", Atom.make<string | null>(null)],
140 ]);
141 const project = (workspaceRoot: string, title: string): ProjectFaviconProject => ({
142 environmentId: EnvironmentId.make("test"),
143 workspaceRoot,
144 title,
145 faviconPath: null,
146 projectIcon: null,
147 });
148 const root = project("/repo", "Sample Mono");
149 try {
150 await using screen = await render(
151 <RegistryContext value={registry}>
152 <ProjectFavicon project={root} fallbackProjects={[project("/repo/apps/web", "web")]} />
153 </RegistryContext>,
154 );
155 await expect
156 .poll(() => screen.container.querySelector("img:not(.hidden)")?.getAttribute("src"))
157 .toBe(appIcon);
158
159 await screen.rerender(
160 <RegistryContext value={registry}>
161 <ProjectFavicon project={root} fallbackProjects={[project("/repo/apps/docs", "docs")]} />
162 </RegistryContext>,
163 );
164 expect(screen.container.querySelector("img")).toBeNull();
165 expect(screen.container.textContent).toBe("SM");
166 } finally {
167 state.faviconByCwd = new Map();
168 registry.dispose();
169 }
170});
171
172it("keeps a remote icon's loading image hidden inside chat markdown", async () => {
173 const registry = AtomRegistry.make();
174 // Never answers, so the icon stays loading.
175 state.favicon = Atom.make<string | null>("http://10.255.255.1/api/assets/large/favicon.svg");
176 const project: ProjectFaviconProject = {
177 environmentId: EnvironmentId.make("test"),
178 workspaceRoot: "/project",
179 title: "Project",
180 faviconPath: null,
181 projectIcon: null,
182 };
183 try {
184 await using screen = await render(
185 <RegistryContext value={registry}>
186 <div className="chat-markdown">
187 <ProjectFavicon project={project} className="size-[1em]" />
188 </div>
189 </RegistryContext>,
190 );
191 const loading = screen.container.querySelector("img.hidden");
192 expect(loading).not.toBeNull();
193 expect(getComputedStyle(loading!).display).toBe("none");
194 } finally {
195 registry.dispose();
196 }
197});