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

preview.test.ts

196 lines7.9 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 "preserves matching PDF URLs on unrelated host %s",
36 (host) => {
37 const source = `https://${host}/api/assets/token/guide.pdf?revision=2#page=3`;
38 expect(previewPdfAssetUrl(source, "https://environment.test")).toBe(source);
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("192.168.1.5:3000")).toBe("https://192.168.1.5:3000/");
79 });
80​
81 it("searches for text that is not an address", () => {
82 expect(resolveAddressBarInput("weather")).toBe("https://duckduckgo.com/?q=weather");
83 expect(resolveAddressBarInput("how to center a div")).toBe(
84 "https://duckduckgo.com/?q=how%20to%20center%20a%20div",
85 );
86 expect(resolveAddressBarInput("what is cnn.com")).toBe(
87 "https://duckduckgo.com/?q=what%20is%20cnn.com",
88 );
89 expect(resolveAddressBarInput("what is https://example.com")).toBe(
90 "https://duckduckgo.com/?q=what%20is%20https%3A%2F%2Fexample.com",
91 );
92 expect(resolveAddressBarInput("what is 10:30")).toBe(
93 "https://duckduckgo.com/?q=what%20is%2010%3A30",
94 );
95 expect(resolveAddressBarInput("note: buy milk")).toBe(
96 "https://duckduckgo.com/?q=note%3A%20buy%20milk",
97 );
98 });
99​
100 it("still rejects empty input and unsupported schemes", () => {
101 expect(() => resolveAddressBarInput(" ")).toThrow(PreviewUrlNormalizationError);
102 expect(() => resolveAddressBarInput("ftp://example.com")).toThrow(PreviewUrlNormalizationError);
103 expect(() => resolveAddressBarInput("mailto:alice@example.com")).toThrow(
104 PreviewUrlNormalizationError,
105 );
106 expect(() => resolveAddressBarInput("data:text/plain,hello")).toThrow(
107 PreviewUrlNormalizationError,
108 );
109 for (const input of ["ftp:21", "tel:5551234"]) {
110 expect(() => resolveAddressBarInput(input)).toThrow(PreviewUrlNormalizationError);
111 }
112 expect(resolveAddressBarInput("devbox:8080")).toBe("https://devbox:8080/");
113 });
114​
115 it("searches with the given template and still opens addresses directly", () => {
116 const google = resolveSearchUrlTemplate({
117 browserSearchEngine: "google",
118 browserCustomSearchUrl: "",
119 });
120 expect(resolveAddressBarInput("how to center a div", google)).toBe(
121 "https://www.google.com/search?q=how%20to%20center%20a%20div",
122 );
123 expect(resolveAddressBarInput("cnn.com", google)).toBe("https://cnn.com/");
124 expect(resolveAddressBarInput("a&b", "https://search.example/?q=%s&lang=en")).toBe(
125 "https://search.example/?q=a%26b&lang=en",
126 );
127 });
128});
129​
130describe("resolveSearchUrlTemplate", () => {
131 it("uses a valid custom template and falls back to DuckDuckGo for an unusable one", () => {
132 expect(
133 resolveSearchUrlTemplate({
134 browserSearchEngine: "custom",
135 browserCustomSearchUrl: " https://search.example/?q=%s ",
136 }),
137 ).toBe("https://search.example/?q=%s");
138 for (const browserCustomSearchUrl of [
139 "https://search.example/?q=",
140 "javascript:alert('%s')",
141 "https://%s.example.com/",
142 "https://%s@search.example/?q=%s",
143 ]) {
144 expect(
145 resolveSearchUrlTemplate({ browserSearchEngine: "custom", browserCustomSearchUrl }),
146 ).toBe("https://duckduckgo.com/?q=%s");
147 }
148 });
149});
150​
151describe("normalizePreviewUrl", () => {
152 it("tells agents how to open a blank tab and which URLs are accepted", () => {
153 expect(() => normalizePreviewUrl("about:blank")).toThrow(
154 /preview_open, omit url when creating a new tab[^.]*blank tab/i,
155 );
156 expect(() => normalizePreviewUrl("about:blank")).toThrow(/http\(s\)/i);
157 expect(() => normalizePreviewUrl("about:blank")).toThrow(/schemeless host/i);
158 });
159​
160 it("rejects unparseable input without retaining credentials or tokens", () => {
161 const rawUrl = "https://user:password@example.com:bad/path?access_token=secret#fragment";
162 try {
163 normalizePreviewUrl(rawUrl);
164 expect.unreachable("expected URL normalization to fail");
165 } catch (error) {
166 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
167 expect(error).toMatchObject({
168 inputLength: rawUrl.length,
169 reason: "parse",
170 protocol: "https:",
171 });
172 expect(error).not.toHaveProperty("rawUrl");
173 expect((error as PreviewUrlNormalizationError).cause).toBeInstanceOf(Error);
174 expect((error as PreviewUrlNormalizationError).message).not.toContain(
175 ((error as PreviewUrlNormalizationError).cause as Error).message,
176 );
177 expect((error as PreviewUrlNormalizationError).message).not.toMatch(
178 /user|password|access_token|secret|fragment/,
179 );
180 }
181 });
182​
183 it("reports explicit non-web schemes instead of reading them as a host", () => {
184 for (const [rawUrl, protocol] of [
185 ["about:blank", "about:"],
186 ["mailto:alice@example.com", "mailto:"],
187 ["javascript:alert(1)", "javascript:"],
188 ] as const) {
189 expect(() => normalizePreviewUrl(rawUrl)).toThrow(
190 expect.objectContaining({ reason: "unsupported-protocol", protocol }),
191 );
192 }
193 expect(normalizePreviewUrl("localhost:3000")).toBe("http://localhost:3000/");
194 expect(normalizePreviewUrl("devbox:8080")).toBe("https://devbox:8080/");
195 });
196});