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