Open History to see the last commit
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 };
70}
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 };
102}
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 };
127}
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 };
153}
154
155function 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
199describe("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 // The same holds for a self-hosted Forgejo.
287 const forgejo = yield* makeService({
288 projects: [
289 project({
290 id: "forge",
291 repository: "acme/web",
292 provider: "forgejo",
293 host: "code.acme.test",
294 }),
295 ],
296 trackers: [fakeTracker({ kind: IssueTrackerKind.make("forgejo") })],
297 });
298 expect(yield* forgejo.resolveUrl("https://code.acme.test/acme/web/issues/7")).toMatchObject({
299 projectId: "forge",
300 host: "code.acme.test",
301 key: "7",
302 });
303
304 // A fork lists its upstream's issues and its own, counted once, and may write to both.
305 const fork = project({ id: "fork", repository: "acme/web" });
306 const forked = yield* makeService({
307 projects: [
308 {
309 ...fork,
310 repositoryIdentity: {
311 ...fork.repositoryIdentity!,
312 origin: { canonicalKey: "github.com/me/web" },
313 },
314 },
315 ],
316 trackers: [
317 fakeTracker({
318 listIssues: (input) =>
319 Effect.succeed({ items: [issue(input.scope)], nextCursor: null }),
320 comment: (input) =>
321 Effect.sync(() => {
322 writes.push(`${input.scope}#${input.key}`);
323 }),
324 }),
325 ],
326 });
327 const listed = yield* forked.list({ state: "open" });
328 expect(listed.entries.map((entry) => entry.scope).sort()).toEqual(["acme/web", "me/web"]);
329 expect(listed.trackers).toEqual([expect.objectContaining({ projectCount: 1 })]);
330 yield* forked.comment({
331 ...own,
332 projectId: "fork" as ProjectId,
333 scope: "me/web",
334 body: "ok",
335 });
336 expect(writes).toEqual(["acme/web#1", "me/web#1"]);
337
338 // Another host is not reachable through this project's credentials at all.
339 const elsewhere = yield* Effect.flip(service.detail({ ...own, host: "ghe.example.test" }));
340 expect(elsewhere).toMatchObject({
341 _tag: "IssueUnavailableError",
342 reason: "tracker-unsupported",
343 });
344 }),
345 );
346
347 it.effect("resolves both legacy Azure work item URLs through the bound project", () =>
348 Effect.gen(function* () {
349 const tracker = IssueTrackerKind.make("azure-devops");
350 const host = "acme.visualstudio.com";
351 const service = yield* makeService({
352 projects: [
353 project({ id: "api", repository: "Api/_git/api", provider: tracker, host }),
354 project({
355 id: "web",
356 repository: "DefaultCollection/Web/_git/web",
357 provider: tracker,
358 host,
359 }),
360 ],
361 trackers: [fakeTracker({ kind: tracker })],
362 });
363 for (const path of ["Web", "DefaultCollection/Web"]) {
364 const ref = yield* service.resolveUrl(
365 `https://${host}/${path}/_workitems/edit/9`,
366 "api" as ProjectId,
367 );
368 expect(ref).toEqual({
369 projectId: "web",
370 tracker,
371 host,
372 scope: "DefaultCollection/Web",
373 key: "9",
374 writable: true,
375 });
376 }
377 }),
378 );
379
380 it.effect("sends writes as interactive requests and reads as background ones", () =>
381 Effect.gen(function* () {
382 const interactive: Array<readonly [string, boolean]> = [];
383 const record = (operation: string) =>
384 Effect.gen(function* () {
385 interactive.push([operation, yield* SourceControlRateLimit.Interactive]);
386 });
387 const service = yield* makeService({
388 projects: [project({ id: "web", repository: "acme/web" })],
389 trackers: [
390 fakeTracker({
391 getComments: () =>
392 record("comments").pipe(Effect.as({ comments: [], nextCursor: null })),
393 comment: () => record("comment"),
394 }),
395 ],
396 });
397 const ref = {
398 projectId: "web" as ProjectId,
399 tracker: GITHUB,
400 scope: "acme/web",
401 key: "42",
402 };
403 yield* service.comments({ ...ref, cursor: "next" });
404 yield* service.comment({ ...ref, body: "Repro attached." });
405 expect(interactive).toEqual([
406 ["comments", false],
407 ["comment", true],
408 ]);
409 }),
410 );
411
412 it.effect("refuses what the tracker did not declare before calling it", () =>
413 Effect.gen(function* () {
414 let calls = 0;
415 const service = yield* makeService({
416 projects: [project({ id: "web", repository: "acme/web" })],
417 trackers: [
418 fakeTracker({
419 setState: () =>
420 Effect.sync(() => {
421 calls += 1;
422 }),
423 setReaction: () =>
424 Effect.sync(() => {
425 calls += 1;
426 }),
427 }),
428 ],
429 });
430 const ref = { projectId: "web" as ProjectId, tracker: GITHUB, scope: "acme/web", key: "1" };
431
432 // The state is the tracker's own name, passed through: the tracker is what proves it.
433 yield* service.setState({ ...ref, state: "In Review" });
434 const reaction = yield* Effect.flip(
435 service.react({ ...ref, content: "heart", reacted: true }),
436 );
437 expect(reaction._tag).toBe("IssueOperationError");
438 expect(calls).toBe(1);
439 }),
440 );
441
442 it.effect("deduplicates and pages repositories despite partial failures", () =>
443 Effect.gen(function* () {
444 const listed: string[] = [];
445 let failContinuation = true;
446 const service = yield* makeService({
447 projects: [
448 project({ id: "web", repository: "Acme/Web" }),
449 project({ id: "web-worktree", repository: "acme/web" }),
450 project({ id: "api", repository: "acme/api" }),
451 project({ id: "docs", repository: "acme/docs" }),
452 project({
453 id: "gitlab",
454 repository: "acme/gl",
455 provider: "gitlab",
456 host: "gitlab.com",
457 }),
458 project({ id: "plain" }),
459 ],
460 trackers: [
461 fakeTracker({
462 listIssues: (input) =>
463 Effect.gen(function* () {
464 listed.push(input.scope);
465 if (input.scope === "acme/api")
466 return yield* new IssueTrackerError({
467 tracker: GITHUB,
468 operation: "listIssues",
469 reason: "failed",
470 // A tracker may fail without a detail; the wire error still needs one.
471 detail: " ",
472 });
473 if (input.scope === "acme/docs")
474 return { items: [issue("3", "2026-07-04T00:00:00Z")], nextCursor: null };
475 if (input.cursor === "next" && failContinuation) {
476 failContinuation = false;
477 return yield* new IssueTrackerError({
478 tracker: GITHUB,
479 operation: "listIssues",
480 reason: "failed",
481 detail: "try again",
482 });
483 }
484 if (input.cursor === "next")
485 return { items: [issue("4", "2026-07-01T00:00:00Z")], nextCursor: null };
486 return {
487 items: [issue("1"), issue("2", "2026-07-05T00:00:00Z")],
488 nextCursor: "next",
489 };
490 }),
491 }),
492 ],
493 });
494 const result = yield* service.list({ state: "open" });
495 expect(listed.sort()).toEqual(["Acme/Web", "acme/api", "acme/docs"]);
496 expect(result.entries.map((entry) => [entry.projectId, entry.key])).toEqual([
497 ["web", "2"],
498 ["docs", "3"],
499 ["web", "1"],
500 ]);
501 expect(
502 result.entries.filter((entry) => entry.projectId === "web").map((entry) => entry.scope),
503 ).toEqual(["Acme/Web", "Acme/Web"]);
504 expect(result.nextCursors).toEqual({ "github.com acme/web": "next" });
505 expect(result.errors).toEqual([
506 { projectId: "api", projectTitle: "api", message: "The issue tracker request failed." },
507 ]);
508 expect(result.viewers).toEqual({});
509 expect(result.trackers).toEqual([
510 expect.objectContaining({ host: "github.com", projectCount: 4, configured: true }),
511 expect.objectContaining({
512 host: "gitlab.com",
513 tracker: "gitlab",
514 projectCount: 1,
515 configured: false,
516 capabilities: null,
517 }),
518 ]);
519
520 // Narrowing to one host still names every tracker, so the client can switch between them.
521 // Narrowing by involvement reads the account it compares against.
522 const narrowed = yield* service.list({
523 state: "open",
524 host: "github.com",
525 involvement: "assigned",
526 });
527 expect(narrowed.viewers).toEqual({ "github.com": "julius" });
528 expect(narrowed.entries.every((entry) => entry.host === "github.com")).toBe(true);
529 expect(narrowed.trackers.map((tracker) => tracker.host)).toEqual([
530 "github.com",
531 "gitlab.com",
532 ]);
533
534 // A continuation only reads the scopes it names.
535 listed.length = 0;
536 const failed = yield* service.list({
537 state: "open",
538 cursors: result.nextCursors,
539 });
540 expect(listed).toEqual(["Acme/Web"]);
541 expect(failed.nextCursors).toEqual(result.nextCursors);
542 expect(failed.errors).toEqual([
543 { projectId: "web", projectTitle: "web", message: "try again" },
544 ]);
545 const next = yield* service.list({ state: "open", cursors: failed.nextCursors });
546 expect(next.entries.map((entry) => [entry.projectId, entry.key])).toEqual([["web", "4"]]);
547 expect(next.nextCursors).toEqual({});
548 expect(next.errors).toEqual([]);
549
550 // The cursor belongs to the scope, not to the project whose spelling first read it.
551 listed.length = 0;
552 const through = yield* service.list({
553 state: "open",
554 projectId: "web-worktree" as ProjectId,
555 cursors: result.nextCursors,
556 });
557 expect(listed).toEqual(["acme/web"]);
558 expect(through.entries.map((entry) => entry.key)).toEqual(["4"]);
559 }),
560 );
561});
562
563describe("IssueService with a standalone tracker", () => {
564 it.effect("reads bindings once per listing and sees changes on the next listing", () =>
565 Effect.gen(function* () {
566 const web = project({ id: "web", repository: "acme/web" });
567 const docs = project({ id: "docs", repository: "acme/docs" });
568 let reads = 0;
569 let scope = "ENG";
570 const service = yield* makeService({
571 projects: [web, docs],
572 trackers: [fakeTracker(), fakeStandaloneTracker()],
573 readBindings: Effect.sync(() => {
574 reads += 1;
575 return new Map([[web.id, { tracker: LINEAR, scope }]]);
576 }),
577 });
578
579 const first = yield* service.list({ state: "open" });
580 expect(first.scopes?.map((entry) => entry.scope)).toEqual(["ENG", "acme/web", "acme/docs"]);
581 expect(reads).toBe(1);
582
583 scope = "OPS";
584 const next = yield* service.list({ state: "open", refresh: true });
585 expect(next.scopes?.map((entry) => entry.scope)).toEqual(["OPS", "acme/web", "acme/docs"]);
586 expect(reads).toBe(2);
587
588 const missing = yield* service.list({ state: "open", projectId: "missing" as ProjectId });
589 expect(missing.scopes).toEqual([]);
590 expect(reads).toBe(2);
591 }),
592 );
593
594 it.effect("rejects a URL from another workspace with the same team and issue key", () =>
595 Effect.gen(function* () {
596 const service = yield* makeService({
597 projects: [project({ id: "web" })],
598 trackers: [fakeStandaloneTracker()],
599 bindings: { web: { tracker: LINEAR, scope: "ENG" } },
600 });
601 expect(
602 yield* service.resolveUrl("https://linear.app/acme/issue/ENG-1/old-title"),
603 ).toMatchObject({ key: "ENG-1", writable: true });
604 const refused = yield* service
605 .resolveUrl("https://linear.app/other-workspace/issue/ENG-1/old-title")
606 .pipe(Effect.result);
607 expect(refused).toMatchObject({
608 _tag: "Failure",
609 failure: {
610 _tag: "IssueOperationError",
611 operation: "resolveUrl",
612 reason: "not-found",
613 detail: "This issue URL does not belong to the connected workspace.",
614 },
615 });
616 }),
617 );
618
619 it.effect("lists a bound project's standalone scopes beside its git host and serves them", () =>
620 Effect.gen(function* () {
621 const listed: string[] = [];
622 const record = (tracker: IssueTrackerKind): Partial<IssueTrackerApi> => ({
623 listIssues: (input) =>
624 Effect.sync(() => {
625 listed.push(`${tracker} ${input.host} ${input.scope}`);
626 return {
627 items: [issue(tracker === LINEAR ? `${input.scope}-1` : "1")],
628 nextCursor: null,
629 };
630 }),
631 });
632 const projects = [project({ id: "web", repository: "acme/web" }), project({ id: "notes" })];
633 const trackers = [fakeTracker(record(GITHUB)), fakeStandaloneTracker(record(LINEAR))];
634 const bound = yield* makeService({
635 projects: [...projects, project({ id: "api", repository: "acme/api" })],
636 trackers,
637 bindings: {
638 ["web" as ProjectId]: { tracker: LINEAR, scope: "ENG", additionalScopes: ["OPS"] },
639 },
640 });
641
642 const result = yield* bound.list({ state: "open" });
643 expect(listed.sort()).toEqual([
644 "github github.com acme/api",
645 "github github.com acme/web",
646 "linear linear.app ENG",
647 "linear linear.app OPS",
648 ]);
649 expect(result.entries.map((entry) => [entry.tracker, entry.scope, entry.key]).sort()).toEqual(
650 [
651 ["github", "acme/api", "1"],
652 ["github", "acme/web", "1"],
653 ["linear", "ENG", "ENG-1"],
654 ["linear", "OPS", "OPS-1"],
655 ],
656 );
657 expect(
658 result.trackers
659 .map((tracker) => [tracker.tracker, tracker.host, tracker.projectCount])
660 .sort(),
661 ).toEqual([
662 ["github", "github.com", 2],
663 ["linear", "linear.app", 1],
664 ]);
665 expect(result.scopes).toContainEqual({
666 projectId: "web",
667 tracker: "linear",
668 host: "linear.app",
669 scope: "ENG",
670 });
671 // Narrowed to one project, both of its trackers are still read.
672 listed.length = 0;
673 const selected = yield* bound.list({ state: "open", projectId: "web" as ProjectId });
674 expect(listed.sort()).toEqual([
675 "github github.com acme/web",
676 "linear linear.app ENG",
677 "linear linear.app OPS",
678 ]);
679 expect(selected.entries.map((entry) => [entry.projectId, entry.scope]).sort()).toEqual([
680 ["web", "ENG"],
681 ["web", "OPS"],
682 ["web", "acme/web"],
683 ]);
684 listed.length = 0;
685 yield* bound.list({ state: "open", excludeProjectIds: ["web" as ProjectId] });
686 expect(listed).toEqual(["github github.com acme/api"]);
687 // A reference picks the project's tracker by kind; both are the project's own, so writable.
688 const linearRef = {
689 projectId: "web" as ProjectId,
690 tracker: LINEAR,
691 scope: "ENG",
692 key: "ENG-1",
693 };
694 expect(yield* bound.detail(linearRef)).toMatchObject({ host: "linear.app", writable: true });
695 expect(yield* bound.detail({ ...linearRef, scope: "OPS", key: "OPS-1" })).toMatchObject({
696 writable: true,
697 });
698 expect(yield* bound.detail({ ...linearRef, scope: "QA", key: "QA-1" })).toMatchObject({
699 writable: false,
700 });
701 expect((yield* bound.projectScope("web" as ProjectId)).scope).toBe("ENG");
702 expect((yield* bound.projectScope("web" as ProjectId, { key: "ops-12" })).scope).toBe("OPS");
703 expect(yield* bound.projectScope("web" as ProjectId, { scope: "ACME/Web" })).toMatchObject({
704 tracker: GITHUB,
705 host: "github.com",
706 scope: "acme/web",
707 });
708 expect(
709 yield* bound.detail({
710 projectId: "web" as ProjectId,
711 tracker: GITHUB,
712 scope: "acme/web",
713 key: "1",
714 }),
715 ).toMatchObject({ host: "github.com", writable: true });
716
717 const resolved = yield* bound
718 .resolveUrl("https://linear.app/acme/issue/ENG-1/issue-title", "web" as ProjectId)
719 .pipe(Effect.result);
720 expect(resolved).toMatchObject({
721 _tag: "Success",
722 success: {
723 projectId: "web",
724 tracker: LINEAR,
725 host: "linear.app",
726 scope: "ENG",
727 key: "ENG-1",
728 writable: true,
729 },
730 });
731
732 // Unbound, the standalone scope is gone and its references are unsupported.
733 listed.length = 0;
734 const unbound = yield* makeService({ projects, trackers });
735 const after = yield* unbound.list({ state: "open" });
736 expect(listed).toEqual(["github github.com acme/web"]);
737 expect(after.trackers.map((tracker) => tracker.tracker)).toEqual(["github"]);
738 const refused = yield* Effect.flip(unbound.detail(linearRef));
739 expect(refused).toMatchObject({
740 _tag: "IssueUnavailableError",
741 reason: "tracker-unsupported",
742 });
743
744 // A project with no remote at all can still be bound, and then has only the standalone one.
745 const standaloneOnly = yield* makeService({
746 projects,
747 trackers,
748 bindings: { ["notes" as ProjectId]: { tracker: LINEAR, scope: "OPS" } },
749 });
750 const notes = yield* standaloneOnly.list({ state: "open", projectId: "notes" as ProjectId });
751 expect(notes.entries.map((entry) => [entry.projectId, entry.scope])).toEqual([
752 ["notes", "OPS"],
753 ]);
754
755 // Bindings that cannot be read fail the listing; they are not taken for "none bound".
756 const unreadable = yield* makeService({
757 projects,
758 trackers,
759 readBindings: Effect.fail(
760 new PersistenceSqlError({
761 operation: "IssueTrackerBindingsRepository.list",
762 detail: "disk I/O error",
763 }),
764 ),
765 });
766 expect(yield* Effect.flip(unreadable.list({ state: "open" }))).toMatchObject({
767 _tag: "IssueOperationError",
768 operation: "listProjects",
769 });
770 }),
771 );
772});