|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | + |
| 5 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +import { |
| 8 | + isEmbeddedPiRunActive, |
| 9 | + isEmbeddedPiRunStreaming, |
| 10 | + runEmbeddedPiAgent, |
| 11 | +} from "../agents/pi-embedded.js"; |
| 12 | +import { getReplyFromConfig } from "./reply.js"; |
| 13 | + |
| 14 | +vi.mock("../agents/pi-embedded.js", () => ({ |
| 15 | + abortEmbeddedPiRun: vi.fn().mockReturnValue(false), |
| 16 | + runEmbeddedPiAgent: vi.fn(), |
| 17 | + queueEmbeddedPiMessage: vi.fn().mockReturnValue(false), |
| 18 | + resolveEmbeddedSessionLane: (key: string) => |
| 19 | + `session:${key.trim() || "main"}`, |
| 20 | + isEmbeddedPiRunActive: vi.fn().mockReturnValue(false), |
| 21 | + isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false), |
| 22 | +})); |
| 23 | + |
| 24 | +function makeResult(text: string) { |
| 25 | + return { |
| 26 | + payloads: [{ text }], |
| 27 | + meta: { |
| 28 | + durationMs: 5, |
| 29 | + agentMeta: { sessionId: "s", provider: "p", model: "m" }, |
| 30 | + }, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +async function withTempHome<T>(fn: (home: string) => Promise<T>): Promise<T> { |
| 35 | + const base = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-queue-")); |
| 36 | + const previousHome = process.env.HOME; |
| 37 | + process.env.HOME = base; |
| 38 | + try { |
| 39 | + vi.mocked(runEmbeddedPiAgent).mockReset(); |
| 40 | + return await fn(base); |
| 41 | + } finally { |
| 42 | + process.env.HOME = previousHome; |
| 43 | + try { |
| 44 | + await fs.rm(base, { recursive: true, force: true }); |
| 45 | + } catch (err) { |
| 46 | + const msg = err instanceof Error ? err.message : String(err); |
| 47 | + if (!msg.includes("ENOTEMPTY")) throw err; |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function makeCfg(home: string, queue?: Record<string, unknown>) { |
| 53 | + return { |
| 54 | + agent: { |
| 55 | + model: "anthropic/claude-opus-4-5", |
| 56 | + workspace: path.join(home, "clawd"), |
| 57 | + }, |
| 58 | + whatsapp: { allowFrom: ["*"] }, |
| 59 | + session: { store: path.join(home, "sessions.json") }, |
| 60 | + routing: queue ? { queue } : undefined, |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +describe("queue followups", () => { |
| 65 | + afterEach(() => { |
| 66 | + vi.useRealTimers(); |
| 67 | + }); |
| 68 | + |
| 69 | + it("collects queued messages and drains after run completes", async () => { |
| 70 | + vi.useFakeTimers(); |
| 71 | + await withTempHome(async (home) => { |
| 72 | + const prompts: string[] = []; |
| 73 | + vi.mocked(runEmbeddedPiAgent).mockImplementation(async (params) => { |
| 74 | + prompts.push(params.prompt); |
| 75 | + if (params.prompt.includes("[Queued messages while agent was busy]")) { |
| 76 | + return makeResult("followup"); |
| 77 | + } |
| 78 | + return makeResult("main"); |
| 79 | + }); |
| 80 | + |
| 81 | + vi.mocked(isEmbeddedPiRunActive).mockReturnValue(true); |
| 82 | + vi.mocked(isEmbeddedPiRunStreaming).mockReturnValue(true); |
| 83 | + |
| 84 | + const cfg = makeCfg(home, { |
| 85 | + mode: "collect", |
| 86 | + debounceMs: 200, |
| 87 | + cap: 10, |
| 88 | + drop: "summarize", |
| 89 | + }); |
| 90 | + |
| 91 | + const first = await getReplyFromConfig( |
| 92 | + { Body: "first", From: "+1001", To: "+2000" }, |
| 93 | + {}, |
| 94 | + cfg, |
| 95 | + ); |
| 96 | + expect(first).toBeUndefined(); |
| 97 | + expect(runEmbeddedPiAgent).not.toHaveBeenCalled(); |
| 98 | + |
| 99 | + vi.mocked(isEmbeddedPiRunActive).mockReturnValue(false); |
| 100 | + vi.mocked(isEmbeddedPiRunStreaming).mockReturnValue(false); |
| 101 | + |
| 102 | + const second = await getReplyFromConfig( |
| 103 | + { Body: "second", From: "+1001", To: "+2000" }, |
| 104 | + {}, |
| 105 | + cfg, |
| 106 | + ); |
| 107 | + |
| 108 | + const secondText = Array.isArray(second) |
| 109 | + ? second[0]?.text |
| 110 | + : second?.text; |
| 111 | + expect(secondText).toBe("main"); |
| 112 | + |
| 113 | + await vi.runAllTimersAsync(); |
| 114 | + await Promise.resolve(); |
| 115 | + |
| 116 | + expect(runEmbeddedPiAgent).toHaveBeenCalledTimes(2); |
| 117 | + expect( |
| 118 | + prompts.some((p) => |
| 119 | + p.includes("[Queued messages while agent was busy]"), |
| 120 | + ), |
| 121 | + ).toBe(true); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it("summarizes dropped followups when cap is exceeded", async () => { |
| 126 | + vi.useFakeTimers(); |
| 127 | + await withTempHome(async (home) => { |
| 128 | + const prompts: string[] = []; |
| 129 | + vi.mocked(runEmbeddedPiAgent).mockImplementation(async (params) => { |
| 130 | + prompts.push(params.prompt); |
| 131 | + return makeResult("ok"); |
| 132 | + }); |
| 133 | + |
| 134 | + vi.mocked(isEmbeddedPiRunActive).mockReturnValue(true); |
| 135 | + vi.mocked(isEmbeddedPiRunStreaming).mockReturnValue(false); |
| 136 | + |
| 137 | + const cfg = makeCfg(home, { |
| 138 | + mode: "followup", |
| 139 | + debounceMs: 0, |
| 140 | + cap: 1, |
| 141 | + drop: "summarize", |
| 142 | + }); |
| 143 | + |
| 144 | + await getReplyFromConfig( |
| 145 | + { Body: "one", From: "+1002", To: "+2000" }, |
| 146 | + {}, |
| 147 | + cfg, |
| 148 | + ); |
| 149 | + await getReplyFromConfig( |
| 150 | + { Body: "two", From: "+1002", To: "+2000" }, |
| 151 | + {}, |
| 152 | + cfg, |
| 153 | + ); |
| 154 | + |
| 155 | + vi.mocked(isEmbeddedPiRunActive).mockReturnValue(false); |
| 156 | + await getReplyFromConfig( |
| 157 | + { Body: "three", From: "+1002", To: "+2000" }, |
| 158 | + {}, |
| 159 | + cfg, |
| 160 | + ); |
| 161 | + |
| 162 | + await vi.runAllTimersAsync(); |
| 163 | + await Promise.resolve(); |
| 164 | + |
| 165 | + expect(prompts.some((p) => p.includes("[Queue overflow]"))).toBe(true); |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments