test: share subagent command fixtures

This commit is contained in:
Peter Steinberger
2026-04-20 19:57:33 +01:00
parent 8e519aa826
commit a2f158e5ed
6 changed files with 51 additions and 66 deletions

View File

@@ -2,10 +2,10 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import type { SessionEntry } from "../../config/sessions/types.js";
import type { SessionBindingRecord } from "../../infra/outbound/session-binding-service.js";
import { createEmptyInlineDirectives } from "./commands-subagents.test-helpers.js";
import { handleSubagentsFocusAction } from "./commands-subagents/action-focus.js";
import { handleSubagentsUnfocusAction } from "./commands-subagents/action-unfocus.js";
import type { HandleCommandsParams } from "./commands-types.js";
import type { InlineDirectives } from "./directive-handling.js";
const THREAD_CHANNEL = "thread-chat";
const ROOM_CHANNEL = "room-chat";
@@ -159,26 +159,6 @@ function buildCommandParams(params?: {
senderId?: string;
sessionEntry?: SessionEntry;
}): HandleCommandsParams {
const directives: InlineDirectives = {
cleaned: "",
hasThinkDirective: false,
hasVerboseDirective: false,
hasFastDirective: false,
hasReasoningDirective: false,
hasTraceDirective: false,
hasElevatedDirective: false,
hasExecDirective: false,
hasExecOptions: false,
invalidExecHost: false,
invalidExecSecurity: false,
invalidExecAsk: false,
invalidExecNode: false,
hasStatusDirective: false,
hasModelDirective: false,
hasQueueDirective: false,
queueReset: false,
hasQueueOptions: false,
};
return {
cfg: params?.cfg ?? baseCfg,
ctx: {
@@ -194,7 +174,7 @@ function buildCommandParams(params?: {
rawBodyNormalized: "",
commandBodyNormalized: "",
},
directives,
directives: createEmptyInlineDirectives(),
elevated: { enabled: false, allowed: false, failures: [] },
sessionEntry: params?.sessionEntry,
sessionKey: "agent:main:main",

View File

@@ -1,5 +1,16 @@
import { vi } from "vitest";
import { buildSubagentsSendContext } from "./commands-subagents.test-helpers.js";
export const subagentControlMocks = {
sendControlledSubagentMessage: vi.fn(),
steerControlledSubagentRun: vi.fn(),
};
vi.doMock("./commands-subagents-control.runtime.js", () => ({
sendControlledSubagentMessage: subagentControlMocks.sendControlledSubagentMessage,
steerControlledSubagentRun: subagentControlMocks.steerControlledSubagentRun,
}));
export function buildSubagentsDispatchContext(params: {
handledPrefix: string;
restTokens: string[];

View File

@@ -1,15 +1,10 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { buildSubagentsDispatchContext } from "./commands-subagents-send-steer.test-support.js";
import {
buildSubagentsDispatchContext,
subagentControlMocks,
} from "./commands-subagents-send-steer.test-support.js";
import { handleSubagentsSendAction } from "./commands-subagents/action-send.js";
const sendControlledSubagentMessageMock = vi.hoisted(() => vi.fn());
const steerControlledSubagentRunMock = vi.hoisted(() => vi.fn());
vi.mock("./commands-subagents-control.runtime.js", () => ({
sendControlledSubagentMessage: sendControlledSubagentMessageMock,
steerControlledSubagentRun: steerControlledSubagentRunMock,
}));
const buildContext = () =>
buildSubagentsDispatchContext({
handledPrefix: "/subagents",
@@ -22,7 +17,7 @@ describe("subagents send action", () => {
});
it("formats accepted send replies", async () => {
sendControlledSubagentMessageMock.mockResolvedValue({
subagentControlMocks.sendControlledSubagentMessage.mockResolvedValue({
status: "accepted",
runId: "run-followup-1",
replyText: "custom reply",
@@ -35,7 +30,7 @@ describe("subagents send action", () => {
});
it("formats forbidden send replies", async () => {
sendControlledSubagentMessageMock.mockResolvedValue({
subagentControlMocks.sendControlledSubagentMessage.mockResolvedValue({
status: "forbidden",
error: "Leaf subagents cannot control other sessions.",
});

View File

@@ -2,9 +2,9 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import type { SpawnSubagentResult } from "../../agents/subagent-spawn.js";
import type { OpenClawConfig } from "../../config/config.js";
import type { SessionEntry } from "../../config/sessions/types.js";
import { createEmptyInlineDirectives } from "./commands-subagents.test-helpers.js";
import { handleSubagentsSpawnAction } from "./commands-subagents/action-spawn.js";
import type { HandleCommandsParams } from "./commands-types.js";
import type { InlineDirectives } from "./directive-handling.js";
const spawnSubagentDirectMock = vi.hoisted(() => vi.fn());
@@ -41,26 +41,6 @@ function buildContext(params?: {
context?: Partial<HandleCommandsParams["ctx"]>;
sessionEntry?: SessionEntry | undefined;
}) {
const directives: InlineDirectives = {
cleaned: "",
hasThinkDirective: false,
hasVerboseDirective: false,
hasFastDirective: false,
hasReasoningDirective: false,
hasTraceDirective: false,
hasElevatedDirective: false,
hasExecDirective: false,
hasExecOptions: false,
invalidExecHost: false,
invalidExecSecurity: false,
invalidExecAsk: false,
invalidExecNode: false,
hasStatusDirective: false,
hasModelDirective: false,
hasQueueDirective: false,
queueReset: false,
hasQueueOptions: false,
};
const ctx = {
OriginatingChannel: "whatsapp",
OriginatingTo: "channel:origin",
@@ -82,7 +62,7 @@ function buildContext(params?: {
commandBodyNormalized: "",
to: params?.commandTo ?? "channel:command",
},
directives,
directives: createEmptyInlineDirectives(),
elevated: { enabled: false, allowed: false, failures: [] },
sessionKey: "agent:main:main",
workspaceDir: "/tmp/openclaw-subagents-spawn",

View File

@@ -1,15 +1,10 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { buildSubagentsDispatchContext } from "./commands-subagents-send-steer.test-support.js";
import {
buildSubagentsDispatchContext,
subagentControlMocks,
} from "./commands-subagents-send-steer.test-support.js";
import { handleSubagentsSendAction } from "./commands-subagents/action-send.js";
const sendControlledSubagentMessageMock = vi.hoisted(() => vi.fn());
const steerControlledSubagentRunMock = vi.hoisted(() => vi.fn());
vi.mock("./commands-subagents-control.runtime.js", () => ({
sendControlledSubagentMessage: sendControlledSubagentMessageMock,
steerControlledSubagentRun: steerControlledSubagentRunMock,
}));
const buildContext = () =>
buildSubagentsDispatchContext({
handledPrefix: "/steer",
@@ -22,7 +17,7 @@ describe("subagents steer action", () => {
});
it("formats accepted steer replies", async () => {
steerControlledSubagentRunMock.mockResolvedValue({
subagentControlMocks.steerControlledSubagentRun.mockResolvedValue({
status: "accepted",
runId: "run-steer-1",
});
@@ -34,7 +29,7 @@ describe("subagents steer action", () => {
});
it("formats steer dispatch errors", async () => {
steerControlledSubagentRunMock.mockResolvedValue({
subagentControlMocks.steerControlledSubagentRun.mockResolvedValue({
status: "error",
error: "dispatch failed",
});

View File

@@ -1,6 +1,7 @@
import type { SubagentRunRecord } from "../../agents/subagent-registry.types.js";
import type { OpenClawConfig } from "../../config/types.openclaw.js";
import type { handleSubagentsSendAction } from "./commands-subagents/action-send.js";
import type { InlineDirectives } from "./directive-handling.js";
export function buildSubagentRun(): SubagentRunRecord {
return {
@@ -42,3 +43,26 @@ export function buildSubagentsSendContext(params?: {
restTokens: params?.restTokens ?? [],
} as Parameters<typeof handleSubagentsSendAction>[0];
}
export function createEmptyInlineDirectives(): InlineDirectives {
return {
cleaned: "",
hasThinkDirective: false,
hasVerboseDirective: false,
hasFastDirective: false,
hasReasoningDirective: false,
hasTraceDirective: false,
hasElevatedDirective: false,
hasExecDirective: false,
hasExecOptions: false,
invalidExecHost: false,
invalidExecSecurity: false,
invalidExecAsk: false,
invalidExecNode: false,
hasStatusDirective: false,
hasModelDirective: false,
hasQueueDirective: false,
queueReset: false,
hasQueueOptions: false,
};
}