test: merge allow-from trigger shard and dedupe inline cases

This commit is contained in:
Peter Steinberger
2026-02-23 13:18:03 +00:00
parent f6ee1c99a7
commit 67bccc1fa0
2 changed files with 140 additions and 195 deletions

View File

@@ -1,146 +0,0 @@
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
getRunEmbeddedPiAgentMock,
installTriggerHandlingReplyHarness,
makeCfg,
runGreetingPromptForBareNewOrReset,
withTempHome,
} from "./reply.triggers.trigger-handling.test-harness.js";
let getReplyFromConfig: typeof import("./reply.js").getReplyFromConfig;
installTriggerHandlingReplyHarness((loader) => {
getReplyFromConfig = loader;
});
async function expectResetBlockedForNonOwner(params: {
home: string;
commandAuthorized: boolean;
getReplyFromConfig: typeof import("./reply.js").getReplyFromConfig;
}): Promise<void> {
const { home, commandAuthorized, getReplyFromConfig } = params;
const cfg = makeCfg(home);
cfg.channels ??= {};
cfg.channels.whatsapp = {
...cfg.channels.whatsapp,
allowFrom: ["+1999"],
};
cfg.session = {
...cfg.session,
store: join(tmpdir(), `openclaw-session-test-${Date.now()}.json`),
};
const res = await getReplyFromConfig(
{
Body: "/reset",
From: "+1003",
To: "+2000",
CommandAuthorized: commandAuthorized,
},
{},
cfg,
);
expect(res).toBeUndefined();
expect(getRunEmbeddedPiAgentMock()).not.toHaveBeenCalled();
}
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 cfg = makeCfg(home);
cfg.channels ??= {};
cfg.channels.whatsapp = {
...cfg.channels.whatsapp,
allowFrom: ["*"],
groups: { "*": { requireMention: false } },
};
cfg.messages = {
...cfg.messages,
groupChat: {},
};
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)",
},
{},
cfg,
);
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 /reset", async () => {
await withTempHome(async (home) => {
await runGreetingPromptForBareNewOrReset({ home, body: "/reset", getReplyFromConfig });
});
});
it("runs a greeting prompt for a bare /new", async () => {
await withTempHome(async (home) => {
await runGreetingPromptForBareNewOrReset({ home, body: "/new", getReplyFromConfig });
});
});
it("does not reset for unauthorized /reset", async () => {
await withTempHome(async (home) => {
await expectResetBlockedForNonOwner({
home,
commandAuthorized: false,
getReplyFromConfig,
});
});
});
it("blocks /reset for non-owner senders", async () => {
await withTempHome(async (home) => {
await expectResetBlockedForNonOwner({
home,
commandAuthorized: true,
getReplyFromConfig,
});
});
});
});

View File

