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 | isPreviewStage, |
| 5 | isPreviewTunnelName, |
| 6 | isPreviewTunnelRecordName, |
| 7 | previewSweepTargets, |
| 8 | tunnelCnameContent, |
| 9 | } from "./previewStages.ts"; |
| 10 | |
| 11 | const KEY = "0123456789abcdef"; |
| 12 | |
| 13 | describe("preview stages", () => { |
| 14 | it("accepts only pull-request stages", () => { |
| 15 | expect(isPreviewStage("pr-42")).toBe(true); |
| 16 | expect(isPreviewStage("prod")).toBe(false); |
| 17 | expect(isPreviewStage("dev_julius")).toBe(false); |
| 18 | expect(isPreviewStage("pr-0")).toBe(false); |
| 19 | expect(isPreviewStage("pr-42/../prod")).toBe(false); |
| 20 | }); |
| 21 | |
| 22 | it("matches only tunnels of the exact stage", () => { |
| 23 | expect(isPreviewTunnelName(`t3coderelay-managedendpoint-pr-42-${KEY}`, "pr-42")).toBe(true); |
| 24 | expect(isPreviewTunnelName(`t3coderelay-managedendpoint-pr-42-${KEY}`, "pr-4")).toBe(false); |
| 25 | expect(isPreviewTunnelName(`t3coderelay-managedendpoint-prod-${KEY}`, "pr-42")).toBe(false); |
| 26 | expect(isPreviewTunnelName(`t3coderelay-managedendpoint-pr-42-xyz`, "pr-42")).toBe(false); |
| 27 | }); |
| 28 | |
| 29 | it("matches only hostnames of the exact stage under the tunnel zone", () => { |
| 30 | const zone = "tunnels.example.com"; |
| 31 | expect(isPreviewTunnelRecordName(`pr-42-${KEY}.${zone}`, "pr-42", zone)).toBe(true); |
| 32 | expect(isPreviewTunnelRecordName(`PR-42-${KEY}.Tunnels.example.com`, "pr-42", zone)).toBe(true); |
| 33 | expect(isPreviewTunnelRecordName(`pr-4-${KEY}.${zone}`, "pr-42", zone)).toBe(false); |
| 34 | expect(isPreviewTunnelRecordName(`pr-42-${KEY}.other.com`, "pr-42", zone)).toBe(false); |
| 35 | expect(isPreviewTunnelRecordName(`relay.${zone}`, "pr-42", zone)).toBe(false); |
| 36 | }); |
| 37 | |
| 38 | it("targets only this stage's tunnels and the records that point at them", () => { |
| 39 | const zone = "tunnels.example.com"; |
| 40 | const mine = `t3coderelay-managedendpoint-pr-42-${KEY}`; |
| 41 | const otherStage = `t3coderelay-managedendpoint-pr-7-${KEY}`; |
| 42 | const otherKey = "fedcba9876543210"; |
| 43 | const targets = previewSweepTargets({ |
| 44 | stage: "pr-42", |
| 45 | tunnelZoneName: zone, |
| 46 | tunnels: [ |
| 47 | { id: "t-mine", name: mine }, |
| 48 | { id: "t-other", name: otherStage }, |
| 49 | ], |
| 50 | records: [ |
| 51 | { id: "r-mine", name: `pr-42-${KEY}.${zone}`, content: tunnelCnameContent("t-mine") }, |
| 52 | { id: "r-other", name: `pr-7-${KEY}.${zone}`, content: tunnelCnameContent("t-other") }, |
| 53 | // Right label, but it targets a tunnel outside this sweep. |
| 54 | { id: "r-stranger", name: `pr-42-${otherKey}.${zone}`, content: "x.cfargotunnel.com" }, |
| 55 | ], |
| 56 | }); |
| 57 | expect(targets.tunnels.map((tunnel) => tunnel.id)).toEqual(["t-mine"]); |
| 58 | expect(targets.records.map((record) => record.id)).toEqual(["r-mine"]); |
| 59 | }); |
| 60 | }); |