diff --git a/src/cli/run/event-handlers.test.ts b/src/cli/run/event-handlers.test.ts new file mode 100644 index 00000000..7e514831 --- /dev/null +++ b/src/cli/run/event-handlers.test.ts @@ -0,0 +1,73 @@ +import { describe, it, expect } from "bun:test" +import type { RunContext } from "./types" +import { createEventState } from "./events" +import { handleSessionStatus } from "./event-handlers" + +const createMockContext = (sessionID: string = "test-session"): RunContext => ({ + sessionID, +} as RunContext) + +describe("handleSessionStatus", () => { + it("recognizes idle from session.status event (not just deprecated session.idle)", () => { + //#given - state with mainSessionIdle=false + const ctx = createMockContext("test-session") + const state = createEventState() + state.mainSessionIdle = false + + const payload = { + type: "session.status", + properties: { + sessionID: "test-session", + status: { type: "idle" as const }, + }, + } + + //#when - handleSessionStatus called with idle status + handleSessionStatus(ctx, payload as any, state) + + //#then - state.mainSessionIdle === true + expect(state.mainSessionIdle).toBe(true) + }) + + it("handleSessionStatus sets idle=false on busy", () => { + //#given - state with mainSessionIdle=true + const ctx = createMockContext("test-session") + const state = createEventState() + state.mainSessionIdle = true + + const payload = { + type: "session.status", + properties: { + sessionID: "test-session", + status: { type: "busy" as const }, + }, + } + + //#when - handleSessionStatus called with busy status + handleSessionStatus(ctx, payload as any, state) + + //#then - state.mainSessionIdle === false + expect(state.mainSessionIdle).toBe(false) + }) + + it("does nothing for different session ID", () => { + //#given - state with mainSessionIdle=true + const ctx = createMockContext("test-session") + const state = createEventState() + state.mainSessionIdle = true + + const payload = { + type: "session.status", + properties: { + sessionID: "other-session", + status: { type: "idle" as const }, + }, + } + + //#when - handleSessionStatus called with different session ID + handleSessionStatus(ctx, payload as any, state) + + //#then - state.mainSessionIdle remains unchanged + expect(state.mainSessionIdle).toBe(true) + }) +}) diff --git a/src/cli/run/event-handlers.ts b/src/cli/run/event-handlers.ts index 50390095..c12d3b04 100644 --- a/src/cli/run/event-handlers.ts +++ b/src/cli/run/event-handlers.ts @@ -26,8 +26,12 @@ export function handleSessionStatus(ctx: RunContext, payload: EventPayload, stat if (payload.type !== "session.status") return const props = payload.properties as SessionStatusProps | undefined - if (props?.sessionID === ctx.sessionID && props?.status?.type === "busy") { + if (props?.sessionID !== ctx.sessionID) return + + if (props?.status?.type === "busy") { state.mainSessionIdle = false + } else if (props?.status?.type === "idle") { + state.mainSessionIdle = true } }