File size: 6,375 Bytes
fd2c364 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | import React from "react";
import { act, renderHook } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { useSlashCommand } from "#/hooks/chat/use-slash-command";
import { ActiveBackendProvider } from "#/contexts/active-backend-context";
import {
__resetActiveStoreForTests,
setActiveSelection,
setRegisteredBackends,
} from "#/api/backend-registry/active-store";
import type { Backend } from "#/api/backend-registry/types";
import { useFreeModelsStore } from "#/stores/free-models-store";
const mockSkills = vi.hoisted(() => ({
data: undefined as unknown[] | undefined,
isLoading: false,
}));
const mockConversation = vi.hoisted(() => ({
data: undefined as { conversation_version?: "V0" | "V1" } | undefined,
}));
vi.mock("#/hooks/query/use-skills", () => ({
useSkills: () => mockSkills,
}));
const mockLlmProfiles = vi.hoisted(() => ({
data: undefined as
| {
profiles: Array<{
name: string;
model: string | null;
base_url: string | null;
api_key_set: boolean;
}>;
active_profile: string | null;
}
| undefined,
isLoading: false,
}));
vi.mock("#/hooks/query/use-conversation-skills", () => ({
useConversationSkills: () => mockSkills,
}));
vi.mock("#/hooks/query/use-llm-profiles", () => ({
useLlmProfiles: () => mockLlmProfiles,
}));
vi.mock("#/hooks/query/use-active-conversation", () => ({
useActiveConversation: () => mockConversation,
}));
function makeSkill(
name: string,
triggers: string[] = [],
type: "agentskills" | "knowledge" = "agentskills",
) {
return { name, type, content: `Description of ${name}`, triggers };
}
function makeChatInputRef() {
return { current: document.createElement("div") };
}
function setInputText(element: HTMLDivElement, text: string) {
element.textContent = text;
element.innerText = text;
document.body.appendChild(element);
const textNode = element.firstChild;
if (!textNode) return;
const range = document.createRange();
const selection = window.getSelection();
range.setStart(textNode, text.length);
range.collapse(true);
selection?.removeAllRanges();
selection?.addRange(range);
}
const cloudBackend: Backend = {
id: "prod",
name: "Production",
host: "https://app.all-hands.dev",
apiKey: "bearer-token",
kind: "cloud",
};
describe("useSlashCommand", () => {
beforeEach(() => {
vi.clearAllMocks();
mockSkills.data = undefined;
mockSkills.isLoading = false;
mockLlmProfiles.data = undefined;
mockLlmProfiles.isLoading = false;
mockConversation.data = undefined;
useFreeModelsStore.getState().setFlags({
freeModels: new Set(),
defaultModel: null,
});
});
afterEach(() => {
document.body.innerHTML = "";
window.localStorage.clear?.();
__resetActiveStoreForTests();
});
it("excludes /new from the built-in commands on a local backend", () => {
// Arrange — default active backend is the bundled local one.
mockConversation.data = { conversation_version: "V1" };
mockSkills.data = [makeSkill("code-search", ["/code-search"])];
// Act
const ref = makeChatInputRef();
const { result } = renderHook(() => useSlashCommand(ref));
// Assert
const commands = result.current.filteredItems.map((i) => i.command);
expect(commands).not.toContain("/new");
expect(commands).toEqual(expect.arrayContaining(["/btw", "/code-search"]));
});
it("includes /new in the built-in commands on a cloud backend", () => {
// Arrange
setRegisteredBackends([cloudBackend]);
setActiveSelection({ backendId: cloudBackend.id });
mockConversation.data = { conversation_version: "V1" };
mockSkills.data = [];
const wrapper = ({ children }: { children: React.ReactNode }) =>
React.createElement(ActiveBackendProvider, null, children);
// Act
const ref = makeChatInputRef();
const { result } = renderHook(() => useSlashCommand(ref), { wrapper });
// Assert
const commands = result.current.filteredItems.map((i) => i.command);
expect(commands).toContain("/new");
});
it("suggests saved LLM profiles after /model on a local backend", () => {
// The active backend store is reset before each test, which restores the default local backend.
mockSkills.data = [];
useFreeModelsStore.getState().setFlags({
freeModels: new Set(["openhands/glm-5.2"]),
defaultModel: "openhands/glm-5.2",
});
mockLlmProfiles.data = {
profiles: [
{
name: "haiku",
model: "anthropic/claude-haiku-4-5",
base_url: null,
api_key_set: true,
},
{
name: "gpt",
model: "openai/gpt-5.1",
base_url: null,
api_key_set: true,
},
{
name: "free",
model: "openhands/glm-5.2",
base_url: null,
api_key_set: true,
},
],
active_profile: "haiku",
};
const ref = makeChatInputRef();
setInputText(ref.current, "/model");
const { result } = renderHook(() => useSlashCommand(ref));
act(() => result.current.updateSlashMenu());
expect(result.current.isMenuOpen).toBe(true);
expect(result.current.filteredItems.map((i) => i.command)).toEqual([
"/model haiku",
"/model gpt",
"/model free",
]);
expect(
result.current.filteredItems.find((i) => i.command === "/model free")
?.skill.content,
).toBe("Switch to openhands/glm-5.2 (free)");
});
it("filters saved LLM profile suggestions by profile name or model", () => {
mockSkills.data = [];
mockLlmProfiles.data = {
profiles: [
{
name: "haiku",
model: "anthropic/claude-haiku-4-5",
base_url: null,
api_key_set: true,
},
{
name: "gpt",
model: "openai/gpt-5.1",
base_url: null,
api_key_set: true,
},
],
active_profile: null,
};
const ref = makeChatInputRef();
setInputText(ref.current, "/model claude");
const { result } = renderHook(() => useSlashCommand(ref));
act(() => result.current.updateSlashMenu());
expect(result.current.filteredItems.map((i) => i.command)).toEqual([
"/model haiku",
]);
});
});
|