import { beforeEach, describe, expect, it, vi } from "vitest"; const callGatewayMock = vi.fn(); vi.mock("../gateway/call.js", () => ({ callGateway: (opts: unknown) => callGatewayMock(opts), })); let configOverride: ReturnType<(typeof import("../config/config.js"))["loadConfig"]> = { session: { mainKey: "main", scope: "per-sender", }, }; vi.mock("../config/config.js", async (importOriginal) => { const actual = await importOriginal(); return { ...actual, loadConfig: () => configOverride, resolveGatewayPort: () => 18789, }; }); import { createClawdbotTools } from "./clawdbot-tools.js"; import { resetSubagentRegistryForTests } from "./subagent-registry.js"; describe("clawdbot-tools: subagents", () => { beforeEach(() => { configOverride = { session: { mainKey: "main", scope: "per-sender", }, }; }); it("sessions_spawn announces via agent.wait when lifecycle events are missing", async () => { resetSubagentRegistryForTests(); callGatewayMock.mockReset(); const calls: Array<{ method?: string; params?: unknown }> = []; let agentCallCount = 0; let sendParams: { to?: string; channel?: string; message?: string } = {}; let deletedKey: string | undefined; let childRunId: string | undefined; let childSessionKey: string | undefined; const waitCalls: Array<{ runId?: string; timeoutMs?: number }> = []; const sessionLastAssistantText = new Map(); callGatewayMock.mockImplementation(async (opts: unknown) => { const request = opts as { method?: string; params?: unknown }; calls.push(request); if (request.method === "agent") { agentCallCount += 1; const runId = `run-${agentCallCount}`; const params = request.params as { message?: string; sessionKey?: string; channel?: string; timeout?: number; }; const message = params?.message ?? ""; const sessionKey = params?.sessionKey ?? ""; if (message === "Sub-agent announce step.") { sessionLastAssistantText.set(sessionKey, "announce now"); } else { childRunId = runId; childSessionKey = sessionKey; sessionLastAssistantText.set(sessionKey, "result"); expect(params?.channel).toBe("discord"); expect(params?.timeout).toBe(1); } return { runId, status: "accepted", acceptedAt: 2000 + agentCallCount, }; } if (request.method === "agent.wait") { const params = request.params as { runId?: string; timeoutMs?: number } | undefined; waitCalls.push(params ?? {}); return { runId: params?.runId ?? "run-1", status: "ok", startedAt: 3000, endedAt: 4000, }; } if (request.method === "chat.history") { const params = request.params as { sessionKey?: string } | undefined; const text = sessionLastAssistantText.get(params?.sessionKey ?? "") ?? ""; return { messages: [{ role: "assistant", content: [{ type: "text", text }] }], }; } if (request.method === "send") { const params = request.params as | { to?: string; channel?: string; message?: string } | undefined; sendParams = { to: params?.to, channel: params?.channel, message: params?.message, }; return { messageId: "m-announce" }; } if (request.method === "sessions.delete") { const params = request.params as { key?: string } | undefined; deletedKey = params?.key; return { ok: true }; } return {}; }); const tool = createClawdbotTools({ agentSessionKey: "discord:group:req", agentChannel: "discord", }).find((candidate) => candidate.name === "sessions_spawn"); if (!tool) throw new Error("missing sessions_spawn tool"); const result = await tool.execute("call1b", { task: "do thing", runTimeoutSeconds: 1, cleanup: "delete", }); expect(result.details).toMatchObject({ status: "accepted", runId: "run-1", }); await new Promise((resolve) => setTimeout(resolve, 0)); await new Promise((resolve) => setTimeout(resolve, 0)); await new Promise((resolve) => setTimeout(resolve, 0)); const childWait = waitCalls.find((call) => call.runId === childRunId); expect(childWait?.timeoutMs).toBe(1000); expect(childSessionKey?.startsWith("agent:main:subagent:")).toBe(true); const agentCalls = calls.filter((call) => call.method === "agent"); expect(agentCalls).toHaveLength(2); const second = agentCalls[1]?.params as | { channel?: string; deliver?: boolean; lane?: string } | undefined; expect(second?.lane).toBe("nested"); expect(second?.deliver).toBe(false); expect(second?.channel).toBe("webchat"); expect(sendParams.channel).toBe("discord"); expect(sendParams.to).toBe("channel:req"); expect(sendParams.message ?? "").toContain("announce now"); expect(sendParams.message ?? "").toContain("Stats:"); expect(deletedKey?.startsWith("agent:main:subagent:")).toBe(true); }); });