Files
oh-my-opencode/src/plugin/build-team-idle-wake-hint-client.test.ts
T
YeonGyu-Kim a43215f24e fix(plugin/event): bind team-idle-wake-hint client methods to SDK Session
The team-mode wiring at `createEventHandler` extracted
`pluginContext.client.session.promptAsync` and `.status` into a fresh
wrapper object. The methods were copied by reference, so the prompt-async
gate's `session.promptAsync.bind(session)` was binding to that plain
wrapper rather than the underlying SDK `Session` instance. The opencode
SDK's `promptAsync` reads `this._client.post(...)`, so production calls
threw `TypeError: undefined is not an object (evaluating 'this._client')`
on the very first dispatch — fingerprinted in /tmp/oh-my-opencode.log as
688 `background-agent-parent-wake`, 47 `model-suggestion-retry`, and 4
`team-idle-wake-hint` failures over the past three days.

Move the wrapper construction into `buildTeamIdleWakeHintClient`, which
preserves the narrow factory contract while binding both methods back to
the SDK `Session` so `_client` survives the dispatch. Cover the contract
with four BDD-style tests including the historical destructure-only
failure mode so any future regression is caught at unit-test time.
2026-05-17 02:06:52 +09:00

101 lines
3.5 KiB
TypeScript

/// <reference path="../../bun-test.d.ts" />
import { describe, test, expect } from "bun:test"
import { buildTeamIdleWakeHintClient } from "./build-team-idle-wake-hint-client"
type FakeSdkHttp = {
post: (args: { url: string; body?: unknown }) => Promise<{ url: string; body?: unknown }>
}
type SdkLikeSession = {
_client: FakeSdkHttp
promptAsync: (options: { path: { id: string }; body?: unknown }) => Promise<{ url: string; body?: unknown }>
status: () => Promise<{ url: string; _client: FakeSdkHttp }>
}
function createSdkLikeSession(http: FakeSdkHttp): SdkLikeSession {
return {
_client: http,
async promptAsync(options) {
return this._client.post({ url: `/session/${options.path.id}/prompt_async`, body: options.body })
},
async status() {
return { url: "/session", _client: this._client }
},
}
}
describe("buildTeamIdleWakeHintClient", () => {
test("#given a real-SDK-like session whose promptAsync reads this._client #when the wrapper dispatches the bound method #then the SDK receives the call with _client preserved", async () => {
// given
const calls: Array<{ url: string; body?: unknown }> = []
const http: FakeSdkHttp = {
post: async (args) => {
calls.push(args)
return args
},
}
const session = createSdkLikeSession(http)
const sdkClient = { session } as unknown as Parameters<typeof buildTeamIdleWakeHintClient>[0]
// when
const wrapped = buildTeamIdleWakeHintClient(sdkClient)
await wrapped.session.promptAsync?.({ path: { id: "ses_regression" }, body: { hello: "world" } } as never)
// then
expect(calls).toHaveLength(1)
expect(calls[0]?.url).toBe("/session/ses_regression/prompt_async")
expect(calls[0]?.body).toEqual({ hello: "world" })
})
test("#given a real-SDK-like session whose status reads this._client #when the wrapper dispatches the bound status #then _client is preserved", async () => {
// given
const http: FakeSdkHttp = {
post: async (args) => args,
}
const session = createSdkLikeSession(http)
const sdkClient = { session } as unknown as Parameters<typeof buildTeamIdleWakeHintClient>[0]
// when
const wrapped = buildTeamIdleWakeHintClient(sdkClient)
const result = (await wrapped.session.status?.()) as { _client?: FakeSdkHttp } | undefined
// then
expect(result?._client).toBe(http)
})
test("#given a session without optional methods #when the wrapper is built #then it gracefully exposes undefined entries", async () => {
// given
const partial = { session: {} } as unknown as Parameters<typeof buildTeamIdleWakeHintClient>[0]
// when
const wrapped = buildTeamIdleWakeHintClient(partial)
// then
expect(wrapped.session.promptAsync).toBeUndefined()
expect(wrapped.session.status).toBeUndefined()
})
test("#given a destructure-without-bind pattern #when promptAsync is invoked via a plain wrapper #then this._client is undefined (historical bug)", async () => {
// given
const http: FakeSdkHttp = { post: async (args) => args }
const session = createSdkLikeSession(http)
const brokenWrapper = {
session: {
promptAsync: session.promptAsync,
},
}
// when
let caughtMessage = ""
try {
await brokenWrapper.session.promptAsync({ path: { id: "ses_x" } } as never)
} catch (error) {
caughtMessage = error instanceof Error ? error.message : String(error)
}
// then
expect(caughtMessage).toContain("_client")
})
})