| 1 | import { describe, expect, it } from "vite-plus/test"; |
| 2 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/models"; |
| 3 | import { pullRequestFallbackEnvironment } from "./pullRequestEnvironmentFallback"; |
| 4 | |
| 5 | const project = (environmentId: string, id: string, canonicalKey: string) => |
| 6 | ({ |
| 7 | environmentId, |
| 8 | id, |
| 9 | repositoryIdentity: { canonicalKey }, |
| 10 | }) as unknown as EnvironmentProject; |
| 11 | |
| 12 | const projects = [ |
| 13 | project("laptop", "t3", "github.com/pingdotgg/t3code"), |
| 14 | project("desktop", "other", "github.com/pingdotgg/other"), |
| 15 | project("server", "t3-copy", "github.com/pingdotgg/t3code"), |
| 16 | ]; |
| 17 | const target = { |
| 18 | environmentId: "laptop", |
| 19 | projectId: "t3", |
| 20 | repository: "pingdotgg/t3code", |
| 21 | host: "github.com", |
| 22 | }; |
| 23 | |
| 24 | describe("pullRequestFallbackEnvironment", () => { |
| 25 | it("moves to a connected copy of the repository when the original is down", () => { |
| 26 | expect( |
| 27 | pullRequestFallbackEnvironment(target, projects, new Set(["desktop", "server"])), |
| 28 | ).toEqual({ environmentId: "server", projectId: "t3-copy" }); |
| 29 | }); |
| 30 | |
| 31 | it("stays put while the original is connected or nothing else holds the repository", () => { |
| 32 | expect(pullRequestFallbackEnvironment(target, projects, new Set(["laptop", "server"]))).toBe( |
| 33 | null, |
| 34 | ); |
| 35 | expect(pullRequestFallbackEnvironment(target, projects, new Set(["desktop"]))).toBe(null); |
| 36 | }); |
| 37 | |
| 38 | it("derives the host from the original project when the route has none", () => { |
| 39 | const { host: _host, ...hostless } = target; |
| 40 | expect(pullRequestFallbackEnvironment(hostless, projects, new Set(["server"]))).toEqual({ |
| 41 | environmentId: "server", |
| 42 | projectId: "t3-copy", |
| 43 | }); |
| 44 | }); |
| 45 | }); |