test: narrow standalone directive checks

This commit is contained in:
Peter Steinberger
2026-04-11 06:30:58 +01:00
parent e4e6f42192
commit d35bd8d264

View File

@@ -1,6 +1,8 @@
import "./reply.directive.directive-behavior.e2e-mocks.js";
import { describe, expect, it } from "vitest";
import { loadSessionStore } from "../config/sessions.js";
import type { ModelAliasIndex } from "../agents/model-selection.js";
import type { OpenClawConfig } from "../config/config.js";
import type { SessionEntry } from "../config/sessions.js";
import {
installDirectiveBehaviorE2EHooks,
makeWhatsAppDirectiveConfig,
@@ -10,60 +12,75 @@ import {
} from "./reply.directive.directive-behavior.e2e-harness.js";
import { runEmbeddedPiAgentMock } from "./reply.directive.directive-behavior.e2e-mocks.js";
import { getReplyFromConfig } from "./reply.js";
import { handleDirectiveOnly } from "./reply/directive-handling.impl.js";
import type { HandleDirectiveOnlyParams } from "./reply/directive-handling.params.js";
import { parseInlineDirectives } from "./reply/directive-handling.parse.js";
async function runThinkDirectiveAndGetText(home: string): Promise<string | undefined> {
const res = await getReplyFromConfig(
{ Body: "/think", From: "+1222", To: "+1222", CommandAuthorized: true },
{},
makeWhatsAppDirectiveConfig(home, {
model: "anthropic/claude-opus-4-6",
thinkingDefault: "high",
}),
);
return replyText(res);
const emptyAliasIndex: ModelAliasIndex = {
byAlias: new Map(),
byKey: new Map(),
};
async function runDirectiveOnly(
body: string,
overrides: Partial<HandleDirectiveOnlyParams> = {},
): Promise<{ text?: string; sessionEntry: SessionEntry }> {
const sessionKey = "agent:main:whatsapp:+1222";
const sessionEntry: SessionEntry = {
sessionId: "directive",
updatedAt: Date.now(),
};
const result = await handleDirectiveOnly({
cfg: {
commands: { text: true },
agents: {
defaults: {
model: "anthropic/claude-opus-4-6",
workspace: "/tmp/openclaw",
},
},
} as OpenClawConfig,
directives: parseInlineDirectives(body),
sessionEntry,
sessionStore: { [sessionKey]: sessionEntry },
sessionKey,
elevatedEnabled: false,
elevatedAllowed: false,
defaultProvider: "anthropic",
defaultModel: "claude-opus-4-6",
aliasIndex: emptyAliasIndex,
allowedModelKeys: new Set(["anthropic/claude-opus-4-6"]),
allowedModelCatalog: [],
resetModelOverride: false,
provider: "anthropic",
model: "claude-opus-4-6",
initialModelLabel: "anthropic/claude-opus-4-6",
formatModelSwitchEvent: (label) => `Switched to ${label}`,
...overrides,
});
return { text: result?.text, sessionEntry };
}
describe("directive behavior", () => {
installDirectiveBehaviorE2EHooks();
it("handles standalone verbose directives and persistence", async () => {
await withTempHome(async (home) => {
const storePath = sessionStorePath(home);
const enabled = await runDirectiveOnly("/verbose on");
expect(enabled.text).toMatch(/^⚙️ Verbose logging enabled\./);
expect(enabled.sessionEntry.verboseLevel).toBe("on");
const enabledRes = await getReplyFromConfig(
{ Body: "/verbose on", From: "+1222", To: "+1222", CommandAuthorized: true },
{},
makeWhatsAppDirectiveConfig(home, { model: "anthropic/claude-opus-4-6" }),
);
expect(replyText(enabledRes)).toMatch(/^⚙️ Verbose logging enabled\./);
const disabledRes = await getReplyFromConfig(
{ Body: "/verbose off", From: "+1222", To: "+1222", CommandAuthorized: true },
{},
makeWhatsAppDirectiveConfig(
home,
{ model: "anthropic/claude-opus-4-6" },
{
session: { store: storePath },
},
),
);
const text = replyText(disabledRes);
expect(text).toMatch(/Verbose logging disabled\./);
const store = loadSessionStore(storePath);
const entry = Object.values(store)[0];
expect(entry?.verboseLevel).toBe("off");
expect(runEmbeddedPiAgentMock).not.toHaveBeenCalled();
});
const disabled = await runDirectiveOnly("/verbose off");
expect(disabled.text).toMatch(/Verbose logging disabled\./);
expect(disabled.sessionEntry.verboseLevel).toBe("off");
expect(runEmbeddedPiAgentMock).not.toHaveBeenCalled();
});
it("covers think status", async () => {
await withTempHome(async (home) => {
const text = await runThinkDirectiveAndGetText(home);
expect(text).toContain("Current thinking level: high");
expect(text).toContain("Options: off, minimal, low, medium, high, adaptive.");
expect(runEmbeddedPiAgentMock).not.toHaveBeenCalled();
const { text } = await runDirectiveOnly("/think", {
currentThinkLevel: "high",
});
expect(text).toContain("Current thinking level: high");
expect(text).toContain("Options: off, minimal, low, medium, high, adaptive.");
expect(runEmbeddedPiAgentMock).not.toHaveBeenCalled();
});
it("keeps reserved command aliases from matching after trimming", async () => {
await withTempHome(async (home) => {