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, 03:37 UTC
| 1 | import { it } from "@effect/vitest"; |
| 2 | import * as Deferred from "effect/Deferred"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Fiber from "effect/Fiber"; |
| 5 | import * as PubSub from "effect/PubSub"; |
| 6 | import * as Queue from "effect/Queue"; |
| 7 | import * as Stream from "effect/Stream"; |
| 8 | import { describe, expect } from "vite-plus/test"; |
| 9 | |
| 10 | import { |
| 11 | bufferLatestLiveStream, |
| 12 | makeLiveStreamBudget, |
| 13 | replayAndBufferProjectedLiveEvents, |
| 14 | type RetainedLiveItem, |
| 15 | } from "./LiveStreamBudget.ts"; |
| 16 | |
| 17 | describe("LiveStreamBudget", () => { |
| 18 | it.effect("closes the source without releasing a batch still waiting for an ACK", () => |
| 19 | Effect.gen(function* () { |
| 20 | const budget = yield* makeLiveStreamBudget({ maxItems: 3 }); |
| 21 | const queue = yield* Queue.unbounded<RetainedLiveItem<{ text: string }>>(); |
| 22 | const sourceClosed = yield* Deferred.make<void>(); |
| 23 | const first = yield* budget.retain({ text: "first" }); |
| 24 | const second = yield* budget.retain({ text: "second" }); |
| 25 | const third = yield* budget.retain({ text: "third" }); |
| 26 | yield* Queue.offerAll(queue, [first, second, third]); |
| 27 | yield* Effect.scoped( |
| 28 | Effect.gen(function* () { |
| 29 | const pull = yield* Stream.toPull( |
| 30 | budget.deliver( |
| 31 | Stream.fromQueue(queue).pipe( |
| 32 | Stream.rechunk(1), |
| 33 | Stream.ensuring(Deferred.succeed(sourceClosed, undefined)), |
| 34 | ), |
| 35 | ), |
| 36 | ); |
| 37 | expect(yield* pull).toEqual([{ text: "first" }]); |
| 38 | expect(yield* Queue.size(queue)).toBe(0); |
| 39 | expect((yield* budget.usage).retainedItems).toBe(3); |
| 40 | const overflow = yield* budget.retain({ text: "fourth" }).pipe(Effect.result); |
| 41 | expect(overflow._tag).toBe("Failure"); |
| 42 | yield* Deferred.await(sourceClosed); |
| 43 | yield* budget.closed; |
| 44 | expect((yield* budget.usage).retainedItems).toBe(1); |
| 45 | }), |
| 46 | ); |
| 47 | expect(yield* budget.usage).toEqual({ retainedItems: 0, retainedSerializedBytes: 0 }); |
| 48 | }), |
| 49 | ); |
| 50 | }); |
| 51 | |
| 52 | type Event = { readonly sequence: number; readonly text: string; readonly threadId?: string }; |
| 53 | |
| 54 | describe("bufferLatestLiveStream", () => { |
| 55 | it.effect.each(["items", "bytes"] as const)( |
| 56 | "counts an unacknowledged state against the %s limit when its replacement arrives", |
| 57 | (limit) => |
| 58 | Effect.gen(function* () { |
| 59 | const releaseUpdate = yield* Deferred.make<void>(); |
| 60 | const closed = yield* Deferred.make<void>(); |
| 61 | const first = { sequence: 1, text: "é".repeat(20) }; |
| 62 | const next = { ...first, sequence: 2 }; |
| 63 | const pull = yield* Stream.toPull( |
| 64 | bufferLatestLiveStream( |
| 65 | Stream.concat( |
| 66 | Stream.succeed(first), |
| 67 | Stream.fromEffect(Deferred.await(releaseUpdate)).pipe(Stream.map(() => next)), |
| 68 | ).pipe(Stream.ensuring(Deferred.succeed(closed, undefined))), |
| 69 | () => "same-thread", |
| 70 | limit === "items" |
| 71 | ? { maxItems: 1 } |
| 72 | : { maxSerializedBytes: Buffer.byteLength(JSON.stringify(first)) }, |
| 73 | ), |
| 74 | ); |
| 75 | expect(yield* pull).toEqual([first]); |
| 76 | yield* Deferred.succeed(releaseUpdate, undefined); |
| 77 | yield* Deferred.await(closed); |
| 78 | const result = yield* pull.pipe(Effect.result); |
| 79 | expect(result._tag).toBe("Failure"); |
| 80 | if (result._tag === "Failure") expect(result.failure._tag).toBe("LiveStreamBufferError"); |
| 81 | }), |
| 82 | ); |
| 83 | }); |
| 84 | |
| 85 | describe("replayAndBufferProjectedLiveEvents", () => { |
| 86 | it.effect.each(["items", "bytes"] as const)( |
| 87 | "replays the undelivered tail after a %s overflow without losing or duplicating events", |
| 88 | (limit) => |
| 89 | Effect.gen(function* () { |
| 90 | const pubsub = yield* PubSub.unbounded<Event>(); |
| 91 | const subscribed = yield* Deferred.make<PubSub.Subscription<Event>>(); |
| 92 | const events = [ |
| 93 | { sequence: 1, text: "one" }, |
| 94 | { sequence: 2, text: "two" }, |
| 95 | { sequence: 3, text: "three" }, |
| 96 | ]; |
| 97 | const limits = limit === "items" ? { maxItems: 1 } : { maxSerializedBytes: 40 }; |
| 98 | const pull = yield* Stream.toPull( |
| 99 | replayAndBufferProjectedLiveEvents( |
| 100 | { |
| 101 | project: (event: Event) => event, |
| 102 | subscribe: PubSub.subscribe(pubsub).pipe( |
| 103 | Effect.tap((subscription) => Deferred.succeed(subscribed, subscription)), |
| 104 | ), |
| 105 | latestSequence: Effect.succeed(1), |
| 106 | replay: () => Stream.succeed(events[0]!), |
| 107 | }, |
| 108 | limits, |
| 109 | ), |
| 110 | ); |
| 111 | const received = [...(yield* pull)]; |
| 112 | // Hold the ACK while more durable events arrive. |
| 113 | yield* PubSub.publishAll(pubsub, events.slice(1)); |
| 114 | yield* (yield* Deferred.await(subscribed)).shutdownHook.await; |
| 115 | const overflow = yield* pull.pipe(Effect.result); |
| 116 | expect(overflow._tag).toBe("Failure"); |
| 117 | if (overflow._tag === "Failure") |
| 118 | expect(overflow.failure._tag).toBe("LiveStreamBufferError"); |
| 119 | |
| 120 | const afterSequence = received.at(-1)!.sequence; |
| 121 | const resumed = yield* replayAndBufferProjectedLiveEvents( |
| 122 | { |
| 123 | project: (event: Event) => event, |
| 124 | subscribe: PubSub.subscribe(pubsub), |
| 125 | afterSequence, |
| 126 | latestSequence: Effect.succeed(3), |
| 127 | replay: (throughSequence) => |
| 128 | Stream.fromIterable( |
| 129 | events.filter( |
| 130 | (event) => event.sequence > afterSequence && event.sequence <= throughSequence, |
| 131 | ), |
| 132 | ), |
| 133 | }, |
| 134 | limits, |
| 135 | ).pipe(Stream.take(2), Stream.runCollect); |
| 136 | expect([...received, ...resumed]).toEqual(events); |
| 137 | expect(yield* PubSub.size(pubsub)).toBe(0); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | it.effect.each(["high-water", "replay"] as const)( |
| 142 | "unsubscribes and cancels a blocked %s read on live overflow", |
| 143 | (phase) => |
| 144 | Effect.gen(function* () { |
| 145 | const pubsub = yield* PubSub.unbounded<Event>(); |
| 146 | const subscribed = yield* Deferred.make<PubSub.Subscription<Event>>(); |
| 147 | const readStarted = yield* Deferred.make<void>(); |
| 148 | const readClosed = yield* Deferred.make<void>(); |
| 149 | const blockedRead = Deferred.succeed(readStarted, undefined).pipe( |
| 150 | Effect.andThen(Effect.never), |
| 151 | Effect.ensuring(Deferred.succeed(readClosed, undefined)), |
| 152 | ); |
| 153 | const pull = yield* Stream.toPull( |
| 154 | replayAndBufferProjectedLiveEvents( |
| 155 | { |
| 156 | project: (event: Event) => event, |
| 157 | subscribe: PubSub.subscribe(pubsub).pipe( |
| 158 | Effect.tap((subscription) => Deferred.succeed(subscribed, subscription)), |
| 159 | ), |
| 160 | latestSequence: |
| 161 | phase === "high-water" ? blockedRead.pipe(Effect.as(0)) : Effect.succeed(0), |
| 162 | replay: () => (phase === "replay" ? Stream.fromEffect(blockedRead) : Stream.empty), |
| 163 | }, |
| 164 | { maxItems: 2 }, |
| 165 | ), |
| 166 | ); |
| 167 | const reader = yield* pull.pipe(Effect.result, Effect.forkScoped); |
| 168 | yield* Deferred.await(readStarted); |
| 169 | yield* PubSub.publishAll(pubsub, [ |
| 170 | { sequence: 1, text: "one" }, |
| 171 | { sequence: 2, text: "two" }, |
| 172 | { sequence: 3, text: "overflow" }, |
| 173 | ]); |
| 174 | yield* (yield* Deferred.await(subscribed)).shutdownHook.await; |
| 175 | yield* Deferred.await(readClosed); |
| 176 | const result = yield* Fiber.join(reader); |
| 177 | expect(result._tag).toBe("Failure"); |
| 178 | if (result._tag === "Failure") expect(result.failure._tag).toBe("LiveStreamBufferError"); |
| 179 | expect(yield* PubSub.size(pubsub)).toBe(0); |
| 180 | yield* PubSub.publish(pubsub, { sequence: 4, text: "after overflow" }); |
| 181 | expect(yield* PubSub.size(pubsub)).toBe(0); |
| 182 | }), |
| 183 | ); |
| 184 | |
| 185 | it.effect("lets a fast reader replay a database page larger than both buffer limits", () => |
| 186 | Effect.gen(function* () { |
| 187 | const pubsub = yield* PubSub.unbounded<Event>(); |
| 188 | const events = Array.from({ length: 20 }, (_, index) => ({ |
| 189 | sequence: index + 1, |
| 190 | text: "historical event", |
| 191 | })); |
| 192 | const replayed = yield* replayAndBufferProjectedLiveEvents( |
| 193 | { |
| 194 | project: (event: Event) => event, |
| 195 | subscribe: PubSub.subscribe(pubsub), |
| 196 | latestSequence: Effect.succeed(20), |
| 197 | replay: () => Stream.fromIterable(events), |
| 198 | }, |
| 199 | { maxItems: 2, maxSerializedBytes: 100 }, |
| 200 | ).pipe(Stream.take(20), Stream.runCollect); |
| 201 | expect(replayed).toEqual(events); |
| 202 | expect(yield* PubSub.size(pubsub)).toBe(0); |
| 203 | }), |
| 204 | ); |
| 205 | |
| 206 | it.effect( |
| 207 | "filters unrelated events before buffering and deduplicates the replay/live overlap", |
| 208 | () => |
| 209 | Effect.gen(function* () { |
| 210 | const pubsub = yield* PubSub.unbounded<Event>(); |
| 211 | const highWaterStarted = yield* Deferred.make<void>(); |
| 212 | const highWater = yield* Deferred.make<number>(); |
| 213 | const first = { sequence: 1, text: "first", threadId: "selected" }; |
| 214 | const unrelatedReplay = { sequence: 0, text: "unrelated replay", threadId: "other" }; |
| 215 | const overlap = { sequence: 2, text: "overlap", threadId: "selected" }; |
| 216 | const last = { sequence: 10, text: "last", threadId: "selected" }; |
| 217 | const reader = yield* replayAndBufferProjectedLiveEvents( |
| 218 | { |
| 219 | project: (event: Event) => event, |
| 220 | subscribe: PubSub.subscribe(pubsub), |
| 221 | latestSequence: Deferred.succeed(highWaterStarted, undefined).pipe( |
| 222 | Effect.andThen(Deferred.await(highWater)), |
| 223 | ), |
| 224 | replay: () => Stream.make(unrelatedReplay, first, overlap), |
| 225 | filter: (event) => event.threadId === "selected", |
| 226 | }, |
| 227 | { maxItems: 4 }, |
| 228 | ).pipe(Stream.take(3), Stream.runCollect, Effect.forkScoped); |
| 229 | yield* Deferred.await(highWaterStarted); |
| 230 | yield* PubSub.publishAll(pubsub, [ |
| 231 | overlap, |
| 232 | ...Array.from({ length: 6 }, (_, index) => ({ |
| 233 | sequence: index + 3, |
| 234 | text: "unrelated", |
| 235 | threadId: "other", |
| 236 | })), |
| 237 | last, |
| 238 | ]); |
| 239 | yield* Deferred.succeed(highWater, 2); |
| 240 | expect(yield* Fiber.join(reader)).toEqual([first, overlap, last]); |
| 241 | expect(yield* PubSub.size(pubsub)).toBe(0); |
| 242 | }), |
| 243 | ); |
| 244 | }); |