The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | normalizePreviewUrl, |
| 5 | PreviewUrlNormalizationError, |
| 6 | resolveAddressBarInput, |
| 7 | previewPdfAssetUrl, |
| 8 | resolveSearchUrlTemplate, |
| 9 | } from "./preview.ts"; |
| 10 | |
| 11 | describe("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 | |
| 60 | describe("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 | |
| 130 | describe("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 | |
| 151 | describe("normalizePreviewUrl", () => { |
| 152 | it.each([ |
| 153 | ["127.0.0.2:3000/path", "http://127.0.0.2:3000/path"], |
| 154 | ["127.255.255.254:443/path", "http://127.255.255.254:443/path"], |
| 155 | ["0x7f.0.0.2:3000", "http://127.0.0.2:3000/"], |
| 156 | ["127.0.0.2.example.com:3000", "https://127.0.0.2.example.com:3000/"], |
| 157 | ["https://127.0.0.2:80/path", "https://127.0.0.2:80/path"], |
| 158 | ])("infers the scheme for loopback-looking %s", (input, expected) => { |
| 159 | expect(normalizePreviewUrl(input)).toBe(expected); |
| 160 | }); |
| 161 | |
| 162 | it("tells agents how to open a blank tab and which URLs are accepted", () => { |
| 163 | expect(() => normalizePreviewUrl("about:blank")).toThrow( |
| 164 | /preview_open, omit url when creating a new tab[^.]*blank tab/i, |
| 165 | ); |
| 166 | expect(() => normalizePreviewUrl("about:blank")).toThrow(/http\(s\)/i); |
| 167 | expect(() => normalizePreviewUrl("about:blank")).toThrow(/schemeless host/i); |
| 168 | }); |
| 169 | |
| 170 | it("rejects unparseable input without retaining credentials or tokens", () => { |
| 171 | const rawUrl = "https://user:password@example.com:bad/path?access_token=secret#fragment"; |
| 172 | try { |
| 173 | normalizePreviewUrl(rawUrl); |
| 174 | expect.unreachable("expected URL normalization to fail"); |
| 175 | } catch (error) { |
| 176 | expect(error).toBeInstanceOf(PreviewUrlNormalizationError); |
| 177 | expect(error).toMatchObject({ |
| 178 | inputLength: rawUrl.length, |
| 179 | reason: "parse", |
| 180 | protocol: "https:", |
| 181 | }); |
| 182 | expect(error).not.toHaveProperty("rawUrl"); |
| 183 | expect((error as PreviewUrlNormalizationError).cause).toBeInstanceOf(Error); |
| 184 | expect((error as PreviewUrlNormalizationError).message).not.toContain( |
| 185 | ((error as PreviewUrlNormalizationError).cause as Error).message, |
| 186 | ); |
| 187 | expect((error as PreviewUrlNormalizationError).message).not.toMatch( |
| 188 | /user|password|access_token|secret|fragment/, |
| 189 | ); |
| 190 | } |
| 191 | }); |
| 192 | |
| 193 | it("reports explicit non-web schemes instead of reading them as a host", () => { |
| 194 | for (const [rawUrl, protocol] of [ |
| 195 | ["about:blank", "about:"], |
| 196 | ["mailto:alice@example.com", "mailto:"], |
| 197 | ["javascript:alert(1)", "javascript:"], |
| 198 | ] as const) { |
| 199 | expect(() => normalizePreviewUrl(rawUrl)).toThrow( |
| 200 | expect.objectContaining({ reason: "unsupported-protocol", protocol }), |
| 201 | ); |
| 202 | } |
| 203 | expect(normalizePreviewUrl("localhost:3000")).toBe("http://localhost:3000/"); |
| 204 | expect(normalizePreviewUrl("devbox:8080")).toBe("https://devbox:8080/"); |
| 205 | }); |
| 206 | }); |