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

preview.test.ts

240 lines9.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 isLoopbackHost,
5 newPreviewTabId,
6 normalizePreviewUrl,
7 PreviewUrlNormalizationError,
8 resolveAddressBarInput,
9 previewPdfAssetUrl,
10 resolveSearchUrlTemplate,
11} from "./preview.ts";
12​
13describe("previewPdfAssetUrl", () => {
14 it.each([
15 "http://localhost:6497",
16 "https://host.tailnet.test",
17 "https://environment.tunnel.test",
18 ])("loads PDF bytes through the client's connection at %s", (base) => {
19 expect(
20 previewPdfAssetUrl(
21 "http://127.0.0.1:14537/api/assets/signed-token/Study%20guide.PDF?revision=2#page=8",
22 base,
23 ),
24 ).toBe(`${base}/api/assets/signed-token/Study%20guide.PDF?revision=2#page=8`);
25 });
26​
27 it.each([
28 "http://localhost:6497/api/assets/token/index.html",
29 "https://example.com/paper.pdf",
30 "file:///api/assets/token/paper.pdf",
31 "javascript:alert(1)",
32 "invalid",
33 ])("leaves non-asset documents on their browser session: %s", (url) => {
34 expect(previewPdfAssetUrl(url, "https://environment.test")).toBeNull();
35 });
36​
37 it("waits for the environment connection", () => {
38 expect(previewPdfAssetUrl("http://localhost/api/assets/token/paper.pdf", null)).toBeNull();
39 });
40​
41 it.each(["another-environment.test", "127.0.0.2.example.com", "128.0.0.1"])(
42 "preserves matching PDF URLs on unrelated host %s",
43 (host) => {
44 const source = `https://${host}/api/assets/token/guide.pdf?revision=2#page=3`;
45 expect(previewPdfAssetUrl(source, "https://environment.test")).toBe(source);
46 },
47 );
48​
49 it("keeps URLs from the connected environment", () => {
50 const source = "https://environment.test/api/assets/token/guide.pdf#page=2";
51 expect(previewPdfAssetUrl(source, "https://environment.test")).toBe(source);
52 });
53​
54 it.each(["127.0.0.2", "127.255.255.254", "localhost.", "[::1]", "0.0.0.0"])(
55 "remaps local assets on %s through the client connection",
56 (host) => {
57 expect(
58 previewPdfAssetUrl(
59 `http://${host}:14537/api/assets/token/guide.pdf?revision=2#page=3`,
60 "https://remote.test",
61 ),
62 ).toBe("https://remote.test/api/assets/token/guide.pdf?revision=2#page=3");
63 },
64 );
65});
66​
67describe("resolveAddressBarInput", () => {
68 it.each([
69 ["localhost:3000/?next=https://example.com", "http://localhost:3000/?next=https://example.com"],
70 ["127.0.0.1:3000/#https://example.com", "http://127.0.0.1:3000/#https://example.com"],
71 ["example.com/https://example.org", "https://example.com/https://example.org"],
72 ])("opens the outer address when it contains another URL: %s", (input, expected) => {
73 expect(resolveAddressBarInput(input)).toBe(expected);
74 });
75​
76 it("opens what users type as addresses", () => {
77 expect(resolveAddressBarInput("localhost:5173")).toBe("http://localhost:5173/");
78 expect(resolveAddressBarInput("127.0.0.1:3000/path")).toBe("http://127.0.0.1:3000/path");
79 expect(resolveAddressBarInput("my-box.tailnet.ts.net")).toBe("https://my-box.tailnet.ts.net/");
80 expect(resolveAddressBarInput("cnn.com")).toBe("https://cnn.com/");
81 expect(resolveAddressBarInput("devbox:8080")).toBe("https://devbox:8080/");
82 expect(resolveAddressBarInput(" https://example.com/a b ")).toBe("https://example.com/a%20b");
83 expect(resolveAddressBarInput("example.com:8080")).toBe("https://example.com:8080/");
84 expect(resolveAddressBarInput("[::1]:3000")).toBe("http://[::1]:3000/");
85 expect(resolveAddressBarInput("192.168.1.5:3000")).toBe("https://192.168.1.5:3000/");
86 });
87​
88 it("searches for text that is not an address", () => {
89 expect(resolveAddressBarInput("weather")).toBe("https://duckduckgo.com/?q=weather");
90 expect(resolveAddressBarInput("how to center a div")).toBe(
91 "https://duckduckgo.com/?q=how%20to%20center%20a%20div",
92 );
93 expect(resolveAddressBarInput("what is cnn.com")).toBe(
94 "https://duckduckgo.com/?q=what%20is%20cnn.com",
95 );
96 expect(resolveAddressBarInput("what is https://example.com")).toBe(
97 "https://duckduckgo.com/?q=what%20is%20https%3A%2F%2Fexample.com",
98 );
99 expect(resolveAddressBarInput("what is 10:30")).toBe(
100 "https://duckduckgo.com/?q=what%20is%2010%3A30",
101 );
102 expect(resolveAddressBarInput("note: buy milk")).toBe(
103 "https://duckduckgo.com/?q=note%3A%20buy%20milk",
104 );
105 });
106​
107 it("still rejects empty input and unsupported schemes", () => {
108 expect(() => resolveAddressBarInput(" ")).toThrow(PreviewUrlNormalizationError);
109 expect(() => resolveAddressBarInput("ftp://example.com")).toThrow(PreviewUrlNormalizationError);
110 expect(() => resolveAddressBarInput("mailto:alice@example.com")).toThrow(
111 PreviewUrlNormalizationError,
112 );
113 expect(() => resolveAddressBarInput("data:text/plain,hello")).toThrow(
114 PreviewUrlNormalizationError,
115 );
116 for (const input of ["ftp:21", "tel:5551234"]) {
117 expect(() => resolveAddressBarInput(input)).toThrow(PreviewUrlNormalizationError);
118 }
119 expect(resolveAddressBarInput("devbox:8080")).toBe("https://devbox:8080/");
120 });
121​
122 it("searches with the given template and still opens addresses directly", () => {
123 const google = resolveSearchUrlTemplate({
124 browserSearchEngine: "google",
125 browserCustomSearchUrl: "",
126 });
127 expect(resolveAddressBarInput("how to center a div", google)).toBe(
128 "https://www.google.com/search?q=how%20to%20center%20a%20div",
129 );
130 expect(resolveAddressBarInput("cnn.com", google)).toBe("https://cnn.com/");
131 expect(resolveAddressBarInput("a&b", "https://search.example/?q=%s&lang=en")).toBe(
132 "https://search.example/?q=a%26b&lang=en",
133 );
134 });
135});
136​
137describe("resolveSearchUrlTemplate", () => {
138 it("uses a valid custom template and falls back to DuckDuckGo for an unusable one", () => {
139 expect(
140 resolveSearchUrlTemplate({
141 browserSearchEngine: "custom",
142 browserCustomSearchUrl: " https://search.example/?q=%s ",
143 }),
144 ).toBe("https://search.example/?q=%s");
145 for (const browserCustomSearchUrl of [
146 "https://search.example/?q=",
147 "javascript:alert('%s')",
148 "https://%s.example.com/",
149 "https://%s@search.example/?q=%s",
150 ]) {
151 expect(
152 resolveSearchUrlTemplate({ browserSearchEngine: "custom", browserCustomSearchUrl }),
153 ).toBe("https://duckduckgo.com/?q=%s");
154 }
155 });
156});
157​
158describe("newPreviewTabId", () => {
159 it("returns a unique tab id every call", () => {
160 const a = newPreviewTabId();
161 const b = newPreviewTabId();
162 expect(a).not.toBe(b);
163 expect(a.startsWith("tab_")).toBe(true);
164 });
165});
166​
167describe("isLoopbackHost", () => {
168 it.each(["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"])("%s is loopback", (host) => {
169 expect(isLoopbackHost(host)).toBe(true);
170 });
171​
172 it.each(["example.com", "192.168.1.10", "10.0.0.1", ""])("%s is not loopback", (host) => {
173 expect(isLoopbackHost(host)).toBe(false);
174 });
175});
176​
177describe("normalizePreviewUrl", () => {
178 it("treats bare loopback hosts as http", () => {
179 expect(normalizePreviewUrl("localhost:5173")).toBe("http://localhost:5173/");
180 expect(normalizePreviewUrl("127.0.0.1:3000")).toBe("http://127.0.0.1:3000/");
181 });
182​
183 it("treats bare public hosts as https", () => {
184 expect(normalizePreviewUrl("example.com")).toBe("https://example.com/");
185 });
186​
187 it("respects explicit schemes", () => {
188 expect(normalizePreviewUrl("https://localhost:5173")).toBe("https://localhost:5173/");
189 expect(normalizePreviewUrl("http://example.com/path?q=1")).toBe("http://example.com/path?q=1");
190 });
191​
192 it("rejects empty input", () => {
193 try {
194 normalizePreviewUrl(" ");
195 expect.unreachable("expected URL normalization to fail");
196 } catch (error) {
197 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
198 expect(error).toMatchObject({ inputLength: 3, reason: "empty" });
199 expect(error).not.toHaveProperty("rawUrl");
200 expect("cause" in (error as object)).toBe(false);
201 }
202 });
203​
204 it("rejects unsupported protocols", () => {
205 try {
206 normalizePreviewUrl("ftp://example.com");
207 expect.unreachable("expected URL normalization to fail");
208 } catch (error) {
209 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
210 expect(error).toMatchObject({
211 inputLength: "ftp://example.com".length,
212 reason: "unsupported-protocol",
213 protocol: "ftp:",
214 });
215 }
216 });
217​
218 it("rejects unparseable input without retaining credentials or tokens", () => {
219 const rawUrl = "https://user:password@example.com:bad/path?access_token=secret#fragment";
220 try {
221 normalizePreviewUrl(rawUrl);
222 expect.unreachable("expected URL normalization to fail");
223 } catch (error) {
224 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
225 expect(error).toMatchObject({
226 inputLength: rawUrl.length,
227 reason: "parse",
228 protocol: "https:",
229 });
230 expect(error).not.toHaveProperty("rawUrl");
231 expect((error as PreviewUrlNormalizationError).cause).toBeInstanceOf(Error);
232 expect((error as PreviewUrlNormalizationError).message).not.toContain(
233 ((error as PreviewUrlNormalizationError).cause as Error).message,
234 );
235 expect((error as PreviewUrlNormalizationError).message).not.toMatch(
236 /user|password|access_token|secret|fragment/,
237 );
238 }
239 });
240});