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

preview.test.ts

210 lines8.7 KB

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