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

preview.test.ts

141 lines5.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 isLoopbackHost,
5 newPreviewTabId,
6 normalizePreviewUrl,
7 PreviewUrlNormalizationError,
8 resolveAddressBarInput,
9} from "./preview.ts";
10​
11describe("resolveAddressBarInput", () => {
12 it("opens what users type as addresses", () => {
13 expect(resolveAddressBarInput("localhost:5173")).toBe("http://localhost:5173/");
14 expect(resolveAddressBarInput("127.0.0.1:3000/path")).toBe("http://127.0.0.1:3000/path");
15 expect(resolveAddressBarInput("my-box.tailnet.ts.net")).toBe("https://my-box.tailnet.ts.net/");
16 expect(resolveAddressBarInput("cnn.com")).toBe("https://cnn.com/");
17 expect(resolveAddressBarInput("devbox:8080")).toBe("https://devbox:8080/");
18 expect(resolveAddressBarInput(" https://example.com/a b ")).toBe("https://example.com/a%20b");
19 expect(resolveAddressBarInput("example.com:8080")).toBe("https://example.com:8080/");
20 expect(resolveAddressBarInput("[::1]:3000")).toBe("http://[::1]:3000/");
21 expect(resolveAddressBarInput("192.168.1.5:3000")).toBe("https://192.168.1.5:3000/");
22 });
23​
24 it("searches for text that is not an address", () => {
25 expect(resolveAddressBarInput("weather")).toBe("https://duckduckgo.com/?q=weather");
26 expect(resolveAddressBarInput("how to center a div")).toBe(
27 "https://duckduckgo.com/?q=how%20to%20center%20a%20div",
28 );
29 expect(resolveAddressBarInput("what is cnn.com")).toBe(
30 "https://duckduckgo.com/?q=what%20is%20cnn.com",
31 );
32 expect(resolveAddressBarInput("what is https://example.com")).toBe(
33 "https://duckduckgo.com/?q=what%20is%20https%3A%2F%2Fexample.com",
34 );
35 expect(resolveAddressBarInput("what is 10:30")).toBe(
36 "https://duckduckgo.com/?q=what%20is%2010%3A30",
37 );
38 expect(resolveAddressBarInput("note: buy milk")).toBe(
39 "https://duckduckgo.com/?q=note%3A%20buy%20milk",
40 );
41 });
42​
43 it("still rejects empty input and unsupported schemes", () => {
44 expect(() => resolveAddressBarInput(" ")).toThrow(PreviewUrlNormalizationError);
45 expect(() => resolveAddressBarInput("ftp://example.com")).toThrow(PreviewUrlNormalizationError);
46 expect(() => resolveAddressBarInput("mailto:alice@example.com")).toThrow(
47 PreviewUrlNormalizationError,
48 );
49 expect(() => resolveAddressBarInput("data:text/plain,hello")).toThrow(
50 PreviewUrlNormalizationError,
51 );
52 for (const input of ["ftp:21", "tel:5551234"]) {
53 expect(() => resolveAddressBarInput(input)).toThrow(PreviewUrlNormalizationError);
54 }
55 expect(resolveAddressBarInput("devbox:8080")).toBe("https://devbox:8080/");
56 });
57});
58​
59describe("newPreviewTabId", () => {
60 it("returns a unique tab id every call", () => {
61 const a = newPreviewTabId();
62 const b = newPreviewTabId();
63 expect(a).not.toBe(b);
64 expect(a.startsWith("tab_")).toBe(true);
65 });
66});
67​
68describe("isLoopbackHost", () => {
69 it.each(["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"])("%s is loopback", (host) => {
70 expect(isLoopbackHost(host)).toBe(true);
71 });
72​
73 it.each(["example.com", "192.168.1.10", "10.0.0.1", ""])("%s is not loopback", (host) => {
74 expect(isLoopbackHost(host)).toBe(false);
75 });
76});
77​
78describe("normalizePreviewUrl", () => {
79 it("treats bare loopback hosts as http", () => {
80 expect(normalizePreviewUrl("localhost:5173")).toBe("http://localhost:5173/");
81 expect(normalizePreviewUrl("127.0.0.1:3000")).toBe("http://127.0.0.1:3000/");
82 });
83​
84 it("treats bare public hosts as https", () => {
85 expect(normalizePreviewUrl("example.com")).toBe("https://example.com/");
86 });
87​
88 it("respects explicit schemes", () => {
89 expect(normalizePreviewUrl("https://localhost:5173")).toBe("https://localhost:5173/");
90 expect(normalizePreviewUrl("http://example.com/path?q=1")).toBe("http://example.com/path?q=1");
91 });
92​
93 it("rejects empty input", () => {
94 try {
95 normalizePreviewUrl(" ");
96 expect.unreachable("expected URL normalization to fail");
97 } catch (error) {
98 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
99 expect(error).toMatchObject({ inputLength: 3, reason: "empty" });
100 expect(error).not.toHaveProperty("rawUrl");
101 expect("cause" in (error as object)).toBe(false);
102 }
103 });
104​
105 it("rejects unsupported protocols", () => {
106 try {
107 normalizePreviewUrl("ftp://example.com");
108 expect.unreachable("expected URL normalization to fail");
109 } catch (error) {
110 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
111 expect(error).toMatchObject({
112 inputLength: "ftp://example.com".length,
113 reason: "unsupported-protocol",
114 protocol: "ftp:",
115 });
116 }
117 });
118​
119 it("rejects unparseable input without retaining credentials or tokens", () => {
120 const rawUrl = "https://user:password@example.com:bad/path?access_token=secret#fragment";
121 try {
122 normalizePreviewUrl(rawUrl);
123 expect.unreachable("expected URL normalization to fail");
124 } catch (error) {
125 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
126 expect(error).toMatchObject({
127 inputLength: rawUrl.length,
128 reason: "parse",
129 protocol: "https:",
130 });
131 expect(error).not.toHaveProperty("rawUrl");
132 expect((error as PreviewUrlNormalizationError).cause).toBeInstanceOf(Error);
133 expect((error as PreviewUrlNormalizationError).message).not.toContain(
134 ((error as PreviewUrlNormalizationError).cause as Error).message,
135 );
136 expect((error as PreviewUrlNormalizationError).message).not.toMatch(
137 /user|password|access_token|secret|fragment/,
138 );
139 }
140 });
141});