@@ -38,7 +38,7 @@ import * as ServerActivation from "../serverActivation.ts"; |
38 | 38 | import * as Orchestrator from "./Orchestrator.ts"; |
39 | 39 | import * as ProjectionStore from "./ProjectionStore.ts"; |
40 | 40 | import * as PullRequestSyncReactor from "./PullRequestSyncReactor.ts"; |
41 | | -import type { PullRequestTestThread } from "./testkit/pullRequestFixtures.ts"; |
| 41 | +import { type PullRequestTestThread, v2PullRequestThread } from "./testkit/pullRequestFixtures.ts"; |
42 | 42 |
|
43 | 43 | const NOW = "2026-08-28T12:00:00.000Z"; |
44 | 44 | const PROJECT_ID = ProjectId.make("sync-project"); |
@@ -162,6 +162,17 @@ function makeSummary( |
162 | 162 | }; |
163 | 163 | } |
164 | 164 |
|
| 165 | +/** How a host answers for a pull request it cannot find, such as one in a deleted repository. */ |
| 166 | +function notFound(input: PullRequestRef) { |
| 167 | + return Effect.fail( |
| 168 | + new PullRequestOperationError({ |
| 169 | + operation: "summary", |
| 170 | + detail: `GitHub could not find ${input.repository}#${input.number}.`, |
| 171 | + reason: "not-found", |
| 172 | + }), |
| 173 | + ); |
| 174 | +} |
| 175 | + |
165 | 176 | interface HarnessOptions { |
166 | 177 | readonly invalidate?: PullRequestService.PullRequestService["Service"]["invalidate"]; |
167 | 178 | readonly snapshot: TestShellSnapshot; |
@@ -364,6 +375,16 @@ function commandRan(threadId: ThreadId, input: string): OrchestrationV2DomainEve |
364 | 375 | }; |
365 | 376 | } |
366 | 377 |
|
| 378 | +function pullRequestsSynced(thread: PullRequestTestThread): OrchestrationV2DomainEvent { |
| 379 | + return { |
| 380 | + type: "thread.pull-request-synced", |
| 381 | + id: EventId.make(`event:${thread.id}:pull-requests`), |
| 382 | + threadId: thread.id, |
| 383 | + occurredAt: DateTime.makeUnsafe(NOW), |
| 384 | + payload: { ...v2PullRequestThread(thread), lastVisitedAt: null }, |
| 385 | + }; |
| 386 | +} |
| 387 | + |
367 | 388 | /** What the reactor would have persisted, so the next sweep sees its own writes. */ |
368 | 389 | function applySync( |
369 | 390 | snapshot: TestShellSnapshot, |
@@ -921,7 +942,11 @@ describe("PullRequestSyncReactor", () => { |
921 | 942 | makeThread("settled", { |
922 | 943 | settledOverride: "settled", |
923 | 944 | settledAt: "2026-08-21T00:00:00.000Z", |
924 | | - pullRequests: [makeLink(5, { state: "open" }), makeLink(6, { state: "closed" })], |
| 945 | + pullRequests: [ |
| 946 | + makeLink(5, { state: "open" }), |
| 947 | + makeLink(6, { state: "closed" }), |
| 948 | + makeLink(7), |
| 949 | + ], |
925 | 950 | }), |
926 | 951 | ]), |
927 | 952 | }); |
@@ -942,7 +967,7 @@ describe("PullRequestSyncReactor", () => { |
942 | 967 | yield* sweepAgain(fixture, reactor); |
943 | 968 | assert.deepStrictEqual( |
944 | 969 | (yield* Ref.get(fixture.summaryCalls)).map((call) => call.number).toSorted(), |
945 | | - [5, 6], |
| 970 | + [5, 6, 7], |
946 | 971 | ); |
947 | 972 | }).pipe(Effect.provide(fixture.layer)); |
948 | 973 | }), |
@@ -1029,12 +1054,163 @@ describe("PullRequestSyncReactor", () => { |
1029 | 1054 | }); |
1030 | 1055 |
|
1031 | 1056 | yield* Effect.gen(function* () { |
1032 | | - yield* startAndSweep(fixture); |
| 1057 | + const reactor = yield* startAndSweep(fixture); |
1033 | 1058 |
|
1034 | 1059 | assert.deepStrictEqual( |
1035 | 1060 | (yield* Ref.get(fixture.syncCommands)).map((command) => command.number), |
1036 | 1061 | [8], |
1037 | 1062 | ); |
| 1063 | + yield* sweepAgain(fixture, reactor); |
| 1064 | + assert.strictEqual( |
| 1065 | + (yield* Ref.get(fixture.summaryCalls)).filter((call) => call.number === 7).length, |
| 1066 | + 2, |
| 1067 | + ); |
| 1068 | + }).pipe(Effect.provide(fixture.layer)); |
| 1069 | + }), |
| 1070 | + ), |
| 1071 | + ); |
| 1072 | + |
| 1073 | + it.effect( |
| 1074 | + "asks every fifteen minutes for pull requests the host cannot find, until it finds them", |
| 1075 | + () => |
| 1076 | + Effect.scoped( |
| 1077 | + Effect.gen(function* () { |
| 1078 | + yield* TestClock.setTime(Date.parse(NOW)); |
| 1079 | + const found = yield* Ref.make(false); |
| 1080 | + const fixture = yield* makeHarness({ |
| 1081 | + snapshot: makeSnapshot([ |
| 1082 | + makeThread("renamed", { |
| 1083 | + pullRequests: [makeLink(9), makeLink(10, { state: "open" })], |
| 1084 | + }), |
| 1085 | + ]), |
| 1086 | + summary: (input) => |
| 1087 | + Ref.get(found).pipe( |
| 1088 | + Effect.flatMap((isFound) => |
| 1089 | + isFound ? Effect.succeed(makeSummary(input)) : notFound(input), |
| 1090 | + ), |
| 1091 | + ), |
| 1092 | + }); |
| 1093 | + |
| 1094 | + yield* Effect.gen(function* () { |
| 1095 | + const reactor = yield* startAndSweep(fixture); |
| 1096 | + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2); |
| 1097 | + for (let index = 0; index < 14; index += 1) yield* sweepAgain(fixture, reactor); |
| 1098 | + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2); |
| 1099 | + |
| 1100 | + // The repository is back, so its open pull requests return to the open cadence. |
| 1101 | + yield* Ref.set(found, true); |
| 1102 | + yield* sweepAgain(fixture, reactor); |
| 1103 | + assert.deepStrictEqual( |
| 1104 | + (yield* Ref.get(fixture.summaryCalls)) |
| 1105 | + .map((call) => call.number) |
| 1106 | + .toSorted((left, right) => left - right), |
| 1107 | + [9, 9, 10, 10], |
| 1108 | + ); |
| 1109 | + const commands = yield* Ref.get(fixture.syncCommands); |
| 1110 | + yield* Ref.update(fixture.snapshots, (snapshot) => applySync(snapshot, commands)); |
| 1111 | + // Fork: the thread is idle, so its open pull requests wait out the ten minute pace. |
| 1112 | + yield* sweepAgain(fixture, reactor); |
| 1113 | + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 4); |
| 1114 | + }).pipe(Effect.provide(fixture.layer)); |
| 1115 | + }), |
| 1116 | + ), |
| 1117 | + ); |
| 1118 | + |
| 1119 | + it.effect("keeps the slow cadence for a missing pull request whose stack read failed", () => |
| 1120 | + Effect.scoped( |
| 1121 | + Effect.gen(function* () { |
| 1122 | + yield* TestClock.setTime(Date.parse(NOW)); |
| 1123 | + const found = yield* Ref.make(true); |
| 1124 | + const fixture = yield* makeHarness({ |
| 1125 | + snapshot: makeSnapshot([makeThread("renamed", { pullRequests: [makeLink(4)] })]), |
| 1126 | + summary: (input) => |
| 1127 | + Ref.get(found).pipe( |
| 1128 | + Effect.flatMap((isFound) => |
| 1129 | + isFound ? Effect.succeed(makeSummary(input)) : notFound(input), |
| 1130 | + ), |
| 1131 | + ), |
| 1132 | + stack: () => |
| 1133 | + Effect.fail(new PullRequestOperationError({ operation: "stack", detail: "host down" })), |
| 1134 | + }); |
| 1135 | + |
| 1136 | + yield* Effect.gen(function* () { |
| 1137 | + const reactor = yield* startAndSweep(fixture); |
| 1138 | + // The next sweep retries the failed stack read, and finds the repository gone. |
| 1139 | + yield* Ref.set(found, false); |
| 1140 | + for (let index = 0; index < 15; index += 1) yield* sweepAgain(fixture, reactor); |
| 1141 | + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 2); |
| 1142 | + }).pipe(Effect.provide(fixture.layer)); |
| 1143 | + }), |
| 1144 | + ), |
| 1145 | + ); |
| 1146 | + |
| 1147 | + it.effect("stops asking for a requested pull request once the host cannot find it", () => |
| 1148 | + Effect.scoped( |
| 1149 | + Effect.gen(function* () { |
| 1150 | + yield* TestClock.setTime(Date.parse(NOW)); |
| 1151 | + const fixture = yield* makeHarness({ |
| 1152 | + snapshot: makeSnapshot([ |
| 1153 | + makeThread("settled", { |
| 1154 | + settledOverride: "settled", |
| 1155 | + settledAt: "2026-08-21T00:00:00.000Z", |
| 1156 | + pullRequests: [makeLink(5, { state: "open" })], |
| 1157 | + }), |
| 1158 | + ]), |
| 1159 | + summary: notFound, |
| 1160 | + }); |
| 1161 | + |
| 1162 | + yield* Effect.gen(function* () { |
| 1163 | + const reactor = yield* startAndSweep(fixture); |
| 1164 | + yield* Queue.offer(fixture.stateChanges, { |
| 1165 | + host: "github.com", |
| 1166 | + repository: "owner/repository", |
| 1167 | + number: 5, |
| 1168 | + }); |
| 1169 | + yield* Queue.take(fixture.snapshotReads); |
| 1170 | + yield* reactor.drain; |
| 1171 | + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1); |
| 1172 | + |
| 1173 | + for (let index = 0; index < 15; index += 1) yield* sweepAgain(fixture, reactor); |
| 1174 | + assert.strictEqual((yield* Ref.get(fixture.summaryCalls)).length, 1); |
| 1175 | + }).pipe(Effect.provide(fixture.layer)); |
| 1176 | + }), |
| 1177 | + ), |
| 1178 | + ); |
| 1179 | + |
| 1180 | + it.effect("reads a missing pull request again for a reader, not for another link's sync", () => |
| 1181 | + Effect.scoped( |
| 1182 | + Effect.gen(function* () { |
| 1183 | + yield* TestClock.setTime(Date.parse(NOW)); |
| 1184 | + const fixture = yield* makeHarness({ |
| 1185 | + snapshot: makeSnapshot([makeThread("renamed", { pullRequests: [makeLink(9)] })]), |
| 1186 | + summary: (input) => |
| 1187 | + input.number === 9 ? notFound(input) : Effect.succeed(makeSummary(input)), |
| 1188 | + }); |
| 1189 | + |
| 1190 | + yield* Effect.gen(function* () { |
| 1191 | + const reactor = yield* startAndSweep(fixture); |
| 1192 | + // Linking another pull request syncs the thread's links, the unreadable one included. |
| 1193 | + const linked = makeThread("renamed", { pullRequests: [makeLink(9), makeLink(8)] }); |
| 1194 | + yield* Ref.set(fixture.snapshots, makeSnapshot([linked], 2)); |
| 1195 | + yield* Queue.offer(fixture.domainEvents, pullRequestsSynced(linked)); |
| 1196 | + yield* Queue.take(fixture.snapshotReads); |
| 1197 | + yield* reactor.drain; |
| 1198 | + assert.deepStrictEqual( |
| 1199 | + (yield* Ref.get(fixture.summaryCalls)).map((call) => call.number), |
| 1200 | + [9, 8], |
| 1201 | + ); |
| 1202 | + |
| 1203 | + yield* Queue.offer(fixture.stateChanges, { |
| 1204 | + host: "github.com", |
| 1205 | + repository: "owner/repository", |
| 1206 | + number: 9, |
| 1207 | + }); |
| 1208 | + yield* Queue.take(fixture.snapshotReads); |
| 1209 | + yield* reactor.drain; |
| 1210 | + assert.deepStrictEqual( |
| 1211 | + (yield* Ref.get(fixture.summaryCalls)).map((call) => call.number), |
| 1212 | + [9, 8, 9], |
| 1213 | + ); |
1038 | 1214 | }).pipe(Effect.provide(fixture.layer)); |
1039 | 1215 | }), |
1040 | 1216 | ), |
|