| 1 | import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "vite-plus/test"; |
| 3 | |
| 4 | import { |
| 5 | environmentAxisValue, |
| 6 | projectAxisValue, |
| 7 | selectEnvironmentAxis, |
| 8 | selectProjectAxis, |
| 9 | settingsScopeEnvironmentLabel, |
| 10 | } from "./settingsScopeAxis"; |
| 11 | |
| 12 | const first = { |
| 13 | environmentId: EnvironmentId.make("first"), |
| 14 | label: "Development", |
| 15 | displayUrl: "https://first.example.com", |
| 16 | }; |
| 17 | const second = { |
| 18 | environmentId: EnvironmentId.make("second"), |
| 19 | label: "Development", |
| 20 | displayUrl: "https://second.example.com", |
| 21 | }; |
| 22 | |
| 23 | describe("settings scope environment labels", () => { |
| 24 | it("distinguishes same-name environments by address", () => { |
| 25 | const environments = [first, second]; |
| 26 | expect( |
| 27 | environments.map((environment) => settingsScopeEnvironmentLabel(environment, environments)), |
| 28 | ).toEqual([ |
| 29 | "Development · https://first.example.com", |
| 30 | "Development · https://second.example.com", |
| 31 | ]); |
| 32 | }); |
| 33 | |
| 34 | it("falls back to environment IDs when duplicate names have no display URL", () => { |
| 35 | const environments = [first, second].map((environment) => ({ |
| 36 | ...environment, |
| 37 | displayUrl: null, |
| 38 | })); |
| 39 | expect( |
| 40 | environments.map((environment) => settingsScopeEnvironmentLabel(environment, environments)), |
| 41 | ).toEqual(["Development · first", "Development · second"]); |
| 42 | }); |
| 43 | |
| 44 | it("keeps unique names compact and removes disambiguation after a rename", () => { |
| 45 | expect(settingsScopeEnvironmentLabel(first, [first])).toBe("Development"); |
| 46 | expect(settingsScopeEnvironmentLabel(first, [first, { ...second, label: "Production" }])).toBe( |
| 47 | "Development", |
| 48 | ); |
| 49 | }); |
| 50 | }); |
| 51 | |
| 52 | describe("settings scope axes", () => { |
| 53 | it("maps each axis to its search key and back", () => { |
| 54 | expect(projectAxisValue({})).toBe("all"); |
| 55 | expect(projectAxisValue({ project: "app" })).toBe("app"); |
| 56 | expect(selectProjectAxis({ machine: "second" }, "app")).toEqual({ |
| 57 | project: "app", |
| 58 | machine: "second", |
| 59 | }); |
| 60 | expect(selectProjectAxis({ machine: "second", project: "app" }, "all")).toEqual({ |
| 61 | machine: "second", |
| 62 | }); |
| 63 | expect(selectEnvironmentAxis({ project: "app" }, "first")).toEqual({ |
| 64 | project: "app", |
| 65 | machine: "first", |
| 66 | }); |
| 67 | expect(selectEnvironmentAxis({ project: "app", machine: "first" }, "all")).toEqual({ |
| 68 | project: "app", |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | it("drops a checkout narrowing from older links when either axis changes", () => { |
| 73 | const checkout = { project: "app", checkout: "app@first", machine: "first" }; |
| 74 | expect(selectEnvironmentAxis(checkout, "second")).toEqual({ |
| 75 | project: "app", |
| 76 | machine: "second", |
| 77 | }); |
| 78 | expect(selectProjectAxis(checkout, "app")).toEqual({ project: "app", machine: "first" }); |
| 79 | }); |
| 80 | }); |
| 81 | |
| 82 | describe("environmentAxisValue", () => { |
| 83 | it("shows the checkout's environment for a legacy checkout link", () => { |
| 84 | expect(environmentAxisValue({ project: "p", checkout: "c" }, "laptop")).toBe("laptop"); |
| 85 | expect(environmentAxisValue({ project: "p" }, null)).toBe("all"); |
| 86 | expect(environmentAxisValue({ machine: "desk" }, "laptop")).toBe("desk"); |
| 87 | }); |
| 88 | }); |