Skip to content

Commit 90cd35c

Browse files
committed
test(client-runtime): cover bounded shell event batches
1 parent 230061f commit 90cd35c

2 files changed

Lines changed: 18 additions & 13 deletions

File tree

‎packages/client-runtime/src/state/shell-sync.test.ts‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,13 @@ describe("environment shell synchronization", () => {
152152
}),
153153
);
154154

155-
it.live("applies one chunk of live events as one state change", () =>
155+
it.live.each([
156+
{ bufferSize: Infinity, expectedSequences: [51] },
157+
// RpcClient defaults to a 16-event buffer, which splits larger server chunks.
158+
{ bufferSize: 16, expectedSequences: [17, 33, 49, 51] },
159+
])("batches live events with a $bufferSize event buffer", ({ bufferSize, expectedSequences }) =>
156160
Effect.gen(function* () {
157-
const events = yield* Queue.unbounded<OrchestrationShellStreamItem>();
161+
const events = yield* Queue.bounded<OrchestrationShellStreamItem>(bufferSize);
158162
const client = {
159163
[ORCHESTRATION_WS_METHODS.subscribeShell]: () => Stream.fromQueue(events),
160164
} as unknown as WsRpcProtocolClient;
@@ -210,27 +214,29 @@ describe("environment shell synchronization", () => {
210214
Stream.runHead,
211215
);
212216

213-
// A bulk action (snooze 30 threads) reaches the client as one server
214-
// coalesced chunk. The shell state must change once for it, not 30 times.
217+
// Observe before publishing so no batch can arrive before the subscription.
215218
const observed = yield* SubscriptionRef.changes(shellState).pipe(
216219
Stream.drop(1),
217220
Stream.takeUntil(
218-
(state) => Option.isSome(state.snapshot) && state.snapshot.value.threads.length === 30,
221+
(state) => Option.isSome(state.snapshot) && state.snapshot.value.threads.length === 50,
219222
),
220223
Stream.runCollect,
221-
Effect.forkScoped,
224+
Effect.forkScoped({ startImmediately: true }),
222225
);
223226
yield* Queue.offerAll(
224227
events,
225-
Array.from({ length: 30 }, (_, index) => ({
228+
Array.from({ length: 50 }, (_, index) => ({
226229
kind: "thread-upserted" as const,
227230
sequence: 2 + index,
228231
thread: { id: `thread-${index}` } as never,
229232
})),
230233
);
231234
const states = yield* Fiber.join(observed);
232-
expect(states).toHaveLength(1);
233-
expect(Option.getOrThrow(states[0]!.snapshot).snapshotSequence).toBe(31);
235+
const snapshots = states.map((state) => Option.getOrThrow(state.snapshot));
236+
expect(snapshots.map((snapshot) => snapshot.snapshotSequence)).toEqual(expectedSequences);
237+
expect(snapshots.at(-1)!.threads.map((thread) => thread.id)).toEqual(
238+
Array.from({ length: 50 }, (_, index) => `thread-${index}`),
239+
);
234240
}).pipe(Effect.scoped),
235241
);
236242

‎packages/client-runtime/src/state/shell.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,9 @@ export const makeEnvironmentShellState = Effect.fn("EnvironmentShellState.make")
135135
),
136136
);
137137

138-
// The server coalesces shell events into one chunk per window, and each
139-
// chunk arrives here as one array. Folding the whole chunk into a single
140-
// state write keeps one bulk action (snoozing 50 threads) at one sidebar
141-
// render instead of one per thread.
138+
// Apply each received batch with one state write. The RPC client's bounded
139+
// buffer can split a server chunk, so a bulk action can still need several
140+
// writes, but each write includes every event in that batch.
142141
const applyItems = Effect.fn("EnvironmentShellState.applyItems")(function* (
143142
items: ReadonlyArray<OrchestrationShellStreamItem>,
144143
) {

0 commit comments

Comments
 (0)