import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { getReplyFromConfig } from "./reply.js"; import { getRunEmbeddedPiAgentMock, installTriggerHandlingE2eTestHooks, makeCfg, withTempHome, } from "./reply.triggers.trigger-handling.test-harness.js"; installTriggerHandlingE2eTestHooks(); describe("trigger handling", () => { it("allows /activation from allowFrom in groups", async () => { await withTempHome(async (home) => { const cfg = makeCfg(home); const res = await getReplyFromConfig( { Body: "/activation mention", From: "123@g.us", To: "+2000", ChatType: "group", Provider: "whatsapp", SenderE164: "+999", CommandAuthorized: true, }, {}, cfg, ); const text = Array.isArray(res) ? res[0]?.text : res?.text; expect(text).toBe("⚙️ Group activation set to mention."); expect(getRunEmbeddedPiAgentMock()).not.toHaveBeenCalled(); }); }); it("injects group activation context into the system prompt", async () => { await withTempHome(async (home) => { getRunEmbeddedPiAgentMock().mockResolvedValue({ payloads: [{ text: "ok" }], meta: { durationMs: 1, agentMeta: { sessionId: "s", provider: "p", model: "m" }, }, }); const res = await getReplyFromConfig( { Body: "hello group", From: "123@g.us", To: "+2000", ChatType: "group", Provider: "whatsapp", SenderE164: "+2000", GroupSubject: "Test Group", GroupMembers: "Alice (+1), Bob (+2)", }, {}, { agents: { defaults: { model: "anthropic/claude-opus-4-5", workspace: join(home, "openclaw"), }, }, channels: { whatsapp: { allowFrom: ["*"], groups: { "*": { requireMention: false } }, }, }, messages: { groupChat: {}, }, session: { store: join(home, "sessions.json") }, }, ); const text = Array.isArray(res) ? res[0]?.text : res?.text; expect(text).toBe("ok"); expect(getRunEmbeddedPiAgentMock()).toHaveBeenCalledOnce(); const extra = getRunEmbeddedPiAgentMock().mock.calls[0]?.[0]?.extraSystemPrompt ?? ""; expect(extra).toContain('"chat_type": "group"'); expect(extra).toContain("Activation: always-on"); }); }); it("runs a greeting prompt for a bare /new", async () => { await withTempHome(async (home) => { getRunEmbeddedPiAgentMock().mockResolvedValue({ payloads: [{ text: "hello" }], meta: { durationMs: 1, agentMeta: { sessionId: "s", provider: "p", model: "m" }, }, }); const res = await getReplyFromConfig( { Body: "/new", From: "+1003", To: "+2000", CommandAuthorized: true, }, {}, { agents: { defaults: { model: "anthropic/claude-opus-4-5", workspace: join(home, "openclaw"), }, }, channels: { whatsapp: { allowFrom: ["*"], }, }, session: { store: join(tmpdir(), `openclaw-session-test-${Date.now()}.json`), }, }, ); const text = Array.isArray(res) ? res[0]?.text : res?.text; expect(text).toBe("hello"); expect(getRunEmbeddedPiAgentMock()).toHaveBeenCalledOnce(); const prompt = getRunEmbeddedPiAgentMock().mock.calls[0]?.[0]?.prompt ?? ""; expect(prompt).toContain("A new session was started via /new or /reset"); }); }); });