import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it, vi } from "vitest"; vi.mock("../agents/pi-embedded.js", () => ({ runEmbeddedPiAgent: vi.fn(), })); import { runEmbeddedPiAgent } from "../agents/pi-embedded.js"; import { extractThinkDirective, extractVerboseDirective, getReplyFromConfig, } from "./reply.js"; async function withTempHome(fn: (home: string) => Promise): Promise { const base = await fs.mkdtemp(path.join(os.tmpdir(), "clawdis-reply-")); const previousHome = process.env.HOME; process.env.HOME = base; try { return await fn(base); } finally { process.env.HOME = previousHome; await fs.rm(base, { recursive: true, force: true }); } } describe("directive parsing", () => { afterEach(() => { vi.restoreAllMocks(); }); it("ignores verbose directive inside URL", () => { const body = "https://x.com/verioussmith/status/1997066835133669687"; const res = extractVerboseDirective(body); expect(res.hasDirective).toBe(false); expect(res.cleaned).toBe(body); }); it("ignores typoed /verioussmith", () => { const body = "/verioussmith"; const res = extractVerboseDirective(body); expect(res.hasDirective).toBe(false); expect(res.cleaned).toBe(body.trim()); }); it("ignores think directive inside URL", () => { const body = "see https://example.com/path/thinkstuff"; const res = extractThinkDirective(body); expect(res.hasDirective).toBe(false); }); it("matches verbose with leading space", () => { const res = extractVerboseDirective(" please /verbose on now"); expect(res.hasDirective).toBe(true); expect(res.verboseLevel).toBe("on"); }); it("matches think at start of line", () => { const res = extractThinkDirective("/think:high run slow"); expect(res.hasDirective).toBe(true); expect(res.thinkLevel).toBe("high"); }); it("applies inline think and still runs agent content", async () => { await withTempHome(async (home) => { vi.mocked(runEmbeddedPiAgent).mockResolvedValue({ payloads: [{ text: "done" }], meta: { durationMs: 5, agentMeta: { sessionId: "s", provider: "p", model: "m" }, }, }); const res = await getReplyFromConfig( { Body: "please sync /think:high now", From: "+1004", To: "+2000", }, {}, { inbound: { allowFrom: ["*"], workspace: path.join(home, "clawd"), agent: { provider: "anthropic", model: "claude-opus-4-5" }, session: { store: path.join(home, "sessions.json") }, }, }, ); const text = Array.isArray(res) ? res[0]?.text : res?.text; expect(text).toBe("done"); expect(runEmbeddedPiAgent).toHaveBeenCalledOnce(); }); }); it("acks verbose directive immediately with system marker", async () => { await withTempHome(async (home) => { vi.mocked(runEmbeddedPiAgent).mockReset(); const res = await getReplyFromConfig( { Body: "/verbose on", From: "+1222", To: "+1222" }, {}, { inbound: { workspace: path.join(home, "clawd"), agent: { provider: "anthropic", model: "claude-opus-4-5" }, session: { store: path.join(home, "sessions.json") }, }, }, ); const text = Array.isArray(res) ? res[0]?.text : res?.text; expect(text).toMatch(/^⚙️ Verbose logging enabled\./); expect(runEmbeddedPiAgent).not.toHaveBeenCalled(); }); }); });