Skip to content

Commit 65f5cb4

Browse files
test(registerCommands): pin await semantics with deferred-promise pattern
CodeRabbit nit on the test additions: the two await-asserting tests for focusInput and toggleAutoApprove only verified the call payload, so they would still pass if `await` were replaced with `void` in the handler. Switched both to a deferred-promise pattern that observes the handler's pending state before the underlying postMessageToWebview resolves — now a regression that drops the `await` would be caught at the test boundary.
1 parent 3952b3c commit 65f5cb4

1 file changed

Lines changed: 43 additions & 2 deletions

File tree

src/activate/__tests__/registerCommands.spec.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,27 @@ describe("registerCommands handlers", () => {
221221
})
222222

223223
it("toggleAutoApprove awaits postMessage with toggleAutoApprove action", async () => {
224-
await handlers["zoo-code.toggleAutoApprove"]()
224+
// Deferred-promise pattern: pin that the handler actually awaits
225+
// postMessageToWebview rather than fire-and-forgetting it. If `await`
226+
// were dropped in the handler, handlerPromise would resolve before
227+
// resolvePost() is called and `settled` would flip true at the
228+
// microtask flush below, failing the pending-state assertion.
229+
let resolvePost!: () => void
230+
const postPromise = new Promise<void>((resolve) => {
231+
resolvePost = resolve
232+
})
233+
mockVisibleProvider.postMessageToWebview.mockReturnValueOnce(postPromise)
234+
235+
const handlerPromise = handlers["zoo-code.toggleAutoApprove"]() as Promise<unknown>
236+
let settled = false
237+
void handlerPromise.then(() => {
238+
settled = true
239+
})
240+
await Promise.resolve()
241+
expect(settled).toBe(false)
242+
243+
resolvePost()
244+
await handlerPromise
225245

226246
expect(mockVisibleProvider.postMessageToWebview).toHaveBeenCalledWith({
227247
type: "action",
@@ -233,7 +253,28 @@ describe("registerCommands handlers", () => {
233253
const fakeSidebar = {} as vscode.WebviewView
234254
setPanel(fakeSidebar, "sidebar")
235255

236-
await handlers["zoo-code.focusInput"]()
256+
// Same deferred-promise pattern as above. focusInput first awaits
257+
// focusPanel() (mocked to resolve sync) and then awaits
258+
// provider.postMessageToWebview — so we flush two microtasks before
259+
// asserting the pending state, to let the handler advance past the
260+
// focusPanel await and suspend on the deferred postPromise.
261+
let resolvePost!: () => void
262+
const postPromise = new Promise<void>((resolve) => {
263+
resolvePost = resolve
264+
})
265+
mockProvider.postMessageToWebview.mockReturnValueOnce(postPromise)
266+
267+
const handlerPromise = handlers["zoo-code.focusInput"]() as Promise<unknown>
268+
let settled = false
269+
void handlerPromise.then(() => {
270+
settled = true
271+
})
272+
await Promise.resolve()
273+
await Promise.resolve()
274+
expect(settled).toBe(false)
275+
276+
resolvePost()
277+
await handlerPromise
237278

238279
expect(mockProvider.postMessageToWebview).toHaveBeenCalledWith({
239280
type: "action",

0 commit comments

Comments
 (0)