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 NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import { expect, it } from "@effect/vitest"; |
| 5 | import { |
| 6 | EnvironmentId, |
| 7 | PreviewAutomationClientDisconnectedError, |
| 8 | PreviewAutomationMalformedResponseError, |
| 9 | PreviewAutomationNoAvailableHostError, |
| 10 | PreviewTabId, |
| 11 | ProviderInstanceId, |
| 12 | ThreadId, |
| 13 | type PreviewAutomationHost, |
| 14 | type PreviewAutomationRequest, |
| 15 | type PreviewAutomationStreamEvent, |
| 16 | SERVER_BROWSER_AUTOMATION_CLIENT_ID, |
| 17 | } from "@t3tools/contracts"; |
| 18 | import * as Effect from "effect/Effect"; |
| 19 | import * as Deferred from "effect/Deferred"; |
| 20 | import * as Fiber from "effect/Fiber"; |
| 21 | import * as Queue from "effect/Queue"; |
| 22 | import * as Result from "effect/Result"; |
| 23 | import * as Stream from "effect/Stream"; |
| 24 | import * as TestClock from "effect/testing/TestClock"; |
| 25 | |
| 26 | import { PreviewBrowserRootError } from "../preview/PreviewBrowserHost.ts"; |
| 27 | import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts"; |
| 28 | |
| 29 | const makeBroker = PreviewAutomationBroker.make.pipe( |
| 30 | Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)), |
| 31 | ); |
| 32 | |
| 33 | const scope = { |
| 34 | environmentId: EnvironmentId.make("environment-1"), |
| 35 | requestNamespace: "provider-session-1", |
| 36 | thread: { |
| 37 | threadId: ThreadId.make("thread-1"), |
| 38 | providerSessionId: "provider-session-1", |
| 39 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 40 | }, |
| 41 | client: undefined, |
| 42 | capabilities: new Set(["preview"] as const), |
| 43 | issuedAt: 1, |
| 44 | }; |
| 45 | |
| 46 | const makeHost = (overrides: Partial<PreviewAutomationHost> = {}): PreviewAutomationHost => ({ |
| 47 | clientId: "client-1", |
| 48 | environmentId: scope.environmentId, |
| 49 | ...overrides, |
| 50 | }); |
| 51 | |
| 52 | type RoutedRequest = PreviewAutomationRequest & { |
| 53 | readonly connectionId: PreviewAutomationStreamEvent["connectionId"]; |
| 54 | }; |
| 55 | |
| 56 | const requestsFrom = ( |
| 57 | events: Stream.Stream<PreviewAutomationStreamEvent>, |
| 58 | onConnected: (connectionId: PreviewAutomationStreamEvent["connectionId"]) => void = () => {}, |
| 59 | ): Stream.Stream<RoutedRequest> => |
| 60 | events.pipe( |
| 61 | Stream.filterMap((event) => { |
| 62 | if (event.type === "connected") { |
| 63 | onConnected(event.connectionId); |
| 64 | return Result.failVoid; |
| 65 | } |
| 66 | return Result.succeed({ ...event.request, connectionId: event.connectionId }); |
| 67 | }), |
| 68 | ); |
| 69 | |
| 70 | it.effect("atomically registers a connected host and correlates its response", () => |
| 71 | Effect.gen(function* () { |
| 72 | const broker = yield* makeBroker; |
| 73 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 74 | yield* Stream.runForEach(requests, (request) => |
| 75 | broker.respond({ |
| 76 | clientId: "client-1", |
| 77 | connectionId: request.connectionId, |
| 78 | requestId: request.requestId, |
| 79 | ok: true, |
| 80 | result: { available: true }, |
| 81 | }), |
| 82 | ).pipe(Effect.forkScoped); |
| 83 | yield* Effect.yieldNow; |
| 84 | |
| 85 | const result = yield* broker.invoke<{ available: boolean }>({ |
| 86 | scope, |
| 87 | operation: "open", |
| 88 | input: {}, |
| 89 | }); |
| 90 | |
| 91 | expect(result).toEqual({ available: true }); |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | it.effect("targets multiple tabs explicitly while retaining a default tab", () => |
| 96 | Effect.gen(function* () { |
| 97 | const broker = yield* makeBroker; |
| 98 | const appTabId = PreviewTabId.make("tab-web-app"); |
| 99 | const simulatorTabId = PreviewTabId.make("tab-ios-simulator"); |
| 100 | const openedTabIds = [appTabId, simulatorTabId]; |
| 101 | let openIndex = 0; |
| 102 | const routedRequests: RoutedRequest[] = []; |
| 103 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 104 | yield* Stream.runForEach(requests, (request) => { |
| 105 | routedRequests.push(request); |
| 106 | return broker.respond({ |
| 107 | clientId: "client-1", |
| 108 | connectionId: request.connectionId, |
| 109 | requestId: request.requestId, |
| 110 | ok: true, |
| 111 | result: |
| 112 | request.operation === "open" |
| 113 | ? { available: true, tabId: openedTabIds[openIndex++] } |
| 114 | : { url: "http://localhost:3200" }, |
| 115 | }); |
| 116 | }).pipe(Effect.forkScoped); |
| 117 | yield* Effect.yieldNow; |
| 118 | |
| 119 | yield* broker.invoke({ scope, operation: "open", input: { reuseExistingTab: false } }); |
| 120 | yield* broker.invoke({ scope, operation: "open", input: { reuseExistingTab: false } }); |
| 121 | yield* broker.invoke({ scope, operation: "snapshot", input: {} }); |
| 122 | yield* broker.invoke({ scope, operation: "snapshot", input: {}, tabId: appTabId }); |
| 123 | yield* broker.invoke({ scope, operation: "snapshot", input: {} }); |
| 124 | |
| 125 | expect(routedRequests).toHaveLength(5); |
| 126 | expect(routedRequests[0]?.tabId).toBeUndefined(); |
| 127 | expect(routedRequests[1]?.tabId).toBe(appTabId); |
| 128 | expect(routedRequests[2]?.tabId).toBe(simulatorTabId); |
| 129 | expect(routedRequests[2]?.tabIdExplicit).toBe(false); |
| 130 | expect(routedRequests[3]?.tabId).toBe(appTabId); |
| 131 | expect(routedRequests[3]?.tabIdExplicit).toBe(true); |
| 132 | expect(routedRequests[4]?.tabId).toBe(appTabId); |
| 133 | }), |
| 134 | ); |
| 135 | |
| 136 | it.effect.each([true, false])( |
| 137 | "keeps an older target stable while a newer explicit tab responds (implicit: %s)", |
| 138 | (implicit) => |
| 139 | Effect.gen(function* () { |
| 140 | const broker = yield* makeBroker; |
| 141 | const olderTabId = PreviewTabId.make("tab-older-request"); |
| 142 | const newerTabId = PreviewTabId.make("tab-newer-request"); |
| 143 | const releaseOlderResponse = yield* Deferred.make<void>(); |
| 144 | const routedRequests: RoutedRequest[] = []; |
| 145 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 146 | yield* Stream.runForEach(requests, (request) => { |
| 147 | routedRequests.push(request); |
| 148 | const response = Effect.gen(function* () { |
| 149 | if (request.tabId === olderTabId && request.operation === "snapshot") { |
| 150 | yield* Deferred.await(releaseOlderResponse); |
| 151 | } |
| 152 | yield* broker.respond({ |
| 153 | clientId: "client-1", |
| 154 | connectionId: request.connectionId, |
| 155 | requestId: request.requestId, |
| 156 | ok: true, |
| 157 | result: { url: "http://localhost:3200" }, |
| 158 | }); |
| 159 | if (request.tabId === newerTabId) { |
| 160 | yield* Deferred.succeed(releaseOlderResponse, undefined); |
| 161 | } |
| 162 | }); |
| 163 | return response.pipe(Effect.forkScoped, Effect.asVoid); |
| 164 | }).pipe(Effect.forkScoped); |
| 165 | yield* Effect.yieldNow; |
| 166 | |
| 167 | yield* broker.invoke({ scope, operation: "status", input: {}, tabId: olderTabId }); |
| 168 | let capturedTabId: PreviewTabId | undefined; |
| 169 | const older = yield* broker |
| 170 | .invoke({ |
| 171 | scope, |
| 172 | operation: "snapshot", |
| 173 | input: {}, |
| 174 | ...(implicit ? {} : { tabId: olderTabId }), |
| 175 | onTargetTab: (tabId) => { |
| 176 | capturedTabId = tabId; |
| 177 | }, |
| 178 | }) |
| 179 | .pipe(Effect.forkScoped); |
| 180 | yield* Effect.yieldNow; |
| 181 | const newer = yield* broker |
| 182 | .invoke({ scope, operation: "snapshot", input: {}, tabId: newerTabId }) |
| 183 | .pipe(Effect.forkScoped); |
| 184 | yield* Fiber.join(newer); |
| 185 | yield* Fiber.join(older); |
| 186 | yield* broker.invoke({ |
| 187 | scope, |
| 188 | operation: "status", |
| 189 | input: {}, |
| 190 | tabId: olderTabId, |
| 191 | updateCurrentTab: false, |
| 192 | }); |
| 193 | yield* broker.invoke({ scope, operation: "snapshot", input: {} }); |
| 194 | |
| 195 | expect(routedRequests.at(-1)?.tabId).toBe(newerTabId); |
| 196 | expect(capturedTabId).toBe(olderTabId); |
| 197 | }), |
| 198 | ); |
| 199 | |
| 200 | it.effect("does not let a no-tab response suppress an earlier tab decision", () => |
| 201 | Effect.gen(function* () { |
| 202 | const broker = yield* makeBroker; |
| 203 | const initialTabId = PreviewTabId.make("tab-initial"); |
| 204 | const openedTabId = PreviewTabId.make("tab-opened-late"); |
| 205 | const releaseOpenResponse = yield* Deferred.make<void>(); |
| 206 | const routedRequests: RoutedRequest[] = []; |
| 207 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 208 | yield* Stream.runForEach(requests, (request) => { |
| 209 | routedRequests.push(request); |
| 210 | const marker = |
| 211 | typeof request.input === "object" && request.input !== null && "marker" in request.input |
| 212 | ? request.input.marker |
| 213 | : undefined; |
| 214 | const response = Effect.gen(function* () { |
| 215 | if (marker === "older") { |
| 216 | yield* Deferred.await(releaseOpenResponse); |
| 217 | } |
| 218 | yield* broker.respond({ |
| 219 | clientId: "client-1", |
| 220 | connectionId: request.connectionId, |
| 221 | requestId: request.requestId, |
| 222 | ok: true, |
| 223 | result: |
| 224 | request.operation === "open" |
| 225 | ? { available: true, tabId: marker === "older" ? openedTabId : initialTabId } |
| 226 | : { url: "http://localhost:3200" }, |
| 227 | }); |
| 228 | if (marker === "newer") { |
| 229 | yield* Deferred.succeed(releaseOpenResponse, undefined); |
| 230 | } |
| 231 | }); |
| 232 | return response.pipe(Effect.forkScoped, Effect.asVoid); |
| 233 | }).pipe(Effect.forkScoped); |
| 234 | yield* Effect.yieldNow; |
| 235 | |
| 236 | yield* broker.invoke({ scope, operation: "open", input: {} }); |
| 237 | const older = yield* broker |
| 238 | .invoke({ |
| 239 | scope, |
| 240 | operation: "open", |
| 241 | input: { marker: "older", reuseExistingTab: false }, |
| 242 | }) |
| 243 | .pipe(Effect.forkScoped); |
| 244 | yield* Effect.yieldNow; |
| 245 | const newer = yield* broker |
| 246 | .invoke({ scope, operation: "snapshot", input: { marker: "newer" } }) |
| 247 | .pipe(Effect.forkScoped); |
| 248 | yield* Fiber.join(newer); |
| 249 | yield* Fiber.join(older); |
| 250 | yield* broker.invoke({ scope, operation: "snapshot", input: {} }); |
| 251 | |
| 252 | expect(routedRequests.at(-1)?.tabId).toBe(openedTabId); |
| 253 | }), |
| 254 | ); |
| 255 | |
| 256 | it.effect("announces a live replacement stream before delivering requests", () => |
| 257 | Effect.gen(function* () { |
| 258 | const broker = yield* makeBroker; |
| 259 | const events = yield* broker.connect(makeHost()); |
| 260 | const receivedTypes: PreviewAutomationStreamEvent["type"][] = []; |
| 261 | const consumer = yield* events.pipe( |
| 262 | Stream.take(2), |
| 263 | Stream.runForEach((event) => { |
| 264 | receivedTypes.push(event.type); |
| 265 | return event.type === "connected" |
| 266 | ? Effect.void |
| 267 | : broker.respond({ |
| 268 | clientId: "client-1", |
| 269 | connectionId: event.connectionId, |
| 270 | requestId: event.request.requestId, |
| 271 | ok: true, |
| 272 | result: "ready", |
| 273 | }); |
| 274 | }), |
| 275 | Effect.forkScoped, |
| 276 | ); |
| 277 | yield* Effect.yieldNow; |
| 278 | |
| 279 | const result = yield* broker.invoke<string>({ scope, operation: "status", input: {} }); |
| 280 | yield* Fiber.join(consumer); |
| 281 | |
| 282 | expect(receivedTypes).toEqual(["connected", "request"]); |
| 283 | expect(result).toBe("ready"); |
| 284 | }), |
| 285 | ); |
| 286 | |
| 287 | it.effect.each([ |
| 288 | { |
| 289 | clientId: SERVER_BROWSER_AUTOMATION_CLIENT_ID, |
| 290 | shown: true, |
| 291 | message: "page.goto: net::ERR_CONNECTION_REFUSED at http://localhost:4719/", |
| 292 | }, |
| 293 | { |
| 294 | clientId: "client-1", |
| 295 | shown: false, |
| 296 | message: "page.goto: net::ERR_CONNECTION_REFUSED at http://localhost:4719/", |
| 297 | }, |
| 298 | { |
| 299 | clientId: SERVER_BROWSER_AUTOMATION_CLIENT_ID, |
| 300 | shown: true, |
| 301 | message: new PreviewBrowserRootError().message, |
| 302 | }, |
| 303 | ])("preserves server browser failure guidance ($clientId)", ({ clientId, shown, message }) => |
| 304 | Effect.gen(function* () { |
| 305 | const broker = yield* makeBroker; |
| 306 | const requests = requestsFrom(yield* broker.connect(makeHost({ clientId }))); |
| 307 | yield* Stream.runForEach(requests, (request) => |
| 308 | broker.respond({ |
| 309 | clientId, |
| 310 | connectionId: request.connectionId, |
| 311 | requestId: request.requestId, |
| 312 | ok: false, |
| 313 | error: { |
| 314 | _tag: "PreviewAutomationExecutionError", |
| 315 | message, |
| 316 | }, |
| 317 | }), |
| 318 | ).pipe(Effect.forkScoped); |
| 319 | yield* Effect.yieldNow; |
| 320 | const error = yield* broker |
| 321 | .invoke<void>({ scope, operation: "open", input: {} }) |
| 322 | .pipe(Effect.flip); |
| 323 | expect(error._tag).toBe("PreviewAutomationExecutionError"); |
| 324 | expect(error.message.includes(message)).toBe(shown); |
| 325 | }), |
| 326 | ); |
| 327 | |
| 328 | it.effect("names the click target that matched nothing when the server browser times out", () => |
| 329 | Effect.gen(function* () { |
| 330 | const broker = yield* makeBroker; |
| 331 | const clientId = SERVER_BROWSER_AUTOMATION_CLIENT_ID; |
| 332 | const requests = requestsFrom(yield* broker.connect(makeHost({ clientId }))); |
| 333 | yield* Stream.runForEach(requests, (request) => |
| 334 | broker.respond({ |
| 335 | clientId, |
| 336 | connectionId: request.connectionId, |
| 337 | requestId: request.requestId, |
| 338 | ok: false, |
| 339 | error: { |
| 340 | _tag: "PreviewAutomationTimeoutError", |
| 341 | message: "No element matches the target.", |
| 342 | detail: { reason: 'No element matches ".definitely-no-such-t3-target".' }, |
| 343 | }, |
| 344 | }), |
| 345 | ).pipe(Effect.forkScoped); |
| 346 | yield* Effect.yieldNow; |
| 347 | |
| 348 | const error = yield* broker |
| 349 | .invoke<void>({ |
| 350 | scope, |
| 351 | operation: "click", |
| 352 | input: { selector: ".definitely-no-such-t3-target", timeoutMs: 15_000 }, |
| 353 | }) |
| 354 | .pipe(Effect.flip); |
| 355 | |
| 356 | expect(error._tag).toBe("PreviewAutomationTimeoutError"); |
| 357 | expect(error.message).toBe( |
| 358 | 'Preview automation click timed out after 15000ms. No element matches ".definitely-no-such-t3-target".', |
| 359 | ); |
| 360 | }), |
| 361 | ); |
| 362 | |
| 363 | it.effect("distinguishes malformed remote failures", () => |
| 364 | Effect.gen(function* () { |
| 365 | const broker = yield* makeBroker; |
| 366 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 367 | yield* Stream.runForEach(requests, (request) => |
| 368 | broker.respond({ |
| 369 | clientId: "client-1", |
| 370 | connectionId: request.connectionId, |
| 371 | requestId: request.requestId, |
| 372 | ok: false, |
| 373 | }), |
| 374 | ).pipe(Effect.forkScoped); |
| 375 | yield* Effect.yieldNow; |
| 376 | |
| 377 | const error = yield* broker |
| 378 | .invoke<void>({ scope, operation: "status", input: {}, timeoutMs: 2_000 }) |
| 379 | .pipe(Effect.flip); |
| 380 | |
| 381 | expect(error).toBeInstanceOf(PreviewAutomationMalformedResponseError); |
| 382 | expect(error).toMatchObject({ |
| 383 | operation: "status", |
| 384 | environmentId: scope.environmentId, |
| 385 | threadId: scope.thread.threadId, |
| 386 | providerSessionId: scope.thread.providerSessionId, |
| 387 | providerInstanceId: scope.thread.providerInstanceId, |
| 388 | clientId: "client-1", |
| 389 | requestId: "preview-0", |
| 390 | timeoutMs: 2_000, |
| 391 | }); |
| 392 | }), |
| 393 | ); |
| 394 | |
| 395 | it.effect("reports the capture deadline and pending stage instead of the request timeout", () => |
| 396 | Effect.scoped( |
| 397 | Effect.gen(function* () { |
| 398 | const broker = yield* makeBroker; |
| 399 | const connected = yield* Deferred.make<void>(); |
| 400 | const requests = requestsFrom( |
| 401 | (yield* broker.connect(makeHost())).pipe( |
| 402 | Stream.tap((event) => |
| 403 | event.type === "connected" ? Deferred.succeed(connected, undefined) : Effect.void, |
| 404 | ), |
| 405 | ), |
| 406 | ); |
| 407 | yield* Stream.runForEach(requests, (request) => |
| 408 | broker.respond({ |
| 409 | clientId: "client-1", |
| 410 | connectionId: request.connectionId, |
| 411 | requestId: request.requestId, |
| 412 | ok: false, |
| 413 | error: { |
| 414 | _tag: "PreviewAutomationTimeoutError", |
| 415 | message: "Snapshot capture did not complete.", |
| 416 | detail: { timeoutMs: 10_000, pendingStages: ["screenshot"] }, |
| 417 | }, |
| 418 | }), |
| 419 | ).pipe(Effect.forkScoped); |
| 420 | yield* Deferred.await(connected); |
| 421 | const error = yield* broker |
| 422 | .invoke<void>({ scope, operation: "snapshot", input: {} }) |
| 423 | .pipe(Effect.flip); |
| 424 | expect(error).toMatchObject({ timeoutMs: 10_000, pendingStages: ["screenshot"] }); |
| 425 | expect(error.message).toBe( |
| 426 | "Preview automation snapshot timed out after 10000ms. Waiting for: screenshot.", |
| 427 | ); |
| 428 | }), |
| 429 | ), |
| 430 | ); |
| 431 | |
| 432 | it.effect("does not create host state from focus updates without a live stream", () => |
| 433 | Effect.gen(function* () { |
| 434 | const broker = yield* makeBroker; |
| 435 | yield* broker.focusHost({ |
| 436 | clientId: "client-1", |
| 437 | environmentId: scope.environmentId, |
| 438 | connectionId: "connection-missing", |
| 439 | focused: true, |
| 440 | }); |
| 441 | |
| 442 | const error = yield* broker |
| 443 | .invoke<void>({ scope, operation: "status", input: {} }) |
| 444 | .pipe(Effect.flip); |
| 445 | expect(error).toBeInstanceOf(PreviewAutomationNoAvailableHostError); |
| 446 | }), |
| 447 | ); |
| 448 | |
| 449 | it.effect("removes host availability when the authoritative request stream disconnects", () => |
| 450 | Effect.gen(function* () { |
| 451 | const broker = yield* makeBroker; |
| 452 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 453 | const beforeAcquisition = yield* broker |
| 454 | .invoke<void>({ scope, operation: "status", input: {} }) |
| 455 | .pipe(Effect.flip); |
| 456 | expect(beforeAcquisition).toBeInstanceOf(PreviewAutomationNoAvailableHostError); |
| 457 | |
| 458 | const consumer = yield* Stream.runDrain(requests).pipe(Effect.forkScoped); |
| 459 | yield* Effect.yieldNow; |
| 460 | yield* Fiber.interrupt(consumer); |
| 461 | |
| 462 | const error = yield* broker |
| 463 | .invoke<void>({ scope, operation: "status", input: {} }) |
| 464 | .pipe(Effect.flip); |
| 465 | expect(error).toBeInstanceOf(PreviewAutomationNoAvailableHostError); |
| 466 | }), |
| 467 | ); |
| 468 | |
| 469 | it.effect("routes requests for background threads through an environment-level host", () => |
| 470 | Effect.gen(function* () { |
| 471 | const broker = yield* makeBroker; |
| 472 | const backgroundThreadId = ThreadId.make("thread-background"); |
| 473 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 474 | let routedThreadId: string | undefined; |
| 475 | yield* Stream.runForEach(requests, (request) => { |
| 476 | routedThreadId = request.threadId; |
| 477 | return broker.respond({ |
| 478 | clientId: "client-1", |
| 479 | connectionId: request.connectionId, |
| 480 | requestId: request.requestId, |
| 481 | ok: true, |
| 482 | result: "background", |
| 483 | }); |
| 484 | }).pipe(Effect.forkScoped); |
| 485 | yield* Effect.yieldNow; |
| 486 | |
| 487 | const result = yield* broker.invoke<string>({ |
| 488 | scope: { |
| 489 | ...scope, |
| 490 | thread: { |
| 491 | ...scope.thread, |
| 492 | threadId: backgroundThreadId, |
| 493 | providerSessionId: "provider-session-background", |
| 494 | }, |
| 495 | }, |
| 496 | operation: "status", |
| 497 | input: {}, |
| 498 | }); |
| 499 | |
| 500 | expect(result).toBe("background"); |
| 501 | expect(routedThreadId).toBe(backgroundThreadId); |
| 502 | }), |
| 503 | ); |
| 504 | |
| 505 | it.effect("never routes a provider session to a host from another environment", () => |
| 506 | Effect.gen(function* () { |
| 507 | const broker = yield* makeBroker; |
| 508 | const matchingRequests = requestsFrom( |
| 509 | yield* broker.connect(makeHost({ clientId: "client-matching" })), |
| 510 | ); |
| 511 | const foreignRequests = requestsFrom( |
| 512 | yield* broker.connect( |
| 513 | makeHost({ |
| 514 | clientId: "client-foreign", |
| 515 | environmentId: EnvironmentId.make("environment-foreign"), |
| 516 | }), |
| 517 | ), |
| 518 | ); |
| 519 | yield* Stream.runForEach(matchingRequests, (request) => |
| 520 | broker.respond({ |
| 521 | clientId: "client-matching", |
| 522 | connectionId: request.connectionId, |
| 523 | requestId: request.requestId, |
| 524 | ok: true, |
| 525 | result: "matching", |
| 526 | }), |
| 527 | ).pipe(Effect.forkScoped); |
| 528 | yield* Stream.runForEach(foreignRequests, (request) => |
| 529 | broker.respond({ |
| 530 | clientId: "client-foreign", |
| 531 | connectionId: request.connectionId, |
| 532 | requestId: request.requestId, |
| 533 | ok: true, |
| 534 | result: "foreign", |
| 535 | }), |
| 536 | ).pipe(Effect.forkScoped); |
| 537 | yield* Effect.yieldNow; |
| 538 | |
| 539 | expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe( |
| 540 | "matching", |
| 541 | ); |
| 542 | }), |
| 543 | ); |
| 544 | |
| 545 | it.effect("pins a provider session to its initial host despite later focus changes", () => |
| 546 | Effect.gen(function* () { |
| 547 | const broker = yield* makeBroker; |
| 548 | let firstConnectionId = ""; |
| 549 | let secondConnectionId = ""; |
| 550 | const firstRequests = requestsFrom( |
| 551 | yield* broker.connect(makeHost({ clientId: "client-first" })), |
| 552 | (connectionId) => { |
| 553 | firstConnectionId = connectionId; |
| 554 | }, |
| 555 | ); |
| 556 | const secondRequests = requestsFrom( |
| 557 | yield* broker.connect(makeHost({ clientId: "client-second" })), |
| 558 | (connectionId) => { |
| 559 | secondConnectionId = connectionId; |
| 560 | }, |
| 561 | ); |
| 562 | yield* Stream.runForEach(firstRequests, (request) => |
| 563 | broker.respond({ |
| 564 | clientId: "client-first", |
| 565 | connectionId: request.connectionId, |
| 566 | requestId: request.requestId, |
| 567 | ok: true, |
| 568 | result: "first", |
| 569 | }), |
| 570 | ).pipe(Effect.forkScoped); |
| 571 | yield* Stream.runForEach(secondRequests, (request) => |
| 572 | broker.respond({ |
| 573 | clientId: "client-second", |
| 574 | connectionId: request.connectionId, |
| 575 | requestId: request.requestId, |
| 576 | ok: true, |
| 577 | result: "second", |
| 578 | }), |
| 579 | ).pipe(Effect.forkScoped); |
| 580 | yield* Effect.yieldNow; |
| 581 | |
| 582 | yield* broker.focusHost({ |
| 583 | clientId: "client-first", |
| 584 | environmentId: scope.environmentId, |
| 585 | connectionId: "connection-stale", |
| 586 | focused: true, |
| 587 | liveTabs: [{ threadId: scope.thread.threadId, tabId: PreviewTabId.make("stale-tab") }], |
| 588 | }); |
| 589 | expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe("second"); |
| 590 | yield* broker.focusHost({ |
| 591 | clientId: "client-first", |
| 592 | environmentId: scope.environmentId, |
| 593 | connectionId: firstConnectionId, |
| 594 | focused: true, |
| 595 | }); |
| 596 | |
| 597 | const firstPinnedScope = { |
| 598 | ...scope, |
| 599 | thread: { ...scope.thread, providerSessionId: "provider-session-first-pinned" }, |
| 600 | }; |
| 601 | expect( |
| 602 | yield* broker.invoke<string>({ scope: firstPinnedScope, operation: "status", input: {} }), |
| 603 | ).toBe("first"); |
| 604 | |
| 605 | yield* broker.focusHost({ |
| 606 | clientId: "client-second", |
| 607 | environmentId: scope.environmentId, |
| 608 | connectionId: secondConnectionId, |
| 609 | focused: true, |
| 610 | }); |
| 611 | |
| 612 | expect( |
| 613 | yield* broker.invoke<string>({ scope: firstPinnedScope, operation: "status", input: {} }), |
| 614 | ).toBe("first"); |
| 615 | expect( |
| 616 | yield* broker.invoke<string>({ |
| 617 | scope: { |
| 618 | ...scope, |
| 619 | thread: { ...scope.thread, providerSessionId: "provider-session-second-pinned" }, |
| 620 | }, |
| 621 | operation: "status", |
| 622 | input: {}, |
| 623 | }), |
| 624 | ).toBe("second"); |
| 625 | }), |
| 626 | ); |
| 627 | |
| 628 | it.effect("prefers the live tab owner for new sessions without moving existing leases", () => |
| 629 | Effect.gen(function* () { |
| 630 | const broker = yield* makeBroker; |
| 631 | const connections = new Map<string, string>(); |
| 632 | for (const clientId of ["owner", "other"]) { |
| 633 | const requests = requestsFrom(yield* broker.connect(makeHost({ clientId })), (connectionId) => |
| 634 | connections.set(clientId, connectionId), |
| 635 | ); |
| 636 | yield* Stream.runForEach(requests, (request) => |
| 637 | broker.respond({ |
| 638 | clientId, |
| 639 | connectionId: request.connectionId, |
| 640 | requestId: request.requestId, |
| 641 | ok: true, |
| 642 | result: clientId, |
| 643 | }), |
| 644 | ).pipe(Effect.forkScoped); |
| 645 | } |
| 646 | yield* Effect.yieldNow; |
| 647 | yield* broker.focusHost({ |
| 648 | clientId: "owner", |
| 649 | environmentId: scope.environmentId, |
| 650 | connectionId: connections.get("owner")!, |
| 651 | focused: false, |
| 652 | liveTabs: [ |
| 653 | { threadId: scope.thread.threadId, tabId: PreviewTabId.make("signed-in"), visible: true }, |
| 654 | ], |
| 655 | }); |
| 656 | yield* broker.focusHost({ |
| 657 | clientId: "other", |
| 658 | environmentId: scope.environmentId, |
| 659 | connectionId: connections.get("other")!, |
| 660 | focused: true, |
| 661 | liveTabs: [ |
| 662 | { |
| 663 | threadId: scope.thread.threadId, |
| 664 | tabId: PreviewTabId.make("signed-in"), |
| 665 | visible: false, |
| 666 | }, |
| 667 | { |
| 668 | threadId: ThreadId.make("another-thread"), |
| 669 | tabId: PreviewTabId.make("different-tab"), |
| 670 | visible: true, |
| 671 | }, |
| 672 | ], |
| 673 | }); |
| 674 | expect(yield* broker.invoke<string>({ scope, operation: "evaluate", input: {} })).toBe("owner"); |
| 675 | expect( |
| 676 | yield* broker.invoke<string>({ |
| 677 | scope: { ...scope, thread: { ...scope.thread, providerSessionId: "explicit-owner" } }, |
| 678 | tabId: PreviewTabId.make("signed-in"), |
| 679 | operation: "snapshot", |
| 680 | input: {}, |
| 681 | }), |
| 682 | ).toBe("owner"); |
| 683 | expect( |
| 684 | yield* broker.invoke<string>({ |
| 685 | scope: { ...scope, thread: { ...scope.thread, providerSessionId: "other-tab" } }, |
| 686 | tabId: PreviewTabId.make("different-tab"), |
| 687 | operation: "evaluate", |
| 688 | input: {}, |
| 689 | }), |
| 690 | ).toBe("other"); |
| 691 | |
| 692 | yield* broker.focusHost({ |
| 693 | clientId: "owner", |
| 694 | environmentId: scope.environmentId, |
| 695 | connectionId: connections.get("owner")!, |
| 696 | focused: false, |
| 697 | liveTabs: [], |
| 698 | }); |
| 699 | expect(yield* broker.invoke<string>({ scope, operation: "evaluate", input: {} })).toBe("owner"); |
| 700 | expect( |
| 701 | yield* broker.invoke<string>({ |
| 702 | scope: { ...scope, thread: { ...scope.thread, providerSessionId: "after-tab-closed" } }, |
| 703 | operation: "evaluate", |
| 704 | input: {}, |
| 705 | }), |
| 706 | ).toBe("other"); |
| 707 | }), |
| 708 | ); |
| 709 | |
| 710 | it.effect("routes resize to a capable host instead of a newer legacy connection", () => |
| 711 | Effect.gen(function* () { |
| 712 | const broker = yield* makeBroker; |
| 713 | const capableRequests = requestsFrom( |
| 714 | yield* broker.connect( |
| 715 | makeHost({ clientId: "client-capable", supportedOperations: ["resize"] }), |
| 716 | ), |
| 717 | ); |
| 718 | const legacyRequests = requestsFrom( |
| 719 | yield* broker.connect(makeHost({ clientId: "client-legacy" })), |
| 720 | ); |
| 721 | yield* Stream.runForEach(capableRequests, (request) => |
| 722 | broker.respond({ |
| 723 | clientId: "client-capable", |
| 724 | connectionId: request.connectionId, |
| 725 | requestId: request.requestId, |
| 726 | ok: true, |
| 727 | result: "capable", |
| 728 | }), |
| 729 | ).pipe(Effect.forkScoped); |
| 730 | yield* Stream.runForEach(legacyRequests, (request) => |
| 731 | broker.respond({ |
| 732 | clientId: "client-legacy", |
| 733 | connectionId: request.connectionId, |
| 734 | requestId: request.requestId, |
| 735 | ok: true, |
| 736 | result: "legacy", |
| 737 | }), |
| 738 | ).pipe(Effect.forkScoped); |
| 739 | yield* Effect.yieldNow; |
| 740 | |
| 741 | expect( |
| 742 | yield* broker.invoke<string>({ scope, operation: "resize", input: { mode: "fill" } }), |
| 743 | ).toBe("capable"); |
| 744 | }), |
| 745 | ); |
| 746 | |
| 747 | it.effect("does not move a live legacy assignment to another runtime for resize", () => |
| 748 | Effect.gen(function* () { |
| 749 | const broker = yield* makeBroker; |
| 750 | const legacyRequests = requestsFrom( |
| 751 | yield* broker.connect(makeHost({ clientId: "client-legacy" })), |
| 752 | ); |
| 753 | yield* Stream.runForEach(legacyRequests, (request) => |
| 754 | broker.respond({ |
| 755 | clientId: "client-legacy", |
| 756 | connectionId: request.connectionId, |
| 757 | requestId: request.requestId, |
| 758 | ok: true, |
| 759 | result: "legacy", |
| 760 | }), |
| 761 | ).pipe(Effect.forkScoped); |
| 762 | yield* Effect.yieldNow; |
| 763 | |
| 764 | expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe("legacy"); |
| 765 | |
| 766 | const capableRequests = requestsFrom( |
| 767 | yield* broker.connect( |
| 768 | makeHost({ clientId: "client-capable", supportedOperations: ["resize"] }), |
| 769 | ), |
| 770 | ); |
| 771 | yield* Stream.runForEach(capableRequests, (request) => |
| 772 | broker.respond({ |
| 773 | clientId: "client-capable", |
| 774 | connectionId: request.connectionId, |
| 775 | requestId: request.requestId, |
| 776 | ok: true, |
| 777 | result: "capable", |
| 778 | }), |
| 779 | ).pipe(Effect.forkScoped); |
| 780 | yield* Effect.yieldNow; |
| 781 | |
| 782 | const error = yield* broker |
| 783 | .invoke<void>({ scope, operation: "resize", input: { mode: "fill" } }) |
| 784 | .pipe(Effect.flip); |
| 785 | expect(error).toBeInstanceOf(PreviewAutomationNoAvailableHostError); |
| 786 | expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe("legacy"); |
| 787 | }), |
| 788 | ); |
| 789 | |
| 790 | it.effect("ignores stale focus updates for a different environment", () => |
| 791 | Effect.gen(function* () { |
| 792 | const broker = yield* makeBroker; |
| 793 | let firstConnectionId = ""; |
| 794 | const firstRequests = requestsFrom( |
| 795 | yield* broker.connect(makeHost({ clientId: "client-first" })), |
| 796 | (connectionId) => { |
| 797 | firstConnectionId = connectionId; |
| 798 | }, |
| 799 | ); |
| 800 | const secondRequests = requestsFrom( |
| 801 | yield* broker.connect(makeHost({ clientId: "client-second" })), |
| 802 | ); |
| 803 | yield* Stream.runForEach(firstRequests, (request) => |
| 804 | broker.respond({ |
| 805 | clientId: "client-first", |
| 806 | connectionId: request.connectionId, |
| 807 | requestId: request.requestId, |
| 808 | ok: true, |
| 809 | result: "first", |
| 810 | }), |
| 811 | ).pipe(Effect.forkScoped); |
| 812 | yield* Stream.runForEach(secondRequests, (request) => |
| 813 | broker.respond({ |
| 814 | clientId: "client-second", |
| 815 | connectionId: request.connectionId, |
| 816 | requestId: request.requestId, |
| 817 | ok: true, |
| 818 | result: "second", |
| 819 | }), |
| 820 | ).pipe(Effect.forkScoped); |
| 821 | yield* Effect.yieldNow; |
| 822 | |
| 823 | yield* broker.focusHost({ |
| 824 | clientId: "client-first", |
| 825 | environmentId: EnvironmentId.make("environment-stale"), |
| 826 | connectionId: firstConnectionId, |
| 827 | focused: true, |
| 828 | }); |
| 829 | |
| 830 | expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe("second"); |
| 831 | }), |
| 832 | ); |
| 833 | |
| 834 | it.effect("fails over a pinned provider session only after its host disconnects", () => |
| 835 | Effect.gen(function* () { |
| 836 | const broker = yield* makeBroker; |
| 837 | const firstTabId = PreviewTabId.make("tab-on-first-host"); |
| 838 | let firstConnectionId = ""; |
| 839 | let secondRoutedTabId: PreviewTabId | undefined; |
| 840 | const firstRequests = requestsFrom( |
| 841 | yield* broker.connect(makeHost({ clientId: "client-first" })), |
| 842 | (connectionId) => { |
| 843 | firstConnectionId = connectionId; |
| 844 | }, |
| 845 | ); |
| 846 | const secondRequests = requestsFrom( |
| 847 | yield* broker.connect(makeHost({ clientId: "client-second" })), |
| 848 | ); |
| 849 | const firstConsumer = yield* Stream.runForEach(firstRequests, (request) => |
| 850 | broker.respond({ |
| 851 | clientId: "client-first", |
| 852 | connectionId: request.connectionId, |
| 853 | requestId: request.requestId, |
| 854 | ok: true, |
| 855 | result: request.operation === "open" ? { host: "first", tabId: firstTabId } : "first", |
| 856 | }), |
| 857 | ).pipe(Effect.forkScoped); |
| 858 | yield* Stream.runForEach(secondRequests, (request) => { |
| 859 | secondRoutedTabId = request.tabId; |
| 860 | return broker.respond({ |
| 861 | clientId: "client-second", |
| 862 | connectionId: request.connectionId, |
| 863 | requestId: request.requestId, |
| 864 | ok: true, |
| 865 | result: "second", |
| 866 | }); |
| 867 | }).pipe(Effect.forkScoped); |
| 868 | yield* Effect.yieldNow; |
| 869 | |
| 870 | yield* broker.focusHost({ |
| 871 | clientId: "client-first", |
| 872 | environmentId: scope.environmentId, |
| 873 | connectionId: firstConnectionId, |
| 874 | focused: true, |
| 875 | liveTabs: [{ threadId: scope.thread.threadId, tabId: firstTabId }], |
| 876 | }); |
| 877 | expect(yield* broker.invoke({ scope, operation: "open", input: {} })).toEqual({ |
| 878 | host: "first", |
| 879 | tabId: firstTabId, |
| 880 | }); |
| 881 | |
| 882 | yield* Fiber.interrupt(firstConsumer); |
| 883 | yield* Effect.yieldNow; |
| 884 | |
| 885 | expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe("second"); |
| 886 | expect(secondRoutedTabId).toBeUndefined(); |
| 887 | }), |
| 888 | ); |
| 889 | |
| 890 | it.effect("keeps a replacement stream authoritative when the old stream finalizes", () => |
| 891 | Effect.gen(function* () { |
| 892 | const broker = yield* makeBroker; |
| 893 | let firstConnectionId = ""; |
| 894 | let replacementConnectionId = ""; |
| 895 | const firstRequests = requestsFrom(yield* broker.connect(makeHost()), (connectionId) => { |
| 896 | firstConnectionId = connectionId; |
| 897 | }); |
| 898 | yield* Stream.runDrain(firstRequests).pipe(Effect.forkScoped); |
| 899 | yield* Effect.yieldNow; |
| 900 | |
| 901 | const replacementRequests = requestsFrom(yield* broker.connect(makeHost()), (connectionId) => { |
| 902 | replacementConnectionId = connectionId; |
| 903 | }); |
| 904 | yield* Stream.runForEach(replacementRequests, (request) => |
| 905 | broker.respond({ |
| 906 | clientId: "client-1", |
| 907 | connectionId: request.connectionId, |
| 908 | requestId: request.requestId, |
| 909 | ok: true, |
| 910 | result: "replacement", |
| 911 | }), |
| 912 | ).pipe(Effect.forkScoped); |
| 913 | yield* Effect.yieldNow; |
| 914 | |
| 915 | expect(replacementConnectionId).not.toBe(firstConnectionId); |
| 916 | const result = yield* broker.invoke<string>({ scope, operation: "status", input: {} }); |
| 917 | expect(result).toBe("replacement"); |
| 918 | }), |
| 919 | ); |
| 920 | |
| 921 | it.effect("does not carry a tab id across a replacement automation stream", () => |
| 922 | Effect.gen(function* () { |
| 923 | const broker = yield* makeBroker; |
| 924 | const openedTabId = PreviewTabId.make("tab-first-webcontents"); |
| 925 | const firstRequests = requestsFrom(yield* broker.connect(makeHost())); |
| 926 | yield* Stream.runForEach(firstRequests, (request) => |
| 927 | broker.respond({ |
| 928 | clientId: "client-1", |
| 929 | connectionId: request.connectionId, |
| 930 | requestId: request.requestId, |
| 931 | ok: true, |
| 932 | result: |
| 933 | request.operation === "open" ? { host: "first", tabId: openedTabId } : { host: "first" }, |
| 934 | }), |
| 935 | ).pipe(Effect.forkScoped); |
| 936 | yield* Effect.yieldNow; |
| 937 | |
| 938 | expect(yield* broker.invoke({ scope, operation: "open", input: {} })).toEqual({ |
| 939 | host: "first", |
| 940 | tabId: openedTabId, |
| 941 | }); |
| 942 | |
| 943 | const routedRequests: RoutedRequest[] = []; |
| 944 | const replacementRequests = requestsFrom(yield* broker.connect(makeHost())); |
| 945 | yield* Stream.runForEach(replacementRequests, (request) => { |
| 946 | routedRequests.push(request); |
| 947 | return broker.respond({ |
| 948 | clientId: "client-1", |
| 949 | connectionId: request.connectionId, |
| 950 | requestId: request.requestId, |
| 951 | ok: true, |
| 952 | result: "replacement", |
| 953 | }); |
| 954 | }).pipe(Effect.forkScoped); |
| 955 | yield* Effect.yieldNow; |
| 956 | |
| 957 | expect(yield* broker.invoke<string>({ scope, operation: "status", input: {} })).toBe( |
| 958 | "replacement", |
| 959 | ); |
| 960 | expect(routedRequests.at(-1)?.tabId).toBeUndefined(); |
| 961 | }), |
| 962 | ); |
| 963 | |
| 964 | it.effect("fails requests assigned to the stream that is replaced", () => |
| 965 | Effect.gen(function* () { |
| 966 | const broker = yield* makeBroker; |
| 967 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 968 | yield* Stream.runDrain(requests).pipe(Effect.forkScoped); |
| 969 | const pending = yield* broker |
| 970 | .invoke<void>({ scope, operation: "status", input: {} }) |
| 971 | .pipe(Effect.flip, Effect.forkScoped); |
| 972 | yield* Effect.yieldNow; |
| 973 | |
| 974 | const replacementRequests = requestsFrom(yield* broker.connect(makeHost())); |
| 975 | yield* Stream.runDrain(replacementRequests).pipe(Effect.forkScoped); |
| 976 | |
| 977 | const error = yield* Fiber.join(pending); |
| 978 | expect(error).toBeInstanceOf(PreviewAutomationClientDisconnectedError); |
| 979 | expect(error).toMatchObject({ |
| 980 | operation: "status", |
| 981 | environmentId: scope.environmentId, |
| 982 | threadId: scope.thread.threadId, |
| 983 | providerSessionId: scope.thread.providerSessionId, |
| 984 | providerInstanceId: scope.thread.providerInstanceId, |
| 985 | clientId: "client-1", |
| 986 | requestId: "preview-0", |
| 987 | timeoutMs: 15_000, |
| 988 | }); |
| 989 | }), |
| 990 | ); |
| 991 | |
| 992 | it.effect( |
| 993 | "keeps another thread's request alive when one request times out on the shared server browser", |
| 994 | () => |
| 995 | Effect.gen(function* () { |
| 996 | const broker = yield* makeBroker; |
| 997 | const clientId = SERVER_BROWSER_AUTOMATION_CLIENT_ID; |
| 998 | const delivered = yield* Queue.unbounded<RoutedRequest>(); |
| 999 | const requests = requestsFrom( |
| 1000 | yield* broker.connect(makeHost({ clientId }), { preferred: true }), |
| 1001 | ); |
| 1002 | yield* Stream.runForEach(requests, (request) => Queue.offer(delivered, request)).pipe( |
| 1003 | Effect.forkScoped, |
| 1004 | ); |
| 1005 | yield* Effect.yieldNow; |
| 1006 | const otherScope = { |
| 1007 | ...scope, |
| 1008 | requestNamespace: "provider-session-2", |
| 1009 | thread: { |
| 1010 | threadId: ThreadId.make("thread-2"), |
| 1011 | providerSessionId: "provider-session-2", |
| 1012 | providerInstanceId: ProviderInstanceId.make("codex"), |
| 1013 | }, |
| 1014 | }; |
| 1015 | |
| 1016 | const otherWait = yield* broker |
| 1017 | .invoke<string>({ scope: otherScope, operation: "waitFor", input: {}, timeoutMs: 40_000 }) |
| 1018 | .pipe(Effect.result, Effect.forkScoped); |
| 1019 | const otherRequest = yield* Queue.take(delivered); |
| 1020 | // The browser never answers this one, as when it waits behind the thread's own running action. |
| 1021 | const stalled = yield* broker |
| 1022 | .invoke<string>({ scope, operation: "evaluate", input: {} }) |
| 1023 | .pipe(Effect.flip, Effect.forkScoped); |
| 1024 | yield* Queue.take(delivered); |
| 1025 | |
| 1026 | yield* TestClock.adjust("16 seconds"); |
| 1027 | expect((yield* Fiber.join(stalled))._tag).toBe("PreviewAutomationTimeoutError"); |
| 1028 | |
| 1029 | yield* broker.respond({ |
| 1030 | clientId, |
| 1031 | connectionId: otherRequest.connectionId, |
| 1032 | requestId: otherRequest.requestId, |
| 1033 | ok: true, |
| 1034 | result: "ready", |
| 1035 | }); |
| 1036 | expect(yield* Fiber.join(otherWait)).toEqual(Result.succeed("ready")); |
| 1037 | }), |
| 1038 | ); |
| 1039 | |
| 1040 | it.effect("accepts responses only from the host that received the request", () => |
| 1041 | Effect.gen(function* () { |
| 1042 | const broker = yield* makeBroker; |
| 1043 | const requests = requestsFrom(yield* broker.connect(makeHost())); |
| 1044 | yield* Stream.runForEach(requests, (request) => |
| 1045 | Effect.gen(function* () { |
| 1046 | yield* broker.respond({ |
| 1047 | clientId: "client-foreign", |
| 1048 | connectionId: request.connectionId, |
| 1049 | requestId: request.requestId, |
| 1050 | ok: true, |
| 1051 | result: "foreign", |
| 1052 | }); |
| 1053 | yield* broker.respond({ |
| 1054 | clientId: "client-1", |
| 1055 | connectionId: "connection-stale", |
| 1056 | requestId: request.requestId, |
| 1057 | ok: true, |
| 1058 | result: "stale", |
| 1059 | }); |
| 1060 | yield* broker.respond({ |
| 1061 | clientId: "client-1", |
| 1062 | connectionId: request.connectionId, |
| 1063 | requestId: request.requestId, |
| 1064 | ok: true, |
| 1065 | result: "owner", |
| 1066 | }); |
| 1067 | }), |
| 1068 | ).pipe(Effect.forkScoped); |
| 1069 | yield* Effect.yieldNow; |
| 1070 | |
| 1071 | const result = yield* broker.invoke<string>({ scope, operation: "status", input: {} }); |
| 1072 | expect(result).toBe("owner"); |
| 1073 | }), |
| 1074 | ); |
| 1075 | |
| 1076 | it.effect("keeps each thread's current tab when threads share a provider session", () => |
| 1077 | Effect.gen(function* () { |
| 1078 | const broker = yield* makeBroker; |
| 1079 | const routed: Array<RoutedRequest> = []; |
| 1080 | const requests = requestsFrom(yield* broker.connect(makeHost(), { preferred: true })); |
| 1081 | yield* Stream.runForEach(requests, (request) => { |
| 1082 | routed.push(request); |
| 1083 | return broker.respond({ |
| 1084 | clientId: "client-1", |
| 1085 | connectionId: request.connectionId, |
| 1086 | requestId: request.requestId, |
| 1087 | ok: true, |
| 1088 | result: { tabId: request.tabId ?? `tab-${request.threadId}` }, |
| 1089 | }); |
| 1090 | }).pipe(Effect.forkScoped); |
| 1091 | yield* Effect.yieldNow; |
| 1092 | // Codex runs every thread of an instance in one provider session. |
| 1093 | const otherThread = { |
| 1094 | ...scope, |
| 1095 | thread: { ...scope.thread, threadId: ThreadId.make("thread-2") }, |
| 1096 | }; |
| 1097 | |
| 1098 | yield* broker.invoke({ scope, operation: "open", input: {} }); |
| 1099 | yield* broker.invoke({ scope: otherThread, operation: "snapshot", input: {} }); |
| 1100 | yield* broker.invoke({ scope, operation: "snapshot", input: {} }); |
| 1101 | yield* broker.invoke({ scope: otherThread, operation: "snapshot", input: {} }); |
| 1102 | |
| 1103 | expect(routed.map(({ threadId, tabId }) => [threadId, tabId])).toEqual([ |
| 1104 | ["thread-1", undefined], |
| 1105 | ["thread-2", undefined], |
| 1106 | ["thread-1", "tab-thread-1"], |
| 1107 | ["thread-2", "tab-thread-2"], |
| 1108 | ]); |
| 1109 | expect(routed[0]!.agentSessionId).not.toBe(routed[1]!.agentSessionId); |
| 1110 | }), |
| 1111 | ); |