The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | PullRequestListInput, |
| 3 | SourceControlProviderKind, |
| 4 | type EnvironmentId, |
| 5 | type ProjectId, |
| 6 | type PullRequestProviderSummary, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { describe, expect, it } from "vite-plus/test"; |
| 10 | |
| 11 | import { |
| 12 | assignProjectsToEnvironments, |
| 13 | rememberPullRequestHosts, |
| 14 | resetPullRequestHosts, |
| 15 | pullRequestProjectQueries, |
| 16 | resolvePickableEnvironments, |
| 17 | type AssignableProject, |
| 18 | } from "./pullRequestProjectAssignment.logic"; |
| 19 | |
| 20 | const project = ( |
| 21 | id: string, |
| 22 | environmentId: string, |
| 23 | canonicalKey?: string, |
| 24 | ): AssignableProject & { workspaceRoot: string } => ({ |
| 25 | id: id as ProjectId, |
| 26 | environmentId: environmentId as EnvironmentId, |
| 27 | repositoryIdentity: canonicalKey === undefined ? null : { canonicalKey }, |
| 28 | workspaceRoot: `/srv/${environmentId}/${id}`, |
| 29 | }); |
| 30 | |
| 31 | const envs = (...ids: ReadonlyArray<string>) => ids as ReadonlyArray<EnvironmentId>; |
| 32 | |
| 33 | const plain = (assignment: Map<EnvironmentId, ProjectId[]>) => |
| 34 | Object.fromEntries([...assignment].map(([id, projectIds]) => [id, projectIds])); |
| 35 | const decodeListInput = Schema.decodeSync(PullRequestListInput); |
| 36 | |
| 37 | const hostAvailability = (hosts: ReadonlyMap<string, boolean>) => ({ |
| 38 | hosts: new Map([...hosts].map(([host, configured]) => [host, { configured, observedAt: 0 }])), |
| 39 | projectHosts: new Map<ProjectId, { host: string; observedAt: number }>(), |
| 40 | }); |
| 41 | |
| 42 | const provider = (host: string, configured: boolean): PullRequestProviderSummary => ({ |
| 43 | host, |
| 44 | kind: "bitbucket" as PullRequestProviderSummary["kind"], |
| 45 | searchesOnHost: false, |
| 46 | projectCount: 1, |
| 47 | configured, |
| 48 | detail: configured ? null : "Credentials rejected", |
| 49 | }); |
| 50 | |
| 51 | describe("host read results", () => { |
| 52 | const environmentId = "env-1" as EnvironmentId; |
| 53 | |
| 54 | it("remembers a failed host after the assignment stops requesting it", () => { |
| 55 | const failed = rememberPullRequestHosts(new Map(), [ |
| 56 | [environmentId, { providers: [provider("BITBUCKET.org", false)] }, 100], |
| 57 | ]); |
| 58 | const next = rememberPullRequestHosts(failed, [ |
| 59 | [environmentId, { providers: [provider("github.com", true)] }, 200], |
| 60 | ]); |
| 61 | expect( |
| 62 | [...next.get(environmentId)!.hosts].map(([host, read]) => [host, read.configured]), |
| 63 | ).toEqual([ |
| 64 | ["bitbucket.org", false], |
| 65 | ["github.com", true], |
| 66 | ]); |
| 67 | expect( |
| 68 | [...failed.get(environmentId)!.hosts].map(([host, read]) => [host, read.configured]), |
| 69 | ).toEqual([["bitbucket.org", false]]); |
| 70 | }); |
| 71 | |
| 72 | it("does not replay cached failures after refresh clears availability", () => { |
| 73 | const cleared = new Map([ |
| 74 | [ |
| 75 | environmentId, |
| 76 | { observedAt: 100, resetAt: 100, ...hostAvailability(new Map<string, boolean>()) }, |
| 77 | ], |
| 78 | ]); |
| 79 | expect( |
| 80 | rememberPullRequestHosts(cleared, [ |
| 81 | [environmentId, { providers: [provider("bitbucket.org", false)] }, 100], |
| 82 | ]), |
| 83 | ).toBe(cleared); |
| 84 | const refreshed = rememberPullRequestHosts(cleared, [ |
| 85 | [environmentId, { providers: [provider("bitbucket.org", true)] }, 200], |
| 86 | ]); |
| 87 | expect(refreshed.get(environmentId)!.hosts.get("bitbucket.org")?.configured).toBe(true); |
| 88 | }); |
| 89 | |
| 90 | it("ignores an older answer when a newer read has already succeeded", () => { |
| 91 | const readable = rememberPullRequestHosts(new Map(), [ |
| 92 | [environmentId, { providers: [provider("bitbucket.org", true)] }, 200], |
| 93 | ]); |
| 94 | expect( |
| 95 | rememberPullRequestHosts(readable, [ |
| 96 | [environmentId, { providers: [provider("bitbucket.org", false)] }, 100], |
| 97 | ]), |
| 98 | ).toBe(readable); |
| 99 | }); |
| 100 | |
| 101 | it.each([false, true])( |
| 102 | "treats a host as readable when any active batch succeeds (reversed: %s)", |
| 103 | (reverse) => { |
| 104 | const observations = [ |
| 105 | [environmentId, { providers: [provider("gitlab.example", true)] }, 100], |
| 106 | [environmentId, { providers: [provider("gitlab.example", false)] }, 200], |
| 107 | ] as const; |
| 108 | const next = rememberPullRequestHosts( |
| 109 | new Map(), |
| 110 | reverse ? observations.toReversed() : observations, |
| 111 | ); |
| 112 | expect(next.get(environmentId)!.hosts.get("gitlab.example")).toEqual({ |
| 113 | configured: true, |
| 114 | observedAt: 200, |
| 115 | }); |
| 116 | expect(next.get(environmentId)!.observedAt).toBe(200); |
| 117 | const cleared = resetPullRequestHosts(next); |
| 118 | expect(rememberPullRequestHosts(cleared, observations)).toBe(cleared); |
| 119 | const failed = rememberPullRequestHosts(next, [ |
| 120 | [environmentId, { providers: [provider("gitlab.example", false)] }, 300], |
| 121 | ]); |
| 122 | expect(failed.get(environmentId)!.hosts.get("gitlab.example")?.configured).toBe(false); |
| 123 | }, |
| 124 | ); |
| 125 | }); |
| 126 | |
| 127 | describe("assignment query limits", () => { |
| 128 | it("splits the healthy remainder after fallback into valid project filters", () => { |
| 129 | const healthy = Array.from({ length: 101 }, (_, index) => |
| 130 | project(`healthy-${index}`, "env-1", `github.com/acme/repo-${index}`), |
| 131 | ); |
| 132 | const projects = [ |
| 133 | project("shared", "env-1", "bitbucket.org/acme/app"), |
| 134 | ...healthy, |
| 135 | project("copy", "env-2", "bitbucket.org/acme/app"), |
| 136 | ]; |
| 137 | const queries = pullRequestProjectQueries( |
| 138 | projects, |
| 139 | envs("env-1", "env-2"), |
| 140 | new Map([["env-1" as EnvironmentId, hostAvailability(new Map([["bitbucket.org", false]]))]]), |
| 141 | ); |
| 142 | expect( |
| 143 | queries.map(({ environmentId, projectIds }) => [environmentId, projectIds?.length]), |
| 144 | ).toEqual([ |
| 145 | ["env-1", 100], |
| 146 | ["env-1", 1], |
| 147 | ["env-2", undefined], |
| 148 | ]); |
| 149 | expect( |
| 150 | queries |
| 151 | .filter(({ environmentId }) => environmentId === "env-1") |
| 152 | .flatMap(({ projectIds }) => projectIds ?? []), |
| 153 | ).toEqual(healthy.map(({ id }) => id)); |
| 154 | for (const { projectIds } of queries) |
| 155 | expect(() => |
| 156 | decodeListInput({ |
| 157 | state: "open", |
| 158 | ...(projectIds === undefined ? {} : { projectIds }), |
| 159 | }), |
| 160 | ).not.toThrow(); |
| 161 | }); |
| 162 | }); |
| 163 | |
| 164 | describe("one server per repository", () => { |
| 165 | it("lets the first server list a repository both hold", () => { |
| 166 | const assignment = assignProjectsToEnvironments( |
| 167 | [ |
| 168 | project("a1", "env-1", "github.com/acme/app"), |
| 169 | project("b1", "env-1", "github.com/acme/tools"), |
| 170 | project("a2", "env-2", "github.com/acme/app"), |
| 171 | project("c2", "env-2", "github.com/acme/site"), |
| 172 | ], |
| 173 | envs("env-1", "env-2"), |
| 174 | "env-1" as EnvironmentId, |
| 175 | ); |
| 176 | expect(plain(assignment)).toEqual({ "env-1": ["a1", "b1"], "env-2": ["c2"] }); |
| 177 | }); |
| 178 | |
| 179 | it("prefers the named server over the first one", () => { |
| 180 | const assignment = assignProjectsToEnvironments( |
| 181 | [ |
| 182 | project("a1", "env-1", "github.com/acme/app"), |
| 183 | project("a2", "env-2", "github.com/acme/app"), |
| 184 | ], |
| 185 | envs("env-1", "env-2"), |
| 186 | "env-2" as EnvironmentId, |
| 187 | ); |
| 188 | expect(plain(assignment)).toEqual({ "env-2": ["a2"] }); |
| 189 | }); |
| 190 | |
| 191 | it("lets a reachable server list a repository an unreachable one also holds", () => { |
| 192 | const assignment = assignProjectsToEnvironments( |
| 193 | [ |
| 194 | project("a1", "env-1", "github.com/acme/app"), |
| 195 | project("b1", "env-1", "github.com/acme/tools"), |
| 196 | project("a2", "env-2", "github.com/acme/app"), |
| 197 | ], |
| 198 | envs("env-1", "env-2"), |
| 199 | "env-1" as EnvironmentId, |
| 200 | undefined, |
| 201 | new Set(envs("env-1")), |
| 202 | ); |
| 203 | expect(plain(assignment)).toEqual({ "env-1": ["b1"], "env-2": ["a2"] }); |
| 204 | const unreadableHost = assignProjectsToEnvironments( |
| 205 | [ |
| 206 | project("a1", "env-1", "github.com/acme/app"), |
| 207 | project("a2", "env-2", "github.com/acme/app"), |
| 208 | ], |
| 209 | envs("env-1", "env-2"), |
| 210 | "env-1" as EnvironmentId, |
| 211 | new Map([["env-2" as EnvironmentId, hostAvailability(new Map([["github.com", false]]))]]), |
| 212 | new Set(envs("env-1")), |
| 213 | ); |
| 214 | expect(plain(unreadableHost)).toEqual({ "env-2": ["a2"] }); |
| 215 | }); |
| 216 | |
| 217 | it("falls back from an unreadable host without moving other repositories", () => { |
| 218 | const assignment = assignProjectsToEnvironments( |
| 219 | [ |
| 220 | project("a1", "env-1", "bitbucket.org/acme/app"), |
| 221 | project("b1", "env-1", "github.com/acme/tools"), |
| 222 | project("a2", "env-2", "bitbucket.org/acme/app"), |
| 223 | project("b2", "env-2", "github.com/acme/tools"), |
| 224 | ], |
| 225 | envs("env-1", "env-2"), |
| 226 | "env-1" as EnvironmentId, |
| 227 | new Map([["env-1" as EnvironmentId, hostAvailability(new Map([["bitbucket.org", false]]))]]), |
| 228 | ); |
| 229 | expect(plain(assignment)).toEqual({ "env-1": ["b1"], "env-2": ["a2"] }); |
| 230 | }); |
| 231 | |
| 232 | it("keeps one stable owner and its error when every copy is unreadable", () => { |
| 233 | const projects = [ |
| 234 | project("a1", "env-1", "bitbucket.org/acme/app"), |
| 235 | project("a2", "env-2", "bitbucket.org/acme/app"), |
| 236 | project("only1", "env-1", "bitbucket.org/acme/only"), |
| 237 | ]; |
| 238 | const availability = new Map( |
| 239 | envs("env-1", "env-2").map((id) => [ |
| 240 | id, |
| 241 | hostAvailability(new Map([["bitbucket.org", false]])), |
| 242 | ]), |
| 243 | ); |
| 244 | expect( |
| 245 | plain( |
| 246 | assignProjectsToEnvironments( |
| 247 | projects, |
| 248 | envs("env-1", "env-2"), |
| 249 | "env-1" as EnvironmentId, |
| 250 | availability, |
| 251 | ), |
| 252 | ), |
| 253 | ).toEqual({ "env-1": ["a1", "only1"] }); |
| 254 | expect( |
| 255 | plain( |
| 256 | assignProjectsToEnvironments( |
| 257 | projects, |
| 258 | envs("env-2"), |
| 259 | "env-2" as EnvironmentId, |
| 260 | availability, |
| 261 | ), |
| 262 | ), |
| 263 | ).toEqual({ "env-2": ["a2"] }); |
| 264 | }); |
| 265 | |
| 266 | it("checks the owner's HTTP authority using its own identity in either project order", () => { |
| 267 | const projects = [ |
| 268 | { |
| 269 | ...project("a1", "env-1", "forge.example/team/repo"), |
| 270 | repositoryIdentity: { |
| 271 | canonicalKey: "forge.example/team/repo", |
| 272 | provider: SourceControlProviderKind.make("forgejo"), |
| 273 | locator: { |
| 274 | source: "git-remote", |
| 275 | remoteName: "origin", |
| 276 | remoteUrl: "https://forge.example:3000/team/repo.git", |
| 277 | }, |
| 278 | }, |
| 279 | }, |
| 280 | { |
| 281 | ...project("a2", "env-2", "forge.example/team/repo"), |
| 282 | repositoryIdentity: { |
| 283 | canonicalKey: "forge.example/team/repo", |
| 284 | provider: SourceControlProviderKind.make("forgejo"), |
| 285 | locator: { |
| 286 | source: "git-remote", |
| 287 | remoteName: "origin", |
| 288 | remoteUrl: "git@forge.example:team/repo.git", |
| 289 | }, |
| 290 | }, |
| 291 | }, |
| 292 | ] as const; |
| 293 | const availability = new Map([ |
| 294 | ["env-1" as EnvironmentId, hostAvailability(new Map([["forge.example:3000", false]]))], |
| 295 | ]); |
| 296 | for (const copies of [projects, projects.toReversed()]) { |
| 297 | expect( |
| 298 | plain( |
| 299 | assignProjectsToEnvironments( |
| 300 | copies, |
| 301 | envs("env-1", "env-2"), |
| 302 | "env-1" as EnvironmentId, |
| 303 | availability, |
| 304 | ), |
| 305 | ), |
| 306 | ).toEqual({ "env-2": ["a2"] }); |
| 307 | } |
| 308 | }); |
| 309 | |
| 310 | it("uses a server-resolved host for SSH without conflating ports on that host", () => { |
| 311 | const projects = envs("env-1", "env-2").map((environmentId) => ({ |
| 312 | ...project(`a-${environmentId}`, environmentId, "ssh.forge.example/team/repo"), |
| 313 | repositoryIdentity: { |
| 314 | canonicalKey: "ssh.forge.example/team/repo", |
| 315 | provider: SourceControlProviderKind.make("forgejo"), |
| 316 | locator: { |
| 317 | source: "git-remote" as const, |
| 318 | remoteName: "origin", |
| 319 | remoteUrl: "ssh://git@ssh.forge.example:2222/team/repo.git", |
| 320 | }, |
| 321 | }, |
| 322 | })); |
| 323 | const availability = rememberPullRequestHosts(new Map(), [ |
| 324 | [ |
| 325 | "env-1" as EnvironmentId, |
| 326 | { |
| 327 | providers: [ |
| 328 | { ...provider("forge.example:3000", false), projectIds: ["a-env-1" as ProjectId] }, |
| 329 | { ...provider("forge.example:4000", true), projectIds: ["unrelated" as ProjectId] }, |
| 330 | ], |
| 331 | }, |
| 332 | 100, |
| 333 | ], |
| 334 | ]); |
| 335 | for (const copies of [projects, projects.toReversed()]) { |
| 336 | expect( |
| 337 | plain( |
| 338 | assignProjectsToEnvironments( |
| 339 | copies, |
| 340 | envs("env-1", "env-2"), |
| 341 | "env-1" as EnvironmentId, |
| 342 | availability, |
| 343 | ), |
| 344 | ), |
| 345 | ).toEqual({ "env-2": ["a-env-2"] }); |
| 346 | } |
| 347 | }); |
| 348 | |
| 349 | it("keeps every copy of a project that has no identity to compare", () => { |
| 350 | const assignment = assignProjectsToEnvironments( |
| 351 | [project("p1", "env-1"), project("p2", "env-2")], |
| 352 | envs("env-1", "env-2"), |
| 353 | "env-1" as EnvironmentId, |
| 354 | ); |
| 355 | expect(plain(assignment)).toEqual({ "env-1": ["p1"], "env-2": ["p2"] }); |
| 356 | }); |
| 357 | |
| 358 | it("keeps a server's own worktrees of the repository it lists", () => { |
| 359 | const assignment = assignProjectsToEnvironments( |
| 360 | [ |
| 361 | project("a1", "env-1", "github.com/acme/app"), |
| 362 | project("a1-wt", "env-1", "GitHub.com/acme/app"), |
| 363 | project("a2", "env-2", "github.com/acme/app"), |
| 364 | ], |
| 365 | envs("env-1", "env-2"), |
| 366 | "env-1" as EnvironmentId, |
| 367 | ); |
| 368 | expect(plain(assignment)).toEqual({ "env-1": ["a1", "a1-wt"] }); |
| 369 | }); |
| 370 | |
| 371 | it("reads two servers' copies of one repository as one however the remote is cased", () => { |
| 372 | const assignment = assignProjectsToEnvironments( |
| 373 | [ |
| 374 | project("a1", "env-1", "git.example.com/Team/App"), |
| 375 | project("a2", "env-2", "GIT.example.com/team/app"), |
| 376 | ], |
| 377 | envs("env-1", "env-2"), |
| 378 | "env-1" as EnvironmentId, |
| 379 | ); |
| 380 | expect(plain(assignment)).toEqual({ "env-1": ["a1"] }); |
| 381 | }); |
| 382 | |
| 383 | it("answers the same whatever order the projects arrive in", () => { |
| 384 | const projects = [ |
| 385 | project("a2", "env-2", "github.com/acme/app"), |
| 386 | project("a1", "env-1", "github.com/acme/app"), |
| 387 | ]; |
| 388 | const forward = assignProjectsToEnvironments(projects, envs("env-1", "env-2"), null); |
| 389 | const backward = assignProjectsToEnvironments( |
| 390 | projects.toReversed(), |
| 391 | envs("env-1", "env-2"), |
| 392 | null, |
| 393 | ); |
| 394 | expect(plain(forward)).toEqual({ "env-1": ["a1"] }); |
| 395 | expect(plain(backward)).toEqual(plain(forward)); |
| 396 | }); |
| 397 | }); |
| 398 | |
| 399 | const connected = (...ids: ReadonlyArray<string>) => |
| 400 | ids.map((id) => ({ environmentId: id as EnvironmentId, label: `Server ${id}` })); |
| 401 | |
| 402 | const on = (environmentId: string, projectId: string) => ({ |
| 403 | environmentId: environmentId as EnvironmentId, |
| 404 | projectId: projectId as ProjectId, |
| 405 | }); |
| 406 | |
| 407 | describe("where a pull request can be acted on", () => { |
| 408 | it("offers every server holding the repository, the panel's own first", () => { |
| 409 | const pickable = resolvePickableEnvironments( |
| 410 | on("env-2", "a2"), |
| 411 | [ |
| 412 | project("a1", "env-1", "github.com/acme/app"), |
| 413 | project("a2", "env-2", "github.com/acme/app"), |
| 414 | project("c3", "env-3", "github.com/acme/site"), |
| 415 | ], |
| 416 | connected("env-1", "env-2", "env-3"), |
| 417 | ); |
| 418 | expect(pickable).toEqual([ |
| 419 | { |
| 420 | environmentId: "env-2", |
| 421 | projectId: "a2", |
| 422 | workspaceRoot: "/srv/env-2/a2", |
| 423 | label: "Server env-2", |
| 424 | }, |
| 425 | { |
| 426 | environmentId: "env-1", |
| 427 | projectId: "a1", |
| 428 | workspaceRoot: "/srv/env-1/a1", |
| 429 | label: "Server env-1", |
| 430 | }, |
| 431 | ]); |
| 432 | }); |
| 433 | |
| 434 | it("names one copy per server, so two worktrees are one choice", () => { |
| 435 | const pickable = resolvePickableEnvironments( |
| 436 | on("env-1", "a1"), |
| 437 | [ |
| 438 | project("a1", "env-1", "github.com/acme/app"), |
| 439 | project("a1-wt", "env-1", "github.com/acme/app"), |
| 440 | project("a2", "env-2", "github.com/acme/app"), |
| 441 | project("a2-wt", "env-2", "github.com/acme/app"), |
| 442 | ], |
| 443 | connected("env-1", "env-2"), |
| 444 | ); |
| 445 | expect(pickable.map((entry) => entry.projectId)).toEqual(["a1", "a2"]); |
| 446 | }); |
| 447 | |
| 448 | it("keeps the panel's own worktree rather than the server's first copy", () => { |
| 449 | const pickable = resolvePickableEnvironments( |
| 450 | on("env-1", "a1-wt"), |
| 451 | [ |
| 452 | project("a1", "env-1", "github.com/acme/app"), |
| 453 | project("a1-wt", "env-1", "github.com/acme/app"), |
| 454 | project("a2", "env-2", "github.com/acme/app"), |
| 455 | ], |
| 456 | connected("env-1", "env-2"), |
| 457 | ); |
| 458 | expect(pickable[0]).toEqual({ |
| 459 | environmentId: "env-1", |
| 460 | projectId: "a1-wt", |
| 461 | workspaceRoot: "/srv/env-1/a1-wt", |
| 462 | label: "Server env-1", |
| 463 | }); |
| 464 | }); |
| 465 | }); |