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

projectFaviconRoute.test.ts

171 lines6.0 KB

1import fs from "node:fs";
2import http from "node:http";
3import os from "node:os";
4import path from "node:path";
5​
6import { afterEach, describe, expect, it } from "vitest";
7import { tryHandleProjectFaviconRequest } from "./projectFaviconRoute";
8​
9interface HttpResponse {
10 statusCode: number;
11 contentType: string | null;
12 body: string;
14​
15const tempDirs: string[] = [];
16​
17function makeTempDir(prefix: string): string {
18 const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
19 tempDirs.push(dir);
20 return dir;
22​
23async function withRouteServer(run: (baseUrl: string) => Promise<void>): Promise<void> {
24 const server = http.createServer((req, res) => {
25 const url = new URL(req.url ?? "/", "http://127.0.0.1");
26 if (tryHandleProjectFaviconRequest(url, res)) {
27 return;
28 }
29 res.writeHead(404, { "Content-Type": "text/plain" });
30 res.end("Not Found");
31 });
32​
33 await new Promise<void>((resolve, reject) => {
34 server.listen(0, "127.0.0.1", (error?: Error) => {
35 if (error) {
36 reject(error);
37 return;
38 }
39 resolve();
40 });
41 });
42​
43 const address = server.address();
44 if (typeof address !== "object" || address === null) {
45 throw new Error("Expected server address to be an object");
46 }
47 const baseUrl = `http://127.0.0.1:${address.port}`;
48​
49 try {
50 await run(baseUrl);
51 } finally {
52 await new Promise<void>((resolve, reject) => {
53 server.close((error?: Error) => {
54 if (error) {
55 reject(error);
56 return;
57 }
58 resolve();
59 });
60 });
61 }
63​
64async function request(baseUrl: string, pathname: string): Promise<HttpResponse> {
65 const response = await fetch(`${baseUrl}${pathname}`);
66 return {
67 statusCode: response.status,
68 contentType: response.headers.get("content-type"),
69 body: await response.text(),
70 };
72​
73describe("tryHandleProjectFaviconRequest", () => {
74 afterEach(() => {
75 for (const dir of tempDirs.splice(0, tempDirs.length)) {
76 fs.rmSync(dir, { recursive: true, force: true });
77 }
78 });
79​
80 it("returns 400 when cwd is missing", async () => {
81 await withRouteServer(async (baseUrl) => {
82 const response = await request(baseUrl, "/api/project-favicon");
83 expect(response.statusCode).toBe(400);
84 expect(response.body).toBe("Missing cwd parameter");
85 });
86 });
87​
88 it("serves a well-known favicon file from the project root", async () => {
89 const projectDir = makeTempDir("t3code-favicon-route-root-");
90 fs.writeFileSync(path.join(projectDir, "favicon.svg"), "<svg>favicon</svg>", "utf8");
91​
92 await withRouteServer(async (baseUrl) => {
93 const pathname = `/api/project-favicon?cwd=${encodeURIComponent(projectDir)}`;
94 const response = await request(baseUrl, pathname);
95 expect(response.statusCode).toBe(200);
96 expect(response.contentType).toContain("image/svg+xml");
97 expect(response.body).toBe("<svg>favicon</svg>");
98 });
99 });
100​
101 it("resolves icon href from source files when no well-known favicon exists", async () => {
102 const projectDir = makeTempDir("t3code-favicon-route-source-");
103 const iconPath = path.join(projectDir, "public", "brand", "logo.svg");
104 fs.mkdirSync(path.dirname(iconPath), { recursive: true });
105 fs.writeFileSync(
106 path.join(projectDir, "index.html"),
107 '<link rel="icon" href="/brand/logo.svg">',
108 );
109 fs.writeFileSync(iconPath, "<svg>brand</svg>", "utf8");
110​
111 await withRouteServer(async (baseUrl) => {
112 const pathname = `/api/project-favicon?cwd=${encodeURIComponent(projectDir)}`;
113 const response = await request(baseUrl, pathname);
114 expect(response.statusCode).toBe(200);
115 expect(response.contentType).toContain("image/svg+xml");
116 expect(response.body).toBe("<svg>brand</svg>");
117 });
118 });
119​
120 it("resolves icon link when href appears before rel in HTML", async () => {
121 const projectDir = makeTempDir("t3code-favicon-route-html-order-");
122 const iconPath = path.join(projectDir, "public", "brand", "logo.svg");
123 fs.mkdirSync(path.dirname(iconPath), { recursive: true });
124 fs.writeFileSync(
125 path.join(projectDir, "index.html"),
126 '<link href="/brand/logo.svg" rel="icon">',
127 );
128 fs.writeFileSync(iconPath, "<svg>brand-html-order</svg>", "utf8");
129​
130 await withRouteServer(async (baseUrl) => {
131 const pathname = `/api/project-favicon?cwd=${encodeURIComponent(projectDir)}`;
132 const response = await request(baseUrl, pathname);
133 expect(response.statusCode).toBe(200);
134 expect(response.contentType).toContain("image/svg+xml");
135 expect(response.body).toBe("<svg>brand-html-order</svg>");
136 });
137 });
138​
139 it("resolves object-style icon metadata when href appears before rel", async () => {
140 const projectDir = makeTempDir("t3code-favicon-route-obj-order-");
141 const iconPath = path.join(projectDir, "public", "brand", "obj.svg");
142 fs.mkdirSync(path.dirname(iconPath), { recursive: true });
143 fs.mkdirSync(path.join(projectDir, "src"), { recursive: true });
144 fs.writeFileSync(
145 path.join(projectDir, "src", "root.tsx"),
146 'const links = [{ href: "/brand/obj.svg", rel: "icon" }];',
147 "utf8",
148 );
149 fs.writeFileSync(iconPath, "<svg>brand-obj-order</svg>", "utf8");
150​
151 await withRouteServer(async (baseUrl) => {
152 const pathname = `/api/project-favicon?cwd=${encodeURIComponent(projectDir)}`;
153 const response = await request(baseUrl, pathname);
154 expect(response.statusCode).toBe(200);
155 expect(response.contentType).toContain("image/svg+xml");
156 expect(response.body).toBe("<svg>brand-obj-order</svg>");
157 });
158 });
159​
160 it("serves a fallback favicon when no icon exists", async () => {
161 const projectDir = makeTempDir("t3code-favicon-route-fallback-");
162​
163 await withRouteServer(async (baseUrl) => {
164 const pathname = `/api/project-favicon?cwd=${encodeURIComponent(projectDir)}`;
165 const response = await request(baseUrl, pathname);
166 expect(response.statusCode).toBe(200);
167 expect(response.contentType).toContain("image/svg+xml");
168 expect(response.body).toContain('data-fallback="project-favicon"');
169 });
170 });
171});