mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 23:08:28 +00:00
123 lines
3.4 KiB
TypeScript
123 lines
3.4 KiB
TypeScript
import { beforeEach, describe, expect, it } from "vitest";
|
|
import { createOpenClawTools } from "./openclaw-tools.js";
|
|
import "./test-helpers/fast-core-tools.js";
|
|
import {
|
|
callGatewayMock,
|
|
resetConfigOverride,
|
|
setConfigOverride,
|
|
} from "./openclaw-tools.subagents.sessions-spawn.mocks.js";
|
|
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
|
|
|
describe("openclaw-tools: subagents", () => {
|
|
beforeEach(() => {
|
|
resetConfigOverride();
|
|
});
|
|
|
|
it("sessions_spawn allows cross-agent spawning when configured", async () => {
|
|
resetSubagentRegistryForTests();
|
|
callGatewayMock.mockReset();
|
|
setConfigOverride({
|
|
session: {
|
|
mainKey: "main",
|
|
scope: "per-sender",
|
|
},
|
|
agents: {
|
|
list: [
|
|
{
|
|
id: "main",
|
|
subagents: {
|
|
allowAgents: ["beta"],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
let childSessionKey: string | undefined;
|
|
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
|
const request = opts as { method?: string; params?: unknown };
|
|
if (request.method === "agent") {
|
|
const params = request.params as { sessionKey?: string } | undefined;
|
|
childSessionKey = params?.sessionKey;
|
|
return { runId: "run-1", status: "accepted", acceptedAt: 5000 };
|
|
}
|
|
if (request.method === "agent.wait") {
|
|
return { status: "timeout" };
|
|
}
|
|
return {};
|
|
});
|
|
|
|
const tool = createOpenClawTools({
|
|
agentSessionKey: "main",
|
|
agentChannel: "whatsapp",
|
|
}).find((candidate) => candidate.name === "sessions_spawn");
|
|
if (!tool) {
|
|
throw new Error("missing sessions_spawn tool");
|
|
}
|
|
|
|
const result = await tool.execute("call7", {
|
|
task: "do thing",
|
|
agentId: "beta",
|
|
});
|
|
|
|
expect(result.details).toMatchObject({
|
|
status: "accepted",
|
|
runId: "run-1",
|
|
});
|
|
expect(childSessionKey?.startsWith("agent:beta:subagent:")).toBe(true);
|
|
});
|
|
it("sessions_spawn allows any agent when allowlist is *", async () => {
|
|
resetSubagentRegistryForTests();
|
|
callGatewayMock.mockReset();
|
|
setConfigOverride({
|
|
session: {
|
|
mainKey: "main",
|
|
scope: "per-sender",
|
|
},
|
|
agents: {
|
|
list: [
|
|
{
|
|
id: "main",
|
|
subagents: {
|
|
allowAgents: ["*"],
|
|
},
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
let childSessionKey: string | undefined;
|
|
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
|
const request = opts as { method?: string; params?: unknown };
|
|
if (request.method === "agent") {
|
|
const params = request.params as { sessionKey?: string } | undefined;
|
|
childSessionKey = params?.sessionKey;
|
|
return { runId: "run-1", status: "accepted", acceptedAt: 5100 };
|
|
}
|
|
if (request.method === "agent.wait") {
|
|
return { status: "timeout" };
|
|
}
|
|
return {};
|
|
});
|
|
|
|
const tool = createOpenClawTools({
|
|
agentSessionKey: "main",
|
|
agentChannel: "whatsapp",
|
|
}).find((candidate) => candidate.name === "sessions_spawn");
|
|
if (!tool) {
|
|
throw new Error("missing sessions_spawn tool");
|
|
}
|
|
|
|
const result = await tool.execute("call8", {
|
|
task: "do thing",
|
|
agentId: "beta",
|
|
});
|
|
|
|
expect(result.details).toMatchObject({
|
|
status: "accepted",
|
|
runId: "run-1",
|
|
});
|
|
expect(childSessionKey?.startsWith("agent:beta:subagent:")).toBe(true);
|
|
});
|
|
});
|