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

ProjectFavicon.test.tsx

221 lines7.4 KB

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 lastResource: 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("../assets/assetUrls", () => ({
61 useAssetUrlState: (_environmentId: unknown, resource: unknown) => {
62 testState.lastResource = resource;
63 return { _tag: "Success", url: testState.faviconUrl };
64 },
65}));
66​
67import { ProjectFavicon } from "./ProjectFavicon";
68​
69type ProjectFaviconImageProps = {
70 readonly cacheKey: string;
71 readonly src: string;
72 readonly className?: string | undefined;
73 readonly fallbackIcon: ComponentType<{ className?: string }>;
74};
75​
76type ImageElement = ReactElement<{
77 readonly src: string;
78 readonly onLoad?: () => void;
79 readonly onError?: () => void;
80}>;
81​
82type ProjectFaviconImageElement = ReactElement<{
83 readonly children: [ReactElement | null, ImageElement | null, ImageElement | null];
84}>;
85​
86function resolveImageComponent(): {
87 readonly Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement;
88 readonly props: ProjectFaviconImageProps;
89} {
90 hooks.beginRender();
91 const element = ProjectFavicon({
92 environmentId: "environment-test" as EnvironmentId,
93 cwd: "/workspace-test",
94 projectName: "workspace-test",
95 }) as ReactElement<ProjectFaviconImageProps>;
96 hooks.reset();
97​
98 return {
99 Component: element.type as (props: ProjectFaviconImageProps) => ProjectFaviconImageElement,
100 props: element.props,
101 };
103​
104function renderImage(
105 Component: (props: ProjectFaviconImageProps) => ProjectFaviconImageElement,
106 props: ProjectFaviconImageProps,
107): ProjectFaviconImageElement {
108 hooks.beginRender();
109 return Component(props);
111​
112describe("ProjectFavicon", () => {
113 beforeEach(() => {
114 hooks.reset();
115 testState.faviconUrl = "https://environment.test/api/assets/token-a/v1-20-favicon.svg";
116 });
117​
118 it("shows a project-name icon when no favicon exists", () => {
119 testState.faviconUrl = `https://environment.test/api/assets/token/${PROJECT_FAVICON_FALLBACK_MARKER}`;
120​
121 const element = ProjectFavicon({
122 environmentId: "environment-test" as EnvironmentId,
123 cwd: "/workspace/analytics-db",
124 projectName: "analytics-db",
125 }) as ReactElement<{
126 readonly colorClassName?: string;
127 readonly emoji?: string;
128 readonly icon?: ComponentType<{ className?: string }>;
129 }>;
130​
131 expect(element.props.icon).toBeDefined();
132 expect(element.props.emoji).toBeUndefined();
133 expect(element.props.colorClassName).toContain("text-cyan-600");
134 });
135​
136 it("chooses a deterministic semantic icon", () => {
137 testState.faviconUrl = `https://environment.test/api/assets/token/${PROJECT_FAVICON_FALLBACK_MARKER}`;
138​
139 const element = ProjectFavicon({
140 environmentId: "environment-test" as EnvironmentId,
141 cwd: "/workspace/agent-runtime",
142 projectName: "agent-runtime",
143 }) as ReactElement<{
144 readonly colorClassName?: string;
145 readonly emoji?: string;
146 readonly icon?: ComponentType<{ className?: string }>;
147 }>;
148​
149 expect(element.props.icon).toBeDefined();
150 expect(element.props.emoji).toBeUndefined();
151 expect(element.props.colorClassName).toContain("text-violet-600");
152 });
153​
154 it("renders a saved Lucide icon and color ahead of an uploaded favicon", () => {
155 const element = ProjectFavicon({
156 environmentId: "environment-test" as EnvironmentId,
157 cwd: "/workspace/test",
158 projectName: "test",
159 faviconPath: "brand/icon.svg",
160 projectIcon: { kind: "lucide", name: "alarm-clock", color: "violet" },
161 }) as ReactElement<{
162 readonly children: ReactElement<{
163 readonly children: ReactElement<{ readonly name: string; readonly className: string }>;
164 }>;
165 readonly className: string;
166 }>;
167​
168 expect(element.props.children.props.children.props.name).toBe("alarm-clock");
169 expect(element.props.className).toContain("text-violet-600");
170 expect(element.props.children.props.children.props.className).toContain("text-violet-600");
171 });
172​
173 it("renders a saved emoji ahead of an uploaded favicon", () => {
174 const element = ProjectFavicon({
175 environmentId: "environment-test" as EnvironmentId,
176 cwd: "/workspace/test",
177 projectName: "test",
178 faviconPath: "brand/icon.svg",
179 projectIcon: { kind: "emoji", emoji: "🦄" },
180 }) as ReactElement<{ readonly emoji: string }>;
181​
182 expect(element.props.emoji).toBe("🦄");
183 });
184​
185 it("falls back when the displayed favicon fails without discarding a valid older image early", () => {
186 const { Component, props } = resolveImageComponent();
187 const initialLoadingImage = renderImage(Component, props).props.children[2];
188 initialLoadingImage?.props.onLoad?.();
189​
190 const refreshedProps = {
191 ...props,
192 src: "https://environment.test/api/assets/token-b/v1-20-favicon.svg",
193 };
194 const refreshing = renderImage(Component, refreshedProps).props.children;
195 expect(refreshing[1]?.props.src).toBe(props.src);
196 refreshing[2]?.props.onError?.();
197​
198 const afterRefreshError = renderImage(Component, refreshedProps).props.children;
199 expect(afterRefreshError[1]?.props.src).toBe(props.src);
200 afterRefreshError[1]?.props.onError?.();
201​
202 const afterDisplayedError = renderImage(Component, refreshedProps).props.children;
203 expect(afterDisplayedError[0]).not.toBeNull();
204 expect(afterDisplayedError[1]).toBeNull();
205 });
206​
207 it("requests a saved favicon path when one is set", () => {
208 ProjectFavicon({
209 environmentId: "environment-test" as EnvironmentId,
210 cwd: "/workspace-test",
211 projectName: "workspace-test",
212 faviconPath: "brand/icon.svg",
213 });
214​
215 expect(testState.lastResource).toEqual({
216 _tag: "project-favicon",
217 cwd: "/workspace-test",
218 path: "brand/icon.svg",
219 });
220 });
221});