File size: 9,273 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 301 302 303 304 305 306 307 308 309 310 311 312 | /**
* ProfilesService tests using SDK client mocks.
*
* Note: We mock ProfilesClient directly because the SDK's HttpClient uses
* native fetch that MSW doesn't intercept reliably in Node.js test environments.
* This approach tests that ProfilesService correctly delegates to ProfilesClient
* methods and propagates errors from the SDK.
*
* For full integration testing, use browser-level tests with MSW.
*/
import { ProfilesClient } from "@openhands/typescript-client/clients";
import { afterEach, describe, expect, it, vi, beforeEach } from "vitest";
import ProfilesService from "#/api/profiles-service/profiles-service.api";
// Mock the ProfilesClient from the SDK using vi.hoisted
const {
mockListProfiles,
mockGetProfile,
mockSaveProfile,
mockDeleteProfile,
mockRenameProfile,
mockActivateProfile,
mockValidateProfile,
mockClose,
} = vi.hoisted(() => ({
mockListProfiles: vi.fn(),
mockGetProfile: vi.fn(),
mockSaveProfile: vi.fn(),
mockDeleteProfile: vi.fn(),
mockRenameProfile: vi.fn(),
mockActivateProfile: vi.fn(),
mockValidateProfile: vi.fn(),
mockClose: vi.fn(),
}));
vi.mock("@openhands/typescript-client/clients", () => ({
ProfilesClient: vi.fn(function ProfilesClientMock() {
return {
listProfiles: mockListProfiles,
getProfile: mockGetProfile,
saveProfile: mockSaveProfile,
deleteProfile: mockDeleteProfile,
renameProfile: mockRenameProfile,
activateProfile: mockActivateProfile,
validateProfile: mockValidateProfile,
close: mockClose,
};
}),
}));
vi.mock("#/api/agent-server-client-options", () => ({
getAgentServerClientOptions: vi.fn(() => ({
host: "http://localhost:3000",
apiKey: "test-key",
})),
}));
describe("ProfilesService", () => {
beforeEach(() => {
mockListProfiles.mockReset();
mockGetProfile.mockReset();
mockSaveProfile.mockReset();
mockDeleteProfile.mockReset();
mockRenameProfile.mockReset();
mockActivateProfile.mockReset();
mockValidateProfile.mockReset();
mockClose.mockReset();
vi.mocked(ProfilesClient).mockClear();
});
afterEach(() => {
vi.clearAllMocks();
});
describe("listProfiles", () => {
it("calls listProfiles and returns profiles list", async () => {
const mockResponse = {
profiles: [
{
name: "gpt-4-profile",
model: "openai/gpt-4",
base_url: null,
api_key_set: true,
},
{
name: "claude-profile",
model: "anthropic/claude-3",
base_url: "https://api.anthropic.com",
api_key_set: false,
},
],
active_profile: null,
};
mockListProfiles.mockResolvedValue(mockResponse);
const result = await ProfilesService.listProfiles();
expect(mockListProfiles).toHaveBeenCalled();
expect(result).toEqual(mockResponse);
expect(result.profiles).toHaveLength(2);
});
it("returns empty profiles array when no profiles exist", async () => {
const mockResponse = { profiles: [], active_profile: null };
mockListProfiles.mockResolvedValue(mockResponse);
const result = await ProfilesService.listProfiles();
expect(result.profiles).toEqual([]);
});
it("propagates errors from the SDK", async () => {
mockListProfiles.mockRejectedValue(new Error("Network error"));
await expect(ProfilesService.listProfiles()).rejects.toThrow(
"Network error",
);
});
});
describe("getProfile", () => {
it("calls getProfile with profile name", async () => {
const mockResponse = {
name: "my-profile",
config: { model: "openai/gpt-4" },
api_key_set: true,
};
mockGetProfile.mockResolvedValue(mockResponse);
const result = await ProfilesService.getProfile("my-profile");
expect(mockGetProfile).toHaveBeenCalledWith("my-profile", {});
expect(result).toEqual(mockResponse);
});
it("passes exposeSecrets option when set", async () => {
const mockResponse = {
name: "my-profile",
config: { model: "openai/gpt-4", api_key: "encrypted_..." },
api_key_set: true,
};
mockGetProfile.mockResolvedValue(mockResponse);
await ProfilesService.getProfile("my-profile", "encrypted");
expect(mockGetProfile).toHaveBeenCalledWith("my-profile", {
exposeSecrets: "encrypted",
});
});
});
describe("saveProfile", () => {
it("calls saveProfile with name and request body", async () => {
const mockResponse = { name: "new-profile", message: "Profile saved" };
mockSaveProfile.mockResolvedValue(mockResponse);
const request = {
llm: {
model: "openai/gpt-4",
api_key: "sk-xxx",
},
include_secrets: true,
};
const result = await ProfilesService.saveProfile("new-profile", request);
expect(mockSaveProfile).toHaveBeenCalledWith("new-profile", request);
expect(result).toEqual(mockResponse);
});
it("saves profile with base_url", async () => {
const mockResponse = { name: "custom-profile", message: "Profile saved" };
mockSaveProfile.mockResolvedValue(mockResponse);
const request = {
llm: {
model: "openai/gpt-4",
base_url: "https://custom.api.com",
},
};
await ProfilesService.saveProfile("custom-profile", request);
expect(mockSaveProfile).toHaveBeenCalledWith("custom-profile", request);
});
});
describe("deleteProfile", () => {
it("calls deleteProfile with profile name", async () => {
const mockResponse = { name: "old-profile", message: "Profile deleted" };
mockDeleteProfile.mockResolvedValue(mockResponse);
const result = await ProfilesService.deleteProfile("old-profile");
expect(mockDeleteProfile).toHaveBeenCalledWith("old-profile");
expect(result).toEqual(mockResponse);
});
});
describe("renameProfile", () => {
it("calls renameProfile with old and new names", async () => {
const mockResponse = {
name: "renamed-profile",
message: "Profile renamed",
};
mockRenameProfile.mockResolvedValue(mockResponse);
const result = await ProfilesService.renameProfile(
"old-name",
"renamed-profile",
);
expect(mockRenameProfile).toHaveBeenCalledWith(
"old-name",
"renamed-profile",
);
expect(result).toEqual(mockResponse);
});
});
describe("activateProfile", () => {
it("calls activateProfile with profile name", async () => {
const mockResponse = {
name: "active-profile",
message: "Profile activated",
llm_applied: true,
};
mockActivateProfile.mockResolvedValue(mockResponse);
const result = await ProfilesService.activateProfile("active-profile");
expect(mockActivateProfile).toHaveBeenCalledWith("active-profile");
expect(result).toEqual(mockResponse);
});
});
describe("validateProfile", () => {
const request = { llm: { model: "openai/gpt-4o" }, include_secrets: true };
it("returns a valid verdict via the typed client", async () => {
mockValidateProfile.mockResolvedValue({ valid: true });
const result = await ProfilesService.validateProfile("gpt", request);
expect(mockValidateProfile).toHaveBeenCalledWith("gpt", request);
expect(result).toEqual({ valid: true });
expect(mockClose).toHaveBeenCalled();
});
it("returns null when the agent-server reports 404", async () => {
mockValidateProfile.mockRejectedValue(
Object.assign(new Error("HTTP 404"), { status: 404 }),
);
await expect(
ProfilesService.validateProfile("gpt", request),
).resolves.toBeNull();
});
it("propagates other errors", async () => {
mockValidateProfile.mockRejectedValue(
Object.assign(new Error("HTTP 400"), { status: 400 }),
);
await expect(
ProfilesService.validateProfile("gpt", request),
).rejects.toThrow("HTTP 400");
});
it.each([429, 503])(
"returns null for transient HTTP %s errors",
async (status) => {
mockValidateProfile.mockRejectedValue(
Object.assign(new Error(`HTTP ${status}`), { status }),
);
await expect(
ProfilesService.validateProfile("gpt", request),
).resolves.toBeNull();
expect(mockClose).toHaveBeenCalled();
},
);
it("returns null for timeout-like errors", async () => {
mockValidateProfile.mockRejectedValue(
new Error("Request failed", {
cause: new DOMException("timed out", "TimeoutError"),
}),
);
await expect(
ProfilesService.validateProfile("gpt", request),
).resolves.toBeNull();
});
it("returns an invalid verdict with its error", async () => {
const response = {
valid: false,
error: { type: "auth", message: "bad key" },
};
mockValidateProfile.mockResolvedValue(response);
await expect(
ProfilesService.validateProfile("gpt", request),
).resolves.toEqual(response);
});
});
});
|