Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

IssueService.test.ts

774 lines27.4 KB

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