Skip to content

Commit 971b98c

Browse files
committed
test: cover new queue modes
1 parent a72da30 commit 971b98c

5 files changed

Lines changed: 206 additions & 3 deletions

File tree

‎src/auto-reply/reply.block-streaming.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ vi.mock("../agents/pi-embedded.js", () => ({
1414
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
1515
resolveEmbeddedSessionLane: (key: string) =>
1616
`session:${key.trim() || "main"}`,
17+
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
18+
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
1719
}));
1820
vi.mock("../agents/model-catalog.js", () => ({
1921
loadModelCatalog: vi.fn(),

‎src/auto-reply/reply.directive.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ vi.mock("../agents/pi-embedded.js", () => ({
2626
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
2727
resolveEmbeddedSessionLane: (key: string) =>
2828
`session:${key.trim() || "main"}`,
29+
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
30+
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
2931
}));
3032
vi.mock("../agents/model-catalog.js", () => ({
3133
loadModelCatalog: vi.fn(),
@@ -109,6 +111,31 @@ describe("directive parsing", () => {
109111
expect(res.cleaned).toBe("please now");
110112
});
111113

114+
it("errors on invalid queue options", async () => {
115+
await withTempHome(async (home) => {
116+
vi.mocked(runEmbeddedPiAgent).mockReset();
117+
118+
const res = await getReplyFromConfig(
119+
{ Body: "/queue collect debounce:bogus cap:zero drop:maybe", From: "+1222", To: "+1222" },
120+
{},
121+
{
122+
agent: {
123+
model: "anthropic/claude-opus-4-5",
124+
workspace: path.join(home, "clawd"),
125+
},
126+
whatsapp: { allowFrom: ["*"] },
127+
session: { store: path.join(home, "sessions.json") },
128+
},
129+
);
130+
131+
const text = Array.isArray(res) ? res[0]?.text : res?.text;
132+
expect(text).toContain("Invalid debounce");
133+
expect(text).toContain("Invalid cap");
134+
expect(text).toContain("Invalid drop policy");
135+
expect(runEmbeddedPiAgent).not.toHaveBeenCalled();
136+
});
137+
});
138+
112139
it("extracts reply_to_current tag", () => {
113140
const res = extractReplyToTag("ok [[reply_to_current]]", "msg-1");
114141
expect(res.replyToId).toBe("msg-1");

‎src/auto-reply/reply.queue.test.ts‎

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
});

‎src/auto-reply/reply.triggers.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ vi.mock("../agents/pi-embedded.js", () => ({
99
queueEmbeddedPiMessage: vi.fn().mockReturnValue(false),
1010
resolveEmbeddedSessionLane: (key: string) =>
1111
`session:${key.trim() || "main"}`,
12+
isEmbeddedPiRunActive: vi.fn().mockReturnValue(false),
13+
isEmbeddedPiRunStreaming: vi.fn().mockReturnValue(false),
1214
}));
1315

1416
import { runEmbeddedPiAgent } from "../agents/pi-embedded.js";

‎src/auto-reply/reply.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,13 @@ function normalizeQueueDropPolicy(raw?: string): QueueDropPolicy | undefined {
218218

219219
function parseQueueDebounce(raw?: string): number | undefined {
220220
if (!raw) return undefined;
221-
const parsed = parseDurationMs(raw.trim(), { defaultUnit: "ms" });
222-
if (!parsed || parsed < 0) return undefined;
223-
return Math.round(parsed);
221+
try {
222+
const parsed = parseDurationMs(raw.trim(), { defaultUnit: "ms" });
223+
if (!parsed || parsed < 0) return undefined;
224+
return Math.round(parsed);
225+
} catch {
226+
return undefined;
227+
}
224228
}
225229

226230
function parseQueueCap(raw?: string): number | undefined {

0 commit comments

Comments
 (0)