test(agents): dedupe agent and cron test scaffolds

This commit is contained in:
Peter Steinberger
2026-03-02 06:40:42 +00:00
parent 281494ae52
commit 7e29d604ba
38 changed files with 3114 additions and 4486 deletions

View File

@@ -1,83 +1,78 @@
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 harness 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: {
thinking: "high",
},
},
},
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 {};
}),
};
});
type ThinkingLevel = "high" | "medium" | "low";
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 applyThinkingDefault(thinking: ThinkingLevel) {
harness.setSessionsSpawnConfigOverride({
session: { mainKey: "main", scope: "per-sender" },
agents: { defaults: { subagents: { thinking } } },
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 findSubagentThinking(
calls: Array<{ method?: string; params?: unknown }>,
): string | undefined {
for (const call of calls) {
if (call.method !== "agent") {
continue;
}
const params = call.params as { lane?: string; thinking?: string } | undefined;
if (params?.lane === "subagent") {
return params.thinking;
}
}
return undefined;
}
async function expectThinkingPropagation(params: {
function findPatchedThinking(
calls: Array<{ method?: string; params?: unknown }>,
): string | undefined {
for (let index = calls.length - 1; index >= 0; index -= 1) {
const entry = calls[index];
if (!entry || entry.method !== "sessions.patch") {
continue;
}
const params = entry.params as { thinkingLevel?: string } | undefined;
if (params?.thinkingLevel) {
return params.thinkingLevel;
}
}
return undefined;
}
async function expectThinkingPropagation(input: {
callId: string;
payload: Record<string, unknown>;
expectedThinking: string;
expected: ThinkingLevel;
}) {
const tool = createSessionsSpawnTool({ agentSessionKey: "agent:test:main" });
const result = await tool.execute(params.callId, params.payload);
const gateway = harness.setupSessionsSpawnGatewayMock({});
const tool = await harness.getSessionsSpawnTool({ agentSessionKey: MAIN_SESSION_KEY });
const result = await tool.execute(input.callId, input.payload);
expect(result.details).toMatchObject({ status: "accepted" });
const calls = await getGatewayCalls();
const agentCall = findLastCall(calls, (call) => call.method === "agent");
const thinkingPatch = findLastCall(
calls,
(call) => call.method === "sessions.patch" && call.params?.thinkingLevel !== undefined,
);
expect(agentCall?.params?.thinking).toBe(params.expectedThinking);
expect(thinkingPatch?.params?.thinkingLevel).toBe(params.expectedThinking);
expect(findSubagentThinking(gateway.calls)).toBe(input.expected);
expect(findPatchedThinking(gateway.calls)).toBe(input.expected);
}
describe("sessions_spawn thinking defaults", () => {
beforeEach(() => {
harness.resetSessionsSpawnConfigOverride();
resetSubagentRegistryForTests();
harness.getCallGatewayMock().mockClear();
applyThinkingDefault("high");
});
it("applies agents.defaults.subagents.thinking when thinking is omitted", async () => {
await expectThinkingPropagation({
callId: "call-1",
payload: { task: "hello" },
expectedThinking: "high",
expected: "high",
});
});
@@ -85,7 +80,7 @@ describe("sessions_spawn thinking defaults", () => {
await expectThinkingPropagation({
callId: "call-2",
payload: { task: "hello", thinking: "low" },
expectedThinking: "low",
expected: "low",
});
});
});