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

PullRequestSyncReactor.test.ts

733 lines25.7 KB

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