|
| 1 | +import { EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | +import { describe, expect, it } from "vite-plus/test"; |
| 3 | + |
| 4 | +import { findScopedProject } from "./pullRequestList.logic"; |
| 5 | +import { pullRequestFilterProjects } from "./pullRequestProjectFilter.logic"; |
| 6 | + |
| 7 | +const cups = EnvironmentId.make("env-cups"); |
| 8 | +const nucbox = EnvironmentId.make("env-nucbox"); |
| 9 | +const labels = new Map([ |
| 10 | + [cups, "cups"], |
| 11 | + [nucbox, "nucbox-1"], |
| 12 | +]); |
| 13 | + |
| 14 | +function project( |
| 15 | + id: string, |
| 16 | + environmentId = nucbox, |
| 17 | + canonicalKey: string | null = "github.com/pingdotgg/t3code", |
| 18 | +) { |
| 19 | + return { |
| 20 | + id: ProjectId.make(id), |
| 21 | + environmentId, |
| 22 | + title: "t3code", |
| 23 | + workspaceRoot: `/work/${id}`, |
| 24 | + repositoryIdentity: canonicalKey === null ? null : { canonicalKey }, |
| 25 | + faviconPath: `${id}/favicon.png`, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +describe("pull request project filter choices", () => { |
| 30 | + it("collapses three checkouts on one server without dropping another server's copy", () => { |
| 31 | + const projects = [ |
| 32 | + project("main"), |
| 33 | + project("worktree-1"), |
| 34 | + project("worktree-2"), |
| 35 | + project("main", cups), |
| 36 | + ]; |
| 37 | + |
| 38 | + const choices = pullRequestFilterProjects(projects, labels); |
| 39 | + |
| 40 | + expect(choices.map(({ id, environmentId, title }) => ({ id, environmentId, title }))).toEqual([ |
| 41 | + { id: "main", environmentId: cups, title: "t3code · cups" }, |
| 42 | + { id: "main", environmentId: nucbox, title: "t3code · nucbox-1" }, |
| 43 | + ]); |
| 44 | + expect(choices[1]?.workspaceRoot).toBe("/work/main"); |
| 45 | + expect(choices[1]?.faviconPath).toBe("main/favicon.png"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("keeps a saved worktree selection as the repository's only choice", () => { |
| 49 | + const projects = [project("main"), project("worktree"), project("worktree", cups)]; |
| 50 | + const selected = findScopedProject(projects, nucbox, "worktree"); |
| 51 | + |
| 52 | + const choices = pullRequestFilterProjects(projects, labels, selected); |
| 53 | + |
| 54 | + expect(choices.filter((choice) => choice.environmentId === nucbox)).toEqual([ |
| 55 | + { ...projects[1], title: "t3code · nucbox-1" }, |
| 56 | + ]); |
| 57 | + expect(findScopedProject(choices, nucbox, "worktree")).toBeDefined(); |
| 58 | + expect(findScopedProject(choices, nucbox, "main")).toBeUndefined(); |
| 59 | + expect(findScopedProject(choices, cups, "worktree")).toBeDefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + it("matches canonical repositories regardless of casing", () => { |
| 63 | + const main = project("main"); |
| 64 | + const worktree = project("worktree", nucbox, "GitHub.com/PingDotGG/T3Code"); |
| 65 | + |
| 66 | + expect(pullRequestFilterProjects([main, worktree], labels)).toEqual([main]); |
| 67 | + }); |
| 68 | + |
| 69 | + it("does not add a server suffix after duplicate checkouts have collapsed", () => { |
| 70 | + const main = project("main"); |
| 71 | + |
| 72 | + expect(pullRequestFilterProjects([main, project("worktree")], labels)).toEqual([main]); |
| 73 | + expect(main.title).toBe("t3code"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("distinguishes same-named repositories on one server by checkout path", () => { |
| 77 | + const projects = [ |
| 78 | + project("upstream"), |
| 79 | + project("fork", nucbox, "github.com/juliusmarminge/t3code"), |
| 80 | + ]; |
| 81 | + |
| 82 | + const choices = pullRequestFilterProjects(projects, labels); |
| 83 | + |
| 84 | + expect(choices.map((choice) => choice.title)).toEqual([ |
| 85 | + "t3code · nucbox-1 · /work/fork", |
| 86 | + "t3code · nucbox-1 · /work/upstream", |
| 87 | + ]); |
| 88 | + }); |
| 89 | + |
| 90 | + it("keeps repositories on different hosts separate", () => { |
| 91 | + const choices = pullRequestFilterProjects( |
| 92 | + [project("github"), project("enterprise", nucbox, "git.example.com/pingdotgg/t3code")], |
| 93 | + labels, |
| 94 | + ); |
| 95 | + |
| 96 | + expect(choices.map((choice) => choice.id)).toEqual(["enterprise", "github"]); |
| 97 | + }); |
| 98 | + |
| 99 | + it("does not merge projects whose repository identity is unknown", () => { |
| 100 | + const choices = pullRequestFilterProjects( |
| 101 | + [project("first", nucbox, null), project("second", nucbox, null)], |
| 102 | + labels, |
| 103 | + ); |
| 104 | + |
| 105 | + expect(choices.map((choice) => choice.title)).toEqual([ |
| 106 | + "t3code · nucbox-1 · /work/first", |
| 107 | + "t3code · nucbox-1 · /work/second", |
| 108 | + ]); |
| 109 | + }); |
| 110 | + |
| 111 | + it("distinguishes servers with the same display name and checkout path", () => { |
| 112 | + const first = project("main"); |
| 113 | + const second = project("main", cups); |
| 114 | + const repeatedLabels = new Map([ |
| 115 | + [cups, "nucbox-1"], |
| 116 | + [nucbox, "nucbox-1"], |
| 117 | + ]); |
| 118 | + |
| 119 | + const choices = pullRequestFilterProjects([first, second], repeatedLabels); |
| 120 | + |
| 121 | + expect(choices.map((choice) => choice.title)).toEqual([ |
| 122 | + "t3code · nucbox-1 · /work/main · env-cups", |
| 123 | + "t3code · nucbox-1 · /work/main · env-nucbox", |
| 124 | + ]); |
| 125 | + }); |
| 126 | + |
| 127 | + it("uses the environment id when its label is unavailable", () => { |
| 128 | + const choices = pullRequestFilterProjects( |
| 129 | + [project("main"), project("remote", cups)], |
| 130 | + new Map(), |
| 131 | + ); |
| 132 | + |
| 133 | + expect(choices.map((choice) => choice.title)).toEqual([ |
| 134 | + "t3code · env-cups", |
| 135 | + "t3code · env-nucbox", |
| 136 | + ]); |
| 137 | + }); |
| 138 | + |
| 139 | + it("can distinguish unresolved project records that also share a checkout path", () => { |
| 140 | + const first = project("first", nucbox, null); |
| 141 | + const second = { ...project("second", nucbox, null), workspaceRoot: first.workspaceRoot }; |
| 142 | + |
| 143 | + const choices = pullRequestFilterProjects([first, second], labels); |
| 144 | + |
| 145 | + expect(choices.map((choice) => choice.title)).toEqual([ |
| 146 | + "t3code · nucbox-1 · /work/first · env-nucbox · first", |
| 147 | + "t3code · nucbox-1 · /work/first · env-nucbox · second", |
| 148 | + ]); |
| 149 | + }); |
| 150 | + |
| 151 | + it("leaves unrelated names unchanged and orders them alphabetically", () => { |
| 152 | + const app = { ...project("app"), title: "Zebra" }; |
| 153 | + const tools = { ...project("tools", nucbox, "github.com/acme/tools"), title: "Alpha" }; |
| 154 | + |
| 155 | + expect(pullRequestFilterProjects([app, tools], labels)).toEqual([tools, app]); |
| 156 | + expect(pullRequestFilterProjects([], labels)).toEqual([]); |
| 157 | + }); |
| 158 | +}); |
0 commit comments