fix: sender fallback for command auth (#755) (thanks @juanpablodlc)
This commit is contained in:
@@ -726,7 +726,8 @@ describe("runEmbeddedPiAgent", () => {
|
||||
const messages = await readSessionMessages(sessionFile);
|
||||
const firstUserIndex = messages.findIndex(
|
||||
(message) =>
|
||||
message?.role === "user" && textFromContent(message.content) === "hello",
|
||||
message?.role === "user" &&
|
||||
textFromContent(message.content) === "hello",
|
||||
);
|
||||
const firstAssistantIndex = messages.findIndex(
|
||||
(message) => message?.role === "assistant",
|
||||
@@ -836,7 +837,8 @@ describe("runEmbeddedPiAgent", () => {
|
||||
);
|
||||
const newUserIndex = messages.findIndex(
|
||||
(message) =>
|
||||
message?.role === "user" && textFromContent(message.content) === "hello",
|
||||
message?.role === "user" &&
|
||||
textFromContent(message.content) === "hello",
|
||||
);
|
||||
const newAssistantIndex = messages.findIndex(
|
||||
(message, index) => index > newUserIndex && message?.role === "assistant",
|
||||
@@ -887,10 +889,12 @@ describe("runEmbeddedPiAgent", () => {
|
||||
const messages = await readSessionMessages(sessionFile);
|
||||
const firstUserIndex = messages.findIndex(
|
||||
(message) =>
|
||||
message?.role === "user" && textFromContent(message.content) === "first",
|
||||
message?.role === "user" &&
|
||||
textFromContent(message.content) === "first",
|
||||
);
|
||||
const firstAssistantIndex = messages.findIndex(
|
||||
(message, index) => index > firstUserIndex && message?.role === "assistant",
|
||||
(message, index) =>
|
||||
index > firstUserIndex && message?.role === "assistant",
|
||||
);
|
||||
const secondUserIndex = messages.findIndex(
|
||||
(message) =>
|
||||
|
||||
30
src/auto-reply/command-auth.test.ts
Normal file
30
src/auto-reply/command-auth.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { ClawdbotConfig } from "../config/config.js";
|
||||
import { resolveCommandAuthorization } from "./command-auth.js";
|
||||
import type { MsgContext } from "./templating.js";
|
||||
|
||||
describe("resolveCommandAuthorization", () => {
|
||||
it("falls back from empty SenderId to SenderE164", () => {
|
||||
const cfg = {
|
||||
whatsapp: { allowFrom: ["+123"] },
|
||||
} as ClawdbotConfig;
|
||||
|
||||
const ctx = {
|
||||
Provider: "whatsapp",
|
||||
Surface: "whatsapp",
|
||||
From: "whatsapp:+999",
|
||||
SenderId: "",
|
||||
SenderE164: "+123",
|
||||
} as MsgContext;
|
||||
|
||||
const auth = resolveCommandAuthorization({
|
||||
ctx,
|
||||
cfg,
|
||||
commandAuthorized: true,
|
||||
});
|
||||
|
||||
expect(auth.senderId).toBe("+123");
|
||||
expect(auth.isAuthorizedSender).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -520,7 +520,7 @@ describe("trigger handling", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("handles inline /status and still runs the agent", async () => {
|
||||
it("strips inline /status and still runs the agent", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
||||
payloads: [{ text: "ok" }],
|
||||
@@ -535,6 +535,9 @@ describe("trigger handling", () => {
|
||||
Body: "please /status now",
|
||||
From: "+1002",
|
||||
To: "+2000",
|
||||
Provider: "whatsapp",
|
||||
Surface: "whatsapp",
|
||||
SenderE164: "+1002",
|
||||
},
|
||||
{
|
||||
onBlockReply: async (payload) => {
|
||||
@@ -543,9 +546,11 @@ describe("trigger handling", () => {
|
||||
},
|
||||
makeCfg(home),
|
||||
);
|
||||
expect(blockReplies.length).toBe(1);
|
||||
expect(blockReplies[0]?.text).toBeTruthy();
|
||||
expect(runEmbeddedPiAgent).toHaveBeenCalled();
|
||||
expect(blockReplies.length).toBe(0);
|
||||
const prompt =
|
||||
vi.mocked(runEmbeddedPiAgent).mock.calls[0]?.[0]?.prompt ?? "";
|
||||
expect(prompt).not.toContain("/status");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -712,7 +717,7 @@ describe("trigger handling", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps inline /status for unauthorized senders", async () => {
|
||||
it("strips inline /status for unauthorized senders", async () => {
|
||||
await withTempHome(async (home) => {
|
||||
vi.mocked(runEmbeddedPiAgent).mockResolvedValue({
|
||||
payloads: [{ text: "ok" }],
|
||||
@@ -749,7 +754,7 @@ describe("trigger handling", () => {
|
||||
expect(runEmbeddedPiAgent).toHaveBeenCalled();
|
||||
const prompt =
|
||||
vi.mocked(runEmbeddedPiAgent).mock.calls[0]?.[0]?.prompt ?? "";
|
||||
expect(prompt).toContain("/status");
|
||||
expect(prompt).not.toContain("/status");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user