mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-12 10:51:10 +00:00
test(agents): dedupe agent and cron test scaffolds
This commit is contained in:
@@ -1,79 +1,61 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createSessionsSpawnTool } from "./tools/sessions-spawn-tool.js";
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import "./test-helpers/fast-core-tools.js";
|
||||
import * as sessionsHarness from "./openclaw-tools.subagents.sessions-spawn.test-harness.js";
|
||||
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
||||
|
||||
vi.mock("../config/config.js", async () => {
|
||||
const actual = await vi.importActual("../config/config.js");
|
||||
return {
|
||||
...actual,
|
||||
loadConfig: () => ({
|
||||
agents: {
|
||||
defaults: {
|
||||
subagents: {
|
||||
runTimeoutSeconds: 900,
|
||||
},
|
||||
},
|
||||
},
|
||||
routing: {
|
||||
sessions: {
|
||||
mainKey: "agent:test:main",
|
||||
},
|
||||
},
|
||||
}),
|
||||
};
|
||||
});
|
||||
const MAIN_SESSION_KEY = "agent:test:main";
|
||||
|
||||
vi.mock("../gateway/call.js", () => {
|
||||
return {
|
||||
callGateway: vi.fn(async ({ method }: { method: string }) => {
|
||||
if (method === "agent") {
|
||||
return { runId: "run-123" };
|
||||
}
|
||||
return {};
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("../plugins/hook-runner-global.js", () => ({
|
||||
getGlobalHookRunner: () => null,
|
||||
}));
|
||||
|
||||
type GatewayCall = { method: string; params?: Record<string, unknown> };
|
||||
|
||||
async function getGatewayCalls(): Promise<GatewayCall[]> {
|
||||
const { callGateway } = await import("../gateway/call.js");
|
||||
return (callGateway as unknown as ReturnType<typeof vi.fn>).mock.calls.map(
|
||||
(call) => call[0] as GatewayCall,
|
||||
);
|
||||
function applySubagentTimeoutDefault(seconds: number) {
|
||||
sessionsHarness.setSessionsSpawnConfigOverride({
|
||||
session: { mainKey: "main", scope: "per-sender" },
|
||||
agents: { defaults: { subagents: { runTimeoutSeconds: seconds } } },
|
||||
routing: { sessions: { mainKey: MAIN_SESSION_KEY } },
|
||||
});
|
||||
}
|
||||
|
||||
function findLastCall(calls: GatewayCall[], predicate: (call: GatewayCall) => boolean) {
|
||||
for (let i = calls.length - 1; i >= 0; i -= 1) {
|
||||
const call = calls[i];
|
||||
if (call && predicate(call)) {
|
||||
return call;
|
||||
function getSubagentTimeout(
|
||||
calls: Array<{ method?: string; params?: unknown }>,
|
||||
): number | undefined {
|
||||
for (const call of calls) {
|
||||
if (call.method !== "agent") {
|
||||
continue;
|
||||
}
|
||||
const params = call.params as { lane?: string; timeout?: number } | undefined;
|
||||
if (params?.lane === "subagent") {
|
||||
return params.timeout;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
describe("sessions_spawn default runTimeoutSeconds", () => {
|
||||
it("uses config default when agent omits runTimeoutSeconds", async () => {
|
||||
const tool = createSessionsSpawnTool({ agentSessionKey: "agent:test:main" });
|
||||
const result = await tool.execute("call-1", { task: "hello" });
|
||||
expect(result.details).toMatchObject({ status: "accepted" });
|
||||
async function spawnSubagent(callId: string, payload: Record<string, unknown>) {
|
||||
const tool = await sessionsHarness.getSessionsSpawnTool({ agentSessionKey: MAIN_SESSION_KEY });
|
||||
const result = await tool.execute(callId, payload);
|
||||
expect(result.details).toMatchObject({ status: "accepted" });
|
||||
}
|
||||
|
||||
const calls = await getGatewayCalls();
|
||||
const agentCall = findLastCall(calls, (call) => call.method === "agent");
|
||||
expect(agentCall?.params?.timeout).toBe(900);
|
||||
describe("sessions_spawn default runTimeoutSeconds", () => {
|
||||
beforeEach(() => {
|
||||
sessionsHarness.resetSessionsSpawnConfigOverride();
|
||||
resetSubagentRegistryForTests();
|
||||
sessionsHarness.getCallGatewayMock().mockClear();
|
||||
});
|
||||
|
||||
it("uses config default when agent omits runTimeoutSeconds", async () => {
|
||||
applySubagentTimeoutDefault(900);
|
||||
const gateway = sessionsHarness.setupSessionsSpawnGatewayMock({});
|
||||
|
||||
await spawnSubagent("call-1", { task: "hello" });
|
||||
|
||||
expect(getSubagentTimeout(gateway.calls)).toBe(900);
|
||||
});
|
||||
|
||||
it("explicit runTimeoutSeconds wins over config default", async () => {
|
||||
const tool = createSessionsSpawnTool({ agentSessionKey: "agent:test:main" });
|
||||
const result = await tool.execute("call-2", { task: "hello", runTimeoutSeconds: 300 });
|
||||
expect(result.details).toMatchObject({ status: "accepted" });
|
||||
applySubagentTimeoutDefault(900);
|
||||
const gateway = sessionsHarness.setupSessionsSpawnGatewayMock({});
|
||||
|
||||
const calls = await getGatewayCalls();
|
||||
const agentCall = findLastCall(calls, (call) => call.method === "agent");
|
||||
expect(agentCall?.params?.timeout).toBe(300);
|
||||
await spawnSubagent("call-2", { task: "hello", runTimeoutSeconds: 300 });
|
||||
|
||||
expect(getSubagentTimeout(gateway.calls)).toBe(300);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user