Open History to see the last commit
1import type { ComponentType, Dispatch, ReactElement, SetStateAction } from "react";
2import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
3import type { EnvironmentId } from "@t3tools/contracts";
4import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon";
5
6const testState = vi.hoisted(() => ({
7 faviconUrl: "https://environment.test/api/assets/token-a/v1-20-favicon.svg",
8 lastTarget: null as unknown,
9}));
10
11const hooks = vi.hoisted(() => {
12 let cursor = 0;
13 let slots: unknown[] = [];
14 const nextIndex = () => cursor++;
15
16 return {
17 beginRender() {
18 cursor = 0;
19 },
20 reset() {
21 cursor = 0;
22 slots = [];
23 },
24 useMemoCache(size: number): unknown[] {
25 const index = nextIndex();
26 if (!slots[index]) {
27 slots[index] = Array.from({ length: size }, () => Symbol.for("react.memo_cache_sentinel"));
28 }
29 return slots[index] as unknown[];
30 },
31 useState<T>(initialValue: T | (() => T)): [T, Dispatch<SetStateAction<T>>] {
32 const index = nextIndex();
33 if (index >= slots.length) {
34 slots[index] =
35 typeof initialValue === "function" ? (initialValue as () => T)() : initialValue;
36 }
37 const setValue: Dispatch<SetStateAction<T>> = (nextValue) => {
38 const previous = slots[index] as T;
39 slots[index] =
40 typeof nextValue === "function" ? (nextValue as (value: T) => T)(previous) : nextValue;
41 };
42 return [slots[index] as T, setValue];
43 },
44 };
45});
46
47vi.mock("react", async (importOriginal) => {
48 const actual = await importOriginal<typeof import("react")>();
49 return {
50 ...actual,
51 useState: hooks.useState,
52 };
53});
54
55vi.mock("react/compiler-runtime", () => ({ c: hooks.useMemoCache }));
56vi.mock("lucide-react/dynamic", () => ({
57 DynamicIcon: "dynamic-icon",
58 iconNames: ["alarm-clock", "folder-code"],
59}));
60vi.mock("@effect/atom-react", () => ({
61 useAtomValue: () => testState.faviconUrl,
62}));
63vi.mock("../state/assets", () => ({
64 projectFaviconUrlAtom: (input: unknown) => {
65 testState.lastTarget = input;
66 },
67}));
68
69import { ProjectFavicon } from "./ProjectFavicon";
70
71type ProjectFaviconImageProps = {
72 readonly cacheKey: string;
73 readonly src: string;
74 readonly className?: string | undefined;
75 readonly fallbackIcon: ComponentType<{ className?: string }>;
76};
77
78type ImageElement = ReactElement<{
79 readonly src: string;
80 readonly onLoad?: () => void;
81 readonly onError?: () => void;
82}>;
83
84type ProjectFaviconImageElement = ReactElement<{
85 readonly children: [ReactElement | null, ImageElement | null, ImageElement | null];
86}>;
87
88function resolveImageComponent(): {
89 readonly Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement;
90 readonly props: ProjectFaviconImageProps;
91} {
92 hooks.beginRender();
93 const element = ProjectFavicon({
94 environmentId: "environment-test" as EnvironmentId,
95 cwd: "/workspace-test",
96 projectName: "workspace-test",
97 }) as ReactElement<ProjectFaviconImageProps>;
98 hooks.reset();
99
100 return {
101 Component: element.type as (props: ProjectFaviconImageProps) => ProjectFaviconImageElement,
102 props: element.props,
103 };
104}
105
106function renderImage(
107 Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement,
108 props: ProjectFaviconImageProps,
109): ProjectFaviconImageElement {
110 hooks.beginRender();
111 return Component(props);
112}
113
114describe("ProjectFavicon", () => {
115 beforeEach(() => {
116 hooks.reset();
117 testState.faviconUrl = "https://environment.test/api/assets/token-a/v1-20-favicon.svg";
118 });
119
120 it("shows a project-name icon when no favicon exists", () => {
121 testState.faviconUrl = `https://environment.test/api/assets/token/${PROJECT_FAVICON_FALLBACK_MARKER}`;
122
123 const element = ProjectFavicon({
124 environmentId: "environment-test" as EnvironmentId,
125 cwd: "/workspace/analytics-db",
126 projectName: "analytics-db",
127 }) as ReactElement<{
128 readonly colorClassName?: string;
129 readonly emoji?: string;
130 readonly icon?: ComponentType<{ className?: string }>;
131 }>;
132
133 expect(element.props.icon).toBeDefined();
134 expect(element.props.emoji).toBeUndefined();
135 expect(element.props.colorClassName).toContain("text-cyan-600");
136 });
137
138 it("chooses a deterministic semantic icon", () => {
139 testState.faviconUrl = `https://environment.test/api/assets/token/${PROJECT_FAVICON_FALLBACK_MARKER}`;
140
141 const element = ProjectFavicon({
142 environmentId: "environment-test" as EnvironmentId,
143 cwd: "/workspace/agent-runtime",
144 projectName: "agent-runtime",
145 }) as ReactElement<{
146 readonly colorClassName?: string;
147 readonly emoji?: string;
148 readonly icon?: ComponentType<{ className?: string }>;
149 }>;
150
151 expect(element.props.icon).toBeDefined();
152 expect(element.props.emoji).toBeUndefined();
153 expect(element.props.colorClassName).toContain("text-violet-600");
154 });
155
156 it("renders a saved Lucide icon and color ahead of an uploaded favicon", () => {
157 const element = ProjectFavicon({
158 environmentId: "environment-test" as EnvironmentId,
159 cwd: "/workspace/test",
160 projectName: "test",
161 faviconPath: "brand/icon.svg",
162 projectIcon: { kind: "lucide", name: "alarm-clock", color: "violet" },
163 }) as ReactElement<{
164 readonly children: ReactElement<{
165 readonly children: ReactElement<{ readonly name: string; readonly className: string }>;
166 }>;
167 readonly className: string;
168 }>;
169
170 expect(element.props.children.props.children.props.name).toBe("alarm-clock");
171 expect(element.props.className).toContain("text-violet-600");
172 expect(element.props.children.props.children.props.className).toContain("text-violet-600");
173 });
174
175 it("renders a saved emoji ahead of an uploaded favicon", () => {
176 const element = ProjectFavicon({
177 environmentId: "environment-test" as EnvironmentId,
178 cwd: "/workspace/test",
179 projectName: "test",
180 faviconPath: "brand/icon.svg",
181 projectIcon: { kind: "emoji", emoji: "🦄" },
182 }) as ReactElement<{ readonly emoji: string }>;
183
184 expect(element.props.emoji).toBe("🦄");
185 });
186
187 it("falls back when the displayed favicon fails without discarding a valid older image early", () => {
188 const { Component, props } = resolveImageComponent();
189 const initialLoadingImage = renderImage(Component, props).props.children[2];
190 initialLoadingImage?.props.onLoad?.();
191
192 const refreshedProps = {
193 ...props,
194 src: "https://environment.test/api/assets/token-b/v1-20-favicon.svg",
195 };
196 const refreshing = renderImage(Component, refreshedProps).props.children;
197 expect(refreshing[1]?.props.src).toBe(props.src);
198 refreshing[2]?.props.onError?.();
199
200 const afterRefreshError = renderImage(Component, refreshedProps).props.children;
201 expect(afterRefreshError[1]?.props.src).toBe(props.src);
202 afterRefreshError[1]?.props.onError?.();
203
204 const afterDisplayedError = renderImage(Component, refreshedProps).props.children;
205 expect(afterDisplayedError[0]).not.toBeNull();
206 expect(afterDisplayedError[1]).toBeNull();
207 });
208
209 it("requests a saved favicon path when one is set", () => {
210 ProjectFavicon({
211 environmentId: "environment-test" as EnvironmentId,
212 cwd: "/workspace-test",
213 projectName: "workspace-test",
214 faviconPath: "brand/icon.svg",
215 });
216
217 expect(testState.lastTarget).toMatchObject({
218 environmentId: "environment-test",
219 cwd: "/workspace-test",
220 faviconPath: "brand/icon.svg",
221 });
222 });
223});