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 { EnvironmentId } from "@t3tools/contracts"; |
| 2 | import { describe, expect, it } from "@effect/vitest"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Fiber from "effect/Fiber"; |
| 5 | import { vi } from "vite-plus/test"; |
| 6 | |
| 7 | import { |
| 8 | attachmentUploadError, |
| 9 | createAttachmentUploadRegistry, |
| 10 | type AttachmentUploadError, |
| 11 | type AttachmentUploadStatus, |
| 12 | } from "./attachmentUploads.ts"; |
| 13 | |
| 14 | interface UploadRequest { |
| 15 | readonly environmentId: EnvironmentId; |
| 16 | readonly attachment: { readonly id: string; readonly fileUri: string }; |
| 17 | } |
| 18 | |
| 19 | const environmentId = EnvironmentId.make("environment-1"); |
| 20 | const uploadKey = (environment: EnvironmentId, id: string) => `${environment}:${id}`; |
| 21 | |
| 22 | function request(id: string, environment = environmentId): UploadRequest { |
| 23 | return { environmentId: environment, attachment: { id, fileUri: `file:///documents/${id}.pdf` } }; |
| 24 | } |
| 25 | |
| 26 | /** The registry over a promise-based transfer, as each client wires its own. */ |
| 27 | function createQueue(options: { |
| 28 | readonly upload: ( |
| 29 | request: UploadRequest, |
| 30 | signal: AbortSignal, |
| 31 | onProgress: (progress: number) => void, |
| 32 | ) => Promise<string>; |
| 33 | readonly onUploaded?: ( |
| 34 | request: UploadRequest, |
| 35 | uploadedId: string, |
| 36 | ) => Effect.Effect<void, AttachmentUploadError>; |
| 37 | readonly onChange: (statuses: Readonly<Record<string, AttachmentUploadStatus>>) => void; |
| 38 | readonly concurrencyPerEnvironment?: number; |
| 39 | }) { |
| 40 | return createAttachmentUploadRegistry<UploadRequest>({ |
| 41 | key: (candidate) => uploadKey(candidate.environmentId, candidate.attachment.id), |
| 42 | environmentId: (candidate) => candidate.environmentId, |
| 43 | concurrencyPerEnvironment: options.concurrencyPerEnvironment ?? 3, |
| 44 | upload: (candidate, report) => |
| 45 | Effect.tryPromise({ |
| 46 | try: (signal) => options.upload(candidate, signal, report), |
| 47 | catch: attachmentUploadError, |
| 48 | }), |
| 49 | ...(options.onUploaded ? { onUploaded: options.onUploaded } : {}), |
| 50 | onChange: options.onChange, |
| 51 | }); |
| 52 | } |
| 53 | |
| 54 | describe("attachment upload registry", () => { |
| 55 | it("bounds concurrency, deduplicates updates, and drains all attachments", async () => { |
| 56 | const gates = new Map<string, ReturnType<typeof Promise.withResolvers<string>>>(); |
| 57 | const fourthStarted = Promise.withResolvers<void>(); |
| 58 | const firstThreeStarted = Promise.withResolvers<void>(); |
| 59 | let active = 0; |
| 60 | let maximum = 0; |
| 61 | const upload = vi.fn(async (input: UploadRequest) => { |
| 62 | active += 1; |
| 63 | maximum = Math.max(maximum, active); |
| 64 | const gate = Promise.withResolvers<string>(); |
| 65 | gates.set(input.attachment.id, gate); |
| 66 | if (gates.size === 3) firstThreeStarted.resolve(); |
| 67 | if (gates.size === 4) fourthStarted.resolve(); |
| 68 | try { |
| 69 | return await gate.promise; |
| 70 | } finally { |
| 71 | active -= 1; |
| 72 | } |
| 73 | }); |
| 74 | const queue = createQueue({ upload, onChange: () => {} }); |
| 75 | const requests = [request("one"), request("two"), request("three"), request("four")]; |
| 76 | queue.sync(requests); |
| 77 | queue.sync(requests); |
| 78 | await firstThreeStarted.promise; |
| 79 | expect(upload).toHaveBeenCalledTimes(3); |
| 80 | gates.get("one")!.resolve("uploaded"); |
| 81 | await fourthStarted.promise; |
| 82 | for (const gate of gates.values()) gate.resolve("uploaded"); |
| 83 | await queue.settled(); |
| 84 | queue.sync(requests); |
| 85 | await queue.settled(); |
| 86 | expect(maximum).toBe(3); |
| 87 | expect(upload).toHaveBeenCalledTimes(4); |
| 88 | queue.dispose(); |
| 89 | }); |
| 90 | |
| 91 | it("cancels on disconnect and resumes from the same local draft on reconnect", async () => { |
| 92 | const started = Promise.withResolvers<void>(); |
| 93 | let states: Readonly<Record<string, AttachmentUploadStatus>> = {}; |
| 94 | let signal: AbortSignal | undefined; |
| 95 | const upload = vi.fn(async (_request: UploadRequest, currentSignal: AbortSignal) => { |
| 96 | signal = currentSignal; |
| 97 | started.resolve(); |
| 98 | return new Promise<string>((resolve) => |
| 99 | currentSignal.addEventListener("abort", () => resolve("aborted"), { once: true }), |
| 100 | ); |
| 101 | }); |
| 102 | const queue = createQueue({ |
| 103 | upload, |
| 104 | onChange: (next) => { |
| 105 | states = next; |
| 106 | }, |
| 107 | }); |
| 108 | const local = request("offline-draft"); |
| 109 | queue.sync([local]); |
| 110 | await started.promise; |
| 111 | queue.sync([]); |
| 112 | await queue.settled(); |
| 113 | expect(signal?.aborted).toBe(true); |
| 114 | expect(states).toEqual({}); |
| 115 | upload.mockResolvedValueOnce("uploaded"); |
| 116 | queue.sync([local]); |
| 117 | await queue.settled(); |
| 118 | expect(upload.mock.calls[1]?.[0]).toBe(local); |
| 119 | expect(states[uploadKey(environmentId, local.attachment.id)]).toEqual({ |
| 120 | status: "ready", |
| 121 | }); |
| 122 | expect(local.attachment).toMatchObject({ fileUri: "file:///documents/offline-draft.pdf" }); |
| 123 | queue.dispose(); |
| 124 | }); |
| 125 | |
| 126 | it("retries a failed attachment when the worker restores requests after reconnect", async () => { |
| 127 | let states: Readonly<Record<string, AttachmentUploadStatus>> = {}; |
| 128 | const upload = vi |
| 129 | .fn() |
| 130 | .mockRejectedValueOnce(new Error("Disconnected")) |
| 131 | .mockResolvedValue("uploaded"); |
| 132 | const queue = createQueue({ |
| 133 | upload, |
| 134 | onChange: (next) => { |
| 135 | states = next; |
| 136 | }, |
| 137 | }); |
| 138 | const local = request("failed-upload"); |
| 139 | const key = uploadKey(environmentId, local.attachment.id); |
| 140 | queue.sync([local]); |
| 141 | await queue.settled(); |
| 142 | expect(states[key]?.status).toBe("failed"); |
| 143 | queue.sync([local]); |
| 144 | await queue.settled(); |
| 145 | expect(upload).toHaveBeenCalledTimes(1); |
| 146 | queue.sync([]); |
| 147 | queue.sync([local]); |
| 148 | await queue.settled(); |
| 149 | expect(upload).toHaveBeenCalledTimes(2); |
| 150 | expect(states[key]?.status).toBe("ready"); |
| 151 | queue.dispose(); |
| 152 | }); |
| 153 | |
| 154 | it("ignores a late completion after removal or environment switch", async () => { |
| 155 | const gate = Promise.withResolvers<string>(); |
| 156 | const started = Promise.withResolvers<void>(); |
| 157 | let states: Readonly<Record<string, AttachmentUploadStatus>> = {}; |
| 158 | const upload = vi.fn(async () => { |
| 159 | started.resolve(); |
| 160 | return gate.promise; |
| 161 | }); |
| 162 | const queue = createQueue({ |
| 163 | upload, |
| 164 | onChange: (next) => { |
| 165 | states = next; |
| 166 | }, |
| 167 | }); |
| 168 | queue.sync([request("photo")]); |
| 169 | await started.promise; |
| 170 | upload.mockResolvedValueOnce("uploaded"); |
| 171 | const other = EnvironmentId.make("environment-2"); |
| 172 | queue.sync([request("photo", other)]); |
| 173 | gate.resolve("uploaded"); |
| 174 | await queue.settled(); |
| 175 | expect(states).toEqual({ [uploadKey(other, "photo")]: { status: "ready" } }); |
| 176 | queue.sync([]); |
| 177 | expect(states).toEqual({}); |
| 178 | queue.dispose(); |
| 179 | }); |
| 180 | |
| 181 | it("keeps failures stable until retry and reports bounded progress", async () => { |
| 182 | let states: Readonly<Record<string, AttachmentUploadStatus>> = {}; |
| 183 | const progress: number[] = []; |
| 184 | const upload = vi.fn( |
| 185 | async ( |
| 186 | _request: UploadRequest, |
| 187 | _signal: AbortSignal, |
| 188 | report: (value: number) => void, |
| 189 | ): Promise<string> => { |
| 190 | report(0.12); |
| 191 | report(0.13); |
| 192 | report(1.1); |
| 193 | throw new Error("Server unavailable"); |
| 194 | }, |
| 195 | ); |
| 196 | const queue = createQueue({ |
| 197 | upload, |
| 198 | onChange: (next) => { |
| 199 | states = next; |
| 200 | const state = next[uploadKey(environmentId, "file")]; |
| 201 | if (state?.status === "uploading") progress.push(state.progress); |
| 202 | }, |
| 203 | }); |
| 204 | queue.sync([request("file")]); |
| 205 | await queue.settled(); |
| 206 | queue.sync([request("file")]); |
| 207 | expect(upload).toHaveBeenCalledOnce(); |
| 208 | expect(states[uploadKey(environmentId, "file")]).toEqual({ |
| 209 | status: "failed", |
| 210 | reason: "Server unavailable", |
| 211 | }); |
| 212 | expect(progress).toEqual([0, 0.1, 1]); |
| 213 | upload.mockImplementationOnce(async (_request, _signal, report) => { |
| 214 | report(0.5); |
| 215 | return "uploaded"; |
| 216 | }); |
| 217 | queue.retry(uploadKey(environmentId, "file")); |
| 218 | expect(states[uploadKey(environmentId, "file")]).toEqual({ |
| 219 | status: "uploading", |
| 220 | progress: 0.5, |
| 221 | retrying: true, |
| 222 | }); |
| 223 | await queue.settled(); |
| 224 | expect(states[uploadKey(environmentId, "file")]).toEqual({ status: "ready" }); |
| 225 | queue.dispose(); |
| 226 | }); |
| 227 | |
| 228 | it.effect("joins a send to the running upload instead of uploading again", () => |
| 229 | Effect.gen(function* () { |
| 230 | const gate = Promise.withResolvers<string>(); |
| 231 | const started = Promise.withResolvers<void>(); |
| 232 | const uploaded = vi.fn(); |
| 233 | const upload = vi.fn(async () => { |
| 234 | started.resolve(); |
| 235 | return gate.promise; |
| 236 | }); |
| 237 | let states: Readonly<Record<string, AttachmentUploadStatus>> = {}; |
| 238 | const queue = createQueue({ |
| 239 | upload, |
| 240 | onUploaded: uploaded, |
| 241 | onChange: (next) => { |
| 242 | states = next; |
| 243 | }, |
| 244 | }); |
| 245 | const local = request("video"); |
| 246 | queue.sync([local]); |
| 247 | yield* Effect.promise(() => started.promise); |
| 248 | // Sending hands the attachment from the draft to the outbox, which uploads it too. |
| 249 | const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true }); |
| 250 | queue.sync([]); |
| 251 | gate.resolve("uploaded-1"); |
| 252 | expect(yield* Fiber.join(sent)).toBe("uploaded-1"); |
| 253 | expect(upload).toHaveBeenCalledOnce(); |
| 254 | expect(uploaded).toHaveBeenCalledExactlyOnceWith(local, "uploaded-1"); |
| 255 | yield* Effect.promise(() => queue.settled()); |
| 256 | expect(states).toEqual({}); |
| 257 | queue.dispose(); |
| 258 | }), |
| 259 | ); |
| 260 | it.effect("restarts a failed upload when a send joins it instead of rejoining the failure", () => |
| 261 | Effect.gen(function* () { |
| 262 | const replacement = Promise.withResolvers<string>(); |
| 263 | const upload = vi |
| 264 | .fn() |
| 265 | .mockRejectedValueOnce(new Error("Server unavailable")) |
| 266 | .mockImplementationOnce(() => replacement.promise); |
| 267 | const queue = createQueue({ upload, onChange: () => {} }); |
| 268 | const local = request("photo"); |
| 269 | const key = uploadKey(environmentId, local.attachment.id); |
| 270 | const release = queue.hold(local); |
| 271 | yield* Effect.promise(() => queue.settled()); |
| 272 | expect(queue.status(key)?.status).toBe("failed"); |
| 273 | |
| 274 | const sent = yield* Effect.forkChild(queue.upload(local), { startImmediately: true }); |
| 275 | expect(queue.status(key)).toEqual({ status: "uploading", progress: 0, retrying: true }); |
| 276 | replacement.resolve("uploaded-2"); |
| 277 | expect(yield* Fiber.join(sent)).toBe("uploaded-2"); |
| 278 | expect(upload).toHaveBeenCalledTimes(2); |
| 279 | release(); |
| 280 | queue.dispose(); |
| 281 | }), |
| 282 | ); |
| 283 | |
| 284 | it.effect("keeps a replacement upload's status when the upload it replaced is released", () => |
| 285 | Effect.gen(function* () { |
| 286 | const replacement = Promise.withResolvers<string>(); |
| 287 | const upload = vi |
| 288 | .fn() |
| 289 | .mockRejectedValueOnce(new Error("Server unavailable")) |
| 290 | .mockImplementationOnce(() => replacement.promise); |
| 291 | let statuses: Readonly<Record<string, AttachmentUploadStatus>> = {}; |
| 292 | const queue = createQueue({ |
| 293 | upload, |
| 294 | onChange: (next) => { |
| 295 | statuses = next; |
| 296 | }, |
| 297 | }); |
| 298 | const local = request("photo"); |
| 299 | const releaseFailed = queue.hold(local); |
| 300 | yield* Effect.promise(() => queue.settled()); |
| 301 | queue.invalidate(uploadKey(environmentId, "photo")); |
| 302 | const releaseReplacement = queue.hold(local); |
| 303 | releaseFailed(); |
| 304 | expect(statuses[uploadKey(environmentId, "photo")]).toEqual({ |
| 305 | status: "uploading", |
| 306 | progress: 0, |
| 307 | }); |
| 308 | replacement.resolve("uploaded-2"); |
| 309 | yield* Effect.promise(() => queue.settled()); |
| 310 | expect(statuses[uploadKey(environmentId, "photo")]).toEqual({ status: "ready" }); |
| 311 | releaseReplacement(); |
| 312 | queue.dispose(); |
| 313 | }), |
| 314 | ); |
| 315 | }); |