| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | |
| 3 | import { webhookAddress } from "./webhookAddress.ts"; |
| 4 | |
| 5 | const path = "/api/hooks/scheduled-task%3Ahook/token"; |
| 6 | const endpoint = (url: string | null) => ({ path, url, hasSecret: false }); |
| 7 | |
| 8 | describe("webhookAddress", () => { |
| 9 | it("uses the T3 Connect URL when the server has one", () => { |
| 10 | expect(webhookAddress(endpoint("https://relay.t3.codes/v1/hooks/k/t/x"), null)).toEqual({ |
| 11 | address: "https://relay.t3.codes/v1/hooks/k/t/x", |
| 12 | copyable: true, |
| 13 | note: null, |
| 14 | }); |
| 15 | }); |
| 16 | |
| 17 | it("builds a direct URL on the environment's address without T3 Connect", () => { |
| 18 | const result = webhookAddress(endpoint(null), "https://mac.tail1234.ts.net/"); |
| 19 | expect(result.address).toBe(`https://mac.tail1234.ts.net${path}`); |
| 20 | expect(result.copyable).toBe(true); |
| 21 | expect(result.note).toContain("Tailscale"); |
| 22 | }); |
| 23 | |
| 24 | it("keeps the environment prefix in direct webhook addresses", () => { |
| 25 | expect( |
| 26 | webhookAddress(endpoint(null), "https://hub.example/prefix/environments/one/").address, |
| 27 | ).toBe(`https://hub.example/prefix/environments/one${path}`); |
| 28 | }); |
| 29 | |
| 30 | it("says only this computer can call a loopback address", () => { |
| 31 | const result = webhookAddress(endpoint(null), "http://127.0.0.1:3773/"); |
| 32 | expect(result.copyable).toBe(true); |
| 33 | expect(result.note).toContain("Only this computer"); |
| 34 | }); |
| 35 | |
| 36 | it("falls back to the path when the address is unknown", () => { |
| 37 | expect(webhookAddress(endpoint(null), null)).toMatchObject({ |
| 38 | address: path, |
| 39 | copyable: false, |
| 40 | }); |
| 41 | }); |
| 42 | }); |