mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 00:42:54 +00:00
refactor(test): share sessions_spawn e2e mocks
This commit is contained in:
@@ -1,44 +1,22 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it } from "vitest";
|
||||||
|
|
||||||
const callGatewayMock = vi.fn();
|
|
||||||
vi.mock("../gateway/call.js", () => ({
|
|
||||||
callGateway: (opts: unknown) => callGatewayMock(opts),
|
|
||||||
}));
|
|
||||||
|
|
||||||
let configOverride: ReturnType<(typeof import("../config/config.js"))["loadConfig"]> = {
|
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig: () => configOverride,
|
|
||||||
resolveGatewayPort: () => 18789,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
import "./test-helpers/fast-core-tools.js";
|
|
||||||
import { createOpenClawTools } from "./openclaw-tools.js";
|
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";
|
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
||||||
|
|
||||||
describe("openclaw-tools: subagents", () => {
|
describe("openclaw-tools: subagents", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
configOverride = {
|
resetConfigOverride();
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sessions_spawn allows cross-agent spawning when configured", async () => {
|
it("sessions_spawn allows cross-agent spawning when configured", async () => {
|
||||||
resetSubagentRegistryForTests();
|
resetSubagentRegistryForTests();
|
||||||
callGatewayMock.mockReset();
|
callGatewayMock.mockReset();
|
||||||
configOverride = {
|
setConfigOverride({
|
||||||
session: {
|
session: {
|
||||||
mainKey: "main",
|
mainKey: "main",
|
||||||
scope: "per-sender",
|
scope: "per-sender",
|
||||||
@@ -53,7 +31,7 @@ describe("openclaw-tools: subagents", () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
let childSessionKey: string | undefined;
|
let childSessionKey: string | undefined;
|
||||||
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
||||||
@@ -91,7 +69,7 @@ describe("openclaw-tools: subagents", () => {
|
|||||||
it("sessions_spawn allows any agent when allowlist is *", async () => {
|
it("sessions_spawn allows any agent when allowlist is *", async () => {
|
||||||
resetSubagentRegistryForTests();
|
resetSubagentRegistryForTests();
|
||||||
callGatewayMock.mockReset();
|
callGatewayMock.mockReset();
|
||||||
configOverride = {
|
setConfigOverride({
|
||||||
session: {
|
session: {
|
||||||
mainKey: "main",
|
mainKey: "main",
|
||||||
scope: "per-sender",
|
scope: "per-sender",
|
||||||
@@ -106,7 +84,7 @@ describe("openclaw-tools: subagents", () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
let childSessionKey: string | undefined;
|
let childSessionKey: string | undefined;
|
||||||
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
||||||
|
|||||||
@@ -1,39 +1,16 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
const callGatewayMock = vi.fn();
|
|
||||||
vi.mock("../gateway/call.js", () => ({
|
|
||||||
callGateway: (opts: unknown) => callGatewayMock(opts),
|
|
||||||
}));
|
|
||||||
|
|
||||||
let configOverride: ReturnType<(typeof import("../config/config.js"))["loadConfig"]> = {
|
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig: () => configOverride,
|
|
||||||
resolveGatewayPort: () => 18789,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
import "./test-helpers/fast-core-tools.js";
|
|
||||||
import { sleep } from "../utils.js";
|
import { sleep } from "../utils.js";
|
||||||
|
import "./test-helpers/fast-core-tools.js";
|
||||||
import { createOpenClawTools } from "./openclaw-tools.js";
|
import { createOpenClawTools } from "./openclaw-tools.js";
|
||||||
|
import {
|
||||||
|
callGatewayMock,
|
||||||
|
resetConfigOverride,
|
||||||
|
} from "./openclaw-tools.subagents.sessions-spawn.mocks.js";
|
||||||
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
||||||
|
|
||||||
describe("openclaw-tools: subagents", () => {
|
describe("openclaw-tools: subagents", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
configOverride = {
|
resetConfigOverride();
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sessions_spawn deletes session when cleanup=delete via agent.wait", async () => {
|
it("sessions_spawn deletes session when cleanup=delete via agent.wait", async () => {
|
||||||
|
|||||||
@@ -1,38 +1,16 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it } from "vitest";
|
||||||
|
|
||||||
const callGatewayMock = vi.fn();
|
|
||||||
vi.mock("../gateway/call.js", () => ({
|
|
||||||
callGateway: (opts: unknown) => callGatewayMock(opts),
|
|
||||||
}));
|
|
||||||
|
|
||||||
let configOverride: ReturnType<(typeof import("../config/config.js"))["loadConfig"]> = {
|
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig: () => configOverride,
|
|
||||||
resolveGatewayPort: () => 18789,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
import "./test-helpers/fast-core-tools.js";
|
|
||||||
import { createOpenClawTools } from "./openclaw-tools.js";
|
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";
|
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
||||||
|
|
||||||
describe("openclaw-tools: subagents", () => {
|
describe("openclaw-tools: subagents", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
configOverride = {
|
resetConfigOverride();
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sessions_spawn applies a model to the child session", async () => {
|
it("sessions_spawn applies a model to the child session", async () => {
|
||||||
@@ -164,10 +142,10 @@ describe("openclaw-tools: subagents", () => {
|
|||||||
it("sessions_spawn applies default subagent model from defaults config", async () => {
|
it("sessions_spawn applies default subagent model from defaults config", async () => {
|
||||||
resetSubagentRegistryForTests();
|
resetSubagentRegistryForTests();
|
||||||
callGatewayMock.mockReset();
|
callGatewayMock.mockReset();
|
||||||
configOverride = {
|
setConfigOverride({
|
||||||
session: { mainKey: "main", scope: "per-sender" },
|
session: { mainKey: "main", scope: "per-sender" },
|
||||||
agents: { defaults: { subagents: { model: "minimax/MiniMax-M2.1" } } },
|
agents: { defaults: { subagents: { model: "minimax/MiniMax-M2.1" } } },
|
||||||
};
|
});
|
||||||
const calls: Array<{ method?: string; params?: unknown }> = [];
|
const calls: Array<{ method?: string; params?: unknown }> = [];
|
||||||
|
|
||||||
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
||||||
|
|||||||
@@ -1,45 +1,23 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
const callGatewayMock = vi.fn();
|
|
||||||
vi.mock("../gateway/call.js", () => ({
|
|
||||||
callGateway: (opts: unknown) => callGatewayMock(opts),
|
|
||||||
}));
|
|
||||||
|
|
||||||
let configOverride: ReturnType<(typeof import("../config/config.js"))["loadConfig"]> = {
|
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig: () => configOverride,
|
|
||||||
resolveGatewayPort: () => 18789,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||||
import "./test-helpers/fast-core-tools.js";
|
|
||||||
import { createOpenClawTools } from "./openclaw-tools.js";
|
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";
|
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
||||||
|
|
||||||
describe("openclaw-tools: subagents", () => {
|
describe("openclaw-tools: subagents", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
configOverride = {
|
resetConfigOverride();
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sessions_spawn normalizes allowlisted agent ids", async () => {
|
it("sessions_spawn normalizes allowlisted agent ids", async () => {
|
||||||
resetSubagentRegistryForTests();
|
resetSubagentRegistryForTests();
|
||||||
callGatewayMock.mockReset();
|
callGatewayMock.mockReset();
|
||||||
configOverride = {
|
setConfigOverride({
|
||||||
session: {
|
session: {
|
||||||
mainKey: "main",
|
mainKey: "main",
|
||||||
scope: "per-sender",
|
scope: "per-sender",
|
||||||
@@ -54,7 +32,7 @@ describe("openclaw-tools: subagents", () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
let childSessionKey: string | undefined;
|
let childSessionKey: string | undefined;
|
||||||
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
||||||
@@ -92,7 +70,7 @@ describe("openclaw-tools: subagents", () => {
|
|||||||
it("sessions_spawn forbids cross-agent spawning when not allowed", async () => {
|
it("sessions_spawn forbids cross-agent spawning when not allowed", async () => {
|
||||||
resetSubagentRegistryForTests();
|
resetSubagentRegistryForTests();
|
||||||
callGatewayMock.mockReset();
|
callGatewayMock.mockReset();
|
||||||
configOverride = {
|
setConfigOverride({
|
||||||
session: {
|
session: {
|
||||||
mainKey: "main",
|
mainKey: "main",
|
||||||
scope: "per-sender",
|
scope: "per-sender",
|
||||||
@@ -107,7 +85,7 @@ describe("openclaw-tools: subagents", () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
|
|
||||||
const tool = createOpenClawTools({
|
const tool = createOpenClawTools({
|
||||||
agentSessionKey: "main",
|
agentSessionKey: "main",
|
||||||
|
|||||||
@@ -1,50 +1,28 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it } from "vitest";
|
||||||
|
|
||||||
const callGatewayMock = vi.fn();
|
|
||||||
vi.mock("../gateway/call.js", () => ({
|
|
||||||
callGateway: (opts: unknown) => callGatewayMock(opts),
|
|
||||||
}));
|
|
||||||
|
|
||||||
let configOverride: ReturnType<(typeof import("../config/config.js"))["loadConfig"]> = {
|
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig: () => configOverride,
|
|
||||||
resolveGatewayPort: () => 18789,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
import "./test-helpers/fast-core-tools.js";
|
|
||||||
import { createOpenClawTools } from "./openclaw-tools.js";
|
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";
|
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
||||||
|
|
||||||
describe("openclaw-tools: subagents", () => {
|
describe("openclaw-tools: subagents", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
configOverride = {
|
resetConfigOverride();
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sessions_spawn prefers per-agent subagent model over defaults", async () => {
|
it("sessions_spawn prefers per-agent subagent model over defaults", async () => {
|
||||||
resetSubagentRegistryForTests();
|
resetSubagentRegistryForTests();
|
||||||
callGatewayMock.mockReset();
|
callGatewayMock.mockReset();
|
||||||
configOverride = {
|
setConfigOverride({
|
||||||
session: { mainKey: "main", scope: "per-sender" },
|
session: { mainKey: "main", scope: "per-sender" },
|
||||||
agents: {
|
agents: {
|
||||||
defaults: { subagents: { model: "minimax/MiniMax-M2.1" } },
|
defaults: { subagents: { model: "minimax/MiniMax-M2.1" } },
|
||||||
list: [{ id: "research", subagents: { model: "opencode/claude" } }],
|
list: [{ id: "research", subagents: { model: "opencode/claude" } }],
|
||||||
},
|
},
|
||||||
};
|
});
|
||||||
const calls: Array<{ method?: string; params?: unknown }> = [];
|
const calls: Array<{ method?: string; params?: unknown }> = [];
|
||||||
|
|
||||||
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
callGatewayMock.mockImplementation(async (opts: unknown) => {
|
||||||
|
|||||||
@@ -1,40 +1,17 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { sleep } from "../utils.ts";
|
|
||||||
|
|
||||||
const callGatewayMock = vi.fn();
|
|
||||||
vi.mock("../gateway/call.js", () => ({
|
|
||||||
callGateway: (opts: unknown) => callGatewayMock(opts),
|
|
||||||
}));
|
|
||||||
|
|
||||||
let configOverride: ReturnType<(typeof import("../config/config.js"))["loadConfig"]> = {
|
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
vi.mock("../config/config.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
loadConfig: () => configOverride,
|
|
||||||
resolveGatewayPort: () => 18789,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||||
import "./test-helpers/fast-core-tools.js";
|
import { sleep } from "../utils.ts";
|
||||||
import { createOpenClawTools } from "./openclaw-tools.js";
|
import { createOpenClawTools } from "./openclaw-tools.js";
|
||||||
|
import "./test-helpers/fast-core-tools.js";
|
||||||
|
import {
|
||||||
|
callGatewayMock,
|
||||||
|
resetConfigOverride,
|
||||||
|
} from "./openclaw-tools.subagents.sessions-spawn.mocks.js";
|
||||||
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
import { resetSubagentRegistryForTests } from "./subagent-registry.js";
|
||||||
|
|
||||||
describe("openclaw-tools: subagents", () => {
|
describe("openclaw-tools: subagents", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
configOverride = {
|
resetConfigOverride();
|
||||||
session: {
|
|
||||||
mainKey: "main",
|
|
||||||
scope: "per-sender",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sessions_spawn runs cleanup flow after subagent completion", async () => {
|
it("sessions_spawn runs cleanup flow after subagent completion", async () => {
|
||||||
|
|||||||
36
src/agents/openclaw-tools.subagents.sessions-spawn.mocks.ts
Normal file
36
src/agents/openclaw-tools.subagents.sessions-spawn.mocks.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { vi } from "vitest";
|
||||||
|
|
||||||
|
export const callGatewayMock = vi.fn();
|
||||||
|
vi.mock("../gateway/call.js", () => ({
|
||||||
|
callGateway: (opts: unknown) => callGatewayMock(opts),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export type SessionsSpawnTestConfig = ReturnType<
|
||||||
|
(typeof import("../config/config.js"))["loadConfig"]
|
||||||
|
>;
|
||||||
|
|
||||||
|
const defaultConfigOverride: SessionsSpawnTestConfig = {
|
||||||
|
session: {
|
||||||
|
mainKey: "main",
|
||||||
|
scope: "per-sender",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
let configOverride: SessionsSpawnTestConfig = defaultConfigOverride;
|
||||||
|
|
||||||
|
export function resetConfigOverride() {
|
||||||
|
configOverride = defaultConfigOverride;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function setConfigOverride(next: SessionsSpawnTestConfig) {
|
||||||
|
configOverride = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
vi.mock("../config/config.js", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("../config/config.js")>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
loadConfig: () => configOverride,
|
||||||
|
resolveGatewayPort: () => 18789,
|
||||||
|
};
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user