The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | EnvironmentId, |
| 3 | ORCHESTRATION_V2_WS_METHODS, |
| 4 | OrchestrationV2GetShellSnapshotError, |
| 5 | ProjectId, |
| 6 | ThreadId, |
| 7 | type OrchestrationV2OlderSettledThreadsPage, |
| 8 | type OrchestrationV2SearchThreadsResult, |
| 9 | type OrchestrationV2ShellSnapshot, |
| 10 | type OrchestrationV2ThreadShell, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import { describe, expect, it } from "@effect/vitest"; |
| 13 | import * as Clock from "effect/Clock"; |
| 14 | import * as DateTime from "effect/DateTime"; |
| 15 | import * as Deferred from "effect/Deferred"; |
| 16 | import * as Effect from "effect/Effect"; |
| 17 | import * as Layer from "effect/Layer"; |
| 18 | import * as Option from "effect/Option"; |
| 19 | import * as Queue from "effect/Queue"; |
| 20 | import * as Stream from "effect/Stream"; |
| 21 | import * as SubscriptionRef from "effect/SubscriptionRef"; |
| 22 | import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity"; |
| 23 | import * as TestClock from "effect/testing/TestClock"; |
| 24 | |
| 25 | import { |
| 26 | AVAILABLE_CONNECTION_STATE, |
| 27 | PrimaryConnectionTarget, |
| 28 | type PreparedConnection, |
| 29 | type SupervisorConnectionState, |
| 30 | } from "../connection/model.ts"; |
| 31 | import * as EnvironmentRegistry from "../connection/registry.ts"; |
| 32 | import * as EnvironmentSupervisor from "../connection/supervisor.ts"; |
| 33 | import type { WsRpcProtocolClient } from "../rpc/protocol.ts"; |
| 34 | import type * as RpcSession from "../rpc/session.ts"; |
| 35 | import { EMPTY_SHELL_STATE, type EnvironmentShellState } from "./shell.ts"; |
| 36 | import { |
| 37 | createOlderSettledThreadAtoms, |
| 38 | isListedSettledThread, |
| 39 | olderSettledCountKey, |
| 40 | settledShelfFrontier, |
| 41 | unloadedOlderSettledCount, |
| 42 | wantedOlderSettledPages, |
| 43 | type OlderSettled, |
| 44 | type OlderSettledPages, |
| 45 | } from "./olderSettledThreads.ts"; |
| 46 | |
| 47 | const DAY = 24 * 60 * 60 * 1000; |
| 48 | const environmentId = EnvironmentId.make("env-a"); |
| 49 | const project = ProjectId.make("project-a"); |
| 50 | const before = DateTime.formatIso(DateTime.makeUnsafe(50 * DAY)); |
| 51 | const older: OlderSettled = { |
| 52 | before, |
| 53 | beforeMs: 50 * DAY, |
| 54 | counts: new Map([[project, 3]]), |
| 55 | }; |
| 56 | |
| 57 | const row = ( |
| 58 | id: string, |
| 59 | input: { |
| 60 | readonly settledDay?: number; |
| 61 | readonly createdDay?: number; |
| 62 | readonly subagent?: boolean; |
| 63 | }, |
| 64 | ) => |
| 65 | ({ |
| 66 | id: ThreadId.make(id), |
| 67 | projectId: project, |
| 68 | lineage: { |
| 69 | parentThreadId: input.subagent === true ? ThreadId.make("parent") : null, |
| 70 | relationshipToParent: input.subagent === true ? "subagent" : null, |
| 71 | rootThreadId: ThreadId.make(id), |
| 72 | }, |
| 73 | settledOverride: input.settledDay === undefined ? null : "settled", |
| 74 | settledAt: input.settledDay === undefined ? null : DateTime.makeUnsafe(input.settledDay * DAY), |
| 75 | createdAt: DateTime.makeUnsafe((input.createdDay ?? 1) * DAY), |
| 76 | }) as unknown as OrchestrationV2ThreadShell; |
| 77 | |
| 78 | describe("olderSettledCountKey", () => { |
| 79 | const key = ( |
| 80 | threads: ReadonlyArray<OrchestrationV2ThreadShell>, |
| 81 | removedAt: ReadonlyMap<ThreadId, number> = new Map(), |
| 82 | ) => olderSettledCountKey(threads, older.beforeMs, removedAt); |
| 83 | const base = [row("kept-old", { settledDay: 10 }), row("open", { createdDay: 60 })]; |
| 84 | |
| 85 | it("changes on every event that can change the older count", () => { |
| 86 | const start = key(base); |
| 87 | // A removal, even of an id removed before (the map keeps its size). |
| 88 | expect(key(base, new Map([[ThreadId.make("x"), 4]]))).not.toBe(start); |
| 89 | expect(key(base, new Map([[ThreadId.make("x"), 9]]))).not.toBe( |
| 90 | key(base, new Map([[ThreadId.make("x"), 4]])), |
| 91 | ); |
| 92 | // An omitted thread that arrives unsettled. |
| 93 | expect(key([...base, row("woken", { createdDay: 5 })])).not.toBe(start); |
| 94 | // A local row that turns older (a backdated auto-settle). |
| 95 | expect(key([base[0]!, row("open", { createdDay: 60, settledDay: 20 })])).not.toBe(start); |
| 96 | // An older row that moves to another project. |
| 97 | expect(key([{ ...base[0]!, projectId: ProjectId.make("project-b") }, base[1]!])).not.toBe( |
| 98 | start, |
| 99 | ); |
| 100 | }); |
| 101 | |
| 102 | it("stays put for new threads and subagents", () => { |
| 103 | const start = key(base); |
| 104 | expect(key([...base, row("new", { createdDay: 70 })])).toBe(start); |
| 105 | expect(key([...base, row("new-settled", { createdDay: 70, settledDay: 71 })])).toBe(start); |
| 106 | expect(key([...base, row("child", { createdDay: 5, subagent: true })])).toBe(start); |
| 107 | }); |
| 108 | }); |
| 109 | |
| 110 | describe("settled shelf listing", () => { |
| 111 | const pages = (frontier: OlderSettledPages["frontier"]): OlderSettledPages => ({ |
| 112 | reads: [], |
| 113 | frontier, |
| 114 | status: "ready", |
| 115 | }); |
| 116 | const at = (day: number) => DateTime.formatIso(DateTime.makeUnsafe(day * DAY)); |
| 117 | |
| 118 | it("lists rows at or above the highest frontier still loading", () => { |
| 119 | // No page yet: the cut is the frontier, so only rows newer than it list. |
| 120 | const one = settledShelfFrontier([{ older, pages: pages({ settledAt: before }) }]); |
| 121 | expect(isListedSettledThread(row("recent", { settledDay: 60 }), one)).toBe(true); |
| 122 | expect(isListedSettledThread(row("old", { settledDay: 30 }), one)).toBe(false); |
| 123 | // A row that became older inside the loaded range lists at its place. |
| 124 | const loaded = settledShelfFrontier([{ older, pages: pages({ settledAt: at(20) }) }]); |
| 125 | expect(isListedSettledThread(row("old", { settledDay: 30 }), loaded)).toBe(true); |
| 126 | expect(isListedSettledThread(row("older", { settledDay: 10 }), loaded)).toBe(false); |
| 127 | // All loaded, or a shell that keeps every settled thread: everything lists. |
| 128 | expect(settledShelfFrontier([{ older, pages: pages(null) }])).toBeNull(); |
| 129 | expect(settledShelfFrontier([{ older: null, pages: pages(null) }])).toBeNull(); |
| 130 | }); |
| 131 | |
| 132 | it("holds back another environment's rows below an environment still loading", () => { |
| 133 | const frontier = settledShelfFrontier([ |
| 134 | { older, pages: pages({ settledAt: at(40) }) }, |
| 135 | { older, pages: pages({ settledAt: at(20) }) }, |
| 136 | ]); |
| 137 | expect(frontier).toEqual({ settledAt: at(40) }); |
| 138 | expect(isListedSettledThread(row("b-loaded", { settledDay: 30 }), frontier)).toBe(false); |
| 139 | // A failed environment holds no one back. |
| 140 | expect( |
| 141 | settledShelfFrontier([ |
| 142 | { older, pages: { ...pages({ settledAt: at(40) }), status: "failed" } }, |
| 143 | { older, pages: pages({ settledAt: at(20) }) }, |
| 144 | ]), |
| 145 | ).toEqual({ settledAt: at(20) }); |
| 146 | }); |
| 147 | |
| 148 | it("on a settle-time tie, stops at the cursor that admits fewest rows", () => { |
| 149 | const strict = pages({ settledAt: at(30) }); |
| 150 | const afterM = pages({ settledAt: at(30), threadId: ThreadId.make("m") }); |
| 151 | const afterK = pages({ settledAt: at(30), threadId: ThreadId.make("k") }); |
| 152 | const frontier = (...all: ReadonlyArray<OlderSettledPages>) => |
| 153 | settledShelfFrontier(all.map((each) => ({ older, pages: each }))); |
| 154 | expect(frontier(strict, afterM)).toEqual(afterM.frontier); |
| 155 | expect(frontier(afterM, strict)).toEqual(afterM.frontier); |
| 156 | expect(frontier(afterM, afterK)).toEqual(afterK.frontier); |
| 157 | }); |
| 158 | |
| 159 | it("follows the server's id order on a settle-time tie", () => { |
| 160 | const frontier = { settledAt: at(30), threadId: ThreadId.make("m") }; |
| 161 | expect(isListedSettledThread(row("a", { settledDay: 30 }), frontier)).toBe(true); |
| 162 | expect(isListedSettledThread(row("z", { settledDay: 30 }), frontier)).toBe(false); |
| 163 | }); |
| 164 | }); |
| 165 | |
| 166 | it("counts older threads the client does not hold, once", () => { |
| 167 | const local = [ |
| 168 | // Kept by the snapshot (snoozed) and loaded by a page: the server counts both. |
| 169 | { source: row("snoozed-old", { settledDay: 10 }), environmentId }, |
| 170 | { source: row("paged-old", { settledDay: 20 }), environmentId }, |
| 171 | { source: row("recent", { settledDay: 60 }), environmentId }, |
| 172 | ]; |
| 173 | const olderSettled = () => older; |
| 174 | expect( |
| 175 | unloadedOlderSettledCount({ |
| 176 | olderSettled, |
| 177 | scopes: [{ environmentId, projectIds: null }], |
| 178 | threads: local, |
| 179 | }), |
| 180 | ).toBe(1); |
| 181 | expect( |
| 182 | unloadedOlderSettledCount({ |
| 183 | olderSettled, |
| 184 | scopes: [{ environmentId, projectIds: [ProjectId.make("other")] }], |
| 185 | threads: local, |
| 186 | }), |
| 187 | ).toBe(0); |
| 188 | // Two projects in one environment: each subtracts only its own loaded rows. |
| 189 | const other = ProjectId.make("project-b"); |
| 190 | const twoProjects = (): OlderSettled => ({ |
| 191 | ...older, |
| 192 | counts: new Map([ |
| 193 | [project, 10], |
| 194 | [other, 10], |
| 195 | ]), |
| 196 | }); |
| 197 | expect( |
| 198 | unloadedOlderSettledCount({ |
| 199 | olderSettled: twoProjects, |
| 200 | scopes: [{ environmentId, projectIds: [project, other] }], |
| 201 | threads: [ |
| 202 | { source: row("a-old", { settledDay: 10 }), environmentId }, |
| 203 | { source: { ...row("b-old", { settledDay: 10 }), projectId: other }, environmentId }, |
| 204 | ], |
| 205 | }), |
| 206 | ).toBe(18); |
| 207 | }); |
| 208 | |
| 209 | describe("wantedOlderSettledPages", () => { |
| 210 | const otherEnvironment = EnvironmentId.make("env-b"); |
| 211 | const full = (settledAt: string): OlderSettledPages => ({ |
| 212 | reads: [], |
| 213 | frontier: { settledAt, threadId: ThreadId.make("t") }, |
| 214 | status: "ready", |
| 215 | }); |
| 216 | const wanted = (input: { |
| 217 | readonly requested?: ReadonlyMap<EnvironmentId, number>; |
| 218 | readonly environments: ReadonlyMap<EnvironmentId, { readonly pages: OlderSettledPages }>; |
| 219 | readonly listed: number; |
| 220 | readonly visible: number; |
| 221 | }) => |
| 222 | wantedOlderSettledPages({ |
| 223 | requested: input.requested ?? new Map(), |
| 224 | environments: new Map( |
| 225 | [...input.environments].map(([id, { pages }]) => [id, { older, pages }] as const), |
| 226 | ), |
| 227 | listedCount: input.listed, |
| 228 | visibleCount: input.visible, |
| 229 | }); |
| 230 | |
| 231 | it("reads the next page of the environment holding the shelf back", () => { |
| 232 | const environments = new Map([ |
| 233 | [environmentId, { pages: full("2026-01-10T00:00:00.000Z") }], |
| 234 | [otherEnvironment, { pages: full("2026-01-20T00:00:00.000Z") }], |
| 235 | ]); |
| 236 | expect(wanted({ environments, listed: 9, visible: 10 })).toEqual( |
| 237 | new Map([[otherEnvironment, 1]]), |
| 238 | ); |
| 239 | // A deleted row drops the listed count again. |
| 240 | expect( |
| 241 | wanted({ requested: new Map([[environmentId, 1]]), environments, listed: 34, visible: 35 }), |
| 242 | ).toEqual( |
| 243 | new Map([ |
| 244 | [environmentId, 1], |
| 245 | [otherEnvironment, 1], |
| 246 | ]), |
| 247 | ); |
| 248 | }); |
| 249 | |
| 250 | it("waits for pending pages and stops at a short page or a closed shelf", () => { |
| 251 | const one = (pages: OlderSettledPages) => new Map([[environmentId, { pages }]]); |
| 252 | const pending = { ...full(before), status: "pending" as const }; |
| 253 | const requested = new Map([[environmentId, 1]]); |
| 254 | expect(wanted({ requested, environments: one(pending), listed: 0, visible: 10 })).toBe( |
| 255 | requested, |
| 256 | ); |
| 257 | const done = { ...full(before), frontier: null }; |
| 258 | expect(wanted({ requested, environments: one(done), listed: 3, visible: 10 })).toBe(requested); |
| 259 | expect(wanted({ requested, environments: one(full(before)), listed: 10, visible: 10 })).toBe( |
| 260 | requested, |
| 261 | ); |
| 262 | expect(wanted({ requested, environments: one(full(before)), listed: 0, visible: 0 })).toBe( |
| 263 | requested, |
| 264 | ); |
| 265 | }); |
| 266 | }); |
| 267 | |
| 268 | const TARGET = new PrimaryConnectionTarget({ |
| 269 | environmentId, |
| 270 | label: "Older settled environment", |
| 271 | httpBaseUrl: "https://older.example.test", |
| 272 | wsBaseUrl: "wss://older.example.test", |
| 273 | }); |
| 274 | |
| 275 | // The atoms over a fake connected environment whose `getOlderSettledThreads` and `searchThreads` |
| 276 | // take their replies, in order, from `replies`. |
| 277 | const makeAtoms = Effect.fn(function* () { |
| 278 | const replies = |
| 279 | yield* Queue.unbounded< |
| 280 | Effect.Effect< |
| 281 | OrchestrationV2OlderSettledThreadsPage | OrchestrationV2SearchThreadsResult, |
| 282 | OrchestrationV2GetShellSnapshotError |
| 283 | > |
| 284 | >(); |
| 285 | const requests: unknown[] = []; |
| 286 | // Each request, with a signal for when its read is stopped. |
| 287 | const requestLog = yield* Queue.unbounded<{ |
| 288 | readonly input: unknown; |
| 289 | readonly stopped: Deferred.Deferred<void>; |
| 290 | }>(); |
| 291 | const reply = (input: unknown) => |
| 292 | Effect.gen(function* () { |
| 293 | requests.push(input); |
| 294 | const stopped = yield* Deferred.make<void>(); |
| 295 | yield* Queue.offer(requestLog, { input, stopped }); |
| 296 | return yield* Effect.flatten(Queue.take(replies)).pipe( |
| 297 | Effect.onInterrupt(() => Deferred.succeed(stopped, undefined)), |
| 298 | ); |
| 299 | }); |
| 300 | const client = { |
| 301 | [ORCHESTRATION_V2_WS_METHODS.getOlderSettledThreads]: reply, |
| 302 | [ORCHESTRATION_V2_WS_METHODS.searchThreads]: reply, |
| 303 | } as unknown as WsRpcProtocolClient; |
| 304 | const session = { |
| 305 | client, |
| 306 | initialConfig: Effect.succeed({} as never), |
| 307 | ready: Effect.void, |
| 308 | probe: Effect.void, |
| 309 | closed: Effect.never, |
| 310 | } as unknown as RpcSession.RpcSession; |
| 311 | const supervisor = EnvironmentSupervisor.EnvironmentSupervisor.of({ |
| 312 | target: TARGET, |
| 313 | state: yield* SubscriptionRef.make<SupervisorConnectionState>({ |
| 314 | ...AVAILABLE_CONNECTION_STATE, |
| 315 | desired: true, |
| 316 | network: "online", |
| 317 | phase: "connected", |
| 318 | attempt: 1, |
| 319 | generation: 1, |
| 320 | }), |
| 321 | session: yield* SubscriptionRef.make(Option.some(session)), |
| 322 | prepared: yield* SubscriptionRef.make(Option.none<PreparedConnection>()), |
| 323 | connect: Effect.void, |
| 324 | disconnect: Effect.void, |
| 325 | retryNow: Effect.void, |
| 326 | } satisfies EnvironmentSupervisor.EnvironmentSupervisor["Service"]); |
| 327 | const run: EnvironmentRegistry.EnvironmentRegistry["Service"]["run"] = (_environmentId, effect) => |
| 328 | Effect.provideService(effect, EnvironmentSupervisor.EnvironmentSupervisor, supervisor); |
| 329 | const followStream: EnvironmentRegistry.EnvironmentRegistry["Service"]["followStream"] = ( |
| 330 | _environmentId, |
| 331 | stream, |
| 332 | ) => Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor); |
| 333 | const runtime = Atom.runtime( |
| 334 | Layer.mergeAll( |
| 335 | Layer.succeed( |
| 336 | EnvironmentRegistry.EnvironmentRegistry, |
| 337 | EnvironmentRegistry.EnvironmentRegistry.of({ |
| 338 | run, |
| 339 | followStream, |
| 340 | } as unknown as EnvironmentRegistry.EnvironmentRegistry["Service"]), |
| 341 | ), |
| 342 | // The test's clock, so the count re-read's wait moves with `TestClock.adjust`. |
| 343 | Layer.succeed(Clock.Clock, yield* Clock.Clock), |
| 344 | ), |
| 345 | ); |
| 346 | const shellState = Atom.make<AsyncResult.AsyncResult<EnvironmentShellState>>( |
| 347 | AsyncResult.success(EMPTY_SHELL_STATE), |
| 348 | ); |
| 349 | const registry = yield* Effect.acquireRelease(Effect.sync(AtomRegistry.make), (registry) => |
| 350 | Effect.sync(() => registry.dispose()), |
| 351 | ); |
| 352 | return { |
| 353 | replies, |
| 354 | requests, |
| 355 | requestLog, |
| 356 | atoms: createOlderSettledThreadAtoms(runtime, () => shellState), |
| 357 | registry, |
| 358 | setShell: (state: Partial<EnvironmentShellState>) => |
| 359 | registry.set(shellState, AsyncResult.success({ ...EMPTY_SHELL_STATE, ...state })), |
| 360 | // The first value that passes, from a subscription. |
| 361 | until: <A>(atom: Atom.Atom<A>, done: (value: A) => boolean) => |
| 362 | AtomRegistry.toStream(registry, atom).pipe( |
| 363 | Stream.filter(done), |
| 364 | Stream.runHead, |
| 365 | Effect.map(Option.getOrThrow), |
| 366 | ), |
| 367 | }; |
| 368 | }); |
| 369 | |
| 370 | const shellSnapshot = (input: { |
| 371 | readonly sequence: number; |
| 372 | readonly asOfSequence: number; |
| 373 | readonly count: number; |
| 374 | }) => |
| 375 | Option.some({ |
| 376 | schemaVersion: 1, |
| 377 | snapshotSequence: input.sequence, |
| 378 | projects: [], |
| 379 | threads: [row("recent", { settledDay: 60 })], |
| 380 | archivedThreads: [], |
| 381 | olderSettled: { |
| 382 | before, |
| 383 | asOfSequence: input.asOfSequence, |
| 384 | counts: [{ projectId: project, count: input.count }], |
| 385 | }, |
| 386 | } as unknown as OrchestrationV2ShellSnapshot); |
| 387 | |
| 388 | describe("createOlderSettledThreadAtoms", () => { |
| 389 | it.effect("keeps counts current: the snapshot's own, then one read after changes settle", () => |
| 390 | Effect.gen(function* () { |
| 391 | const { replies, requests, atoms, registry, setShell, until } = yield* makeAtoms(); |
| 392 | const olderAtom = atoms.olderSettledAtom(environmentId); |
| 393 | const count = (value: OlderSettled | null) => value?.counts.get(project); |
| 394 | registry.mount(olderAtom); |
| 395 | |
| 396 | // A cached shell: its counts stand, read nothing. |
| 397 | setShell({ snapshot: shellSnapshot({ sequence: 5, asOfSequence: 3, count: 5 }) }); |
| 398 | expect(count(registry.get(olderAtom))).toBe(5); |
| 399 | // This session's snapshot: its counts are current until something changes. |
| 400 | setShell({ |
| 401 | snapshot: shellSnapshot({ sequence: 3, asOfSequence: 3, count: 5 }), |
| 402 | generation: 1, |
| 403 | }); |
| 404 | expect(count(registry.get(olderAtom))).toBe(5); |
| 405 | |
| 406 | // A removal can change the count: one read once changes stop for a moment. |
| 407 | yield* Queue.offer( |
| 408 | replies, |
| 409 | Effect.succeed({ |
| 410 | snapshotSequence: 4, |
| 411 | threads: [], |
| 412 | counts: [{ projectId: project, count: 4 }], |
| 413 | }), |
| 414 | ); |
| 415 | setShell({ |
| 416 | snapshot: shellSnapshot({ sequence: 4, asOfSequence: 3, count: 5 }), |
| 417 | generation: 1, |
| 418 | removedAt: new Map([[ThreadId.make("gone"), 4]]), |
| 419 | }); |
| 420 | expect(count(registry.get(olderAtom))).toBe(5); |
| 421 | yield* TestClock.adjust("500 millis"); |
| 422 | expect(count(yield* until(olderAtom, (value) => count(value) === 4))).toBe(4); |
| 423 | expect(requests).toEqual([{ before: { settledAt: before }, limit: 0 }]); |
| 424 | |
| 425 | // A new snapshot brings its own counts and drops the read. |
| 426 | setShell({ |
| 427 | snapshot: shellSnapshot({ sequence: 9, asOfSequence: 9, count: 7 }), |
| 428 | generation: 2, |
| 429 | }); |
| 430 | expect(count(registry.get(olderAtom))).toBe(7); |
| 431 | expect(requests).toHaveLength(1); |
| 432 | }), |
| 433 | ); |
| 434 | |
| 435 | it.effect("reads pages in order until a short one, and retries a failed page", () => |
| 436 | Effect.gen(function* () { |
| 437 | const { replies, requests, atoms, registry, setShell, until } = yield* makeAtoms(); |
| 438 | const scope = { environmentId, projectIds: null }; |
| 439 | const snapshot = shellSnapshot({ sequence: 3, asOfSequence: 3, count: 30 }); |
| 440 | |
| 441 | // A cached shell holds no other environment back: nothing can load yet. |
| 442 | setShell({ snapshot }); |
| 443 | expect(registry.get(atoms.pagesAtom(scope, 1))).toEqual({ |
| 444 | reads: [], |
| 445 | frontier: null, |
| 446 | status: "ready", |
| 447 | }); |
| 448 | |
| 449 | setShell({ snapshot, generation: 1 }); |
| 450 | expect(registry.get(atoms.pagesAtom(scope, 0)).frontier).toEqual({ settledAt: before }); |
| 451 | const full = Array.from({ length: 25 }, (_, index) => |
| 452 | row(`full-${String(index).padStart(2, "0")}`, { settledDay: 40 - index }), |
| 453 | ); |
| 454 | yield* Queue.offer( |
| 455 | replies, |
| 456 | Effect.succeed({ snapshotSequence: 3, threads: full, counts: [] }), |
| 457 | ); |
| 458 | yield* Queue.offer( |
| 459 | replies, |
| 460 | Effect.succeed({ |
| 461 | snapshotSequence: 3, |
| 462 | threads: [row("short", { settledDay: 10 })], |
| 463 | counts: [], |
| 464 | }), |
| 465 | ); |
| 466 | const twoPages = atoms.pagesAtom(scope, 2); |
| 467 | registry.mount(twoPages); |
| 468 | const done = yield* until(twoPages, (pages) => pages.reads.length === 2); |
| 469 | expect(done.frontier).toBeNull(); |
| 470 | expect(done.status).toBe("ready"); |
| 471 | // The second page continues from the first page's last row. |
| 472 | expect(requests).toEqual([ |
| 473 | { before: { settledAt: before }, limit: 25 }, |
| 474 | { |
| 475 | before: { settledAt: DateTime.formatIso(full[24]!.settledAt!), threadId: full[24]!.id }, |
| 476 | limit: 25, |
| 477 | }, |
| 478 | ]); |
| 479 | |
| 480 | // A new snapshot reads again; a failed page shows until a retry reads it. |
| 481 | const onePage = atoms.pagesAtom(scope, 1); |
| 482 | registry.mount(onePage); |
| 483 | setShell({ snapshot, generation: 2 }); |
| 484 | expect(registry.get(onePage).status).toBe("pending"); |
| 485 | yield* Queue.offer( |
| 486 | replies, |
| 487 | Effect.fail(new OrchestrationV2GetShellSnapshotError({ message: "read failed" })), |
| 488 | ); |
| 489 | expect((yield* until(onePage, (pages) => pages.status === "failed")).frontier).toEqual({ |
| 490 | settledAt: before, |
| 491 | }); |
| 492 | atoms.retryPages(registry, scope, 1); |
| 493 | expect(registry.get(onePage).status).toBe("pending"); |
| 494 | yield* Queue.offer( |
| 495 | replies, |
| 496 | Effect.succeed({ snapshotSequence: 3, threads: full.slice(0, 3), counts: [] }), |
| 497 | ); |
| 498 | const retried = yield* until(onePage, (pages) => pages.status === "ready"); |
| 499 | expect(retried.reads).toHaveLength(1); |
| 500 | expect(retried.frontier).toBeNull(); |
| 501 | }), |
| 502 | ); |
| 503 | |
| 504 | it.effect("searches read once per generation, and nothing before the first snapshot", () => |
| 505 | Effect.gen(function* () { |
| 506 | const { replies, requests, atoms, registry, setShell, until } = yield* makeAtoms(); |
| 507 | const snapshot = shellSnapshot({ sequence: 3, asOfSequence: 3, count: 30 }); |
| 508 | const input = { query: "old", olderSettledBefore: before, titlesOnly: true }; |
| 509 | const search = atoms.searchAtom(environmentId, input); |
| 510 | |
| 511 | // That snapshot would drop the rows: loading while it is on its way, idle while offline. |
| 512 | setShell({ snapshot, status: "synchronizing" }); |
| 513 | registry.mount(search); |
| 514 | expect(registry.get(search).waiting).toBe(true); |
| 515 | setShell({ snapshot, status: "cached" }); |
| 516 | expect(registry.get(search).waiting).toBe(false); |
| 517 | |
| 518 | const found = { matches: [], snapshotSequence: 3, threads: [row("old", { settledDay: 9 })] }; |
| 519 | setShell({ snapshot, status: "live", generation: 1 }); |
| 520 | yield* Queue.offer(replies, Effect.succeed(found)); |
| 521 | yield* until(search, AsyncResult.isSuccess); |
| 522 | // A new snapshot reads the rows again. |
| 523 | setShell({ snapshot, status: "live", generation: 2 }); |
| 524 | yield* Queue.offer(replies, Effect.succeed({ ...found, snapshotSequence: 8 })); |
| 525 | yield* until( |
| 526 | search, |
| 527 | (result) => AsyncResult.getOrElse(result, () => null)?.snapshotSequence === 8, |
| 528 | ); |
| 529 | expect(requests).toEqual([input, input]); |
| 530 | }), |
| 531 | ); |
| 532 | |
| 533 | it.effect("growing reads only the next page, and collapsing stops the shelf's reads", () => |
| 534 | Effect.gen(function* () { |
| 535 | const { replies, requestLog, atoms, registry, setShell, until } = yield* makeAtoms(); |
| 536 | setShell({ |
| 537 | snapshot: shellSnapshot({ sequence: 3, asOfSequence: 3, count: 30 }), |
| 538 | generation: 1, |
| 539 | }); |
| 540 | const scope = { environmentId, projectIds: null }; |
| 541 | const full = Array.from({ length: 25 }, (_, index) => |
| 542 | row(`full-${String(index).padStart(2, "0")}`, { settledDay: 40 - index }), |
| 543 | ); |
| 544 | const releaseOne = registry.mount(atoms.pagesAtom(scope, 1)); |
| 545 | expect((yield* Queue.take(requestLog)).input).toEqual({ |
| 546 | before: { settledAt: before }, |
| 547 | limit: 25, |
| 548 | }); |
| 549 | yield* Queue.offer( |
| 550 | replies, |
| 551 | Effect.succeed({ snapshotSequence: 3, threads: full, counts: [] }), |
| 552 | ); |
| 553 | yield* until(atoms.pagesAtom(scope, 1), (pages) => pages.reads.length === 1); |
| 554 | |
| 555 | // React builds the next shelf before the last one lets go, so page 1 is not read again. |
| 556 | registry.get(atoms.pagesAtom(scope, 2)); |
| 557 | releaseOne(); |
| 558 | const releaseTwo = registry.mount(atoms.pagesAtom(scope, 2)); |
| 559 | const second = yield* Queue.take(requestLog); |
| 560 | expect(second.input).toEqual({ |
| 561 | before: { settledAt: DateTime.formatIso(full[24]!.settledAt!), threadId: full[24]!.id }, |
| 562 | limit: 25, |
| 563 | }); |
| 564 | |
| 565 | // Collapsing stops the pending read: nothing is left to run again on a reconnect. |
| 566 | releaseTwo(); |
| 567 | yield* Deferred.await(second.stopped); |
| 568 | }), |
| 569 | ); |
| 570 | }); |