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 * as DocumentText from "../provider/DocumentText.ts"; |
| 2 | // @effect-diagnostics nodeBuiltinImport:off |
| 3 | import * as NodeFS from "node:fs"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | import { expect, it } from "@effect/vitest"; |
| 7 | import { |
| 8 | ChatAttachmentId, |
| 9 | CommandId, |
| 10 | EventId, |
| 11 | GuestMessageId, |
| 12 | MessageId, |
| 13 | RunId, |
| 14 | RuntimeRequestId, |
| 15 | ThreadId, |
| 16 | TurnItemId, |
| 17 | type OrchestrationV2Command, |
| 18 | type OrchestrationV2ServerCommand, |
| 19 | type OrchestrationV2StoredEvent, |
| 20 | type UserInputAttachments, |
| 21 | } from "@t3tools/contracts"; |
| 22 | import * as DateTime from "effect/DateTime"; |
| 23 | import * as Effect from "effect/Effect"; |
| 24 | import * as Layer from "effect/Layer"; |
| 25 | import { |
| 26 | createAttachmentId, |
| 27 | createPendingAttachmentId, |
| 28 | resolveAttachmentPath, |
| 29 | } from "../attachmentStore.ts"; |
| 30 | import * as ServerConfig from "../config.ts"; |
| 31 | import * as ServerSettings from "../serverSettings.ts"; |
| 32 | import { |
| 33 | OrchestratorCommandPreviouslyRejectedError, |
| 34 | OrchestratorDispatchError, |
| 35 | } from "./Orchestrator.ts"; |
| 36 | import * as ThreadManagementService from "./ThreadManagementService.ts"; |
| 37 | import * as ThreadMessageIntake from "./ThreadMessageIntake.ts"; |
| 38 | |
| 39 | const dispatchCommand = (command: OrchestrationV2Command, sender = { managesProviders: true }) => |
| 40 | ThreadMessageIntake.dispatchCommand(command, sender); |
| 41 | |
| 42 | const layerIntakeTest = ServerConfig.layerTest(process.cwd(), { |
| 43 | prefix: "t3-question-intake-", |
| 44 | }).pipe( |
| 45 | Layer.provideMerge(DocumentText.layer.pipe(Layer.provideMerge(NodeServices.layer))), |
| 46 | Layer.provideMerge(ServerSettings.layerTest()), |
| 47 | ); |
| 48 | |
| 49 | const layerFailingDispatchHandler = |
| 50 | (captured: OrchestrationV2ServerCommand[]) => (command: OrchestrationV2ServerCommand) => { |
| 51 | captured.push(command); |
| 52 | return Effect.fail( |
| 53 | new OrchestratorDispatchError({ |
| 54 | commandId: command.commandId, |
| 55 | commandType: command.type, |
| 56 | cause: "Stop after intake", |
| 57 | }), |
| 58 | ); |
| 59 | }; |
| 60 | |
| 61 | const layerFailingDispatch = (captured: OrchestrationV2ServerCommand[]) => |
| 62 | Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 63 | dispatch: layerFailingDispatchHandler(captured), |
| 64 | }); |
| 65 | |
| 66 | const NOW = DateTime.makeUnsafe("2026-09-13T00:00:00.000Z"); |
| 67 | |
| 68 | const answeredRespondEvents = (input: { |
| 69 | readonly threadId: ThreadId; |
| 70 | readonly requestId: string; |
| 71 | readonly attachmentsByQuestionId: UserInputAttachments; |
| 72 | }): OrchestrationV2StoredEvent[] => [ |
| 73 | { |
| 74 | sequence: 1, |
| 75 | commandId: null, |
| 76 | event: { |
| 77 | id: EventId.make("evt-recorded-answer"), |
| 78 | type: "turn-item.updated", |
| 79 | threadId: input.threadId, |
| 80 | occurredAt: NOW, |
| 81 | payload: { |
| 82 | id: TurnItemId.make("item-recorded-answer"), |
| 83 | type: "user_input_request", |
| 84 | threadId: input.threadId, |
| 85 | runId: null, |
| 86 | nodeId: null, |
| 87 | providerThreadId: null, |
| 88 | providerTurnId: null, |
| 89 | nativeItemRef: null, |
| 90 | parentItemId: null, |
| 91 | ordinal: 1, |
| 92 | status: "completed", |
| 93 | title: null, |
| 94 | startedAt: null, |
| 95 | completedAt: NOW, |
| 96 | updatedAt: NOW, |
| 97 | requestId: RuntimeRequestId.make(input.requestId), |
| 98 | questions: [], |
| 99 | questionAnswer: { |
| 100 | requestId: input.requestId, |
| 101 | answers: {}, |
| 102 | attachmentsByQuestionId: input.attachmentsByQuestionId, |
| 103 | }, |
| 104 | }, |
| 105 | }, |
| 106 | }, |
| 107 | ]; |
| 108 | |
| 109 | it.effect("claims question uploads and passes readable paths through the V2 request command", () => |
| 110 | Effect.gen(function* () { |
| 111 | const config = yield* ServerConfig.ServerConfig; |
| 112 | const id = ChatAttachmentId.make(createPendingAttachmentId()!); |
| 113 | NodeFS.writeFileSync( |
| 114 | NodePath.join(config.attachmentsDir, `${id}.png`), |
| 115 | new Uint8Array([1, 2, 3]), |
| 116 | ); |
| 117 | const captured: OrchestrationV2ServerCommand[] = []; |
| 118 | yield* dispatchCommand({ |
| 119 | type: "runtime-request.respond", |
| 120 | commandId: CommandId.make("answer-1"), |
| 121 | threadId: ThreadId.make("thread-answer"), |
| 122 | requestId: RuntimeRequestId.make("request-1"), |
| 123 | answers: { q: ["Selected option"] }, |
| 124 | attachmentsByQuestionId: { |
| 125 | q: [{ type: "image", id, name: "screen.png", mimeType: "image/png", sizeBytes: 3 }], |
| 126 | }, |
| 127 | }).pipe(Effect.provide(layerFailingDispatch(captured)), Effect.result); |
| 128 | expect(captured).toHaveLength(1); |
| 129 | const command = captured[0]!; |
| 130 | expect(command.type).toBe("runtime-request.respond"); |
| 131 | if (command.type !== "runtime-request.respond") return; |
| 132 | const attachment = command.attachmentsByQuestionId?.q?.[0]; |
| 133 | expect(attachment).toBeDefined(); |
| 134 | const path = resolveAttachmentPath({ |
| 135 | attachmentsDir: config.attachmentsDir, |
| 136 | attachment: attachment!, |
| 137 | }); |
| 138 | expect(path).not.toBeNull(); |
| 139 | expect(NodeFS.readFileSync(path!)).toEqual(Buffer.from([1, 2, 3])); |
| 140 | expect(command.answers?.q).toEqual([ |
| 141 | "Selected option", |
| 142 | `Attached image "screen.png": ${JSON.stringify(path)}`, |
| 143 | ]); |
| 144 | }).pipe(Effect.provide(layerIntakeTest)), |
| 145 | ); |
| 146 | |
| 147 | it.effect("retains claimed copies when dispatch failure may have been accepted", () => |
| 148 | Effect.gen(function* () { |
| 149 | const config = yield* ServerConfig.ServerConfig; |
| 150 | const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!); |
| 151 | NodeFS.writeFileSync( |
| 152 | NodePath.join(config.attachmentsDir, `${pendingId}.png`), |
| 153 | new Uint8Array([1, 2, 3]), |
| 154 | ); |
| 155 | const captured: OrchestrationV2ServerCommand[] = []; |
| 156 | const result = yield* dispatchCommand({ |
| 157 | type: "runtime-request.respond", |
| 158 | commandId: CommandId.make("answer-ambiguous"), |
| 159 | threadId: ThreadId.make("thread-ambiguous"), |
| 160 | requestId: RuntimeRequestId.make("request-ambiguous"), |
| 161 | answers: { q: ["one"] }, |
| 162 | attachmentsByQuestionId: { |
| 163 | q: [ |
| 164 | { |
| 165 | type: "image", |
| 166 | id: pendingId, |
| 167 | name: "screen.png", |
| 168 | mimeType: "image/png", |
| 169 | sizeBytes: 3, |
| 170 | }, |
| 171 | ], |
| 172 | }, |
| 173 | }).pipe(Effect.provide(layerFailingDispatch(captured)), Effect.result); |
| 174 | expect(result._tag).toBe("Failure"); |
| 175 | expect(captured).toHaveLength(1); |
| 176 | const threadFiles = NodeFS.readdirSync(config.attachmentsDir).filter((entry) => |
| 177 | entry.startsWith("thread-ambiguous-"), |
| 178 | ); |
| 179 | expect(threadFiles).toHaveLength(1); |
| 180 | expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true); |
| 181 | }).pipe(Effect.provide(layerIntakeTest)), |
| 182 | ); |
| 183 | |
| 184 | it.effect("releases claimed copies when the command was already rejected", () => |
| 185 | Effect.gen(function* () { |
| 186 | const config = yield* ServerConfig.ServerConfig; |
| 187 | const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!); |
| 188 | NodeFS.writeFileSync( |
| 189 | NodePath.join(config.attachmentsDir, `${pendingId}.png`), |
| 190 | new Uint8Array([1, 2, 3]), |
| 191 | ); |
| 192 | const result = yield* dispatchCommand({ |
| 193 | type: "runtime-request.respond", |
| 194 | commandId: CommandId.make("answer-rejected"), |
| 195 | threadId: ThreadId.make("thread-rejected"), |
| 196 | requestId: RuntimeRequestId.make("request-rejected"), |
| 197 | answers: { q: ["one"] }, |
| 198 | attachmentsByQuestionId: { |
| 199 | q: [ |
| 200 | { |
| 201 | type: "image", |
| 202 | id: pendingId, |
| 203 | name: "screen.png", |
| 204 | mimeType: "image/png", |
| 205 | sizeBytes: 3, |
| 206 | }, |
| 207 | ], |
| 208 | }, |
| 209 | }).pipe( |
| 210 | Effect.provide( |
| 211 | Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 212 | dispatch: (command) => |
| 213 | Effect.fail( |
| 214 | new OrchestratorCommandPreviouslyRejectedError({ |
| 215 | commandId: command.commandId, |
| 216 | commandType: command.type, |
| 217 | detail: "rejected earlier", |
| 218 | }), |
| 219 | ), |
| 220 | }), |
| 221 | ), |
| 222 | Effect.result, |
| 223 | ); |
| 224 | expect(result._tag).toBe("Failure"); |
| 225 | expect( |
| 226 | NodeFS.readdirSync(config.attachmentsDir).filter((entry) => |
| 227 | entry.startsWith("thread-rejected-"), |
| 228 | ), |
| 229 | ).toEqual([]); |
| 230 | expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true); |
| 231 | }).pipe(Effect.provide(layerIntakeTest)), |
| 232 | ); |
| 233 | |
| 234 | it.effect("releases claimed copies when dispatch replays an earlier accepted response", () => |
| 235 | Effect.gen(function* () { |
| 236 | const config = yield* ServerConfig.ServerConfig; |
| 237 | const threadId = ThreadId.make("thread-replay"); |
| 238 | const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!); |
| 239 | NodeFS.writeFileSync( |
| 240 | NodePath.join(config.attachmentsDir, `${pendingId}.png`), |
| 241 | new Uint8Array([1, 2, 3]), |
| 242 | ); |
| 243 | const earlierAttachment = { |
| 244 | type: "image" as const, |
| 245 | id: ChatAttachmentId.make("thread-replay-00000000-0000-4000-8000-0000000000aa"), |
| 246 | name: "screen.png", |
| 247 | mimeType: "image/png", |
| 248 | sizeBytes: 3, |
| 249 | }; |
| 250 | NodeFS.writeFileSync( |
| 251 | NodePath.join(config.attachmentsDir, `${earlierAttachment.id}.png`), |
| 252 | new Uint8Array([4, 4, 4]), |
| 253 | ); |
| 254 | const storedEvents = answeredRespondEvents({ |
| 255 | threadId, |
| 256 | requestId: "request-replay", |
| 257 | attachmentsByQuestionId: { q: [earlierAttachment] }, |
| 258 | }); |
| 259 | const result = yield* dispatchCommand({ |
| 260 | type: "runtime-request.respond", |
| 261 | commandId: CommandId.make("answer-replay"), |
| 262 | threadId, |
| 263 | requestId: RuntimeRequestId.make("request-replay"), |
| 264 | answers: { q: ["one"] }, |
| 265 | attachmentsByQuestionId: { |
| 266 | q: [ |
| 267 | { |
| 268 | type: "image", |
| 269 | id: pendingId, |
| 270 | name: "screen.png", |
| 271 | mimeType: "image/png", |
| 272 | sizeBytes: 3, |
| 273 | }, |
| 274 | ], |
| 275 | }, |
| 276 | }).pipe( |
| 277 | Effect.provide( |
| 278 | Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 279 | dispatch: () => Effect.succeed({ sequence: 1, storedEvents }), |
| 280 | }), |
| 281 | ), |
| 282 | Effect.result, |
| 283 | ); |
| 284 | expect(result._tag).toBe("Success"); |
| 285 | expect( |
| 286 | NodeFS.readdirSync(config.attachmentsDir).filter((entry) => |
| 287 | entry.startsWith("thread-replay-"), |
| 288 | ), |
| 289 | ).toEqual([`${earlierAttachment.id}.png`]); |
| 290 | expect( |
| 291 | NodeFS.readFileSync(NodePath.join(config.attachmentsDir, `${earlierAttachment.id}.png`)), |
| 292 | ).toEqual(Buffer.from([4, 4, 4])); |
| 293 | expect(NodeFS.existsSync(NodePath.join(config.attachmentsDir, `${pendingId}.png`))).toBe(true); |
| 294 | }).pipe(Effect.provide(layerIntakeTest)), |
| 295 | ); |
| 296 | |
| 297 | it.effect("a retried response re-claims the preserved pending uploads", () => |
| 298 | Effect.gen(function* () { |
| 299 | const config = yield* ServerConfig.ServerConfig; |
| 300 | const threadId = ThreadId.make("thread-retry"); |
| 301 | const pendingId = ChatAttachmentId.make(createPendingAttachmentId()!); |
| 302 | const lateId = ChatAttachmentId.make(createPendingAttachmentId()!); |
| 303 | NodeFS.writeFileSync( |
| 304 | NodePath.join(config.attachmentsDir, `${pendingId}.png`), |
| 305 | new Uint8Array([1, 2, 3]), |
| 306 | ); |
| 307 | const command: OrchestrationV2Command = { |
| 308 | type: "runtime-request.respond", |
| 309 | commandId: CommandId.make("answer-retry"), |
| 310 | threadId, |
| 311 | requestId: RuntimeRequestId.make("request-retry"), |
| 312 | answers: { q1: ["one"], q2: ["two"] }, |
| 313 | attachmentsByQuestionId: { |
| 314 | q1: [ |
| 315 | { |
| 316 | type: "image", |
| 317 | id: pendingId, |
| 318 | name: "screen.png", |
| 319 | mimeType: "image/png", |
| 320 | sizeBytes: 3, |
| 321 | }, |
| 322 | ], |
| 323 | q2: [ |
| 324 | { |
| 325 | type: "image", |
| 326 | id: lateId, |
| 327 | name: "late.png", |
| 328 | mimeType: "image/png", |
| 329 | sizeBytes: 2, |
| 330 | }, |
| 331 | ], |
| 332 | }, |
| 333 | }; |
| 334 | const captured: OrchestrationV2ServerCommand[] = []; |
| 335 | const first = yield* dispatchCommand(command).pipe( |
| 336 | Effect.provide(layerFailingDispatch(captured)), |
| 337 | Effect.result, |
| 338 | ); |
| 339 | expect(first._tag).toBe("Failure"); |
| 340 | expect(captured).toHaveLength(0); |
| 341 | |
| 342 | NodeFS.writeFileSync( |
| 343 | NodePath.join(config.attachmentsDir, `${lateId}.png`), |
| 344 | new Uint8Array([7, 7]), |
| 345 | ); |
| 346 | const retry = yield* dispatchCommand(command).pipe( |
| 347 | Effect.provide( |
| 348 | Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 349 | dispatch: (dispatched) => { |
| 350 | captured.push(dispatched); |
| 351 | return Effect.succeed({ sequence: 1, storedEvents: [] }); |
| 352 | }, |
| 353 | }), |
| 354 | ), |
| 355 | Effect.result, |
| 356 | ); |
| 357 | expect(retry._tag).toBe("Success"); |
| 358 | const dispatched = captured[0]!; |
| 359 | expect(dispatched.type).toBe("runtime-request.respond"); |
| 360 | if (dispatched.type !== "runtime-request.respond") return; |
| 361 | for (const attachment of [ |
| 362 | ...(dispatched.attachmentsByQuestionId?.q1 ?? []), |
| 363 | ...(dispatched.attachmentsByQuestionId?.q2 ?? []), |
| 364 | ]) { |
| 365 | const path = resolveAttachmentPath({ |
| 366 | attachmentsDir: config.attachmentsDir, |
| 367 | attachment, |
| 368 | }); |
| 369 | expect(path).not.toBeNull(); |
| 370 | expect(NodeFS.existsSync(path!)).toBe(true); |
| 371 | } |
| 372 | expect( |
| 373 | NodeFS.readdirSync(config.attachmentsDir).filter((entry) => |
| 374 | entry.startsWith("thread-retry-"), |
| 375 | ), |
| 376 | ).toHaveLength(2); |
| 377 | }).pipe(Effect.provide(layerIntakeTest)), |
| 378 | ); |
| 379 | |
| 380 | it.effect("sends a guest message with the guest's files, never ones the client names", () => |
| 381 | Effect.gen(function* () { |
| 382 | const config = yield* ServerConfig.ServerConfig; |
| 383 | const guestFile = { |
| 384 | type: "image" as const, |
| 385 | id: ChatAttachmentId.make(createAttachmentId("thread-guest")!), |
| 386 | name: "screen.png", |
| 387 | mimeType: "image/png", |
| 388 | sizeBytes: 3, |
| 389 | }; |
| 390 | NodeFS.writeFileSync( |
| 391 | NodePath.join(config.attachmentsDir, `${guestFile.id}.png`), |
| 392 | new Uint8Array([1, 2, 3]), |
| 393 | ); |
| 394 | const guestMessageId = GuestMessageId.make("guest-message:files"); |
| 395 | const captured: OrchestrationV2ServerCommand[] = []; |
| 396 | const layerGuestShare = Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 397 | dispatch: layerFailingDispatchHandler(captured), |
| 398 | // Only the share is read. |
| 399 | getThreadSnapshotWindow: () => |
| 400 | Effect.succeed({ |
| 401 | projection: { |
| 402 | thread: { |
| 403 | guestShare: { |
| 404 | messages: [{ id: guestMessageId, status: "pending", attachments: [guestFile] }], |
| 405 | }, |
| 406 | }, |
| 407 | }, |
| 408 | } as unknown as Effect.Success< |
| 409 | ReturnType< |
| 410 | ThreadManagementService.ThreadManagementServiceShape["getThreadSnapshotWindow"] |
| 411 | > |
| 412 | >), |
| 413 | }); |
| 414 | yield* dispatchCommand({ |
| 415 | type: "message.dispatch", |
| 416 | commandId: CommandId.make("send-guest-message"), |
| 417 | threadId: ThreadId.make("thread-guest"), |
| 418 | messageId: MessageId.make("message-guest"), |
| 419 | guestMessageId, |
| 420 | text: "", |
| 421 | attachments: [{ ...guestFile, id: ChatAttachmentId.make("thread-guest-owner-file") }], |
| 422 | dispatchMode: { type: "queue_after_active" }, |
| 423 | createdBy: "user", |
| 424 | creationSource: "web", |
| 425 | }).pipe(Effect.provide(layerGuestShare), Effect.result); |
| 426 | const command = captured[0]; |
| 427 | if (command?.type !== "message.dispatch") throw new Error("Expected a message dispatch."); |
| 428 | expect(command.attachments).toHaveLength(1); |
| 429 | // The guest's file, which is already the thread's. |
| 430 | expect(command.attachments).toEqual([guestFile]); |
| 431 | }).pipe(Effect.provide(layerIntakeTest)), |
| 432 | ); |
| 433 | |
| 434 | it.effect("refuses a queued edit that keeps a file another edit already deleted", () => |
| 435 | Effect.gen(function* () { |
| 436 | const config = yield* ServerConfig.ServerConfig; |
| 437 | const storedFile = (name: string) => ({ |
| 438 | type: "image" as const, |
| 439 | id: ChatAttachmentId.make(createAttachmentId("thread-queued-edit")!), |
| 440 | name, |
| 441 | mimeType: "image/png", |
| 442 | sizeBytes: 3, |
| 443 | }); |
| 444 | const kept = storedFile("kept.png"); |
| 445 | const deleted = storedFile("deleted.png"); |
| 446 | NodeFS.writeFileSync( |
| 447 | NodePath.join(config.attachmentsDir, `${kept.id}.png`), |
| 448 | new Uint8Array([1, 2, 3]), |
| 449 | ); |
| 450 | const captured: OrchestrationV2ServerCommand[] = []; |
| 451 | const edit = (attachments: ReadonlyArray<typeof kept>) => |
| 452 | dispatchCommand({ |
| 453 | type: "queued-run.edit", |
| 454 | commandId: CommandId.make(`queued-edit-${attachments.length}`), |
| 455 | threadId: ThreadId.make("thread-queued-edit"), |
| 456 | runId: RunId.make("run-queued-edit"), |
| 457 | text: "Edited from a stale view.", |
| 458 | attachments, |
| 459 | }).pipe( |
| 460 | Effect.provide( |
| 461 | Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 462 | dispatch: layerFailingDispatchHandler(captured), |
| 463 | hasReceipt: (commandId) => |
| 464 | Effect.succeed(captured.some((command) => command.commandId === commandId)), |
| 465 | }), |
| 466 | ), |
| 467 | Effect.result, |
| 468 | ); |
| 469 | |
| 470 | yield* edit([kept]); |
| 471 | expect(captured).toHaveLength(1); |
| 472 | const result = yield* edit([kept, deleted]); |
| 473 | expect(result._tag).toBe("Failure"); |
| 474 | expect(captured).toHaveLength(1); |
| 475 | // A retry of the accepted edit replays its receipt even after a later cleanup removed its file. |
| 476 | NodeFS.rmSync(NodePath.join(config.attachmentsDir, `${kept.id}.png`)); |
| 477 | yield* edit([kept]); |
| 478 | expect(captured).toHaveLength(2); |
| 479 | }).pipe(Effect.provide(layerIntakeTest)), |
| 480 | ); |
| 481 | |
| 482 | it.effect("a queued /logout edit without provider management must keep an attachment", () => |
| 483 | Effect.gen(function* () { |
| 484 | const threadId = ThreadId.make("thread-sign-out-edit"); |
| 485 | const image = { |
| 486 | type: "image" as const, |
| 487 | id: ChatAttachmentId.make(createAttachmentId(threadId)!), |
| 488 | name: "shot.png", |
| 489 | mimeType: "image/png", |
| 490 | sizeBytes: 3, |
| 491 | }; |
| 492 | const config = yield* ServerConfig.ServerConfig; |
| 493 | NodeFS.writeFileSync( |
| 494 | NodePath.join(config.attachmentsDir, `${image.id}.png`), |
| 495 | new Uint8Array([1, 2, 3]), |
| 496 | ); |
| 497 | const captured: OrchestrationV2ServerCommand[] = []; |
| 498 | // The edit omits `attachments`, as an older client's does, so the queued ones stay. |
| 499 | const edit = (run: string, managesProviders: boolean) => |
| 500 | dispatchCommand( |
| 501 | { |
| 502 | type: "queued-run.edit", |
| 503 | commandId: CommandId.make(`edit-${run}-${managesProviders}`), |
| 504 | threadId, |
| 505 | runId: RunId.make(run), |
| 506 | text: " /LOGOUT ", |
| 507 | }, |
| 508 | { managesProviders }, |
| 509 | ).pipe( |
| 510 | Effect.provide( |
| 511 | Layer.mock(ThreadManagementService.ThreadManagementService)({ |
| 512 | dispatch: layerFailingDispatchHandler(captured), |
| 513 | hasReceipt: (commandId) => Effect.succeed(commandId === "edit-replayed-false"), |
| 514 | getThreadRecords: () => |
| 515 | Effect.succeed({ |
| 516 | runs: ["attached", "bare"].map((id) => ({ |
| 517 | id: RunId.make(id), |
| 518 | status: "queued", |
| 519 | userMessageId: MessageId.make(id), |
| 520 | })), |
| 521 | } as never), |
| 522 | getMessagesById: (_threadId, [id]) => |
| 523 | Effect.succeed( |
| 524 | new Map([[id, { attachments: id === "attached" ? [image] : [] }]]) as never, |
| 525 | ), |
| 526 | }), |
| 527 | ), |
| 528 | Effect.flip, |
| 529 | ); |
| 530 | |
| 531 | expect((yield* edit("bare", false))._tag).toBe("EnvironmentAuthorizationError"); |
| 532 | expect((yield* edit("started", false))._tag).toBe("EnvironmentAuthorizationError"); |
| 533 | expect(captured).toEqual([]); |
| 534 | yield* edit("attached", false); |
| 535 | yield* edit("bare", true); |
| 536 | // A retry of an accepted edit reaches its receipt after its run left the queue. |
| 537 | yield* edit("replayed", false); |
| 538 | // The kept files are pinned, so an edit racing this one cannot strip them afterwards. |
| 539 | expect(captured).toMatchObject([ |
| 540 | { runId: "attached", attachments: [image] }, |
| 541 | { runId: "bare" }, |
| 542 | { runId: "replayed" }, |
| 543 | ]); |
| 544 | expect(captured[1]).not.toHaveProperty("attachments"); |
| 545 | expect(captured[2]).not.toHaveProperty("attachments"); |
| 546 | }).pipe(Effect.provide(layerIntakeTest)), |
| 547 | ); |