mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-07 10:21:25 +00:00
test: dedupe auto-reply web and signal flows
This commit is contained in:
@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { resetSubagentRegistryForTests } from "../../agents/subagent-registry.js";
|
||||
import type { SpawnSubagentResult } from "../../agents/subagent-spawn.js";
|
||||
import type { OpenClawConfig } from "../../config/config.js";
|
||||
import { installSubagentsCommandCoreMocks } from "./commands-subagents.test-mocks.js";
|
||||
|
||||
const hoisted = vi.hoisted(() => {
|
||||
const spawnSubagentDirectMock = vi.fn();
|
||||
@@ -18,18 +19,7 @@ vi.mock("../../gateway/call.js", () => ({
|
||||
callGateway: (opts: unknown) => hoisted.callGatewayMock(opts),
|
||||
}));
|
||||
|
||||
vi.mock("../../config/config.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../../config/config.js")>();
|
||||
return {
|
||||
...actual,
|
||||
loadConfig: () => ({}),
|
||||
};
|
||||
});
|
||||
|
||||
// Prevent transitive import chain from reaching discord/monitor which needs https-proxy-agent.
|
||||
vi.mock("../../discord/monitor/gateway-plugin.js", () => ({
|
||||
createDiscordGatewayPlugin: () => ({}),
|
||||
}));
|
||||
installSubagentsCommandCoreMocks();
|
||||
|
||||
// Dynamic import to ensure mocks are installed first.
|
||||
const { handleSubagentsCommand } = await import("./commands-subagents.js");
|
||||
@@ -64,6 +54,41 @@ describe("/subagents spawn command", () => {
|
||||
hoisted.callGatewayMock.mockClear();
|
||||
});
|
||||
|
||||
async function runSpawnWithFlag(
|
||||
flagSegment: string,
|
||||
result: SpawnSubagentResult = acceptedResult(),
|
||||
) {
|
||||
spawnSubagentDirectMock.mockResolvedValue(result);
|
||||
const params = buildCommandTestParams(
|
||||
`/subagents spawn beta do the thing ${flagSegment}`,
|
||||
baseCfg,
|
||||
);
|
||||
const commandResult = await handleSubagentsCommand(params, true);
|
||||
expect(commandResult).not.toBeNull();
|
||||
expect(commandResult?.reply?.text).toContain("Spawned subagent beta");
|
||||
const [spawnParams] = spawnSubagentDirectMock.mock.calls[0];
|
||||
return spawnParams as { model?: string; thinking?: string; task?: string };
|
||||
}
|
||||
|
||||
async function runSuccessfulSpawn(params?: {
|
||||
commandText?: string;
|
||||
context?: Record<string, unknown>;
|
||||
mutateParams?: (commandParams: ReturnType<typeof buildCommandTestParams>) => void;
|
||||
}) {
|
||||
spawnSubagentDirectMock.mockResolvedValue(acceptedResult());
|
||||
const commandParams = buildCommandTestParams(
|
||||
params?.commandText ?? "/subagents spawn beta do the thing",
|
||||
baseCfg,
|
||||
params?.context,
|
||||
);
|
||||
params?.mutateParams?.(commandParams);
|
||||
const result = await handleSubagentsCommand(commandParams, true);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.reply?.text).toContain("Spawned subagent beta");
|
||||
const [spawnParams, spawnCtx] = spawnSubagentDirectMock.mock.calls[0];
|
||||
return { spawnParams, spawnCtx, commandParams, commandResult: result };
|
||||
}
|
||||
|
||||
it("shows usage when agentId is missing", async () => {
|
||||
const params = buildCommandTestParams("/subagents spawn", baseCfg);
|
||||
const result = await handleSubagentsCommand(params, true);
|
||||
@@ -82,16 +107,10 @@ describe("/subagents spawn command", () => {
|
||||
});
|
||||
|
||||
it("spawns subagent and confirms reply text and child session key", async () => {
|
||||
spawnSubagentDirectMock.mockResolvedValue(acceptedResult());
|
||||
const params = buildCommandTestParams("/subagents spawn beta do the thing", baseCfg);
|
||||
const result = await handleSubagentsCommand(params, true);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.reply?.text).toContain("Spawned subagent beta");
|
||||
expect(result?.reply?.text).toContain("agent:beta:subagent:test-uuid");
|
||||
expect(result?.reply?.text).toContain("run-spaw");
|
||||
|
||||
const { spawnParams, spawnCtx, commandResult } = await runSuccessfulSpawn();
|
||||
expect(commandResult?.reply?.text).toContain("agent:beta:subagent:test-uuid");
|
||||
expect(commandResult?.reply?.text).toContain("run-spaw");
|
||||
expect(spawnSubagentDirectMock).toHaveBeenCalledOnce();
|
||||
const [spawnParams, spawnCtx] = spawnSubagentDirectMock.mock.calls[0];
|
||||
expect(spawnParams.task).toBe("do the thing");
|
||||
expect(spawnParams.agentId).toBe("beta");
|
||||
expect(spawnParams.mode).toBe("run");
|
||||
@@ -101,50 +120,32 @@ describe("/subagents spawn command", () => {
|
||||
});
|
||||
|
||||
it("spawns with --model flag and passes model to spawnSubagentDirect", async () => {
|
||||
spawnSubagentDirectMock.mockResolvedValue(acceptedResult({ modelApplied: true }));
|
||||
const params = buildCommandTestParams(
|
||||
"/subagents spawn beta do the thing --model openai/gpt-4o",
|
||||
baseCfg,
|
||||
const spawnParams = await runSpawnWithFlag(
|
||||
"--model openai/gpt-4o",
|
||||
acceptedResult({ modelApplied: true }),
|
||||
);
|
||||
const result = await handleSubagentsCommand(params, true);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.reply?.text).toContain("Spawned subagent beta");
|
||||
|
||||
const [spawnParams] = spawnSubagentDirectMock.mock.calls[0];
|
||||
expect(spawnParams.model).toBe("openai/gpt-4o");
|
||||
expect(spawnParams.task).toBe("do the thing");
|
||||
});
|
||||
|
||||
it("spawns with --thinking flag and passes thinking to spawnSubagentDirect", async () => {
|
||||
spawnSubagentDirectMock.mockResolvedValue(acceptedResult());
|
||||
const params = buildCommandTestParams(
|
||||
"/subagents spawn beta do the thing --thinking high",
|
||||
baseCfg,
|
||||
);
|
||||
const result = await handleSubagentsCommand(params, true);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.reply?.text).toContain("Spawned subagent beta");
|
||||
|
||||
const [spawnParams] = spawnSubagentDirectMock.mock.calls[0];
|
||||
const spawnParams = await runSpawnWithFlag("--thinking high");
|
||||
expect(spawnParams.thinking).toBe("high");
|
||||
expect(spawnParams.task).toBe("do the thing");
|
||||
});
|
||||
|
||||
it("passes group context from session entry to spawnSubagentDirect", async () => {
|
||||
spawnSubagentDirectMock.mockResolvedValue(acceptedResult());
|
||||
const params = buildCommandTestParams("/subagents spawn beta do the thing", baseCfg);
|
||||
params.sessionEntry = {
|
||||
sessionId: "session-main",
|
||||
updatedAt: Date.now(),
|
||||
groupId: "group-1",
|
||||
groupChannel: "#group-channel",
|
||||
space: "workspace-1",
|
||||
};
|
||||
const result = await handleSubagentsCommand(params, true);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.reply?.text).toContain("Spawned subagent beta");
|
||||
|
||||
const [, spawnCtx] = spawnSubagentDirectMock.mock.calls[0];
|
||||
const { spawnCtx } = await runSuccessfulSpawn({
|
||||
mutateParams: (commandParams) => {
|
||||
commandParams.sessionEntry = {
|
||||
sessionId: "session-main",
|
||||
updatedAt: Date.now(),
|
||||
groupId: "group-1",
|
||||
groupChannel: "#group-channel",
|
||||
space: "workspace-1",
|
||||
};
|
||||
},
|
||||
});
|
||||
expect(spawnCtx).toMatchObject({
|
||||
agentGroupId: "group-1",
|
||||
agentGroupChannel: "#group-channel",
|
||||
@@ -153,38 +154,32 @@ describe("/subagents spawn command", () => {
|
||||
});
|
||||
|
||||
it("prefers CommandTargetSessionKey for native /subagents spawn", async () => {
|
||||
spawnSubagentDirectMock.mockResolvedValue(acceptedResult());
|
||||
const params = buildCommandTestParams("/subagents spawn beta do the thing", baseCfg, {
|
||||
CommandSource: "native",
|
||||
CommandTargetSessionKey: "agent:main:main",
|
||||
OriginatingChannel: "discord",
|
||||
OriginatingTo: "channel:12345",
|
||||
const { spawnCtx } = await runSuccessfulSpawn({
|
||||
context: {
|
||||
CommandSource: "native",
|
||||
CommandTargetSessionKey: "agent:main:main",
|
||||
OriginatingChannel: "discord",
|
||||
OriginatingTo: "channel:12345",
|
||||
},
|
||||
mutateParams: (commandParams) => {
|
||||
commandParams.sessionKey = "agent:main:slack:slash:u1";
|
||||
},
|
||||
});
|
||||
params.sessionKey = "agent:main:slack:slash:u1";
|
||||
|
||||
const result = await handleSubagentsCommand(params, true);
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.reply?.text).toContain("Spawned subagent beta");
|
||||
const [, spawnCtx] = spawnSubagentDirectMock.mock.calls[0];
|
||||
expect(spawnCtx.agentSessionKey).toBe("agent:main:main");
|
||||
expect(spawnCtx.agentChannel).toBe("discord");
|
||||
expect(spawnCtx.agentTo).toBe("channel:12345");
|
||||
});
|
||||
|
||||
it("falls back to OriginatingTo for agentTo when command.to is missing", async () => {
|
||||
spawnSubagentDirectMock.mockResolvedValue(acceptedResult());
|
||||
const params = buildCommandTestParams("/subagents spawn beta do the thing", baseCfg, {
|
||||
OriginatingTo: "channel:manual",
|
||||
To: "channel:fallback-from-to",
|
||||
const { spawnCtx } = await runSuccessfulSpawn({
|
||||
context: {
|
||||
OriginatingTo: "channel:manual",
|
||||
To: "channel:fallback-from-to",
|
||||
},
|
||||
mutateParams: (commandParams) => {
|
||||
commandParams.command.to = undefined;
|
||||
},
|
||||
});
|
||||
params.command.to = undefined;
|
||||
|
||||
const result = await handleSubagentsCommand(params, true);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.reply?.text).toContain("Spawned subagent beta");
|
||||
|
||||
const [, spawnCtx] = spawnSubagentDirectMock.mock.calls[0];
|
||||
expect(spawnCtx).toMatchObject({ agentTo: "channel:manual" });
|
||||
});
|
||||
it("returns forbidden for unauthorized cross-agent spawn", async () => {
|
||||
@@ -199,11 +194,8 @@ describe("/subagents spawn command", () => {
|
||||
});
|
||||
|
||||
it("allows cross-agent spawn when in allowlist", async () => {
|
||||
spawnSubagentDirectMock.mockResolvedValue(acceptedResult());
|
||||
const params = buildCommandTestParams("/subagents spawn beta do the thing", baseCfg);
|
||||
const result = await handleSubagentsCommand(params, true);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.reply?.text).toContain("Spawned subagent beta");
|
||||
await runSuccessfulSpawn();
|
||||
expect(spawnSubagentDirectMock).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("ignores unauthorized sender (silent, no reply)", async () => {
|
||||
|
||||
Reference in New Issue
Block a user