test(core): stabilize browser idle attachment (#50418)

Co-authored-by: rekram1-node <rekram1-node@users.noreply.github.com>
This commit is contained in:
opencode-agent[bot]
2026-09-21 14:45:33 -05:00
committed by GitHub
parent 4c944a86d7
commit cd39063622
+16 -20
View File
@@ -1,5 +1,5 @@
import { expect } from "bun:test"
import { Context, Deferred, Effect, Fiber, RcMap, Stream } from "effect"
import { Context, Effect, Fiber, RcMap, Schedule } from "effect"
import { Browser } from "@opencode/plugin-browser/rpc"
import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder"
import { LayerNode } from "@opencode/util/effect/layer-node"
@@ -51,19 +51,17 @@ it.live(
const session = yield* sessions.create({ location: ref })
const rpc = Context.get(context, Rpc.Service).client(Browser.Definition)
const attachment = { sessionID: session.id, connectionID: crypto.randomUUID() }
const attached = yield* Deferred.make<void>()
yield* rpc.events.subscribe("control").pipe(
Stream.runForEach((event) =>
event.data.type === "attached" ? Deferred.succeed(attached, undefined) : Effect.void,
),
Effect.forkScoped({ startImmediately: true }),
)
const state = { ...attachment, state: { tabs: [], focusedTabID: null } }
const pending = yield* rpc
.attach({ ...attachment, version: 4 })
.pipe(Effect.provide(locations.get(ref)), Effect.flip, Effect.forkScoped)
yield* Deferred.await(attached)
const state = { ...attachment, state: { tabs: [], focusedTabID: null } }
yield* rpc.state(state)
yield* rpc.state(state).pipe(
Effect.retry({
while: (error) => "type" in error && error.type === "unavailable",
schedule: Schedule.spaced("10 millis"),
}),
Effect.timeout("5 seconds"),
)
// Real idle cleanup must end the long-lived request, not leave a second registry behind it.
expect(yield* Fiber.join(pending).pipe(Effect.timeout("10 seconds"))).toMatchObject({ type: "rpc.unavailable" })
@@ -73,16 +71,14 @@ it.live(
const replacement = yield* locations.contextEffect(ref)
yield* Plugin.awaitActivation.pipe(Effect.provideContext(replacement))
const fresh = Context.get(replacement, Rpc.Service).client(Browser.Definition)
const restored = yield* Deferred.make<void>()
yield* fresh.events.subscribe("control").pipe(
Stream.runForEach((event) =>
event.data.type === "attached" ? Deferred.succeed(restored, undefined) : Effect.void,
),
Effect.forkScoped({ startImmediately: true }),
)
const resumed = yield* fresh.attach({ ...attachment, version: 4 }).pipe(Effect.forkScoped)
yield* Deferred.await(restored)
yield* fresh.state(state)
yield* fresh.state(state).pipe(
Effect.retry({
while: (error) => "type" in error && error.type === "unavailable",
schedule: Schedule.spaced("10 millis"),
}),
Effect.timeout("5 seconds"),
)
expect(resumed.pollUnsafe()).toBeUndefined()
yield* Fiber.interrupt(resumed)
}),