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

PullRequestSyncReactor.test.ts

730 lines25.5 KB

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