The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { |
| 4 | buildPairingAppLink, |
| 5 | readPairingAppLink, |
| 6 | RemoteBackendUrlMissingError, |
| 7 | RemotePairingTokenMissingError, |
| 8 | RemotePairingUrlInvalidError, |
| 9 | resolveRemotePairingTarget, |
| 10 | } from "./remote.ts"; |
| 11 | |
| 12 | describe("remote", () => { |
| 13 | it("derives backend urls and token from a pairing url", () => { |
| 14 | expect( |
| 15 | resolveRemotePairingTarget({ |
| 16 | pairingUrl: "https://remote.example.com/pair#token=pairing-token", |
| 17 | }), |
| 18 | ).toEqual({ |
| 19 | credential: "pairing-token", |
| 20 | httpBaseUrl: "https://remote.example.com/", |
| 21 | wsBaseUrl: "wss://remote.example.com/", |
| 22 | }); |
| 23 | }); |
| 24 | |
| 25 | it("accepts pairing urls that still use a query token", () => { |
| 26 | expect( |
| 27 | resolveRemotePairingTarget({ |
| 28 | pairingUrl: "https://remote.example.com/pair?token=pairing-token", |
| 29 | }), |
| 30 | ).toEqual({ |
| 31 | credential: "pairing-token", |
| 32 | httpBaseUrl: "https://remote.example.com/", |
| 33 | wsBaseUrl: "wss://remote.example.com/", |
| 34 | }); |
| 35 | }); |
| 36 | |
| 37 | it("derives backend urls from hosted app pairing links", () => { |
| 38 | expect( |
| 39 | resolveRemotePairingTarget({ |
| 40 | pairingUrl: |
| 41 | "https://app.t3.codes/pair?host=https%3A%2F%2Fdesktop.tailnet.ts.net%3A44342%2F#token=pairing-token", |
| 42 | }), |
| 43 | ).toEqual({ |
| 44 | credential: "pairing-token", |
| 45 | httpBaseUrl: "https://desktop.tailnet.ts.net:44342/", |
| 46 | wsBaseUrl: "wss://desktop.tailnet.ts.net:44342/", |
| 47 | }); |
| 48 | }); |
| 49 | |
| 50 | it("derives backend urls from a host and pairing code", () => { |
| 51 | expect( |
| 52 | resolveRemotePairingTarget({ |
| 53 | host: "https://remote.example.com", |
| 54 | pairingCode: "pairing-token", |
| 55 | }), |
| 56 | ).toEqual({ |
| 57 | credential: "pairing-token", |
| 58 | httpBaseUrl: "https://remote.example.com/", |
| 59 | wsBaseUrl: "wss://remote.example.com/", |
| 60 | }); |
| 61 | }); |
| 62 | |
| 63 | it("treats a protocol-relative host as https", () => { |
| 64 | expect( |
| 65 | resolveRemotePairingTarget({ |
| 66 | host: "//remote.example.com", |
| 67 | pairingCode: "pairing-token", |
| 68 | }), |
| 69 | ).toEqual({ |
| 70 | credential: "pairing-token", |
| 71 | httpBaseUrl: "https://remote.example.com/", |
| 72 | wsBaseUrl: "wss://remote.example.com/", |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | it("collapses extra leading slashes instead of producing an empty host", () => { |
| 77 | expect( |
| 78 | resolveRemotePairingTarget({ |
| 79 | host: "///example.com", |
| 80 | pairingCode: "pairing-token", |
| 81 | }), |
| 82 | ).toEqual({ |
| 83 | credential: "pairing-token", |
| 84 | httpBaseUrl: "https://example.com/", |
| 85 | wsBaseUrl: "wss://example.com/", |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | it("does not double-prepend https when the host already carries a scheme", () => { |
| 90 | expect( |
| 91 | resolveRemotePairingTarget({ |
| 92 | host: "//https://example.com", |
| 93 | pairingCode: "pairing-token", |
| 94 | }), |
| 95 | ).toEqual({ |
| 96 | credential: "pairing-token", |
| 97 | httpBaseUrl: "https://example.com/", |
| 98 | wsBaseUrl: "wss://example.com/", |
| 99 | }); |
| 100 | }); |
| 101 | |
| 102 | it("preserves host ports when normalizing a bare host input", () => { |
| 103 | expect( |
| 104 | resolveRemotePairingTarget({ |
| 105 | host: "myserver.com:3000", |
| 106 | pairingCode: "pairing-token", |
| 107 | }), |
| 108 | ).toEqual({ |
| 109 | credential: "pairing-token", |
| 110 | httpBaseUrl: "https://myserver.com:3000/", |
| 111 | wsBaseUrl: "wss://myserver.com:3000/", |
| 112 | }); |
| 113 | }); |
| 114 | |
| 115 | it("rejects unsupported direct pairing URL protocols", () => { |
| 116 | let pairingUrlError: unknown; |
| 117 | try { |
| 118 | resolveRemotePairingTarget({ |
| 119 | pairingUrl: "ftp://remote.example.com/pair#token=pairing-token", |
| 120 | }); |
| 121 | } catch (cause) { |
| 122 | pairingUrlError = cause; |
| 123 | } |
| 124 | |
| 125 | expect(pairingUrlError).toBeInstanceOf(RemotePairingUrlInvalidError); |
| 126 | expect(pairingUrlError).toMatchObject({ protocol: "ftp:" }); |
| 127 | expect((pairingUrlError as RemotePairingUrlInvalidError).cause).toBeUndefined(); |
| 128 | }); |
| 129 | |
| 130 | it("uses distinct structural errors for missing pairing inputs", () => { |
| 131 | expect(() => resolveRemotePairingTarget({})).toThrowError(RemoteBackendUrlMissingError); |
| 132 | expect(() => |
| 133 | resolveRemotePairingTarget({ pairingUrl: "https://remote.example.com/pair" }), |
| 134 | ).toThrowError(RemotePairingTokenMissingError); |
| 135 | expect(() => |
| 136 | resolveRemotePairingTarget({ |
| 137 | host: "https://user:secret@remote.example.com/path?token=sensitive#fragment", |
| 138 | }), |
| 139 | ).toThrowError( |
| 140 | expect.objectContaining({ |
| 141 | _tag: "RemotePairingCodeMissingError", |
| 142 | host: "remote.example.com", |
| 143 | }), |
| 144 | ); |
| 145 | }); |
| 146 | |
| 147 | it("round-trips pairing urls through app links on every channel scheme", () => { |
| 148 | const pairingUrl = "https://vps.example.com/pair#token=ABC123"; |
| 149 | const appLink = buildPairingAppLink(pairingUrl); |
| 150 | expect(appLink.startsWith("t3code://pair?")).toBe(true); |
| 151 | expect(readPairingAppLink(appLink)).toBe(pairingUrl); |
| 152 | expect(readPairingAppLink(appLink.replace("t3code:", "t3code-dev:"))).toBe(pairingUrl); |
| 153 | expect(readPairingAppLink("t3code://pair")).toBeNull(); |
| 154 | expect(readPairingAppLink(appLink.replace("//pair?", "//pair/other?"))).toBeNull(); |
| 155 | expect( |
| 156 | readPairingAppLink(`t3code://threads?pairingUrl=${encodeURIComponent(pairingUrl)}`), |
| 157 | ).toBeNull(); |
| 158 | expect( |
| 159 | readPairingAppLink(`https://pair?pairingUrl=${encodeURIComponent(pairingUrl)}`), |
| 160 | ).toBeNull(); |
| 161 | }); |
| 162 | }); |