continue-custom/extensions/cli/src/services/SystemMessageService.test.ts

144 lines
3.7 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { SystemMessageService } from "./SystemMessageService.js";
import { services } from "./index.js";
const toolPermissionService = services.toolPermissions;
// Mock the systemMessage module
vi.mock("../systemMessage.js", () => ({
constructSystemMessage: vi.fn(),
}));
// Mock the logger
vi.mock("../util/logger.js", () => ({
logger: {
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
},
}));
describe("SystemMessageService", () => {
let service: SystemMessageService;
let constructSystemMessageMock: any;
beforeEach(async () => {
// Reset mocks
vi.clearAllMocks();
// Get mock reference
const { constructSystemMessage } = await import("../systemMessage.js");
constructSystemMessageMock = constructSystemMessage as any;
// Create new service instance
service = new SystemMessageService();
});
afterEach(async () => {
await service.cleanup();
});
describe("initialization", () => {
it("should initialize with provided configuration", async () => {
const config = {
additionalRules: ["rule1", "rule2"],
format: "json" as const,
headless: true,
};
const state = await service.initialize(config);
expect(state).toEqual(config);
expect(service.isReady()).toBe(true);
});
it("should initialize with empty configuration", async () => {
const state = await service.initialize({});
expect(state).toEqual({});
expect(service.isReady()).toBe(true);
});
});
describe("getSystemMessage", () => {
it("should call constructSystemMessage with current configuration and mode", async () => {
const config = {
additionalRules: ["rule1"],
format: "json" as const,
headless: true,
};
constructSystemMessageMock.mockResolvedValue("Test system message");
await service.initialize(config);
const message = await service.getSystemMessage("normal");
expect(constructSystemMessageMock).toHaveBeenCalledWith(
["rule1"],
"json",
true,
"normal", // Default mode
);
expect(message).toBe("Test system message");
});
});
describe("updateConfig", () => {
it("should update configuration partially", async () => {
await service.initialize({
additionalRules: ["rule1"],
headless: false,
});
service.updateConfig({
format: "json",
});
constructSystemMessageMock.mockResolvedValue("Updated message");
await service.getSystemMessage("normal");
expect(constructSystemMessageMock).toHaveBeenCalledWith(
["rule1"],
"json",
false,
"normal",
);
});
it("should override existing configuration", async () => {
await service.initialize({
additionalRules: ["rule1"],
format: "json" as const,
});
service.updateConfig({
additionalRules: ["rule2", "rule3"],
});
constructSystemMessageMock.mockResolvedValue("Updated message");
await service.getSystemMessage("normal");
expect(constructSystemMessageMock).toHaveBeenCalledWith(
["rule2", "rule3"],
"json",
undefined,
"normal",
);
});
});
describe("error handling", () => {
it("should handle constructSystemMessage errors gracefully", async () => {
constructSystemMessageMock.mockRejectedValue(
new Error("Failed to construct"),
);
await service.initialize({});
await expect(service.getSystemMessage("normal")).rejects.toThrow(
"Failed to construct",
);
});
});
});