Open History to see the last commit
1import {
2 ProjectId,
3 ProviderInstanceId,
4 PullRequestOperationError,
5 ThreadId,
6 type OrchestrationV2Command as OrchestrationCommand,
7 type OrchestrationProjectShell,
8 type OrchestrationShellSnapshot,
9 type OrchestrationThreadShell,
10 type PullRequestRef,
11 type PullRequestStack,
12 type PullRequestSummary,
13 type ThreadPullRequestLink,
14 type ThreadPullRequestSnapshot,
15} from "@t3tools/contracts";
16import { assert, describe, it } from "@effect/vitest";
17import * as Crypto from "effect/Crypto";
18import * as Deferred from "effect/Deferred";
19import * as Effect from "effect/Effect";
20import * as Layer from "effect/Layer";
21import * as Queue from "effect/Queue";
22import * as Ref from "effect/Ref";
23import { TestClock } from "effect/testing";
24
25import { PullRequestService } from "../pullRequest/PullRequestService.ts";
26import { ServerActivation } from "../serverActivation.ts";
27import { OrchestratorV2, type OrchestratorV2Shape } from "../orchestration-v2/Orchestrator.ts";
28import { v2PullRequestThread } from "../orchestration-v2/testkit/pullRequestFixtures.ts";
29import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";
30import * as PullRequestSyncReactor from "./PullRequestSyncReactor.ts";
31
32const NOW = "2026-08-28T12:00:00.000Z";
33const PROJECT_ID = ProjectId.make("sync-project");
34
35type SyncCommand = Extract<
36 OrchestrationCommand,
37 { readonly type: "thread.pull-request-link.sync" }
38>;
39type LinkCommand = Extract<OrchestrationCommand, { readonly type: "thread.pull-request.link" }>;
40
41const testCrypto = Crypto.make({
42 randomBytes: (size) => new Uint8Array(size).fill(1),
43 digest: (_algorithm, data) => Effect.succeed(data),
44});
45
46function makeProject(id: ProjectId = PROJECT_ID): OrchestrationProjectShell {
47 return {
48 id,
49 title: `Project ${id}`,
50 workspaceRoot: "/workspace/project",
51 defaultModelSelection: null,
52 scripts: [],
53 createdAt: "2026-08-01T00:00:00.000Z",
54 updatedAt: NOW,
55 };
56}
57
58function makeThread(
59 id: string,
60 overrides: Partial<OrchestrationThreadShell> = {},
61): OrchestrationThreadShell {
62 return {
63 id: ThreadId.make(id),
64 projectId: PROJECT_ID,
65 title: id,
66 modelSelection: {
67 instanceId: ProviderInstanceId.make("codex"),
68 model: "gpt-5",
69 },
70 runtimeMode: "full-access",
71 interactionMode: "default",
72 branch: null,
73 worktreePath: null,
74 pullRequests: [],
75 latestTurn: null,
76 createdAt: "2026-08-01T00:00:00.000Z",
77 updatedAt: "2026-08-20T00:00:00.000Z",
78 archivedAt: null,
79 settledOverride: null,
80 settledAt: null,
81 session: null,
82 latestUserMessageAt: "2026-08-20T00:00:00.000Z",
83 hasPendingApprovals: false,
84 hasPendingUserInput: false,
85 hasActionableProposedPlan: false,
86 ...overrides,
87 };
88}
89
90function makeLink(
91 number: number,
92 snapshot: Partial<ThreadPullRequestSnapshot> | null = null,
93 overrides: Partial<ThreadPullRequestLink> = {},
94): ThreadPullRequestLink {
95 return {
96 host: "github.com",
97 repository: "owner/repository",
98 number,
99 url: `https://github.com/owner/repository/pull/${number}`,
100 source: "manual",
101 linkedAt: "2026-08-10T00:00:00.000Z",
102 snapshot:
103 snapshot === null
104 ? null
105 : {
106 state: "open",
107 title: "Pull request",
108 headBranch: "feature",
109 baseBranch: "main",
110 isDraft: false,
111 updatedAt: "2026-08-27T00:00:00.000Z",
112 syncedAt: "2026-08-27T00:00:00.000Z",
113 ...snapshot,
114 },
115 stack: null,
116 ...overrides,
117 };
118}
119
120function makeSnapshot(
121 threads: ReadonlyArray<OrchestrationThreadShell>,
122 snapshotSequence = 1,
123): OrchestrationShellSnapshot {
124 return {
125 snapshotSequence,
126 projects: [makeProject()],
127 threads,
128 updatedAt: NOW,
129 };
130}
131
132function makeSummary(
133 input: PullRequestRef,
134 overrides: Partial<PullRequestSummary> = {},
135): PullRequestSummary {
136 return {
137 provider: "github",
138 projectId: input.projectId,
139 repository: input.repository,
140 number: input.number,
141 title: "Pull request",
142 url: `https://github.com/${input.repository}/pull/${input.number}`,
143 state: "open",
144 headBranch: "feature",
145 baseBranch: "main",
146 updatedAt: "2026-08-27T00:00:00.000Z",
147 ...overrides,
148 };
149}
150
151interface HarnessOptions {
152 readonly invalidate?: PullRequestService["Service"]["invalidate"];
153 readonly snapshot: OrchestrationShellSnapshot;
154 readonly summary?: (
155 input: PullRequestRef,
156 ) => Effect.Effect<PullRequestSummary, PullRequestOperationError>;
157 readonly stack?: (
158 input: PullRequestRef,
159 ) => Effect.Effect<PullRequestStack | null, PullRequestOperationError>;
160}
161
162const makeHarness = Effect.fn("makePullRequestSyncHarness")(function* (options: HarnessOptions) {
163 const activation = yield* Deferred.make<void>();
164 const snapshots = yield* Ref.make(options.snapshot);
165 const snapshotReads = yield* Queue.unbounded<void>();
166 const syncCommands = yield* Ref.make<ReadonlyArray<SyncCommand>>([]);
167 const linkCommands = yield* Ref.make<ReadonlyArray<LinkCommand>>([]);
168 const summaryCalls = yield* Ref.make<ReadonlyArray<PullRequestRef>>([]);
169 const stackCalls = yield* Ref.make<ReadonlyArray<PullRequestRef>>([]);
170
171 const summary: PullRequestService["Service"]["summary"] = (input, readOptions) =>
172 Effect.gen(function* () {
173 assert.strictEqual(readOptions?.recoverTransientFailure, false);
174 yield* Ref.update(summaryCalls, (calls) => [...calls, input]);
175 return yield* options.summary?.(input) ?? Effect.succeed(makeSummary(input));
176 });
177
178 const stack: PullRequestService["Service"]["stack"] = (input, readOptions) =>
179 Effect.gen(function* () {
180 assert.strictEqual(readOptions?.includeDetails, false);
181 yield* Ref.update(stackCalls, (calls) => [...calls, input]);
182 return yield* options.stack?.(input) ?? Effect.succeed(null);
183 });
184
185 const dispatch: OrchestratorV2Shape["dispatch"] = (command) => {
186 if (command.type === "thread.pull-request-link.sync") {
187 return Ref.update(syncCommands, (recorded) => [...recorded, command]).pipe(
188 Effect.as({ sequence: 1, storedEvents: [] }),
189 );
190 }
191 if (command.type === "thread.pull-request.link") {
192 return Ref.update(linkCommands, (recorded) => [...recorded, command]).pipe(
193 Effect.as({ sequence: 1, storedEvents: [] }),
194 );
195 }
196 return Effect.die(new Error(`Unexpected command: ${command.type}`));
197 };
198
199 const dependencies = Layer.mergeAll(
200 Layer.mock(ProjectionSnapshotQuery)({}),
201 Layer.mock(PullRequestService)({
202 summary,
203 stack,
204 invalidate: options.invalidate ?? (() => Effect.void),
205 }),
206 Layer.mock(OrchestratorV2)({
207 getShellSnapshot: () =>
208 Queue.offer(snapshotReads, undefined).pipe(
209 Effect.andThen(Ref.get(snapshots)),
210 Effect.map((snapshot) => ({
211 schemaVersion: 2,
212 snapshotSequence: snapshot.snapshotSequence,
213 threads: snapshot.threads.map(v2PullRequestThread),
214 archivedThreads: [],
215 })),
216 ),
217 dispatch,
218 }),
219 Layer.succeed(ServerActivation, Deferred.await(activation)),
220 Layer.succeed(Crypto.Crypto, testCrypto),
221 );
222
223 return {
224 activation,
225 snapshots,
226 snapshotReads,
227 syncCommands,
228 linkCommands,
229 summaryCalls,
230 stackCalls,
231 layer: PullRequestSyncReactor.layer.pipe(Layer.provide(dependencies)),
232 };
233});
234
235type Harness = Effect.Success<ReturnType<typeof makeHarness>>;
236
237const startAndSweep = Effect.fn("startPullRequestSyncHarness")(function* (fixture: Harness) {
238 const reactor = yield* PullRequestSyncReactor.PullRequestSyncReactor;
239 yield* reactor.start();
240 yield* Deferred.succeed(fixture.activation, undefined);
241 yield* Queue.take(fixture.snapshotReads);
242 yield* reactor.drain;
243 return reactor;
244});
245
246const sweepAgain = Effect.fn("sweepPullRequestSyncHarness")(function* (
247 fixture: Harness,
248 reactor: PullRequestSyncReactor.PullRequestSyncReactor["Service"],
249) {
250 yield* TestClock.adjust("1 minute");
251 yield* Queue.take(fixture.snapshotReads);
252 yield* reactor.drain;
253});
254
255/** What the reactor would have persisted, so the next sweep sees its own writes. */
256function applySync(
257 snapshot: OrchestrationShellSnapshot,
258 commands: ReadonlyArray<SyncCommand>,
259): OrchestrationShellSnapshot {
260 return {
261 ...snapshot,
262 snapshotSequence: snapshot.snapshotSequence + 1,
263 threads: snapshot.threads.map((thread) => ({
264 ...thread,
265 pullRequests: thread.pullRequests.map((link) => {
266 const command = commands.findLast(
267 (candidate) => candidate.threadId === thread.id && candidate.number === link.number,
268 );
269 return command === undefined
270 ? link
271 : { ...link, snapshot: command.snapshot, stack: command.stack };
272 }),
273 })),
274 };
275}
276
277describe("PullRequestSyncReactor", () => {
278 it.effect("retries a failed stack read after the summary becomes terminal", () =>
279 Effect.scoped(
280 Effect.gen(function* () {
281 yield* TestClock.setTime(Date.parse(NOW));
282 let attempts = 0;
283 const nativeStack: PullRequestStack = {
284 id: "stack",
285 number: 7,
286 url: "https://github.com/owner/repository/stacks/7",
287 base: "main",
288 layers: [{ number: 7, headBranch: "feature", state: "merged" }],
289 };
290 const fixture = yield* makeHarness({
291 snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(7)] })]),
292 summary: (input) =>
293 Effect.succeed(makeSummary(input, { state: "merged", mergedAt: NOW })),
294 stack: () =>
295 ++attempts === 1
296 ? Effect.fail(
297 new PullRequestOperationError({
298 operation: "stack",
299 detail: "temporary failure",
300 }),
301 )
302 : Effect.succeed(nativeStack),
303 });
304 yield* Effect.gen(function* () {
305 const reactor = yield* startAndSweep(fixture);
306 const commands = yield* Ref.get(fixture.syncCommands);
307 yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands));
308 yield* sweepAgain(fixture, reactor);
309 assert.strictEqual(attempts, 2);
310 assert.deepStrictEqual((yield* Ref.get(fixture.syncCommands)).at(-1)?.stack, {
311 kind: "native",
312 ...nativeStack,
313 });
314 }).pipe(Effect.provide(fixture.layer));
315 }),
316 ),
317 );
318
319 it.effect("explicit refresh reads a changed stack even when its PR summary is unchanged", () =>
320 Effect.scoped(
321 Effect.gen(function* () {
322 yield* TestClock.setTime(Date.parse(NOW));
323 const fixture = yield* makeHarness({
324 snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(7, {})] })]),
325 });
326 yield* Effect.gen(function* () {
327 const reactor = yield* startAndSweep(fixture);
328 assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 0);
329 yield* reactor.requestSync({
330 host: "github.com",
331 repository: "owner/repository",
332 number: 7,
333 });
334 yield* Queue.take(fixture.snapshotReads);
335 yield* reactor.drain;
336 assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 1);
337 }).pipe(Effect.provide(fixture.layer));
338 }),
339 ),
340 );
341 it.effect("snapshots an unsynced link once and writes it to the thread", () =>
342 Effect.scoped(
343 Effect.gen(function* () {
344 yield* TestClock.setTime(Date.parse(NOW));
345 const fixture = yield* makeHarness({
346 snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(42)] })]),
347 summary: (input) =>
348 Effect.succeed(makeSummary(input, { title: "Ship it", isDraft: true })),
349 });
350
351 yield* Effect.gen(function* () {
352 yield* startAndSweep(fixture);
353
354 assert.deepStrictEqual(yield* Ref.get(fixture.summaryCalls), [
355 {
356 projectId: PROJECT_ID,
357 host: "github.com",
358 repository: "owner/repository",
359 number: 42,
360 },
361 ]);
362 assert.deepStrictEqual(
363 (yield* Ref.get(fixture.syncCommands)).map(({ commandId: _, ...rest }) => rest),
364 [
365 {
366 type: "thread.pull-request-link.sync",
367 threadId: ThreadId.make("one"),
368 host: "github.com",
369 repository: "owner/repository",
370 number: 42,
371 snapshot: {
372 state: "open",
373 title: "Ship it",
374 headBranch: "feature",
375 baseBranch: "main",
376 isDraft: true,
377 updatedAt: "2026-08-27T00:00:00.000Z",
378 syncedAt: NOW,
379 closedAt: null,
380 mergedAt: null,
381 },
382 stack: null,
383 },
384 ],
385 );
386 assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 1);
387 }).pipe(Effect.provide(fixture.layer));
388 }),
389 ),
390 );
391
392 it.effect("asks the host once for a pull request shared by two threads", () =>
393 Effect.scoped(
394 Effect.gen(function* () {
395 yield* TestClock.setTime(Date.parse(NOW));
396 const fixture = yield* makeHarness({
397 snapshot: makeSnapshot([
398 makeThread("one", { pullRequests: [makeLink(42)] }),
399 makeThread("two", {
400 pullRequests: [makeLink(42, null, { repository: "Owner/Repository" })],
401 }),
402 ]),
403 });
404
405 yield* Effect.gen(function* () {
406 yield* startAndSweep(fixture);
407
408 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
409 const commands = yield* Ref.get(fixture.syncCommands);
410 assert.deepStrictEqual(
411 commands
412 .map((command) => [command.threadId, command.repository] as const)
413 .sort((left, right) => left[0].localeCompare(right[0])),
414 [
415 [ThreadId.make("one"), "owner/repository"],
416 [ThreadId.make("two"), "Owner/Repository"],
417 ],
418 );
419 }).pipe(Effect.provide(fixture.layer));
420 }),
421 ),
422 );
423
424 it.effect("dispatches nothing when the host snapshot is unchanged", () =>
425 Effect.scoped(
426 Effect.gen(function* () {
427 yield* TestClock.setTime(Date.parse(NOW));
428 const fixture = yield* makeHarness({
429 snapshot: makeSnapshot([makeThread("one", { pullRequests: [makeLink(42)] })]),
430 });
431
432 yield* Effect.gen(function* () {
433 const reactor = yield* startAndSweep(fixture);
434 const firstSweep = yield* Ref.get(fixture.syncCommands);
435 assert.strictEqual(firstSweep.length, 1);
436 yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, firstSweep));
437
438 yield* sweepAgain(fixture, reactor);
439
440 // Still open on an active thread, so the host was asked again, but nothing changed.
441 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2);
442 assert.strictEqual((yield* Ref.get(fixture.stackCalls)).length, 1);
443 assert.strictEqual((yield* Ref.get(fixture.syncCommands)).length, 1);
444 }).pipe(Effect.provide(fixture.layer));
445 }),
446 ),
447 );
448
449 it.effect("refreshes closed links through the reactor's project after reopening elsewhere", () =>
450 Effect.scoped(
451 Effect.gen(function* () {
452 yield* TestClock.setTime(Date.parse(NOW));
453 const stale = yield* Ref.make(true);
454 const fixture = yield* makeHarness({
455 snapshot: makeSnapshot([
456 makeThread("first", { pullRequests: [makeLink(42, { state: "closed" })] }),
457 makeThread("second", {
458 projectId: ProjectId.make("second-project"),
459 pullRequests: [makeLink(42, { state: "closed" })],
460 }),
461 ]),
462 invalidate: ({ reference }) =>
463 reference?.projectId === makeProject().id && reference.host === "github.com"
464 ? Ref.set(stale, false)
465 : Effect.void,
466 summary: (input) =>
467 Ref.get(stale).pipe(
468 Effect.map((cached) => makeSummary(input, { state: cached ? "closed" : "open" })),
469 ),
470 });
471 yield* Effect.gen(function* () {
472 const reactor = yield* startAndSweep(fixture);
473 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
474 yield* reactor.requestSync({
475 host: "github.com",
476 repository: "owner/repository",
477 number: 42,
478 });
479 yield* Queue.take(fixture.snapshotReads);
480 yield* reactor.drain;
481 const commands = yield* Ref.get(fixture.syncCommands);
482 yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands));
483 const snapshot = yield* Ref.get(fixture.snapshots);
484 assert.deepStrictEqual(
485 snapshot.threads.map((thread) => thread.pullRequests[0]?.snapshot?.state),
486 ["open", "open"],
487 );
488 }).pipe(Effect.provide(fixture.layer));
489 }),
490 ),
491 );
492
493 it.effect("stops asking the host once a pull request is merged", () =>
494 Effect.scoped(
495 Effect.gen(function* () {
496 yield* TestClock.setTime(Date.parse(NOW));
497 const fixture = yield* makeHarness({
498 snapshot: makeSnapshot([
499 makeThread("merged", { pullRequests: [makeLink(1, { state: "merged" })] }),
500 ]),
501 });
502
503 yield* Effect.gen(function* () {
504 const reactor = yield* startAndSweep(fixture);
505 yield* sweepAgain(fixture, reactor);
506
507 assert.deepStrictEqual(yield* Ref.get(fixture.summaryCalls), []);
508 assert.deepStrictEqual(yield* Ref.get(fixture.syncCommands), []);
509
510 yield* reactor.requestSync({
511 host: "github.com",
512 repository: "owner/repository",
513 number: 1,
514 });
515 yield* Queue.take(fixture.snapshotReads);
516 yield* reactor.drain;
517
518 assert.deepStrictEqual(
519 (yield* Ref.get(fixture.summaryCalls)).map((call) => call.number),
520 [1],
521 );
522 }).pipe(Effect.provide(fixture.layer));
523 }),
524 ),
525 );
526
527 it.effect("discovers externally reopened pull requests after fifteen minutes", () =>
528 Effect.scoped(
529 Effect.gen(function* () {
530 yield* TestClock.setTime(Date.parse(NOW));
531 const state = yield* Ref.make<"closed" | "open">("closed");
532 const fixture = yield* makeHarness({
533 snapshot: makeSnapshot([
534 makeThread("closed", { pullRequests: [makeLink(2, { state: "closed" })] }),
535 ]),
536 summary: (input) =>
537 Ref.get(state).pipe(Effect.map((state) => makeSummary(input, { state }))),
538 });
539 yield* Effect.gen(function* () {
540 const reactor = yield* startAndSweep(fixture);
541 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
542 yield* Ref.set(state, "open");
543 for (let index = 0; index < 14; index += 1) yield* sweepAgain(fixture, reactor);
544 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
545 yield* sweepAgain(fixture, reactor);
546 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2);
547 assert.strictEqual((yield* Ref.get(fixture.syncCommands)).at(-1)?.snapshot.state, "open");
548 }).pipe(Effect.provide(fixture.layer));
549 }),
550 ),
551 );
552
553 it.effect("preserves a refresh requested while an older host read is in flight", () =>
554 Effect.scoped(
555 Effect.gen(function* () {
556 yield* TestClock.setTime(Date.parse(NOW));
557 const reading = yield* Deferred.make<void>();
558 const release = yield* Deferred.make<void>();
559 let calls = 0;
560 const fixture = yield* makeHarness({
561 snapshot: makeSnapshot([]),
562 summary: (input) =>
563 Effect.gen(function* () {
564 calls += 1;
565 if (calls === 1) {
566 yield* Deferred.succeed(reading, undefined);
567 yield* Deferred.await(release);
568 }
569 return makeSummary(input, { state: calls === 1 ? "closed" : "open" });
570 }),
571 });
572 yield* Effect.gen(function* () {
573 const reactor = yield* startAndSweep(fixture);
574 yield* Ref.set(
575 fixture.snapshots,
576 makeSnapshot([
577 makeThread("closed", { pullRequests: [makeLink(2, { state: "closed" })] }),
578 ]),
579 );
580 const key = { host: "github.com", repository: "owner/repository", number: 2 };
581 yield* reactor.requestSync(key);
582 yield* Deferred.await(reading);
583 yield* reactor.requestSync(key);
584 yield* Deferred.succeed(release, undefined);
585 yield* reactor.drain;
586 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2);
587 assert.strictEqual((yield* Ref.get(fixture.syncCommands)).at(-1)?.snapshot.state, "open");
588 }).pipe(Effect.provide(fixture.layer));
589 }),
590 ),
591 );
592
593 it.effect("polls open pull requests on settled threads every fifteen minutes", () =>
594 Effect.scoped(
595 Effect.gen(function* () {
596 yield* TestClock.setTime(Date.parse(NOW));
597 const fixture = yield* makeHarness({
598 snapshot: makeSnapshot([
599 makeThread("settled", {
600 settledOverride: "settled",
601 settledAt: "2026-08-21T00:00:00.000Z",
602 pullRequests: [makeLink(5, { state: "open" })],
603 }),
604 ]),
605 });
606
607 yield* Effect.gen(function* () {
608 const reactor = yield* startAndSweep(fixture);
609 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
610
611 yield* sweepAgain(fixture, reactor);
612 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
613
614 for (let index = 0; index < 13; index += 1) yield* sweepAgain(fixture, reactor);
615 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1);
616
617 yield* sweepAgain(fixture, reactor);
618 assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2);
619 }).pipe(Effect.provide(fixture.layer));
620 }),
621 ),
622 );
623
624 it.effect("auto-links missing native stack layers and leaves dismissed ones alone", () =>
625 Effect.scoped(
626 Effect.gen(function* () {
627 yield* TestClock.setTime(Date.parse(NOW));
628 const stack: PullRequestStack = {
629 id: "stack-1",
630 number: 42,
631 url: "https://github.com/owner/repository/stack/1",
632 base: "main",
633 layers: [
634 { number: 41, headBranch: "layer-1", state: "merged" },
635 { number: 42, headBranch: "layer-2", state: "open" },
636 { number: 43, headBranch: "layer-3", state: "open" },
637 ],
638 };
639 const fixture = yield* makeHarness({
640 snapshot: makeSnapshot([
641 makeThread("one", {
642 pullRequests: [
643 makeLink(42),
644 makeLink(41, { state: "merged" }, { source: "stack-dismissed" }),
645 ],
646 }),
647 ]),
648 stack: () => Effect.succeed(stack),
649 });
650
651 yield* Effect.gen(function* () {
652 yield* startAndSweep(fixture);
653
654 const syncCommands = yield* Ref.get(fixture.syncCommands);
655 assert.deepStrictEqual(
656 syncCommands.map((command) => [command.number, command.stack] as const),
657 [[42, { kind: "native", ...stack }]],
658 );
659 assert.deepStrictEqual(
660 (yield* Ref.get(fixture.linkCommands)).map(({ commandId: _, ...rest }) => rest),
661 [
662 {
663 type: "thread.pull-request.link",
664 threadId: ThreadId.make("one"),
665 host: "github.com",
666 repository: "owner/repository",
667 number: 43,
668 url: "https://github.com/owner/repository/pull/43",
669 source: "stack",
670 },
671 ],
672 );
673 }).pipe(Effect.provide(fixture.layer));
674 }),
675 ),
676 );
677
678 it.effect("keeps existing snapshots and continues when the host fails", () =>
679 Effect.scoped(
680 Effect.gen(function* () {
681 yield* TestClock.setTime(Date.parse(NOW));
682 const fixture = yield* makeHarness({
683 snapshot: makeSnapshot([
684 makeThread("failing", { pullRequests: [makeLink(7, { state: "open" })] }),
685 makeThread("fine", { pullRequests: [makeLink(8)] }),
686 ]),
687 summary: (input) =>
688 input.number === 7
689 ? Effect.fail(
690 new PullRequestOperationError({ operation: "summary", detail: "host down" }),
691 )
692 : Effect.succeed(makeSummary(input)),
693 });
694
695 yield* Effect.gen(function* () {
696 yield* startAndSweep(fixture);
697
698 assert.deepStrictEqual(
699 (yield* Ref.get(fixture.syncCommands)).map((command) => command.number),
700 [8],
701 );
702 }).pipe(Effect.provide(fixture.layer));
703 }),
704 ),
705 );
706});