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

preview.test.ts

92 lines3.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import {
4 isLoopbackHost,
5 newPreviewTabId,
6 normalizePreviewUrl,
7 PreviewUrlNormalizationError,
8} from "./preview.ts";
9​
10describe("newPreviewTabId", () => {
11 it("returns a unique tab id every call", () => {
12 const a = newPreviewTabId();
13 const b = newPreviewTabId();
14 expect(a).not.toBe(b);
15 expect(a.startsWith("tab_")).toBe(true);
16 });
17});
18​
19describe("isLoopbackHost", () => {
20 it.each(["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"])("%s is loopback", (host) => {
21 expect(isLoopbackHost(host)).toBe(true);
22 });
23​
24 it.each(["example.com", "192.168.1.10", "10.0.0.1", ""])("%s is not loopback", (host) => {
25 expect(isLoopbackHost(host)).toBe(false);
26 });
27});
28​
29describe("normalizePreviewUrl", () => {
30 it("treats bare loopback hosts as http", () => {
31 expect(normalizePreviewUrl("localhost:5173")).toBe("http://localhost:5173/");
32 expect(normalizePreviewUrl("127.0.0.1:3000")).toBe("http://127.0.0.1:3000/");
33 });
34​
35 it("treats bare public hosts as https", () => {
36 expect(normalizePreviewUrl("example.com")).toBe("https://example.com/");
37 });
38​
39 it("respects explicit schemes", () => {
40 expect(normalizePreviewUrl("https://localhost:5173")).toBe("https://localhost:5173/");
41 expect(normalizePreviewUrl("http://example.com/path?q=1")).toBe("http://example.com/path?q=1");
42 });
43​
44 it("rejects empty input", () => {
45 try {
46 normalizePreviewUrl(" ");
47 expect.unreachable("expected URL normalization to fail");
48 } catch (error) {
49 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
50 expect(error).toMatchObject({ inputLength: 3, reason: "empty" });
51 expect(error).not.toHaveProperty("rawUrl");
52 expect("cause" in (error as object)).toBe(false);
53 }
54 });
55​
56 it("rejects unsupported protocols", () => {
57 try {
58 normalizePreviewUrl("ftp://example.com");
59 expect.unreachable("expected URL normalization to fail");
60 } catch (error) {
61 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
62 expect(error).toMatchObject({
63 inputLength: "ftp://example.com".length,
64 reason: "unsupported-protocol",
65 protocol: "ftp:",
66 });
67 }
68 });
69​
70 it("rejects unparseable input without retaining credentials or tokens", () => {
71 const rawUrl = "https://user:password@example.com:bad/path?access_token=secret#fragment";
72 try {
73 normalizePreviewUrl(rawUrl);
74 expect.unreachable("expected URL normalization to fail");
75 } catch (error) {
76 expect(error).toBeInstanceOf(PreviewUrlNormalizationError);
77 expect(error).toMatchObject({
78 inputLength: rawUrl.length,
79 reason: "parse",
80 protocol: "https:",
81 });
82 expect(error).not.toHaveProperty("rawUrl");
83 expect((error as PreviewUrlNormalizationError).cause).toBeInstanceOf(Error);
84 expect((error as PreviewUrlNormalizationError).message).not.toContain(
85 ((error as PreviewUrlNormalizationError).cause as Error).message,
86 );
87 expect((error as PreviewUrlNormalizationError).message).not.toMatch(
88 /user|password|access_token|secret|fragment/,
89 );
90 }
91 });
92});