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