Open History to see the last commit
1import {
2 CheckpointRef,
3 EventId,
4 GitManagerError,
5 ProjectId,
6 ProviderInstanceId,
7 ThreadId,
8 TurnId,
9 type OrchestrationCommand,
10 type OrchestrationEvent,
11 type OrchestrationProjectShell,
12 type OrchestrationShellSnapshot,
13 type OrchestrationThreadShell,
14 type PullRequestRef,
15 type PullRequestSummary,
16 type ThreadLinkedPullRequest,
17} from "@t3tools/contracts";
18import { describe, expect, it } from "@effect/vitest";
19import * as Crypto from "effect/Crypto";
20import * as Deferred from "effect/Deferred";
21import * as Effect from "effect/Effect";
22import * as FileSystem from "effect/FileSystem";
23import * as Layer from "effect/Layer";
24import * as Option from "effect/Option";
25import * as PubSub from "effect/PubSub";
26import * as Queue from "effect/Queue";
27import * as Ref from "effect/Ref";
28import * as Stream from "effect/Stream";
29import { TestClock } from "effect/testing";
30
31import { GitManager, type GitBranchPullRequest } from "../git/GitManager.ts";
32import { PullRequestService } from "../pullRequest/PullRequestService.ts";
33import { RepositoryIdentityResolver } from "../project/RepositoryIdentityResolver.ts";
34import { ServerActivation } from "../serverActivation.ts";
35import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts";
36import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";
37import * as ThreadPullRequestReactor from "./ThreadPullRequestReactor.ts";
38
39const NOW = "2026-09-01T12:00:00.000Z";
40const PROJECT_ID = ProjectId.make("project");
41const REPOSITORY = "owner/repository";
42const REPOSITORY_KEY = `github.com/${REPOSITORY}`;
43type SyncCommand = Extract<OrchestrationCommand, { type: "thread.pull-request.sync" }>;
44
45function reference(number: number): ThreadLinkedPullRequest {
46 return {
47 projectId: PROJECT_ID,
48 repository: REPOSITORY,
49 number,
50 url: `https://github.com/${REPOSITORY}/pull/${number}`,
51 };
52}
53
54function branchPullRequest(
55 number = 42,
56 state: GitBranchPullRequest["state"] = "open",
57): GitBranchPullRequest {
58 return {
59 ...reference(number),
60 title: "Branch pull request",
61 baseRef: "main",
62 headRef: "feature",
63 state,
64 updatedAt: NOW,
65 repositoryKey: REPOSITORY_KEY,
66 };
67}
68
69function summary(input: PullRequestRef, state: PullRequestSummary["state"]): PullRequestSummary {
70 return {
71 ...input,
72 provider: "github",
73 title: "Pull request",
74 url: reference(input.number).url,
75 state,
76 headBranch: "feature",
77 baseBranch: "main",
78 updatedAt: NOW,
79 };
80}
81
82function thread(
83 id: string,
84 overrides: Partial<OrchestrationThreadShell> = {},
85): OrchestrationThreadShell {
86 return {
87 id: ThreadId.make(id),
88 projectId: PROJECT_ID,
89 title: id,
90 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
91 runtimeMode: "full-access",
92 interactionMode: "default",
93 pullRequests: [],
94 branch: "feature",
95 worktreePath: null,
96 latestTurn: null,
97 createdAt: NOW,
98 updatedAt: NOW,
99 archivedAt: null,
100 settledOverride: null,
101 settledAt: null,
102 session: null,
103 latestUserMessageAt: NOW,
104 hasPendingApprovals: false,
105 hasPendingUserInput: false,
106 hasActionableProposedPlan: false,
107 ...overrides,
108 };
109}
110
111const project = {
112 id: PROJECT_ID,
113 title: "Project",
114 workspaceRoot: "/workspace/project",
115 repositoryIdentity: {
116 canonicalKey: REPOSITORY_KEY,
117 displayName: REPOSITORY,
118 rootPath: "/workspace/project",
119 locator: {
120 source: "git-remote",
121 remoteName: "origin",
122 remoteUrl: `git@github.com:${REPOSITORY}.git`,
123 },
124 },
125 defaultModelSelection: null,
126 scripts: [],
127 createdAt: NOW,
128 updatedAt: NOW,
129} satisfies OrchestrationProjectShell;
130
131const makeHarness = Effect.fn("makeThreadPullRequestHarness")(function* (options: {
132 readonly threads: ReadonlyArray<OrchestrationThreadShell>;
133 readonly branchPullRequest?: GitManager["Service"]["branchPullRequest"];
134 readonly summary?: PullRequestService["Service"]["summary"];
135 readonly existingWorktrees?: ReadonlyArray<string>;
136 readonly project?: OrchestrationProjectShell;
137 readonly resolveRepositoryIdentity?: RepositoryIdentityResolver["Service"]["resolve"];
138}) {
139 const activation = yield* Deferred.make<void>();
140 const snapshots = yield* Ref.make<OrchestrationShellSnapshot>({
141 snapshotSequence: 1,
142 projects: [options.project ?? project],
143 threads: options.threads,
144 updatedAt: NOW,
145 });
146 // Each shell read: a thread id for a one-thread read, null for a full read.
147 const reads = yield* Queue.unbounded<ThreadId | null>();
148 const events = yield* PubSub.unbounded<OrchestrationEvent>();
149 const commands = yield* Ref.make<ReadonlyArray<SyncCommand>>([]);
150 const branchCalls = yield* Ref.make<
151 ReadonlyArray<{ readonly cwd: string; readonly branch: string; readonly refresh: boolean }>
152 >([]);
153 const summaryCalls = yield* Ref.make<ReadonlyArray<PullRequestRef>>([]);
154 let uuid = 0;
155 const dependencies = Layer.mergeAll(
156 Layer.mock(ProjectionSnapshotQuery)({
157 getShellSnapshot: () => Ref.get(snapshots).pipe(Effect.tap(() => Queue.offer(reads, null))),
158 getSnapshotSequence: () =>
159 Ref.get(snapshots).pipe(Effect.map(({ snapshotSequence }) => ({ snapshotSequence }))),
160 getThreadShellById: (threadId) =>
161 Ref.get(snapshots).pipe(
162 Effect.map(({ threads }) =>
163 Option.fromUndefinedOr(
164 threads.find((thread) => thread.id === threadId && thread.archivedAt === null),
165 ),
166 ),
167 Effect.tap(() => Queue.offer(reads, threadId)),
168 ),
169 getProjectShells: (projectIds) =>
170 Ref.get(snapshots).pipe(
171 Effect.map(({ projects }) =>
172 projects.filter((project) => projectIds?.includes(project.id) ?? true),
173 ),
174 ),
175 }),
176 Layer.mock(GitManager)({
177 branchPullRequest: (input, readOptions) =>
178 Ref.update(branchCalls, (calls) => [
179 ...calls,
180 { ...input, refresh: readOptions?.refresh === true },
181 ]).pipe(
182 Effect.andThen(options.branchPullRequest?.(input, readOptions) ?? Effect.succeed(null)),
183 ),
184 }),
185 Layer.mock(PullRequestService)({
186 summary: (input, readOptions) =>
187 Ref.update(summaryCalls, (calls) => [...calls, input]).pipe(
188 Effect.andThen(
189 options.summary?.(input, readOptions) ?? Effect.succeed(summary(input, "open")),
190 ),
191 ),
192 }),
193 Layer.mock(RepositoryIdentityResolver)({
194 resolve:
195 options.resolveRepositoryIdentity ??
196 (() => Effect.succeed(options.project?.repositoryIdentity ?? project.repositoryIdentity)),
197 }),
198 Layer.mock(OrchestrationEngineService)({
199 subscribeDomainEvents: PubSub.subscribe(events).pipe(
200 Effect.map((subscription) => Stream.fromSubscription(subscription)),
201 ),
202 dispatch: (command) => {
203 if (command.type !== "thread.pull-request.sync") {
204 return Effect.die(`Unexpected command: ${command.type}`);
205 }
206 return Ref.update(commands, (current) => [...current, command]).pipe(
207 Effect.andThen(
208 Ref.updateAndGet(snapshots, (snapshot) => ({
209 ...snapshot,
210 snapshotSequence: snapshot.snapshotSequence + 1,
211 threads: snapshot.threads.map((current) =>
212 current.id === command.threadId
213 ? {
214 ...current,
215 branchPullRequest: command.branchPullRequest,
216 ...(command.linkedPullRequest !== undefined
217 ? { linkedPullRequest: command.linkedPullRequest }
218 : {}),
219 }
220 : current,
221 ),
222 })),
223 ),
224 Effect.map((snapshot) => ({ sequence: snapshot.snapshotSequence })),
225 );
226 },
227 }),
228 Layer.succeed(ServerActivation, Deferred.await(activation)),
229 Layer.succeed(
230 Crypto.Crypto,
231 Crypto.make({
232 randomBytes: (size) => new Uint8Array(size).fill(++uuid),
233 digest: (_algorithm, data) => Effect.succeed(data),
234 }),
235 ),
236 FileSystem.layerNoop({
237 exists: (path) => Effect.succeed(options.existingWorktrees?.includes(path) ?? false),
238 }),
239 );
240
241 const start = Effect.fn("startThreadPullRequestHarness")(function* () {
242 const reactor = yield* ThreadPullRequestReactor.ThreadPullRequestReactor;
243 yield* reactor.start();
244 yield* Deferred.succeed(activation, undefined);
245 yield* Queue.take(reads);
246 yield* reactor.drain;
247 return reactor;
248 });
249
250 return {
251 start,
252 reads,
253 snapshots,
254 commands,
255 branchCalls,
256 summaryCalls,
257 publish: (event: OrchestrationEvent) => PubSub.publish(events, event),
258 layer: ThreadPullRequestReactor.layer.pipe(Layer.provide(dependencies)),
259 };
260});
261
262describe("ThreadPullRequestReactor", () => {
263 it.effect("discovers saved branch PRs without a client and shares branch lookups", () =>
264 Effect.scoped(
265 Effect.gen(function* () {
266 const fixture = yield* makeHarness({
267 threads: [
268 thread("first"),
269 thread("second"),
270 thread("archived", { archivedAt: NOW }),
271 thread("no-branch", { branch: null }),
272 ],
273 branchPullRequest: () => Effect.succeed(branchPullRequest()),
274 });
275 yield* Effect.gen(function* () {
276 const reactor = yield* fixture.start();
277 expect(yield* Ref.get(fixture.branchCalls)).toEqual([
278 { cwd: project.workspaceRoot, branch: "feature", refresh: false },
279 { cwd: project.workspaceRoot, branch: "feature", refresh: false },
280 ]);
281 expect((yield* Ref.get(fixture.commands)).map((command) => command.threadId)).toEqual([
282 "first",
283 "second",
284 ]);
285 expect((yield* Ref.get(fixture.snapshots)).threads[0]?.branchPullRequest).toEqual(
286 reference(42),
287 );
288
289 yield* TestClock.adjust("1 minute");
290 yield* Queue.take(fixture.reads);
291 yield* reactor.drain;
292 expect(yield* Ref.get(fixture.commands)).toHaveLength(2);
293 }).pipe(Effect.provide(fixture.layer));
294 }),
295 ),
296 );
297
298 it.effect("replaces terminal manual links but preserves open links and explicit unlink", () =>
299 Effect.scoped(
300 Effect.gen(function* () {
301 const fixture = yield* makeHarness({
302 threads: [
303 thread("merged", { linkedPullRequest: reference(1) }),
304 thread("closed", { linkedPullRequest: reference(2) }),
305 thread("open", { linkedPullRequest: reference(3) }),
306 thread("unlinked", { linkedPullRequest: null }),
307 ],
308 branchPullRequest: () => Effect.succeed(branchPullRequest()),
309 summary: (input) =>
310 Effect.succeed(
311 summary(
312 input,
313 input.number === 1 ? "merged" : input.number === 2 ? "closed" : "open",
314 ),
315 ),
316 });
317 yield* Effect.gen(function* () {
318 yield* fixture.start();
319 const snapshot = yield* Ref.get(fixture.snapshots);
320 expect(snapshot.threads.map((current) => current.linkedPullRequest)).toEqual([
321 reference(42),
322 reference(42),
323 reference(3),
324 null,
325 ]);
326 expect(
327 snapshot.threads.every((current) => current.branchPullRequest?.number === 42),
328 ).toBe(true);
329 }).pipe(Effect.provide(fixture.layer));
330 }),
331 ),
332 );
333
334 it.effect.each(["session-first", "checkpoint-first"] as const)(
335 "refreshes discovery once after a turn ends with %s event ordering",
336 (order) =>
337 Effect.scoped(
338 Effect.gen(function* () {
339 const current = thread("turn-thread");
340 const detected = yield* Ref.make<GitBranchPullRequest | null>(null);
341 const fixture = yield* makeHarness({
342 threads: [current],
343 branchPullRequest: (_input, options) =>
344 options?.refresh
345 ? Ref.set(detected, branchPullRequest()).pipe(Effect.andThen(Ref.get(detected)))
346 : Ref.get(detected),
347 });
348 yield* Effect.gen(function* () {
349 const reactor = yield* fixture.start();
350 expect(yield* Ref.get(fixture.commands)).toHaveLength(0);
351 const sessionEvent: OrchestrationEvent = {
352 type: "thread.session-set",
353 sequence: 2,
354 eventId: EventId.make("turn-finished"),
355 aggregateKind: "thread",
356 aggregateId: current.id,
357 occurredAt: NOW,
358 commandId: null,
359 causationEventId: null,
360 correlationId: null,
361 metadata: {},
362 payload: {
363 threadId: current.id,
364 session: {
365 threadId: current.id,
366 status: "ready",
367 providerName: "Codex",
368 runtimeMode: "full-access",
369 activeTurnId: null,
370 lastError: null,
371 updatedAt: NOW,
372 },
373 },
374 };
375 const checkpointEvent: OrchestrationEvent = {
376 ...sessionEvent,
377 type: "thread.turn-diff-completed",
378 sequence: 3,
379 eventId: EventId.make("checkpoint-finished"),
380 payload: {
381 threadId: current.id,
382 turnId: TurnId.make("turn"),
383 checkpointTurnCount: 1,
384 checkpointRef: CheckpointRef.make("checkpoint"),
385 status: "ready",
386 files: [],
387 assistantMessageId: null,
388 completedAt: NOW,
389 },
390 };
391 const events =
392 order === "session-first"
393 ? [sessionEvent, checkpointEvent]
394 : [checkpointEvent, sessionEvent];
395 for (const event of events) {
396 yield* fixture.publish(event);
397 expect(yield* Queue.take(fixture.reads)).toBe(current.id);
398 yield* reactor.drain;
399 }
400 expect((yield* Ref.get(fixture.commands))[0]?.branchPullRequest).toEqual(reference(42));
401 expect(
402 (yield* Ref.get(fixture.branchCalls)).filter((call) => call.refresh),
403 ).toHaveLength(1);
404 }).pipe(Effect.provide(fixture.layer));
405 }),
406 ),
407 );
408
409 it.effect("refreshes the project identity when a turn adds the remote", () =>
410 Effect.scoped(
411 Effect.gen(function* () {
412 const current = thread("new-remote");
413 const fixture = yield* makeHarness({
414 threads: [current],
415 project: { ...project, repositoryIdentity: null },
416 branchPullRequest: () => Effect.succeed(branchPullRequest()),
417 resolveRepositoryIdentity: (_cwd, options) =>
418 Effect.succeed(options?.refresh ? project.repositoryIdentity : null),
419 });
420 yield* Effect.gen(function* () {
421 const reactor = yield* fixture.start();
422 expect(yield* Ref.get(fixture.commands)).toHaveLength(0);
423
424 yield* fixture.publish({
425 type: "thread.turn-diff-completed",
426 sequence: 2,
427 eventId: EventId.make("checkpoint-finished"),
428 aggregateKind: "thread",
429 aggregateId: current.id,
430 occurredAt: NOW,
431 commandId: null,
432 causationEventId: null,
433 correlationId: null,
434 metadata: {},
435 payload: {
436 threadId: current.id,
437 turnId: TurnId.make("turn"),
438 checkpointTurnCount: 1,
439 checkpointRef: CheckpointRef.make("checkpoint"),
440 status: "ready",
441 files: [],
442 assistantMessageId: null,
443 completedAt: NOW,
444 },
445 });
446 yield* Queue.take(fixture.reads);
447 yield* reactor.drain;
448 expect((yield* Ref.get(fixture.commands))[0]?.branchPullRequest).toEqual(reference(42));
449 }).pipe(Effect.provide(fixture.layer));
450 }),
451 ),
452 );
453
454 it.effect("uses live worktrees and falls back to the project for removed worktrees", () =>
455 Effect.scoped(
456 Effect.gen(function* () {
457 const fixture = yield* makeHarness({
458 threads: [
459 thread("live", { worktreePath: "/workspace/worktree" }),
460 thread("removed", { worktreePath: "/workspace/removed" }),
461 ],
462 existingWorktrees: ["/workspace/worktree"],
463 branchPullRequest: () => Effect.succeed(branchPullRequest()),
464 });
465 yield* Effect.gen(function* () {
466 yield* fixture.start();
467 expect(new Set((yield* Ref.get(fixture.branchCalls)).map((call) => call.cwd))).toEqual(
468 new Set(["/workspace/project", "/workspace/worktree"]),
469 );
470 }).pipe(Effect.provide(fixture.layer));
471 }),
472 ),
473 );
474
475 it.effect("retains only terminal PRs on shared checkouts and clears a removed branch", () =>
476 Effect.scoped(
477 Effect.gen(function* () {
478 const fixture = yield* makeHarness({
479 threads: [
480 thread("terminal", { branch: "main", branchPullRequest: reference(1) }),
481 thread("open", { branchPullRequest: reference(2) }),
482 thread("worktree", {
483 worktreePath: "/workspace/worktree",
484 branchPullRequest: reference(1),
485 }),
486 thread("cleared", {
487 branch: null,
488 branchPullRequest: reference(1),
489 linkedPullRequest: reference(3),
490 }),
491 ],
492 summary: (input) =>
493 Effect.succeed(summary(input, input.number === 1 ? "merged" : "open")),
494 });
495 yield* Effect.gen(function* () {
496 yield* fixture.start();
497 const snapshot = yield* Ref.get(fixture.snapshots);
498 expect(snapshot.threads.map((current) => current.branchPullRequest)).toEqual([
499 reference(1),
500 null,
501 null,
502 null,
503 ]);
504 expect(snapshot.threads[3]?.linkedPullRequest).toEqual(reference(3));
505 }).pipe(Effect.provide(fixture.layer));
506 }),
507 ),
508 );
509
510 it.effect("keeps saved links on lookup failures and rejects a different repository", () =>
511 Effect.scoped(
512 Effect.gen(function* () {
513 const fixture = yield* makeHarness({
514 threads: [
515 thread("failed", { branch: "failed", branchPullRequest: reference(1) }),
516 thread("wrong-repository", {
517 branch: "wrong-repository",
518 branchPullRequest: reference(2),
519 }),
520 thread("healthy"),
521 ],
522 branchPullRequest: ({ cwd, branch }) =>
523 branch === "failed"
524 ? Effect.fail(
525 new GitManagerError({
526 operation: "branchPullRequest",
527 cwd,
528 detail: "Lookup failed",
529 }),
530 )
531 : Effect.succeed({
532 ...branchPullRequest(),
533 repositoryKey:
534 branch === "wrong-repository" ? "github.com/other/repository" : REPOSITORY_KEY,
535 }),
536 });
537 yield* Effect.gen(function* () {
538 yield* fixture.start();
539 expect(
540 (yield* Ref.get(fixture.snapshots)).threads.map((current) => current.branchPullRequest),
541 ).toEqual([reference(1), reference(2), reference(42)]);
542 expect(yield* Ref.get(fixture.commands)).toHaveLength(1);
543 }).pipe(Effect.provide(fixture.layer));
544 }),
545 ),
546 );
547
548 it.effect.each(["settled", null] as const)(
549 "retries settled backfills and stops after success with settledOverride %s",
550 (settledOverride) =>
551 Effect.scoped(
552 Effect.gen(function* () {
553 const online = yield* Ref.make(false);
554 const fixture = yield* makeHarness({
555 threads: [
556 thread("backfill", { settledOverride, settledAt: NOW }),
557 thread("known", {
558 branch: "known",
559 settledOverride,
560 settledAt: NOW,
561 branchPullRequest: reference(1),
562 }),
563 ],
564 branchPullRequest: ({ cwd }) =>
565 Ref.get(online).pipe(
566 Effect.flatMap((connected) =>
567 connected
568 ? Effect.succeed(branchPullRequest(42, "merged"))
569 : Effect.fail(
570 new GitManagerError({
571 operation: "branchPullRequest",
572 cwd,
573 detail: "Offline",
574 }),
575 ),
576 ),
577 ),
578 });
579 yield* Effect.gen(function* () {
580 const reactor = yield* fixture.start();
581 expect(yield* Ref.get(fixture.commands)).toHaveLength(0);
582 // A one-thread read cannot show that other pending threads are gone.
583 const gone = ThreadId.make("gone");
584 yield* fixture.publish({
585 type: "thread.unarchived",
586 sequence: 2,
587 eventId: EventId.make("gone-unarchived"),
588 aggregateKind: "thread",
589 aggregateId: gone,
590 occurredAt: NOW,
591 commandId: null,
592 causationEventId: null,
593 correlationId: null,
594 metadata: {},
595 payload: { threadId: gone, updatedAt: NOW },
596 });
597 expect(yield* Queue.take(fixture.reads)).toBe(gone);
598 yield* reactor.drain;
599 yield* Ref.set(online, true);
600 yield* TestClock.adjust("1 minute");
601 yield* Queue.take(fixture.reads);
602 yield* reactor.drain;
603 expect((yield* Ref.get(fixture.commands))[0]?.threadId).toBe("backfill");
604 yield* TestClock.adjust("1 minute");
605 yield* Queue.take(fixture.reads);
606 yield* reactor.drain;
607 expect((yield* Ref.get(fixture.branchCalls)).map((call) => call.branch)).toEqual([
608 "feature",
609 "feature",
610 "feature",
611 ]);
612 }).pipe(Effect.provide(fixture.layer));
613 }),
614 ),
615 );
616
617 it.effect("stops retrying a settled backfill after repeated lookup failures", () =>
618 Effect.scoped(
619 Effect.gen(function* () {
620 const fixture = yield* makeHarness({
621 threads: [thread("backfill", { settledOverride: "settled", settledAt: NOW })],
622 branchPullRequest: ({ cwd }) =>
623 Effect.fail(
624 new GitManagerError({ operation: "branchPullRequest", cwd, detail: "No gh" }),
625 ),
626 });
627 yield* Effect.gen(function* () {
628 const reactor = yield* fixture.start();
629 for (
630 let attempt = 1;
631 attempt < ThreadPullRequestReactor.BACKFILL_ATTEMPTS + 2;
632 attempt++
633 ) {
634 yield* TestClock.adjust("1 minute");
635 yield* Queue.take(fixture.reads);
636 yield* reactor.drain;
637 }
638 expect(yield* Ref.get(fixture.branchCalls)).toHaveLength(
639 ThreadPullRequestReactor.BACKFILL_ATTEMPTS,
640 );
641 expect(yield* Ref.get(fixture.commands)).toHaveLength(0);
642 }).pipe(Effect.provide(fixture.layer));
643 }),
644 ),
645 );
646
647 it.effect("matches Azure SSH projects to HTTPS PRs with the provider repository selector", () =>
648 Effect.scoped(
649 Effect.gen(function* () {
650 const fixture = yield* makeHarness({
651 threads: [thread("azure")],
652 project: {
653 ...project,
654 repositoryIdentity: {
655 canonicalKey: "ssh.dev.azure.com/v3/org/project/repository",
656 displayName: "v3/org/project/repository",
657 name: "repository",
658 provider: "azure-devops",
659 rootPath: project.workspaceRoot,
660 locator: {
661 source: "git-remote",
662 remoteName: "origin",
663 remoteUrl: "git@ssh.dev.azure.com:v3/org/project/repository",
664 },
665 },
666 },
667 branchPullRequest: () =>
668 Effect.succeed({
669 ...branchPullRequest(),
670 repositoryKey: "dev.azure.com/org/project/_git/repository",
671 url: "https://dev.azure.com/org/project/_git/repository/pullrequest/42",
672 }),
673 });
674 yield* Effect.gen(function* () {
675 yield* fixture.start();
676 expect((yield* Ref.get(fixture.commands))[0]?.branchPullRequest).toEqual({
677 projectId: PROJECT_ID,
678 repository: "repository",
679 number: 42,
680 url: "https://dev.azure.com/org/project/_git/repository/pullrequest/42",
681 });
682 }).pipe(Effect.provide(fixture.layer));
683 }),
684 ),
685 );
686
687 it.effect.each(["primary", "branch"] as const)(
688 "rejects the group's links if the %s remote changes during a summary read",
689 (changedRemote) =>
690 Effect.scoped(
691 Effect.gen(function* () {
692 const identity = yield* Ref.make(project.repositoryIdentity);
693 const detected = yield* Ref.make(branchPullRequest());
694 const fixture = yield* makeHarness({
695 threads: [thread("manual", { linkedPullRequest: reference(1) }), thread("automatic")],
696 branchPullRequest: () => Ref.get(detected),
697 resolveRepositoryIdentity: (_cwd, options) =>
698 options?.refresh ? Ref.get(identity) : Effect.succeed(project.repositoryIdentity),
699 summary: (input) =>
700 (changedRemote === "primary"
701 ? Ref.set(identity, {
702 ...project.repositoryIdentity,
703 canonicalKey: "github.com/other/repository",
704 displayName: "other/repository",
705 })
706 : Ref.set(detected, {
707 ...branchPullRequest(99),
708 repositoryKey: "github.com/other/repository",
709 url: "https://github.com/other/repository/pull/99",
710 })
711 ).pipe(Effect.as(summary(input, "merged"))),
712 });
713 yield* Effect.gen(function* () {
714 yield* fixture.start();
715 expect(yield* Ref.get(fixture.commands)).toEqual([]);
716 expect((yield* Ref.get(fixture.snapshots)).threads[0]?.linkedPullRequest).toEqual(
717 reference(1),
718 );
719 }).pipe(Effect.provide(fixture.layer));
720 }),
721 ),
722 );
723});