mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 11:51:37 +00:00
refactor: deduplicate shared helpers and test setup
This commit is contained in:
@@ -39,6 +39,24 @@ function baseConfig(): OpenClawConfig {
|
||||
} as unknown as OpenClawConfig;
|
||||
}
|
||||
|
||||
function resolveModelSelectionForCommand(params: {
|
||||
command: string;
|
||||
allowedModelKeys: Set<string>;
|
||||
allowedModelCatalog: Array<{ provider: string; id: string }>;
|
||||
}) {
|
||||
return resolveModelSelectionFromDirective({
|
||||
directives: parseInlineDirectives(params.command),
|
||||
cfg: { commands: { text: true } } as unknown as OpenClawConfig,
|
||||
agentDir: "/tmp/agent",
|
||||
defaultProvider: "anthropic",
|
||||
defaultModel: "claude-opus-4-5",
|
||||
aliasIndex: baseAliasIndex(),
|
||||
allowedModelKeys: params.allowedModelKeys,
|
||||
allowedModelCatalog: params.allowedModelCatalog,
|
||||
provider: "anthropic",
|
||||
});
|
||||
}
|
||||
|
||||
describe("/model chat UX", () => {
|
||||
it("shows summary for /model with no args", async () => {
|
||||
const directives = parseInlineDirectives("/model");
|
||||
@@ -114,19 +132,10 @@ describe("/model chat UX", () => {
|
||||
});
|
||||
|
||||
it("rejects numeric /model selections with a guided error", () => {
|
||||
const directives = parseInlineDirectives("/model 99");
|
||||
const cfg = { commands: { text: true } } as unknown as OpenClawConfig;
|
||||
|
||||
const resolved = resolveModelSelectionFromDirective({
|
||||
directives,
|
||||
cfg,
|
||||
agentDir: "/tmp/agent",
|
||||
defaultProvider: "anthropic",
|
||||
defaultModel: "claude-opus-4-5",
|
||||
aliasIndex: baseAliasIndex(),
|
||||
const resolved = resolveModelSelectionForCommand({
|
||||
command: "/model 99",
|
||||
allowedModelKeys: new Set(["anthropic/claude-opus-4-5", "openai/gpt-4o"]),
|
||||
allowedModelCatalog: [],
|
||||
provider: "anthropic",
|
||||
});
|
||||
|
||||
expect(resolved.modelSelection).toBeUndefined();
|
||||
@@ -135,19 +144,10 @@ describe("/model chat UX", () => {
|
||||
});
|
||||
|
||||
it("treats explicit default /model selection as resettable default", () => {
|
||||
const directives = parseInlineDirectives("/model anthropic/claude-opus-4-5");
|
||||
const cfg = { commands: { text: true } } as unknown as OpenClawConfig;
|
||||
|
||||
const resolved = resolveModelSelectionFromDirective({
|
||||
directives,
|
||||
cfg,
|
||||
agentDir: "/tmp/agent",
|
||||
defaultProvider: "anthropic",
|
||||
defaultModel: "claude-opus-4-5",
|
||||
aliasIndex: baseAliasIndex(),
|
||||
const resolved = resolveModelSelectionForCommand({
|
||||
command: "/model anthropic/claude-opus-4-5",
|
||||
allowedModelKeys: new Set(["anthropic/claude-opus-4-5", "openai/gpt-4o"]),
|
||||
allowedModelCatalog: [],
|
||||
provider: "anthropic",
|
||||
});
|
||||
|
||||
expect(resolved.errorText).toBeUndefined();
|
||||
@@ -159,19 +159,10 @@ describe("/model chat UX", () => {
|
||||
});
|
||||
|
||||
it("keeps openrouter provider/model split for exact selections", () => {
|
||||
const directives = parseInlineDirectives("/model openrouter/anthropic/claude-opus-4-5");
|
||||
const cfg = { commands: { text: true } } as unknown as OpenClawConfig;
|
||||
|
||||
const resolved = resolveModelSelectionFromDirective({
|
||||
directives,
|
||||
cfg,
|
||||
agentDir: "/tmp/agent",
|
||||
defaultProvider: "anthropic",
|
||||
defaultModel: "claude-opus-4-5",
|
||||
aliasIndex: baseAliasIndex(),
|
||||
const resolved = resolveModelSelectionForCommand({
|
||||
command: "/model openrouter/anthropic/claude-opus-4-5",
|
||||
allowedModelKeys: new Set(["openrouter/anthropic/claude-opus-4-5"]),
|
||||
allowedModelCatalog: [],
|
||||
provider: "anthropic",
|
||||
});
|
||||
|
||||
expect(resolved.errorText).toBeUndefined();
|
||||
|
||||
@@ -14,6 +14,74 @@ vi.mock("./commands.js", () => ({
|
||||
|
||||
// Import after mocks.
|
||||
const { handleInlineActions } = await import("./get-reply-inline-actions.js");
|
||||
type HandleInlineActionsInput = Parameters<typeof handleInlineActions>[0];
|
||||
|
||||
const createTypingController = (): TypingController => ({
|
||||
onReplyStart: async () => {},
|
||||
startTypingLoop: async () => {},
|
||||
startTypingOnText: async () => {},
|
||||
refreshTypingTtl: () => {},
|
||||
isActive: () => false,
|
||||
markRunComplete: () => {},
|
||||
markDispatchIdle: () => {},
|
||||
cleanup: vi.fn(),
|
||||
});
|
||||
|
||||
const createHandleInlineActionsInput = (params: {
|
||||
ctx: ReturnType<typeof buildTestCtx>;
|
||||
typing: TypingController;
|
||||
cleanedBody: string;
|
||||
command?: Partial<HandleInlineActionsInput["command"]>;
|
||||
overrides?: Partial<Omit<HandleInlineActionsInput, "ctx" | "sessionCtx" | "typing" | "command">>;
|
||||
}): HandleInlineActionsInput => {
|
||||
const baseCommand: HandleInlineActionsInput["command"] = {
|
||||
surface: "whatsapp",
|
||||
channel: "whatsapp",
|
||||
channelId: "whatsapp",
|
||||
ownerList: [],
|
||||
senderIsOwner: false,
|
||||
isAuthorizedSender: false,
|
||||
senderId: undefined,
|
||||
abortKey: "whatsapp:+999",
|
||||
rawBodyNormalized: params.cleanedBody,
|
||||
commandBodyNormalized: params.cleanedBody,
|
||||
from: "whatsapp:+999",
|
||||
to: "whatsapp:+999",
|
||||
};
|
||||
return {
|
||||
ctx: params.ctx,
|
||||
sessionCtx: params.ctx as unknown as TemplateContext,
|
||||
cfg: {},
|
||||
agentId: "main",
|
||||
sessionKey: "s:main",
|
||||
workspaceDir: "/tmp",
|
||||
isGroup: false,
|
||||
typing: params.typing,
|
||||
allowTextCommands: false,
|
||||
inlineStatusRequested: false,
|
||||
command: {
|
||||
...baseCommand,
|
||||
...params.command,
|
||||
},
|
||||
directives: clearInlineDirectives(params.cleanedBody),
|
||||
cleanedBody: params.cleanedBody,
|
||||
elevatedEnabled: false,
|
||||
elevatedAllowed: false,
|
||||
elevatedFailures: [],
|
||||
defaultActivation: () => "always",
|
||||
resolvedThinkLevel: undefined,
|
||||
resolvedVerboseLevel: undefined,
|
||||
resolvedReasoningLevel: "off",
|
||||
resolvedElevatedLevel: "off",
|
||||
resolveDefaultThinkingLevel: async () => "off",
|
||||
provider: "openai",
|
||||
model: "gpt-4o-mini",
|
||||
contextTokens: 0,
|
||||
abortedLastRun: false,
|
||||
sessionScope: "per-sender",
|
||||
...params.overrides,
|
||||
};
|
||||
};
|
||||
|
||||
describe("handleInlineActions", () => {
|
||||
beforeEach(() => {
|
||||
@@ -21,65 +89,21 @@ describe("handleInlineActions", () => {
|
||||
});
|
||||
|
||||
it("skips whatsapp replies when config is empty and From !== To", async () => {
|
||||
const typing: TypingController = {
|
||||
onReplyStart: async () => {},
|
||||
startTypingLoop: async () => {},
|
||||
startTypingOnText: async () => {},
|
||||
refreshTypingTtl: () => {},
|
||||
isActive: () => false,
|
||||
markRunComplete: () => {},
|
||||
markDispatchIdle: () => {},
|
||||
cleanup: vi.fn(),
|
||||
};
|
||||
const typing = createTypingController();
|
||||
|
||||
const ctx = buildTestCtx({
|
||||
From: "whatsapp:+999",
|
||||
To: "whatsapp:+123",
|
||||
Body: "hi",
|
||||
});
|
||||
|
||||
const result = await handleInlineActions({
|
||||
ctx,
|
||||
sessionCtx: ctx as unknown as TemplateContext,
|
||||
cfg: {},
|
||||
agentId: "main",
|
||||
sessionKey: "s:main",
|
||||
workspaceDir: "/tmp",
|
||||
isGroup: false,
|
||||
typing,
|
||||
allowTextCommands: false,
|
||||
inlineStatusRequested: false,
|
||||
command: {
|
||||
surface: "whatsapp",
|
||||
channel: "whatsapp",
|
||||
channelId: "whatsapp",
|
||||
ownerList: [],
|
||||
senderIsOwner: false,
|
||||
isAuthorizedSender: false,
|
||||
senderId: undefined,
|
||||
abortKey: "whatsapp:+999",
|
||||
rawBodyNormalized: "hi",
|
||||
commandBodyNormalized: "hi",
|
||||
from: "whatsapp:+999",
|
||||
to: "whatsapp:+123",
|
||||
},
|
||||
directives: clearInlineDirectives("hi"),
|
||||
cleanedBody: "hi",
|
||||
elevatedEnabled: false,
|
||||
elevatedAllowed: false,
|
||||
elevatedFailures: [],
|
||||
defaultActivation: () => "always",
|
||||
resolvedThinkLevel: undefined,
|
||||
resolvedVerboseLevel: undefined,
|
||||
resolvedReasoningLevel: "off",
|
||||
resolvedElevatedLevel: "off",
|
||||
resolveDefaultThinkingLevel: async () => "off",
|
||||
provider: "openai",
|
||||
model: "gpt-4o-mini",
|
||||
contextTokens: 0,
|
||||
abortedLastRun: false,
|
||||
sessionScope: "per-sender",
|
||||
});
|
||||
const result = await handleInlineActions(
|
||||
createHandleInlineActionsInput({
|
||||
ctx,
|
||||
typing,
|
||||
cleanedBody: "hi",
|
||||
command: { to: "whatsapp:+123" },
|
||||
}),
|
||||
);
|
||||
|
||||
expect(result).toEqual({ kind: "reply", reply: undefined });
|
||||
expect(typing.cleanup).toHaveBeenCalled();
|
||||
@@ -87,16 +111,7 @@ describe("handleInlineActions", () => {
|
||||
});
|
||||
|
||||
it("forwards agentDir into handleCommands", async () => {
|
||||
const typing: TypingController = {
|
||||
onReplyStart: async () => {},
|
||||
startTypingLoop: async () => {},
|
||||
startTypingOnText: async () => {},
|
||||
refreshTypingTtl: () => {},
|
||||
isActive: () => false,
|
||||
markRunComplete: () => {},
|
||||
markDispatchIdle: () => {},
|
||||
cleanup: vi.fn(),
|
||||
};
|
||||
const typing = createTypingController();
|
||||
|
||||
handleCommandsMock.mockResolvedValue({ shouldContinue: false, reply: { text: "done" } });
|
||||
|
||||
@@ -106,49 +121,22 @@ describe("handleInlineActions", () => {
|
||||
});
|
||||
const agentDir = "/tmp/inline-agent";
|
||||
|
||||
const result = await handleInlineActions({
|
||||
ctx,
|
||||
sessionCtx: ctx as unknown as TemplateContext,
|
||||
cfg: { commands: { text: true } },
|
||||
agentId: "main",
|
||||
agentDir,
|
||||
sessionKey: "s:main",
|
||||
workspaceDir: "/tmp",
|
||||
isGroup: false,
|
||||
typing,
|
||||
allowTextCommands: false,
|
||||
inlineStatusRequested: false,
|
||||
command: {
|
||||
surface: "whatsapp",
|
||||
channel: "whatsapp",
|
||||
channelId: "whatsapp",
|
||||
ownerList: [],
|
||||
senderIsOwner: false,
|
||||
isAuthorizedSender: true,
|
||||
senderId: "sender-1",
|
||||
abortKey: "sender-1",
|
||||
rawBodyNormalized: "/status",
|
||||
commandBodyNormalized: "/status",
|
||||
from: "whatsapp:+999",
|
||||
to: "whatsapp:+999",
|
||||
},
|
||||
directives: clearInlineDirectives("/status"),
|
||||
cleanedBody: "/status",
|
||||
elevatedEnabled: false,
|
||||
elevatedAllowed: false,
|
||||
elevatedFailures: [],
|
||||
defaultActivation: () => "always",
|
||||
resolvedThinkLevel: undefined,
|
||||
resolvedVerboseLevel: undefined,
|
||||
resolvedReasoningLevel: "off",
|
||||
resolvedElevatedLevel: "off",
|
||||
resolveDefaultThinkingLevel: async () => "off",
|
||||
provider: "openai",
|
||||
model: "gpt-4o-mini",
|
||||
contextTokens: 0,
|
||||
abortedLastRun: false,
|
||||
sessionScope: "per-sender",
|
||||
});
|
||||
const result = await handleInlineActions(
|
||||
createHandleInlineActionsInput({
|
||||
ctx,
|
||||
typing,
|
||||
cleanedBody: "/status",
|
||||
command: {
|
||||
isAuthorizedSender: true,
|
||||
senderId: "sender-1",
|
||||
abortKey: "sender-1",
|
||||
},
|
||||
overrides: {
|
||||
cfg: { commands: { text: true } },
|
||||
agentDir,
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
expect(result).toEqual({ kind: "reply", reply: { text: "done" } });
|
||||
expect(handleCommandsMock).toHaveBeenCalledTimes(1);
|
||||
|
||||
Reference in New Issue
Block a user