The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { SourceControlProviderKind } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | EnvironmentId, |
| 4 | ProjectId, |
| 5 | type PullRequestListEntry, |
| 6 | type PullRequestListInput, |
| 7 | PullRequestUnavailableError, |
| 8 | PullRequestOperationError, |
| 9 | type PullRequestListResult, |
| 10 | type PullRequestSummary, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import * as Cause from "effect/Cause"; |
| 13 | import * as Effect from "effect/Effect"; |
| 14 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 15 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 16 | |
| 17 | const listAnswers = vi.hoisted(() => new Map<string, unknown>()); |
| 18 | const registry = await vi.hoisted(async () => |
| 19 | (await import("effect/reactivity")).AtomRegistry.make(), |
| 20 | ); |
| 21 | |
| 22 | vi.mock("@effect/atom-react", async (importOriginal) => ({ |
| 23 | ...(await importOriginal<typeof import("@effect/atom-react")>()), |
| 24 | useAtomValue: (atom: Atom.Atom<unknown>) => registry.get(atom), |
| 25 | })); |
| 26 | vi.mock("../rpc/atomRegistry", () => ({ appAtomRegistry: registry })); |
| 27 | vi.mock("@t3tools/client-runtime/state/pull-requests", async (importOriginal) => { |
| 28 | const actual = |
| 29 | await importOriginal<typeof import("@t3tools/client-runtime/state/pull-requests")>(); |
| 30 | return { |
| 31 | ...actual, |
| 32 | createPullRequestEnvironmentAtoms: ( |
| 33 | ...args: Parameters<typeof actual.createPullRequestEnvironmentAtoms> |
| 34 | ) => ({ |
| 35 | ...actual.createPullRequestEnvironmentAtoms(...args), |
| 36 | list: ({ environmentId }: { readonly environmentId: string }) => |
| 37 | listAnswers.get(environmentId), |
| 38 | }), |
| 39 | }; |
| 40 | }); |
| 41 | |
| 42 | import { |
| 43 | newestPullRequestObservation, |
| 44 | observedPullRequestSummary, |
| 45 | observePullRequestChecks, |
| 46 | observePullRequestMetadata, |
| 47 | newestPullRequestSummary, |
| 48 | newestDisplayedPullRequestSummary, |
| 49 | rememberPullRequestListAnswer, |
| 50 | restorePullRequestList, |
| 51 | restoredPullRequestList, |
| 52 | threadPullRequestLinkToSummary, |
| 53 | observeFailedPullRequestList, |
| 54 | usePullRequestList, |
| 55 | } from "./pullRequests"; |
| 56 | |
| 57 | it("observes both typed list failures without changing their status or replaying cached timestamps", () => { |
| 58 | const listResult: PullRequestListResult = { |
| 59 | viewers: {}, |
| 60 | providers: [], |
| 61 | entries: [], |
| 62 | errors: [], |
| 63 | truncated: false, |
| 64 | nextCursors: {}, |
| 65 | }; |
| 66 | using clock = vi.spyOn(Date, "now").mockReturnValue(100); |
| 67 | for (const error of [ |
| 68 | new PullRequestUnavailableError({ reason: "cli-unauthenticated", listResult }), |
| 69 | new PullRequestOperationError({ operation: "list", detail: "HTTP 403", listResult }), |
| 70 | ]) { |
| 71 | clock.mockReturnValue(100); |
| 72 | const failure = AsyncResult.failure<PullRequestListResult, unknown>(Cause.fail(error)); |
| 73 | expect(observeFailedPullRequestList(failure)).toEqual({ value: listResult, observedAt: 100 }); |
| 74 | clock.mockReturnValue(200); |
| 75 | expect(observeFailedPullRequestList(failure)?.observedAt).toBe(100); |
| 76 | expect(failure._tag).toBe("Failure"); |
| 77 | expect(observeFailedPullRequestList({ ...failure, waiting: true })).toBeUndefined(); |
| 78 | } |
| 79 | expect( |
| 80 | observeFailedPullRequestList( |
| 81 | AsyncResult.failure(Cause.fail(new PullRequestUnavailableError({ reason: "cli-missing" }))), |
| 82 | ), |
| 83 | ).toBeUndefined(); |
| 84 | }); |
| 85 | |
| 86 | function summary(overrides: Partial<PullRequestSummary> = {}): PullRequestSummary { |
| 87 | return { |
| 88 | provider: SourceControlProviderKind.make("github"), |
| 89 | projectId: ProjectId.make("pull-request-cache-test"), |
| 90 | repository: "acme/widget", |
| 91 | number: 7, |
| 92 | title: "Improve widget", |
| 93 | url: "https://github.com/acme/widget/pull/7", |
| 94 | state: "open", |
| 95 | isDraft: false, |
| 96 | headBranch: "improve-widget", |
| 97 | baseBranch: "main", |
| 98 | updatedAt: "2026-09-10T00:00:00Z", |
| 99 | author: { login: "oliver", name: null, avatarUrl: null }, |
| 100 | mergeability: "mergeable", |
| 101 | checksState: "passing", |
| 102 | ...overrides, |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | const observed = (value: PullRequestSummary, observedAt: number) => ({ |
| 107 | summary: value, |
| 108 | observedAt, |
| 109 | }); |
| 110 | |
| 111 | describe("pull request summary cache", () => { |
| 112 | it("orders same-dated snapshots by the server's read time, not by arrival", () => { |
| 113 | const stale = observed(summary({ observedAt: 200 }), 500); |
| 114 | const held = observed( |
| 115 | summary({ mergeability: "conflicting", checksState: "failing", observedAt: 300 }), |
| 116 | 100, |
| 117 | ); |
| 118 | expect(newestPullRequestObservation(stale, held)?.summary).toMatchObject({ |
| 119 | mergeability: "conflicting", |
| 120 | checksState: "failing", |
| 121 | observedAt: 300, |
| 122 | }); |
| 123 | expect(newestPullRequestObservation(held, stale)).toBe(held); |
| 124 | expect(newestPullRequestObservation(held, { ...held, observedAt: 900 })).toBe(held); |
| 125 | }); |
| 126 | |
| 127 | it("uses arrival order only when neither snapshot carries a server read time", () => { |
| 128 | const first = observed(summary(), 100); |
| 129 | const later = observed(summary({ checksState: "failing" }), 200); |
| 130 | expect(newestPullRequestObservation(first, later)).toMatchObject({ observedAt: 200 }); |
| 131 | expect(newestPullRequestObservation(later, first)).toBe(later); |
| 132 | const stamped = observed(summary({ observedAt: 50 }), 1); |
| 133 | expect(newestPullRequestObservation(later, stamped)?.summary.observedAt).toBe(50); |
| 134 | expect(newestPullRequestObservation(stamped, later)).toBe(stamped); |
| 135 | }); |
| 136 | |
| 137 | it("keeps known status when a newer snapshot omits it, and clears it when told to", () => { |
| 138 | const list = observed(summary({ reviewDecision: "approved", observedAt: 100 }), 100); |
| 139 | const sparse = observed( |
| 140 | summary({ checksState: undefined, reviewDecision: undefined, observedAt: 200 }), |
| 141 | 200, |
| 142 | ); |
| 143 | expect(newestPullRequestObservation(list, sparse)?.summary).toMatchObject({ |
| 144 | checksState: "passing", |
| 145 | reviewDecision: "approved", |
| 146 | }); |
| 147 | const cleared = observed(summary({ checksState: null, observedAt: 300 }), 300); |
| 148 | expect(newestPullRequestObservation(list, cleared)?.summary.checksState).toBeNull(); |
| 149 | }); |
| 150 | |
| 151 | it("treats merged as final and otherwise prefers the later host update", () => { |
| 152 | const merged = summary({ state: "merged", updatedAt: "2026-09-01T00:00:00Z" }); |
| 153 | const reopened = summary({ updatedAt: "2026-09-12T00:00:00Z", observedAt: 900 }); |
| 154 | expect(newestPullRequestSummary(merged, reopened)).toBe(merged); |
| 155 | expect(newestPullRequestSummary(reopened, merged)).toBe(merged); |
| 156 | const older = summary({ updatedAt: "2026-09-11T00:00:00Z", observedAt: 999 }); |
| 157 | expect(newestPullRequestSummary(older, reopened)).toBe(reopened); |
| 158 | expect(newestPullRequestSummary(reopened, older)).toBe(reopened); |
| 159 | const edited = summary({ title: "Accepted title" }); |
| 160 | expect(newestPullRequestSummary(summary(), edited)).toBe(edited); |
| 161 | const loaded = summary(); |
| 162 | expect(newestDisplayedPullRequestSummary(loaded, false, edited, loaded, [])).toBe(loaded); |
| 163 | expect(newestDisplayedPullRequestSummary(summary(), true, edited, loaded, [])).toBe(edited); |
| 164 | const titleEdit = { reference: loaded, title: "Pending title" }; |
| 165 | expect(newestDisplayedPullRequestSummary(loaded, true, null, loaded, [titleEdit])?.title).toBe( |
| 166 | "Pending title", |
| 167 | ); |
| 168 | }); |
| 169 | |
| 170 | it("reuses saved thread state while detail is stale and lets newer detail replace it", () => { |
| 171 | const stale = summary(); |
| 172 | const link = { |
| 173 | host: "github.com", |
| 174 | repository: stale.repository, |
| 175 | number: stale.number, |
| 176 | url: stale.url, |
| 177 | source: "manual" as const, |
| 178 | linkedAt: stale.updatedAt, |
| 179 | stack: null, |
| 180 | snapshot: { |
| 181 | ...stale, |
| 182 | state: "closed" as const, |
| 183 | isDraft: false, |
| 184 | updatedAt: "2026-09-11T00:00:00Z", |
| 185 | syncedAt: "2026-09-12T00:00:00Z", |
| 186 | }, |
| 187 | }; |
| 188 | const saved = threadPullRequestLinkToSummary(link, stale.projectId); |
| 189 | expect(newestPullRequestSummary(stale, saved)?.state).toBe("closed"); |
| 190 | const fresh = summary({ isDraft: true, updatedAt: "2026-09-13T00:00:00Z" }); |
| 191 | expect(newestPullRequestSummary(fresh, saved)).toBe(fresh); |
| 192 | expect(threadPullRequestLinkToSummary({ ...link, snapshot: null }, stale.projectId)).toBeNull(); |
| 193 | expect( |
| 194 | threadPullRequestLinkToSummary( |
| 195 | { ...link, snapshot: { ...link.snapshot, updatedAt: null } }, |
| 196 | stale.projectId, |
| 197 | ), |
| 198 | ).toBeNull(); |
| 199 | }); |
| 200 | }); |
| 201 | |
| 202 | describe("pull request observations", () => { |
| 203 | const fromSummary = (value: PullRequestSummary, observedAt: number) => |
| 204 | observePullRequestMetadata({ metadata: null, checks: null }, observed(value, observedAt)); |
| 205 | const checksRead = ( |
| 206 | checksState: "passing" | "failing" | "pending" | null, |
| 207 | readAt: number | undefined, |
| 208 | observedAt: number, |
| 209 | ) => ({ checksState, readAt, observedAt }); |
| 210 | |
| 211 | it("keeps a newer list's mergeability when a checks read lands after it", () => { |
| 212 | const detail = summary({ |
| 213 | mergeability: "conflicting", |
| 214 | checksState: "pending", |
| 215 | observedAt: 100, |
| 216 | }); |
| 217 | let state = fromSummary(detail, 1); |
| 218 | state = observePullRequestMetadata( |
| 219 | state, |
| 220 | observed(summary({ mergeability: "mergeable", checksState: "pending", observedAt: 200 }), 2), |
| 221 | ); |
| 222 | state = observePullRequestChecks(state, checksRead("passing", 300, 3)); |
| 223 | expect(observedPullRequestSummary(state)).toMatchObject({ |
| 224 | mergeability: "mergeable", |
| 225 | checksState: "passing", |
| 226 | }); |
| 227 | }); |
| 228 | |
| 229 | it("keeps a newer checks read when an older list read lands after it", () => { |
| 230 | let state = fromSummary( |
| 231 | summary({ mergeability: "conflicting", checksState: "pending", observedAt: 100 }), |
| 232 | 1, |
| 233 | ); |
| 234 | state = observePullRequestChecks(state, checksRead("passing", 300, 2)); |
| 235 | state = observePullRequestMetadata( |
| 236 | state, |
| 237 | observed(summary({ mergeability: "mergeable", checksState: "pending", observedAt: 250 }), 3), |
| 238 | ); |
| 239 | expect(observedPullRequestSummary(state)).toMatchObject({ |
| 240 | mergeability: "mergeable", |
| 241 | checksState: "passing", |
| 242 | }); |
| 243 | }); |
| 244 | |
| 245 | it("lets a newer read's checks replace an older checks read", () => { |
| 246 | let state = fromSummary(summary({ checksState: "pending", observedAt: 100 }), 1); |
| 247 | state = observePullRequestChecks(state, checksRead("passing", 300, 2)); |
| 248 | state = observePullRequestMetadata( |
| 249 | state, |
| 250 | observed(summary({ checksState: "failing", observedAt: 400 }), 3), |
| 251 | ); |
| 252 | expect(observedPullRequestSummary(state)?.checksState).toBe("failing"); |
| 253 | }); |
| 254 | |
| 255 | it("orders checks by arrival when the server reports no read time", () => { |
| 256 | let state = fromSummary(summary({ checksState: "pending" }), 1); |
| 257 | state = observePullRequestChecks(state, checksRead("passing", undefined, 2)); |
| 258 | expect(observedPullRequestSummary(state)?.checksState).toBe("passing"); |
| 259 | state = observePullRequestChecks(state, checksRead("failing", undefined, 1)); |
| 260 | expect(observedPullRequestSummary(state)?.checksState).toBe("passing"); |
| 261 | }); |
| 262 | |
| 263 | it("shows nothing until some read has described the pull request", () => { |
| 264 | const state = observePullRequestChecks( |
| 265 | { metadata: null, checks: null }, |
| 266 | checksRead("failing", 1, 1), |
| 267 | ); |
| 268 | expect(observedPullRequestSummary(state)).toBeNull(); |
| 269 | }); |
| 270 | }); |
| 271 | |
| 272 | describe("pull request list answers across a reload", () => { |
| 273 | const makeStorage = () => { |
| 274 | const held = new Map<string, string>(); |
| 275 | return { |
| 276 | held, |
| 277 | getItem: (key: string) => held.get(key) ?? null, |
| 278 | setItem: (key: string, value: string) => void held.set(key, value), |
| 279 | removeItem: (key: string) => void held.delete(key), |
| 280 | key: (index: number) => [...held.keys()][index] ?? null, |
| 281 | get length() { |
| 282 | return held.size; |
| 283 | }, |
| 284 | }; |
| 285 | }; |
| 286 | const listEntry = (number: number, updatedAt = "2026-07-02T00:00:00Z") => |
| 287 | ({ |
| 288 | provider: SourceControlProviderKind.make("github"), |
| 289 | host: "github.com", |
| 290 | projectId: ProjectId.make("project-1"), |
| 291 | projectTitle: "t3code", |
| 292 | repository: "pingdotgg/t3code", |
| 293 | number, |
| 294 | title: `Pull request ${number}`, |
| 295 | url: `https://github.com/pingdotgg/t3code/pull/${number}`, |
| 296 | author: { login: "octocat", name: null, avatarUrl: null }, |
| 297 | headBranch: `feat/branch-${number}`, |
| 298 | baseBranch: "main", |
| 299 | state: "open", |
| 300 | isDraft: false, |
| 301 | mergeability: "mergeable", |
| 302 | additions: 1, |
| 303 | deletions: 0, |
| 304 | createdAt: "2026-07-01T00:00:00Z", |
| 305 | updatedAt, |
| 306 | viewerReviewRequested: false, |
| 307 | labels: [], |
| 308 | }) as PullRequestListEntry; |
| 309 | const answer = (entries: ReadonlyArray<PullRequestListEntry>, host = "github.com") => |
| 310 | ({ |
| 311 | entries, |
| 312 | viewers: { [host]: "Bilal" }, |
| 313 | providers: [ |
| 314 | { |
| 315 | host, |
| 316 | kind: SourceControlProviderKind.make("github"), |
| 317 | searchesOnHost: true, |
| 318 | projectCount: 1, |
| 319 | configured: true, |
| 320 | detail: null, |
| 321 | }, |
| 322 | ], |
| 323 | errors: [{ projectId: ProjectId.make("project-1"), projectTitle: "t3code", message: "boom" }], |
| 324 | truncated: true, |
| 325 | nextCursors: { "pingdotgg/t3code": "cursor-1" }, |
| 326 | }) as PullRequestListResult; |
| 327 | const local = EnvironmentId.make("env-local"); |
| 328 | const remote = EnvironmentId.make("env-remote"); |
| 329 | const input: PullRequestListInput = { state: "open", involvement: "all", limit: 99 }; |
| 330 | |
| 331 | it("restores every environment's first page as one merged list without stale failures or cursors", () => { |
| 332 | const storage = makeStorage(); |
| 333 | rememberPullRequestListAnswer( |
| 334 | storage, |
| 335 | { environmentId: local, input }, |
| 336 | answer([listEntry(1, "2026-07-01T00:00:00Z")]), |
| 337 | ); |
| 338 | rememberPullRequestListAnswer( |
| 339 | storage, |
| 340 | { environmentId: remote, input }, |
| 341 | answer([listEntry(2, "2026-07-03T00:00:00Z")], "gitlab.com"), |
| 342 | ); |
| 343 | const restored = restorePullRequestList(storage, [ |
| 344 | { environmentId: local, input }, |
| 345 | { environmentId: remote, input }, |
| 346 | ]); |
| 347 | expect(restored?.entries.map((row) => [row.environmentId, row.number])).toEqual([ |
| 348 | [remote, 2], |
| 349 | [local, 1], |
| 350 | ]); |
| 351 | expect(restored?.viewers).toEqual({ |
| 352 | "env-local github.com": "Bilal", |
| 353 | "env-remote gitlab.com": "Bilal", |
| 354 | }); |
| 355 | expect(restored?.providers.map((provider) => provider.host)).toEqual([ |
| 356 | "github.com", |
| 357 | "gitlab.com", |
| 358 | ]); |
| 359 | expect(restored?.errors).toEqual([]); |
| 360 | expect(restored?.nextCursors).toEqual({}); |
| 361 | }); |
| 362 | |
| 363 | it("falls back to a fresh read when the browser blocks storage", () => { |
| 364 | vi.stubGlobal("window", { |
| 365 | get localStorage(): Storage { |
| 366 | throw new DOMException("The operation is insecure.", "SecurityError"); |
| 367 | }, |
| 368 | }); |
| 369 | try { |
| 370 | expect(restoredPullRequestList([{ environmentId: local, input }])).toBeNull(); |
| 371 | } finally { |
| 372 | vi.unstubAllGlobals(); |
| 373 | } |
| 374 | }); |
| 375 | |
| 376 | it("keeps a page of rows and only first-page reads", () => { |
| 377 | const storage = makeStorage(); |
| 378 | const rows = Array.from({ length: 120 }, (_, index) => listEntry(index + 1)); |
| 379 | rememberPullRequestListAnswer(storage, { environmentId: local, input }, answer(rows)); |
| 380 | const searched = { ...input, query: "fix" }; |
| 381 | rememberPullRequestListAnswer(storage, { environmentId: local, input: searched }, answer(rows)); |
| 382 | const continued = { ...input, cursors: { "pingdotgg/t3code": "cursor-1" } }; |
| 383 | rememberPullRequestListAnswer( |
| 384 | storage, |
| 385 | { environmentId: local, input: continued }, |
| 386 | answer(rows), |
| 387 | ); |
| 388 | expect( |
| 389 | restorePullRequestList(storage, [{ environmentId: local, input }])?.entries, |
| 390 | ).toHaveLength(99); |
| 391 | expect(restorePullRequestList(storage, [{ environmentId: local, input: searched }])).toBeNull(); |
| 392 | expect( |
| 393 | restorePullRequestList(storage, [{ environmentId: local, input: continued }]), |
| 394 | ).toBeNull(); |
| 395 | }); |
| 396 | |
| 397 | it("restores answers kept under a project assignment for a cold start that has none yet", () => { |
| 398 | const storage = makeStorage(); |
| 399 | rememberPullRequestListAnswer( |
| 400 | storage, |
| 401 | { |
| 402 | environmentId: local, |
| 403 | input: { ...input, projectIds: [ProjectId.make("project-1")] }, |
| 404 | }, |
| 405 | answer([listEntry(1)]), |
| 406 | ); |
| 407 | rememberPullRequestListAnswer( |
| 408 | storage, |
| 409 | { |
| 410 | environmentId: remote, |
| 411 | input: { ...input, projectIds: [ProjectId.make("project-2")] }, |
| 412 | }, |
| 413 | answer([listEntry(2, "2026-07-03T00:00:00Z")]), |
| 414 | ); |
| 415 | const restored = restorePullRequestList(storage, [ |
| 416 | { environmentId: local, input }, |
| 417 | { environmentId: remote, input }, |
| 418 | ]); |
| 419 | expect(restored?.entries.map((row) => [row.environmentId, row.number])).toEqual([ |
| 420 | [remote, 2], |
| 421 | [local, 1], |
| 422 | ]); |
| 423 | }); |
| 424 | |
| 425 | it("shows a pull request once when answers from before a reassignment overlap", () => { |
| 426 | const storage = makeStorage(); |
| 427 | rememberPullRequestListAnswer( |
| 428 | storage, |
| 429 | { environmentId: local, input }, |
| 430 | answer([listEntry(1, "2026-07-01T00:00:00Z")]), |
| 431 | ); |
| 432 | rememberPullRequestListAnswer( |
| 433 | storage, |
| 434 | { environmentId: remote, input }, |
| 435 | answer([listEntry(1, "2026-07-03T00:00:00Z")]), |
| 436 | ); |
| 437 | const restored = restorePullRequestList(storage, [ |
| 438 | { environmentId: local, input }, |
| 439 | { environmentId: remote, input }, |
| 440 | ]); |
| 441 | expect(restored?.entries.map((row) => [row.environmentId, row.updatedAt])).toEqual([ |
| 442 | [local, "2026-07-01T00:00:00Z"], |
| 443 | ]); |
| 444 | }); |
| 445 | |
| 446 | it("keeps a shared repository's rows when it moves to an environment that has not answered yet", () => { |
| 447 | const storage = makeStorage(); |
| 448 | const own = ProjectId.make("project-own"); |
| 449 | const shared = { ...listEntry(2), projectId: ProjectId.make("project-shared") }; |
| 450 | rememberPullRequestListAnswer( |
| 451 | storage, |
| 452 | { environmentId: local, input }, |
| 453 | answer([{ ...listEntry(1), projectId: own }, shared]), |
| 454 | ); |
| 455 | rememberPullRequestListAnswer( |
| 456 | storage, |
| 457 | { environmentId: remote, input }, |
| 458 | answer([listEntry(3, "2026-07-03T00:00:00Z")]), |
| 459 | ); |
| 460 | // The shared repository now belongs to the remote environment, which has not answered yet. |
| 461 | rememberPullRequestListAnswer( |
| 462 | storage, |
| 463 | { environmentId: local, input: { ...input, projectIds: [own] } }, |
| 464 | answer([{ ...listEntry(1), projectId: own }]), |
| 465 | ); |
| 466 | const restored = restorePullRequestList(storage, [ |
| 467 | { environmentId: local, input }, |
| 468 | { environmentId: remote, input }, |
| 469 | ]); |
| 470 | expect(restored?.entries.map((row) => row.number).toSorted()).toEqual([1, 2, 3]); |
| 471 | }); |
| 472 | |
| 473 | it("keeps one page per environment when retained rows join a partial answer", () => { |
| 474 | const storage = makeStorage(); |
| 475 | const own = ProjectId.make("project-own"); |
| 476 | const shared = ProjectId.make("project-shared"); |
| 477 | const rows = (projectId: ProjectId, first: number) => |
| 478 | Array.from({ length: 99 }, (_, index) => ({ ...listEntry(first + index), projectId })); |
| 479 | rememberPullRequestListAnswer( |
| 480 | storage, |
| 481 | { environmentId: local, input }, |
| 482 | answer(rows(shared, 1)), |
| 483 | ); |
| 484 | rememberPullRequestListAnswer( |
| 485 | storage, |
| 486 | { environmentId: local, input: { ...input, projectIds: [own] } }, |
| 487 | answer(rows(own, 101)), |
| 488 | ); |
| 489 | const restored = restorePullRequestList(storage, [{ environmentId: local, input }]); |
| 490 | expect(restored?.entries).toHaveLength(99); |
| 491 | expect(restored?.entries.every((row) => row.projectId === own)).toBe(true); |
| 492 | }); |
| 493 | |
| 494 | it("does not keep grown pages", () => { |
| 495 | const storage = makeStorage(); |
| 496 | const grown = { ...input, limit: 198 }; |
| 497 | rememberPullRequestListAnswer(storage, { environmentId: local, input: grown }, answer([])); |
| 498 | expect(restorePullRequestList(storage, [{ environmentId: local, input: grown }])).toBeNull(); |
| 499 | }); |
| 500 | |
| 501 | it("forgets whole scopes, never one environment of a scope that is kept", () => { |
| 502 | const storage = makeStorage(); |
| 503 | const scoped = (projectIndex: number) => ({ |
| 504 | ...input, |
| 505 | projectId: ProjectId.make(`p-${projectIndex}`), |
| 506 | }); |
| 507 | for (let projectIndex = 0; projectIndex < 9; projectIndex++) { |
| 508 | for (const [offset, environmentId] of [local, remote].entries()) { |
| 509 | rememberPullRequestListAnswer( |
| 510 | storage, |
| 511 | { environmentId, input: scoped(projectIndex) }, |
| 512 | answer([listEntry(projectIndex * 2 + offset + 1, `2026-07-0${2 - offset}T00:00:00Z`)]), |
| 513 | ); |
| 514 | } |
| 515 | } |
| 516 | const restore = (projectIndex: number) => |
| 517 | restorePullRequestList(storage, [ |
| 518 | { environmentId: local, input: scoped(projectIndex) }, |
| 519 | { environmentId: remote, input: scoped(projectIndex) }, |
| 520 | ]); |
| 521 | expect(restore(0)).toBeNull(); |
| 522 | for (let projectIndex = 1; projectIndex < 9; projectIndex++) { |
| 523 | expect(restore(projectIndex)?.entries.map((row) => row.environmentId)).toEqual([ |
| 524 | local, |
| 525 | remote, |
| 526 | ]); |
| 527 | } |
| 528 | }); |
| 529 | |
| 530 | it("names the environment whose list failed while the others' rows still show", () => { |
| 531 | listAnswers.set(local, Atom.make(AsyncResult.success(answer([listEntry(1)])))); |
| 532 | listAnswers.set( |
| 533 | remote, |
| 534 | Atom.make( |
| 535 | AsyncResult.failure( |
| 536 | Cause.fail( |
| 537 | new Error("Relay could not reach the environment endpoint (endpoint_request_failed)."), |
| 538 | ), |
| 539 | ), |
| 540 | ), |
| 541 | ); |
| 542 | |
| 543 | const view = usePullRequestList([ |
| 544 | { environmentId: local, input }, |
| 545 | { environmentId: remote, input }, |
| 546 | ]); |
| 547 | |
| 548 | expect(view.data?.entries.map((row) => [row.environmentId, row.number])).toEqual([[local, 1]]); |
| 549 | expect(view.error).toEqual({ |
| 550 | environmentId: remote, |
| 551 | message: "Relay could not reach the environment endpoint (endpoint_request_failed).", |
| 552 | }); |
| 553 | }); |
| 554 | |
| 555 | it("retries only the environment whose list failed, leaving one still loading to finish", () => { |
| 556 | const requests: string[] = []; |
| 557 | const read = (environmentId: string, answer: Effect.Effect<never, string>) => |
| 558 | Atom.make( |
| 559 | Effect.suspend(() => { |
| 560 | requests.push(environmentId); |
| 561 | return answer; |
| 562 | }), |
| 563 | ); |
| 564 | const slow = EnvironmentId.make("env-slow"); |
| 565 | const failing = EnvironmentId.make("env-failing"); |
| 566 | const reads = [read(slow, Effect.never), read(failing, Effect.fail("Unreachable"))]; |
| 567 | listAnswers.set(slow, reads[0]); |
| 568 | listAnswers.set(failing, reads[1]); |
| 569 | const unmounts = reads.map((atom) => registry.mount(atom)); |
| 570 | try { |
| 571 | const view = usePullRequestList([ |
| 572 | { environmentId: slow, input }, |
| 573 | { environmentId: failing, input }, |
| 574 | ]); |
| 575 | expect(view).toMatchObject({ isPending: true, error: { environmentId: failing } }); |
| 576 | |
| 577 | view.retry(); |
| 578 | |
| 579 | expect(requests).toEqual([slow, failing, failing]); |
| 580 | } finally { |
| 581 | for (const unmount of unmounts) unmount(); |
| 582 | } |
| 583 | }); |
| 584 | |
| 585 | it("removes the composite snapshots it replaced", () => { |
| 586 | const storage = makeStorage(); |
| 587 | storage.held.set("t3.pullRequests.list:env-local,env-remote", "{}"); |
| 588 | storage.held.set("t3.pullRequests.list:env-local", "{}"); |
| 589 | storage.held.set("t3.other", "kept"); |
| 590 | rememberPullRequestListAnswer(storage, { environmentId: local, input }, answer([])); |
| 591 | expect( |
| 592 | [...storage.held.keys()].filter((key) => key.startsWith("t3.pullRequests.list:")), |
| 593 | ).toEqual([]); |
| 594 | expect(storage.held.get("t3.other")).toBe("kept"); |
| 595 | }); |
| 596 | }); |