The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Option from "effect/Option"; |
| 5 | import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit"; |
| 6 | import { |
| 7 | IssueTrackerKind, |
| 8 | type IssueTrackerProjectBinding, |
| 9 | SourceControlProviderKind, |
| 10 | type OrchestrationProjectShell, |
| 11 | type ProjectId, |
| 12 | } from "@t3tools/contracts"; |
| 13 | import { |
| 14 | IssueTrackerError, |
| 15 | type IssueTrackerApi, |
| 16 | type StandaloneIssueTrackerApi, |
| 17 | type TrackerIssueDetail, |
| 18 | } from "@t3tools/source-control-core/server/IssueTracker"; |
| 19 | |
| 20 | import { PersistenceSqlError } from "../persistence/Errors.ts"; |
| 21 | import * as IssueTrackerBindings from "../persistence/IssueTrackerBindings.ts"; |
| 22 | import { normalizeGitRemoteUrl } from "@t3tools/shared/git"; |
| 23 | |
| 24 | import * as ProjectService from "../project/ProjectService.ts"; |
| 25 | import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts"; |
| 26 | import * as IssueService from "./IssueService.ts"; |
| 27 | import * as IssueTrackerRegistry from "./IssueTrackerRegistry.ts"; |
| 28 | |
| 29 | const GITHUB = IssueTrackerKind.make("github"); |
| 30 | const LINEAR = IssueTrackerKind.make("linear"); |
| 31 | |
| 32 | /** A standalone tracker shaped like Linear: its own host, team keys for scopes, `ENG-1` keys. */ |
| 33 | function fakeStandaloneTracker( |
| 34 | overrides: Partial<IssueTrackerApi> = {}, |
| 35 | ): StandaloneIssueTrackerApi { |
| 36 | return { |
| 37 | ...fakeTracker({ |
| 38 | getViewer: () => Effect.succeed("usr_julius"), |
| 39 | getIssue: (input) => |
| 40 | Effect.succeed({ |
| 41 | ...issue(input.key), |
| 42 | url: `https://linear.app/acme/issue/${input.key}/canonical-title`, |
| 43 | displayKey: input.key, |
| 44 | viewer: "usr_julius", |
| 45 | }), |
| 46 | ...overrides, |
| 47 | }), |
| 48 | kind: LINEAR, |
| 49 | standalone: { |
| 50 | tracker: LINEAR, |
| 51 | displayName: "Linear", |
| 52 | host: "linear.app", |
| 53 | scopeNoun: "team", |
| 54 | credential: { label: "Personal API key", helpUrl: null }, |
| 55 | }, |
| 56 | parseIssueUrl: (url) => { |
| 57 | const links = new Map([ |
| 58 | ["https://linear.app/acme/issue/ENG-1/old-title", "acme"], |
| 59 | ["https://linear.app/acme/issue/ENG-1/issue-title", "acme"], |
| 60 | ["https://linear.app/acme/issue/ENG-1/canonical-title", "acme"], |
| 61 | ["https://linear.app/other-workspace/issue/ENG-1/old-title", "other-workspace"], |
| 62 | ]); |
| 63 | const workspace = links.get(url.href); |
| 64 | return workspace === undefined |
| 65 | ? null |
| 66 | : { host: "linear.app", workspace, scope: "ENG", key: "ENG-1" }; |
| 67 | }, |
| 68 | probeCredential: () => Effect.succeed({ account: "julius", scopes: [] }), |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | function project(input: { |
| 73 | readonly id: string; |
| 74 | readonly repository?: string; |
| 75 | readonly provider?: string; |
| 76 | readonly host?: string; |
| 77 | }): OrchestrationProjectShell { |
| 78 | const host = input.host ?? "github.com"; |
| 79 | return { |
| 80 | id: input.id as ProjectId, |
| 81 | title: input.id, |
| 82 | workspaceRoot: `/w/${input.id}`, |
| 83 | ...(input.repository |
| 84 | ? { |
| 85 | repositoryIdentity: { |
| 86 | canonicalKey: normalizeGitRemoteUrl(`https://${host}/${input.repository}.git`), |
| 87 | locator: { |
| 88 | source: "git-remote" as const, |
| 89 | remoteName: "origin", |
| 90 | remoteUrl: `https://${host}/${input.repository}.git`, |
| 91 | }, |
| 92 | provider: SourceControlProviderKind.make(input.provider ?? "github"), |
| 93 | displayName: input.repository, |
| 94 | }, |
| 95 | } |
| 96 | : {}), |
| 97 | defaultModelSelection: null, |
| 98 | scripts: [], |
| 99 | createdAt: "2026-07-01T00:00:00Z", |
| 100 | updatedAt: "2026-07-01T00:00:00Z", |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | function issue(key: string, updatedAt = "2026-07-02T00:00:00Z"): TrackerIssueDetail { |
| 105 | return { |
| 106 | key, |
| 107 | displayKey: `#${key}`, |
| 108 | title: `Issue ${key}`, |
| 109 | url: `https://github.com/acme/web/issues/${key}`, |
| 110 | author: null, |
| 111 | state: { category: "open", name: "open" }, |
| 112 | createdAt: "2026-07-01T00:00:00Z", |
| 113 | updatedAt, |
| 114 | closedAt: null, |
| 115 | assignees: [], |
| 116 | labels: [], |
| 117 | commentCount: 0, |
| 118 | body: "", |
| 119 | viewer: "julius", |
| 120 | viewerPermissions: { setState: true, comment: true, edit: true, labels: true, assignees: true }, |
| 121 | states: [], |
| 122 | comments: [], |
| 123 | commentsCursor: null, |
| 124 | events: [], |
| 125 | linkedPullRequests: [], |
| 126 | }; |
| 127 | } |
| 128 | |
| 129 | function fakeTracker(overrides: Partial<IssueTrackerApi> = {}): IssueTrackerApi { |
| 130 | return { |
| 131 | kind: GITHUB, |
| 132 | capabilities: { |
| 133 | create: true, |
| 134 | edit: true, |
| 135 | comment: true, |
| 136 | labels: true, |
| 137 | assignees: true, |
| 138 | reactions: false, |
| 139 | templates: false, |
| 140 | linkedPullRequests: true, |
| 141 | events: true, |
| 142 | search: true, |
| 143 | }, |
| 144 | getViewer: () => Effect.succeed("julius"), |
| 145 | listIssues: () => Effect.succeed({ items: [], nextCursor: null }), |
| 146 | getIssue: (input) => Effect.succeed(issue(input.key)), |
| 147 | getComments: () => Effect.succeed({ comments: [], nextCursor: null }), |
| 148 | comment: () => Effect.void, |
| 149 | update: () => Effect.void, |
| 150 | setState: () => Effect.void, |
| 151 | ...overrides, |
| 152 | }; |
| 153 | } |
| 154 | |
| 155 | function makeService(input: { |
| 156 | readonly projects: ReadonlyArray<OrchestrationProjectShell>; |
| 157 | readonly trackers: ReadonlyArray<IssueTrackerApi>; |
| 158 | readonly bindings?: Record<string, IssueTrackerProjectBinding>; |
| 159 | readonly readBindings?: IssueTrackerBindings.IssueTrackerBindingsRepository["Service"]["list"]; |
| 160 | }) { |
| 161 | return IssueService.IssueService.pipe( |
| 162 | Effect.provide( |
| 163 | IssueService.layer.pipe( |
| 164 | Layer.provide( |
| 165 | Layer.mergeAll( |
| 166 | Layer.mock(IssueTrackerBindings.IssueTrackerBindingsRepository)({ |
| 167 | list: |
| 168 | input.readBindings ?? |
| 169 | Effect.succeed( |
| 170 | new Map( |
| 171 | Object.entries(input.bindings ?? {}).map(([id, binding]) => [ |
| 172 | id as ProjectId, |
| 173 | binding, |
| 174 | ]), |
| 175 | ), |
| 176 | ), |
| 177 | }), |
| 178 | Layer.succeed( |
| 179 | IssueTrackerRegistry.IssueTrackerRegistry, |
| 180 | IssueTrackerRegistry.fromTrackers(input.trackers), |
| 181 | ), |
| 182 | Layer.mock(ProjectService.ProjectService)({ |
| 183 | listShells: () => Effect.succeed(input.projects), |
| 184 | getShell: (projectId) => |
| 185 | Effect.succeed( |
| 186 | Option.fromNullishOr(input.projects.find((p) => p.id === projectId)), |
| 187 | ), |
| 188 | }), |
| 189 | Layer.mock(RepositoryIdentityResolver.RepositoryIdentityResolver)({ |
| 190 | resolve: () => Effect.succeed(null), |
| 191 | }), |
| 192 | ), |
| 193 | ), |
| 194 | ), |
| 195 | ), |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | describe("IssueService", () => { |
| 200 | it.effect("places a project where its host keeps its issues", () => |
| 201 | Effect.gen(function* () { |
| 202 | const azure = IssueTrackerKind.make("azure-devops"); |
| 203 | const service = yield* makeService({ |
| 204 | projects: [ |
| 205 | project({ |
| 206 | id: "p", |
| 207 | provider: "azure-devops", |
| 208 | host: "dev.azure.com", |
| 209 | repository: "acme/Web App/_git/web", |
| 210 | }), |
| 211 | ], |
| 212 | trackers: [fakeTracker({ kind: azure })], |
| 213 | }); |
| 214 | expect(yield* service.projectScope("p" as ProjectId)).toEqual({ |
| 215 | projectId: "p", |
| 216 | tracker: "azure-devops", |
| 217 | host: "dev.azure.com", |
| 218 | scope: "acme/Web App", |
| 219 | }); |
| 220 | }), |
| 221 | ); |
| 222 | |
| 223 | it.effect("reads another repository on the same host but refuses to write to it", () => |
| 224 | Effect.gen(function* () { |
| 225 | const writes: string[] = []; |
| 226 | const service = yield* makeService({ |
| 227 | projects: [project({ id: "web", repository: "acme/web" })], |
| 228 | trackers: [ |
| 229 | fakeTracker({ |
| 230 | comment: (input) => |
| 231 | Effect.sync(() => { |
| 232 | writes.push(`${input.scope}#${input.key}`); |
| 233 | }), |
| 234 | }), |
| 235 | ], |
| 236 | }); |
| 237 | const own = { projectId: "web" as ProjectId, tracker: GITHUB, scope: "acme/web", key: "1" }; |
| 238 | const external = { ...own, scope: "other/backend", key: "7" }; |
| 239 | |
| 240 | expect((yield* service.detail(own)).writable).toBe(true); |
| 241 | // The external issue is served by the project's checkout and credentials, read-only. |
| 242 | const detail = yield* service.detail(external); |
| 243 | expect(detail).toMatchObject({ writable: false, scope: "other/backend", key: "7" }); |
| 244 | |
| 245 | yield* service.comment({ ...own, body: "ok" }); |
| 246 | const refused = yield* service.comment({ ...external, body: "no" }).pipe(Effect.result); |
| 247 | expect(refused).toMatchObject({ |
| 248 | _tag: "Failure", |
| 249 | failure: { _tag: "IssueOperationError", reason: "read-only" }, |
| 250 | }); |
| 251 | expect(writes).toEqual(["acme/web#1"]); |
| 252 | |
| 253 | // A link's port picks the server: the project bound with it, not a default-port namesake. |
| 254 | const ported = yield* makeService({ |
| 255 | projects: [ |
| 256 | project({ id: "plain", repository: "acme/web", provider: "forgejo", host: "gitea.test" }), |
| 257 | project({ |
| 258 | id: "ported", |
| 259 | repository: "acme/web", |
| 260 | provider: "forgejo", |
| 261 | host: "gitea.test:8443", |
| 262 | }), |
| 263 | ], |
| 264 | trackers: [fakeTracker({ kind: IssueTrackerKind.make("forgejo") })], |
| 265 | }); |
| 266 | expect(yield* ported.resolveUrl("https://gitea.test:8443/acme/web/issues/3")).toMatchObject({ |
| 267 | projectId: "ported", |
| 268 | host: "gitea.test:8443", |
| 269 | key: "3", |
| 270 | writable: true, |
| 271 | }); |
| 272 | expect(yield* ported.resolveUrl("https://gitea.test/acme/web/issues/3")).toMatchObject({ |
| 273 | projectId: "plain", |
| 274 | host: "gitea.test", |
| 275 | }); |
| 276 | |
| 277 | // A GitHub Enterprise Server's hostname need not say "github"; its project vouches for it. |
| 278 | const enterprise = yield* makeService({ |
| 279 | projects: [project({ id: "ghes", repository: "acme/web", host: "code.acme.test" })], |
| 280 | trackers: [fakeTracker()], |
| 281 | }); |
| 282 | expect( |
| 283 | yield* enterprise.resolveUrl("https://code.acme.test/acme/web/issues/7"), |
| 284 | ).toMatchObject({ projectId: "ghes", host: "code.acme.test", key: "7", writable: true }); |
| 285 | |
| 286 | // A fork lists its upstream's issues and its own, counted once, and may write to both. |
| 287 | const fork = project({ id: "fork", repository: "acme/web" }); |
| 288 | const forked = yield* makeService({ |
| 289 | projects: [ |
| 290 | { |
| 291 | ...fork, |
| 292 | repositoryIdentity: { |
| 293 | ...fork.repositoryIdentity!, |
| 294 | origin: { canonicalKey: "github.com/me/web" }, |
| 295 | }, |
| 296 | }, |
| 297 | ], |
| 298 | trackers: [ |
| 299 | fakeTracker({ |
| 300 | listIssues: (input) => |
| 301 | Effect.succeed({ items: [issue(input.scope)], nextCursor: null }), |
| 302 | comment: (input) => |
| 303 | Effect.sync(() => { |
| 304 | writes.push(`${input.scope}#${input.key}`); |
| 305 | }), |
| 306 | }), |
| 307 | ], |
| 308 | }); |
| 309 | const listed = yield* forked.list({ state: "open" }); |
| 310 | expect(listed.entries.map((entry) => entry.scope).sort()).toEqual(["acme/web", "me/web"]); |
| 311 | expect(listed.trackers).toEqual([expect.objectContaining({ projectCount: 1 })]); |
| 312 | yield* forked.comment({ |
| 313 | ...own, |
| 314 | projectId: "fork" as ProjectId, |
| 315 | scope: "me/web", |
| 316 | body: "ok", |
| 317 | }); |
| 318 | expect(writes).toEqual(["acme/web#1", "me/web#1"]); |
| 319 | |
| 320 | // Another host is not reachable through this project's credentials at all. |
| 321 | const elsewhere = yield* Effect.flip(service.detail({ ...own, host: "ghe.example.test" })); |
| 322 | expect(elsewhere).toMatchObject({ |
| 323 | _tag: "IssueUnavailableError", |
| 324 | reason: "tracker-unsupported", |
| 325 | }); |
| 326 | }), |
| 327 | ); |
| 328 | |
| 329 | it.effect("resolves both legacy Azure work item URLs through the bound project", () => |
| 330 | Effect.gen(function* () { |
| 331 | const tracker = IssueTrackerKind.make("azure-devops"); |
| 332 | const host = "acme.visualstudio.com"; |
| 333 | const service = yield* makeService({ |
| 334 | projects: [ |
| 335 | project({ id: "api", repository: "Api/_git/api", provider: tracker, host }), |
| 336 | project({ |
| 337 | id: "web", |
| 338 | repository: "DefaultCollection/Web/_git/web", |
| 339 | provider: tracker, |
| 340 | host, |
| 341 | }), |
| 342 | ], |
| 343 | trackers: [fakeTracker({ kind: tracker })], |
| 344 | }); |
| 345 | for (const path of ["Web", "DefaultCollection/Web"]) { |
| 346 | const ref = yield* service.resolveUrl( |
| 347 | `https://${host}/${path}/_workitems/edit/9`, |
| 348 | "api" as ProjectId, |
| 349 | ); |
| 350 | expect(ref).toEqual({ |
| 351 | projectId: "web", |
| 352 | tracker, |
| 353 | host, |
| 354 | scope: "DefaultCollection/Web", |
| 355 | key: "9", |
| 356 | writable: true, |
| 357 | }); |
| 358 | } |
| 359 | }), |
| 360 | ); |
| 361 | |
| 362 | it.effect("sends writes as interactive requests and reads as background ones", () => |
| 363 | Effect.gen(function* () { |
| 364 | const interactive: Array<readonly [string, boolean]> = []; |
| 365 | const record = (operation: string) => |
| 366 | Effect.gen(function* () { |
| 367 | interactive.push([operation, yield* SourceControlRateLimit.Interactive]); |
| 368 | }); |
| 369 | const service = yield* makeService({ |
| 370 | projects: [project({ id: "web", repository: "acme/web" })], |
| 371 | trackers: [ |
| 372 | fakeTracker({ |
| 373 | getComments: () => |
| 374 | record("comments").pipe(Effect.as({ comments: [], nextCursor: null })), |
| 375 | comment: () => record("comment"), |
| 376 | }), |
| 377 | ], |
| 378 | }); |
| 379 | const ref = { |
| 380 | projectId: "web" as ProjectId, |
| 381 | tracker: GITHUB, |
| 382 | scope: "acme/web", |
| 383 | key: "42", |
| 384 | }; |
| 385 | yield* service.comments({ ...ref, cursor: "next" }); |
| 386 | yield* service.comment({ ...ref, body: "Repro attached." }); |
| 387 | expect(interactive).toEqual([ |
| 388 | ["comments", false], |
| 389 | ["comment", true], |
| 390 | ]); |
| 391 | }), |
| 392 | ); |
| 393 | |
| 394 | it.effect("refuses what the tracker did not declare before calling it", () => |
| 395 | Effect.gen(function* () { |
| 396 | let calls = 0; |
| 397 | const service = yield* makeService({ |
| 398 | projects: [project({ id: "web", repository: "acme/web" })], |
| 399 | trackers: [ |
| 400 | fakeTracker({ |
| 401 | setState: () => |
| 402 | Effect.sync(() => { |
| 403 | calls += 1; |
| 404 | }), |
| 405 | setReaction: () => |
| 406 | Effect.sync(() => { |
| 407 | calls += 1; |
| 408 | }), |
| 409 | }), |
| 410 | ], |
| 411 | }); |
| 412 | const ref = { projectId: "web" as ProjectId, tracker: GITHUB, scope: "acme/web", key: "1" }; |
| 413 | |
| 414 | // The state is the tracker's own name, passed through: the tracker is what proves it. |
| 415 | yield* service.setState({ ...ref, state: "In Review" }); |
| 416 | const reaction = yield* Effect.flip( |
| 417 | service.react({ ...ref, content: "heart", reacted: true }), |
| 418 | ); |
| 419 | expect(reaction._tag).toBe("IssueOperationError"); |
| 420 | expect(calls).toBe(1); |
| 421 | }), |
| 422 | ); |
| 423 | |
| 424 | it.effect("deduplicates and pages repositories despite partial failures", () => |
| 425 | Effect.gen(function* () { |
| 426 | const listed: string[] = []; |
| 427 | let failContinuation = true; |
| 428 | const service = yield* makeService({ |
| 429 | projects: [ |
| 430 | project({ id: "web", repository: "Acme/Web" }), |
| 431 | project({ id: "web-worktree", repository: "acme/web" }), |
| 432 | project({ id: "api", repository: "acme/api" }), |
| 433 | project({ id: "docs", repository: "acme/docs" }), |
| 434 | project({ |
| 435 | id: "gitlab", |
| 436 | repository: "acme/gl", |
| 437 | provider: "gitlab", |
| 438 | host: "gitlab.com", |
| 439 | }), |
| 440 | project({ id: "plain" }), |
| 441 | ], |
| 442 | trackers: [ |
| 443 | fakeTracker({ |
| 444 | listIssues: (input) => |
| 445 | Effect.gen(function* () { |
| 446 | listed.push(input.scope); |
| 447 | if (input.scope === "acme/api") |
| 448 | return yield* new IssueTrackerError({ |
| 449 | tracker: GITHUB, |
| 450 | operation: "listIssues", |
| 451 | reason: "failed", |
| 452 | // A tracker may fail without a detail; the wire error still needs one. |
| 453 | detail: " ", |
| 454 | }); |
| 455 | if (input.scope === "acme/docs") |
| 456 | return { items: [issue("3", "2026-07-04T00:00:00Z")], nextCursor: null }; |
| 457 | if (input.cursor === "next" && failContinuation) { |
| 458 | failContinuation = false; |
| 459 | return yield* new IssueTrackerError({ |
| 460 | tracker: GITHUB, |
| 461 | operation: "listIssues", |
| 462 | reason: "failed", |
| 463 | detail: "try again", |
| 464 | }); |
| 465 | } |
| 466 | if (input.cursor === "next") |
| 467 | return { items: [issue("4", "2026-07-01T00:00:00Z")], nextCursor: null }; |
| 468 | return { |
| 469 | items: [issue("1"), issue("2", "2026-07-05T00:00:00Z")], |
| 470 | nextCursor: "next", |
| 471 | }; |
| 472 | }), |
| 473 | }), |
| 474 | ], |
| 475 | }); |
| 476 | const result = yield* service.list({ state: "open" }); |
| 477 | expect(listed.sort()).toEqual(["Acme/Web", "acme/api", "acme/docs"]); |
| 478 | expect(result.entries.map((entry) => [entry.projectId, entry.key])).toEqual([ |
| 479 | ["web", "2"], |
| 480 | ["docs", "3"], |
| 481 | ["web", "1"], |
| 482 | ]); |
| 483 | expect( |
| 484 | result.entries.filter((entry) => entry.projectId === "web").map((entry) => entry.scope), |
| 485 | ).toEqual(["Acme/Web", "Acme/Web"]); |
| 486 | expect(result.nextCursors).toEqual({ "github.com acme/web": "next" }); |
| 487 | expect(result.errors).toEqual([ |
| 488 | { projectId: "api", projectTitle: "api", message: "The issue tracker request failed." }, |
| 489 | ]); |
| 490 | expect(result.viewers).toEqual({}); |
| 491 | expect(result.trackers).toEqual([ |
| 492 | expect.objectContaining({ host: "github.com", projectCount: 4, configured: true }), |
| 493 | expect.objectContaining({ |
| 494 | host: "gitlab.com", |
| 495 | tracker: "gitlab", |
| 496 | projectCount: 1, |
| 497 | configured: false, |
| 498 | capabilities: null, |
| 499 | }), |
| 500 | ]); |
| 501 | |
| 502 | // Narrowing to one host still names every tracker, so the client can switch between them. |
| 503 | // Narrowing by involvement reads the account it compares against. |
| 504 | const narrowed = yield* service.list({ |
| 505 | state: "open", |
| 506 | host: "github.com", |
| 507 | involvement: "assigned", |
| 508 | }); |
| 509 | expect(narrowed.viewers).toEqual({ "github.com": "julius" }); |
| 510 | expect(narrowed.entries.every((entry) => entry.host === "github.com")).toBe(true); |
| 511 | expect(narrowed.trackers.map((tracker) => tracker.host)).toEqual([ |
| 512 | "github.com", |
| 513 | "gitlab.com", |
| 514 | ]); |
| 515 | |
| 516 | // A continuation only reads the scopes it names. |
| 517 | listed.length = 0; |
| 518 | const failed = yield* service.list({ |
| 519 | state: "open", |
| 520 | cursors: result.nextCursors, |
| 521 | }); |
| 522 | expect(listed).toEqual(["Acme/Web"]); |
| 523 | expect(failed.nextCursors).toEqual(result.nextCursors); |
| 524 | expect(failed.errors).toEqual([ |
| 525 | { projectId: "web", projectTitle: "web", message: "try again" }, |
| 526 | ]); |
| 527 | const next = yield* service.list({ state: "open", cursors: failed.nextCursors }); |
| 528 | expect(next.entries.map((entry) => [entry.projectId, entry.key])).toEqual([["web", "4"]]); |
| 529 | expect(next.nextCursors).toEqual({}); |
| 530 | expect(next.errors).toEqual([]); |
| 531 | |
| 532 | // The cursor belongs to the scope, not to the project whose spelling first read it. |
| 533 | listed.length = 0; |
| 534 | const through = yield* service.list({ |
| 535 | state: "open", |
| 536 | projectId: "web-worktree" as ProjectId, |
| 537 | cursors: result.nextCursors, |
| 538 | }); |
| 539 | expect(listed).toEqual(["acme/web"]); |
| 540 | expect(through.entries.map((entry) => entry.key)).toEqual(["4"]); |
| 541 | }), |
| 542 | ); |
| 543 | }); |
| 544 | |
| 545 | describe("IssueService with a standalone tracker", () => { |
| 546 | it.effect("reads bindings once per listing and sees changes on the next listing", () => |
| 547 | Effect.gen(function* () { |
| 548 | const web = project({ id: "web", repository: "acme/web" }); |
| 549 | const docs = project({ id: "docs", repository: "acme/docs" }); |
| 550 | let reads = 0; |
| 551 | let scope = "ENG"; |
| 552 | const service = yield* makeService({ |
| 553 | projects: [web, docs], |
| 554 | trackers: [fakeTracker(), fakeStandaloneTracker()], |
| 555 | readBindings: Effect.sync(() => { |
| 556 | reads += 1; |
| 557 | return new Map([[web.id, { tracker: LINEAR, scope }]]); |
| 558 | }), |
| 559 | }); |
| 560 | |
| 561 | const first = yield* service.list({ state: "open" }); |
| 562 | expect(first.scopes?.map((entry) => entry.scope)).toEqual(["ENG", "acme/web", "acme/docs"]); |
| 563 | expect(reads).toBe(1); |
| 564 | |
| 565 | scope = "OPS"; |
| 566 | const next = yield* service.list({ state: "open", refresh: true }); |
| 567 | expect(next.scopes?.map((entry) => entry.scope)).toEqual(["OPS", "acme/web", "acme/docs"]); |
| 568 | expect(reads).toBe(2); |
| 569 | |
| 570 | const missing = yield* service.list({ state: "open", projectId: "missing" as ProjectId }); |
| 571 | expect(missing.scopes).toEqual([]); |
| 572 | expect(reads).toBe(2); |
| 573 | }), |
| 574 | ); |
| 575 | |
| 576 | it.effect("rejects a URL from another workspace with the same team and issue key", () => |
| 577 | Effect.gen(function* () { |
| 578 | const service = yield* makeService({ |
| 579 | projects: [project({ id: "web" })], |
| 580 | trackers: [fakeStandaloneTracker()], |
| 581 | bindings: { web: { tracker: LINEAR, scope: "ENG" } }, |
| 582 | }); |
| 583 | expect( |
| 584 | yield* service.resolveUrl("https://linear.app/acme/issue/ENG-1/old-title"), |
| 585 | ).toMatchObject({ key: "ENG-1", writable: true }); |
| 586 | const refused = yield* service |
| 587 | .resolveUrl("https://linear.app/other-workspace/issue/ENG-1/old-title") |
| 588 | .pipe(Effect.result); |
| 589 | expect(refused).toMatchObject({ |
| 590 | _tag: "Failure", |
| 591 | failure: { |
| 592 | _tag: "IssueOperationError", |
| 593 | operation: "resolveUrl", |
| 594 | reason: "not-found", |
| 595 | detail: "This issue URL does not belong to the connected workspace.", |
| 596 | }, |
| 597 | }); |
| 598 | }), |
| 599 | ); |
| 600 | |
| 601 | it.effect("lists a bound project's standalone scope beside its git host and serves both", () => |
| 602 | Effect.gen(function* () { |
| 603 | const listed: string[] = []; |
| 604 | const record = (tracker: IssueTrackerKind): Partial<IssueTrackerApi> => ({ |
| 605 | listIssues: (input) => |
| 606 | Effect.sync(() => { |
| 607 | listed.push(`${tracker} ${input.host} ${input.scope}`); |
| 608 | return { items: [issue(tracker === LINEAR ? "ENG-1" : "1")], nextCursor: null }; |
| 609 | }), |
| 610 | }); |
| 611 | const projects = [project({ id: "web", repository: "acme/web" }), project({ id: "notes" })]; |
| 612 | const trackers = [fakeTracker(record(GITHUB)), fakeStandaloneTracker(record(LINEAR))]; |
| 613 | const bound = yield* makeService({ |
| 614 | projects: [...projects, project({ id: "api", repository: "acme/api" })], |
| 615 | trackers, |
| 616 | bindings: { ["web" as ProjectId]: { tracker: LINEAR, scope: "ENG" } }, |
| 617 | }); |
| 618 | |
| 619 | const result = yield* bound.list({ state: "open" }); |
| 620 | expect(listed.sort()).toEqual([ |
| 621 | "github github.com acme/api", |
| 622 | "github github.com acme/web", |
| 623 | "linear linear.app ENG", |
| 624 | ]); |
| 625 | expect(result.entries.map((entry) => [entry.tracker, entry.scope, entry.key]).sort()).toEqual( |
| 626 | [ |
| 627 | ["github", "acme/api", "1"], |
| 628 | ["github", "acme/web", "1"], |
| 629 | ["linear", "ENG", "ENG-1"], |
| 630 | ], |
| 631 | ); |
| 632 | expect(result.trackers.map((tracker) => [tracker.tracker, tracker.host]).sort()).toEqual([ |
| 633 | ["github", "github.com"], |
| 634 | ["linear", "linear.app"], |
| 635 | ]); |
| 636 | expect(result.scopes).toContainEqual({ |
| 637 | projectId: "web", |
| 638 | tracker: "linear", |
| 639 | host: "linear.app", |
| 640 | scope: "ENG", |
| 641 | }); |
| 642 | // Narrowed to one project, both of its trackers are still read. |
| 643 | listed.length = 0; |
| 644 | const selected = yield* bound.list({ state: "open", projectId: "web" as ProjectId }); |
| 645 | expect(listed.sort()).toEqual(["github github.com acme/web", "linear linear.app ENG"]); |
| 646 | expect(selected.entries.map((entry) => [entry.projectId, entry.scope]).sort()).toEqual([ |
| 647 | ["web", "ENG"], |
| 648 | ["web", "acme/web"], |
| 649 | ]); |
| 650 | listed.length = 0; |
| 651 | yield* bound.list({ state: "open", excludeProjectIds: ["web" as ProjectId] }); |
| 652 | expect(listed).toEqual(["github github.com acme/api"]); |
| 653 | // A reference picks the project's tracker by kind; both are the project's own, so writable. |
| 654 | const linearRef = { |
| 655 | projectId: "web" as ProjectId, |
| 656 | tracker: LINEAR, |
| 657 | scope: "ENG", |
| 658 | key: "ENG-1", |
| 659 | }; |
| 660 | expect(yield* bound.detail(linearRef)).toMatchObject({ host: "linear.app", writable: true }); |
| 661 | expect( |
| 662 | yield* bound.detail({ |
| 663 | projectId: "web" as ProjectId, |
| 664 | tracker: GITHUB, |
| 665 | scope: "acme/web", |
| 666 | key: "1", |
| 667 | }), |
| 668 | ).toMatchObject({ host: "github.com", writable: true }); |
| 669 | |
| 670 | const resolved = yield* bound |
| 671 | .resolveUrl("https://linear.app/acme/issue/ENG-1/issue-title", "web" as ProjectId) |
| 672 | .pipe(Effect.result); |
| 673 | expect(resolved).toMatchObject({ |
| 674 | _tag: "Success", |
| 675 | success: { |
| 676 | projectId: "web", |
| 677 | tracker: LINEAR, |
| 678 | host: "linear.app", |
| 679 | scope: "ENG", |
| 680 | key: "ENG-1", |
| 681 | writable: true, |
| 682 | }, |
| 683 | }); |
| 684 | |
| 685 | // Unbound, the standalone scope is gone and its references are unsupported. |
| 686 | listed.length = 0; |
| 687 | const unbound = yield* makeService({ projects, trackers }); |
| 688 | const after = yield* unbound.list({ state: "open" }); |
| 689 | expect(listed).toEqual(["github github.com acme/web"]); |
| 690 | expect(after.trackers.map((tracker) => tracker.tracker)).toEqual(["github"]); |
| 691 | const refused = yield* Effect.flip(unbound.detail(linearRef)); |
| 692 | expect(refused).toMatchObject({ |
| 693 | _tag: "IssueUnavailableError", |
| 694 | reason: "tracker-unsupported", |
| 695 | }); |
| 696 | |
| 697 | // A project with no remote at all can still be bound, and then has only the standalone one. |
| 698 | const standaloneOnly = yield* makeService({ |
| 699 | projects, |
| 700 | trackers, |
| 701 | bindings: { ["notes" as ProjectId]: { tracker: LINEAR, scope: "OPS" } }, |
| 702 | }); |
| 703 | const notes = yield* standaloneOnly.list({ state: "open", projectId: "notes" as ProjectId }); |
| 704 | expect(notes.entries.map((entry) => [entry.projectId, entry.scope])).toEqual([ |
| 705 | ["notes", "OPS"], |
| 706 | ]); |
| 707 | |
| 708 | // Bindings that cannot be read fail the listing; they are not taken for "none bound". |
| 709 | const unreadable = yield* makeService({ |
| 710 | projects, |
| 711 | trackers, |
| 712 | readBindings: Effect.fail( |
| 713 | new PersistenceSqlError({ |
| 714 | operation: "IssueTrackerBindingsRepository.list", |
| 715 | detail: "disk I/O error", |
| 716 | }), |
| 717 | ), |
| 718 | }); |
| 719 | expect(yield* Effect.flip(unreadable.list({ state: "open" }))).toMatchObject({ |
| 720 | _tag: "IssueOperationError", |
| 721 | operation: "listProjects", |
| 722 | }); |
| 723 | }), |
| 724 | ); |
| 725 | }); |