@@ -1,4 +1,6 @@
import fs from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { beforeAll, describe, expect, it } from "vitest";
import {
expectInlineCommandHandledAndStripped,
@@ -7,6 +9,9 @@ import {
loadGetReplyFromConfig,
MAIN_SESSION_KEY,
makeCfg,
mockRunEmbeddedPiAgentOk,
requireSessionStorePath,
runGreetingPromptForBareNewOrReset,
withTempHome,
} from "./reply.triggers.trigger-handling.test-harness.js";
@@ -30,12 +35,33 @@ function makeUnauthorizedWhatsAppCfg(home: string) {
};
}
function requireSessionStorePath(cfg: { session?: { store?: string } }): string {
const storePath = cfg.session?.store;
if (!storePath) {
throw new Error("expected session store path");
}
return storePath;
async function expectResetBlockedForNonOwner(params: {
home: string;
commandAuthorized: boolean;
}): Promise<void> {
const { home } = params;
const cfg = makeCfg(home);
cfg.channels ??= {};
cfg.channels.whatsapp = {
...cfg.channels.whatsapp,
allowFrom: ["+1999"],
};
cfg.session = {
...cfg.session,
store: join(tmpdir(), `openclaw-session-test-${Date.now()}.json`),
};
const res = await getReplyFromConfig(
{
Body: "/reset",
From: "+1003",
To: "+2000",
CommandAuthorized: params.commandAuthorized,
},
{},
cfg,
);
expect(res).toBeUndefined();
expect(getRunEmbeddedPiAgentMock()).not.toHaveBeenCalled();
}
async function expectUnauthorizedCommandDropped(home: string, body: "/status" | "/whoami") {
@@ -59,15 +85,7 @@ async function expectUnauthorizedCommandDropped(home: string, body: "/status" |
}
function mockEmbeddedOk() {
const runEmbeddedPiAgentMock = getRunEmbeddedPiAgentMock();
runEmbeddedPiAgentMock.mockResolvedValue({
payloads: [{ text: "ok" }],
meta: {
durationMs: 1,
agentMeta: { sessionId: "s", provider: "p", model: "m" },
},
});
return runEmbeddedPiAgentMock;
return mockRunEmbeddedPiAgentOk("ok");
}
async function runInlineUnauthorizedCommand(params: {
@@ -90,6 +108,96 @@ async function runInlineUnauthorizedCommand(params: {
}
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 cfg = makeCfg(home);
cfg.channels ??= {};
cfg.channels.whatsapp = {
...cfg.channels.whatsapp,
allowFrom: ["*"],
groups: { "*": { requireMention: false } },
};
cfg.messages = {
...cfg.messages,
groupChat: {},
};
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)",
},
{},
cfg,
);
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 /reset", async () => {
await withTempHome(async (home) => {
await runGreetingPromptForBareNewOrReset({ home, body: "/reset", getReplyFromConfig });
});
});
it("runs a greeting prompt for a bare /new", async () => {
await withTempHome(async (home) => {
await runGreetingPromptForBareNewOrReset({ home, body: "/new", getReplyFromConfig });
});
});
it("blocks /reset for unauthorized sender scenarios", async () => {
await withTempHome(async (home) => {
for (const commandAuthorized of [false, true]) {
await expectResetBlockedForNonOwner({
home,
commandAuthorized,
});
}
});
});
it("handles inline /commands and strips it before the agent", async () => {
await withTempHome(async (home) => {
await expectInlineCommandHandledAndStripped({
@@ -129,45 +237,28 @@ describe("trigger handling", () => {
});
});
it("drops /status for unauthorized senders", async () => {
it("drops top-level restricted commands for unauthorized senders", async () => {
await withTempHome(async (home) => {
await expectUnauthorizedCommandDropped(home, "/status");
for (const command of ["/status", "/whoami"] as const) {
await expectUnauthorizedCommandDropped(home, command);
}
});
});
it("drops /whoami for unauthorized senders", async () => {
it("keeps inline commands for unauthorized senders", async () => {
await withTempHome(async (home) => {
await expectUnauthorizedCommandDropped(home, "/whoami");
});
});
it("keeps inline /status for unauthorized senders", async () => {
await withTempHome(async (home) => {
const runEmbeddedPiAgentMock = mockEmbeddedOk();
const res = await runInlineUnauthorizedCommand({
home,
command: "/status",
});
const text = Array.isArray(res) ? res[0]?.text : res?.text;
expect(text).toBe("ok");
expect(runEmbeddedPiAgentMock).toHaveBeenCalled();
const prompt = runEmbeddedPiAgentMock.mock.calls[0]?.[0]?.prompt ?? "";
expect(prompt).toContain("/status");
});
});
it("keeps inline /help for unauthorized senders", async () => {
await withTempHome(async (home) => {
const runEmbeddedPiAgentMock = mockEmbeddedOk();
const res = await runInlineUnauthorizedCommand({
home,
command: "/help",
});
const text = Array.isArray(res) ? res[0]?.text : res?.text;
expect(text).toBe("ok");
expect(runEmbeddedPiAgentMock).toHaveBeenCalled();
const prompt = runEmbeddedPiAgentMock.mock.calls[0]?.[0]?.prompt ?? "";
expect(prompt).toContain("/help");
for (const command of ["/status", "/help"] as const) {
const runEmbeddedPiAgentMock = mockEmbeddedOk();
const res = await runInlineUnauthorizedCommand({
home,
command,
});
const text = Array.isArray(res) ? res[0]?.text : res?.text;
expect(text).toBe("ok");
expect(runEmbeddedPiAgentMock).toHaveBeenCalled();
const prompt = runEmbeddedPiAgentMock.mock.calls.at(-1)?.[0]?.prompt ?? "";
expect(prompt).toContain(command);
}
});
});