File size: 10,059 Bytes
3201ca6 | 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | import {
ConversationClient,
SettingsClient,
} from "@openhands/typescript-client/clients";
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import React from "react";
import { useCreateConversation } from "#/hooks/mutation/use-create-conversation";
import { getStoredConversationMetadata } from "#/api/conversation-metadata-store";
const {
mockHttpPost,
mockConversationClient,
mockSettingsClient,
mockGetSettings,
mockGetSettingsForConversation,
mockUseLlmProfiles,
mockListInstalledPlugins,
mockGetTelemetryDistinctId,
} = vi.hoisted(() => ({
mockHttpPost: vi.fn(),
mockConversationClient: vi.fn(),
mockSettingsClient: vi.fn(),
mockGetSettings: vi.fn(),
mockGetSettingsForConversation: vi.fn(),
mockUseLlmProfiles: vi.fn(),
mockListInstalledPlugins: vi.fn(),
mockGetTelemetryDistinctId: vi.fn(),
}));
vi.mock("@openhands/typescript-client/clients", async () => {
const actual = await vi.importActual<
typeof import("@openhands/typescript-client/clients")
>("@openhands/typescript-client/clients");
return {
...actual,
ConversationClient: vi.fn(function ConversationClientMock() {
return mockConversationClient();
}),
SettingsClient: vi.fn(function SettingsClientMock() {
return mockSettingsClient();
}),
VSCodeClient: vi.fn(function VSCodeClientMock() {
return { getUrl: vi.fn() };
}),
};
});
vi.mock("#/api/agent-server-config", () => ({
DEFAULT_WORKING_DIR: "workspace/project",
getAgentServerBaseUrl: vi.fn(() => "http://localhost:54928"),
getBakedSessionApiKey: vi.fn(() => "test-session-key"),
getAgentServerSessionApiKey: vi.fn(() => "test-session-key"),
getAgentServerWorkingDir: vi.fn(() => "/workspace/project/agent-canvas"),
getWorkspaceRootForBackend: vi.fn(() => "/workspace/project/agent-canvas"),
buildConversationWorkingDirForBackend: vi.fn(
(id: string) => `/state/workspaces/${id.replace(/-/g, "")}`,
),
shouldLoadPublicSkills: vi.fn(() => true),
syncBakedSessionApiKey: vi.fn(),
getLockedCloudHost: vi.fn(() => null),
}));
vi.mock("#/api/settings-service/settings-service.api", () => ({
default: {
getSettings: mockGetSettings,
getSettingsForConversation: mockGetSettingsForConversation,
},
}));
vi.mock("#/hooks/use-tracking", () => ({
useTracking: () => ({ trackConversationCreated: vi.fn() }),
}));
vi.mock("#/services/telemetry", () => ({
getTelemetryDistinctId: mockGetTelemetryDistinctId,
}));
vi.mock("#/hooks/query/use-llm-profiles", () => ({
useLlmProfiles: () => mockUseLlmProfiles(),
}));
vi.mock("#/api/plugins-management-service", () => ({
default: {
listInstalledPlugins: mockListInstalledPlugins,
},
}));
const wrapper = ({ children }: { children: React.ReactNode }) => {
const client = new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
});
return React.createElement(QueryClientProvider, { client }, children);
};
describe("useCreateConversation persists selected repository metadata", () => {
beforeEach(() => {
window.localStorage.clear();
mockUseLlmProfiles.mockReset();
// Default: no active profile, so metadata is written only for repo/
// workspace attachments (as before). Individual tests override this.
mockUseLlmProfiles.mockReturnValue({ data: { active_profile: null } });
mockListInstalledPlugins.mockReset();
// Default: no installed plugins, so the existing repo/workspace/profile
// assertions are unaffected. Plugin-specific tests override this.
mockListInstalledPlugins.mockResolvedValue([]);
mockGetTelemetryDistinctId.mockReset().mockResolvedValue(null);
mockHttpPost.mockReset();
mockGetSettings.mockReset();
mockGetSettingsForConversation.mockReset();
mockGetSettings.mockResolvedValue({
agent_settings: { llm: { model: "gpt-4o" } },
conversation_settings: {},
});
mockGetSettingsForConversation.mockResolvedValue({
agentSettings: { llm: { model: "gpt-4o" } },
conversationSettings: {},
secretsEncrypted: true,
});
mockConversationClient.mockReset();
vi.mocked(ConversationClient).mockClear();
vi.mocked(SettingsClient).mockClear();
mockConversationClient.mockReturnValue({
createConversation: async (payload: unknown) => {
const response = await mockHttpPost("/api/conversations", payload);
return response.data;
},
});
mockSettingsClient.mockReturnValue({
listSecrets: vi.fn().mockResolvedValue({ secrets: [] }),
});
mockHttpPost.mockResolvedValue({
data: {
id: "conv-new",
created_at: "2026-05-05T00:00:00Z",
updated_at: "2026-05-05T00:00:00Z",
},
});
});
afterEach(() => {
window.localStorage.clear();
});
it("stores the selected repo/branch/provider in the metadata store after a successful create", async () => {
const { result } = renderHook(() => useCreateConversation(), { wrapper });
result.current.mutate({
query: "ship it",
repository: {
name: "octocat/hello-world",
gitProvider: "github",
branch: "main",
},
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(getStoredConversationMetadata("conv-new")).toEqual({
selected_repository: "octocat/hello-world",
selected_branch: "main",
git_provider: "github",
selected_workspace: null,
workspace_mode: "new_worktree",
});
});
it("stores the selected workspace path when only a workspace (no repo) is attached", async () => {
const { result } = renderHook(() => useCreateConversation(), { wrapper });
result.current.mutate({
query: "poke at this repo",
workingDir: "/home/me/code/some-project",
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
// We persist the workspace path so `useHasAttachedSource` can default
// the Files tab to diff view even when no repo was picked.
expect(getStoredConversationMetadata("conv-new")).toEqual({
selected_repository: null,
selected_branch: null,
git_provider: null,
selected_workspace: "/home/me/code/some-project",
workspace_mode: "local_repo",
});
});
it("does not write metadata when neither a repository nor a workspace is attached", async () => {
const { result } = renderHook(() => useCreateConversation(), { wrapper });
result.current.mutate({ query: "scratch session" });
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(getStoredConversationMetadata("conv-new")).toBeNull();
});
it("stamps the active LLM profile even when no repo or workspace is attached (#1082)", async () => {
mockUseLlmProfiles.mockReturnValue({
data: { active_profile: "team-default" },
});
const { result } = renderHook(() => useCreateConversation(), { wrapper });
result.current.mutate({ query: "scratch session" });
await waitFor(() => expect(result.current.isSuccess).toBe(true));
expect(getStoredConversationMetadata("conv-new")).toEqual({
selected_repository: null,
selected_branch: null,
git_provider: null,
selected_workspace: null,
workspace_mode: null,
active_profile: "team-default",
plugins: null,
});
});
it("records the enabled installed plugins (excluding disabled ones) so the in-conversation plugins view can show them", async () => {
// Arrange: one enabled and one disabled plugin are installed locally.
mockListInstalledPlugins.mockResolvedValue([
{
name: "city-weather",
version: "1.0.0",
description: null,
enabled: true,
source: "github:acme/city-weather",
resolved_ref: "abc123",
repo_path: null,
installed_at: "2026-05-05T00:00:00Z",
install_path: "/plugins/city-weather",
},
{
name: "stocks",
version: "1.0.0",
description: null,
enabled: false,
source: "github:acme/stocks",
resolved_ref: null,
repo_path: null,
installed_at: "2026-05-05T00:00:00Z",
install_path: "/plugins/stocks",
},
]);
const { result } = renderHook(() => useCreateConversation(), { wrapper });
result.current.mutate({ query: "what's the weather?" });
await waitFor(() => expect(result.current.isSuccess).toBe(true));
// Only the enabled plugin is snapshotted, mapped to coordinates
// (resolved_ref -> ref) and keeping its name for display.
expect(getStoredConversationMetadata("conv-new")?.plugins).toEqual([
{
source: "github:acme/city-weather",
ref: "abc123",
repo_path: null,
name: "city-weather",
},
]);
});
it("does not duplicate a plugin that is both explicitly attached and enabled-installed", async () => {
// Arrange: the same plugin is passed explicitly and is enabled-installed.
mockListInstalledPlugins.mockResolvedValue([
{
name: "city-weather",
version: "1.0.0",
description: null,
enabled: true,
source: "github:acme/city-weather",
resolved_ref: "main",
repo_path: null,
installed_at: "2026-05-05T00:00:00Z",
install_path: "/plugins/city-weather",
},
]);
const { result } = renderHook(() => useCreateConversation(), { wrapper });
result.current.mutate({
query: "what's the weather?",
plugins: [
{ source: "github:acme/city-weather", ref: "main", repo_path: null },
],
});
await waitFor(() => expect(result.current.isSuccess).toBe(true));
// The plugin is recorded exactly once.
expect(getStoredConversationMetadata("conv-new")?.plugins).toEqual([
{ source: "github:acme/city-weather", ref: "main", repo_path: null },
]);
});
});
|