Elffuss BraidHub

kikocis/continue public

main · 4.8 KiBRawPermalink
1import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
3import { modeService } from "./ModeService.js";
4import { SystemMessageService } from "./SystemMessageService.js";
5
6// Mock the systemMessage module
7vi.mock("../systemMessage.js", () => ({
8 constructSystemMessage: vi.fn(),
9}));
10
11// Mock the logger
12vi.mock("../util/logger.js", () => ({
13 logger: {
14 debug: vi.fn(),
15 info: vi.fn(),
16 warn: vi.fn(),
17 error: vi.fn(),
18 },
19}));
20
21describe("SystemMessageService", () => {
22 let service: SystemMessageService;
23 let constructSystemMessageMock: any;
24
25 beforeEach(async () => {
26 // Reset mocks
27 vi.clearAllMocks();
28
29 // Get mock reference
30 const { constructSystemMessage } = await import("../systemMessage.js");
31 constructSystemMessageMock = constructSystemMessage as any;
32
33 // Create new service instance
34 service = new SystemMessageService();
35 });
36
37 afterEach(async () => {
38 await service.cleanup();
39 });
40
41 describe("initialization", () => {
42 it("should initialize with provided configuration", async () => {
43 const config = {
44 additionalRules: ["rule1", "rule2"],
45 format: "json" as const,
46 headless: true,
47 };
48
49 const state = await service.initialize(config);
50
51 expect(state).toEqual(config);
52 expect(service.isReady()).toBe(true);
53 });
54
55 it("should initialize with empty configuration", async () => {
56 const state = await service.initialize({});
57
58 expect(state).toEqual({});
59 expect(service.isReady()).toBe(true);
60 });
61 });
62
63 describe("getSystemMessage", () => {
64 it("should call constructSystemMessage with current configuration and mode", async () => {
65 const config = {
66 additionalRules: ["rule1"],
67 format: "json" as const,
68 headless: true,
69 };
70
71 constructSystemMessageMock.mockResolvedValue("Test system message");
72
73 await service.initialize(config);
74 const message = await service.getSystemMessage();
75
76 expect(constructSystemMessageMock).toHaveBeenCalledWith(
77 ["rule1"],
78 "json",
79 true,
80 "normal", // Default mode
81 );
82 expect(message).toBe("Test system message");
83 });
84
85 it("should use updated mode when mode changes", async () => {
86 constructSystemMessageMock.mockResolvedValue("Updated system message");
87
88 await service.initialize({});
89
90 // Simulate mode change
91 modeService.emit("modeChanged", "plan", "normal");
92
93 const message = await service.getSystemMessage();
94
95 expect(constructSystemMessageMock).toHaveBeenCalledWith(
96 undefined,
97 undefined,
98 undefined,
99 "plan",
100 );
101 expect(message).toBe("Updated system message");
102 });
103 });
104
105 describe("updateConfig", () => {
106 it("should update configuration partially", async () => {
107 await service.initialize({
108 additionalRules: ["rule1"],
109 headless: false,
110 });
111
112 service.updateConfig({
113 format: "json",
114 });
115
116 constructSystemMessageMock.mockResolvedValue("Updated message");
117 await service.getSystemMessage();
118
119 expect(constructSystemMessageMock).toHaveBeenCalledWith(
120 ["rule1"],
121 "json",
122 false,
123 "normal",
124 );
125 });
126
127 it("should override existing configuration", async () => {
128 await service.initialize({
129 additionalRules: ["rule1"],
130 format: "json" as const,
131 });
132
133 service.updateConfig({
134 additionalRules: ["rule2", "rule3"],
135 });
136
137 constructSystemMessageMock.mockResolvedValue("Updated message");
138 await service.getSystemMessage();
139
140 expect(constructSystemMessageMock).toHaveBeenCalledWith(
141 ["rule2", "rule3"],
142 "json",
143 undefined,
144 "normal",
145 );
146 });
147 });
148
149 describe("mode tracking", () => {
150 it("should track current mode from ModeService", async () => {
151 await service.initialize({});
152
153 expect(service.getCurrentMode()).toBe("normal");
154
155 // Simulate mode change
156 modeService.emit("modeChanged", "auto", "normal");
157
158 expect(service.getCurrentMode()).toBe("auto");
159 });
160
161 it("should initialize with current mode from ModeService", async () => {
162 // Mock modeService.getCurrentMode
163 vi.spyOn(modeService, "getCurrentMode").mockReturnValue("plan");
164
165 const newService = new SystemMessageService();
166 await newService.initialize({});
167
168 expect(newService.getCurrentMode()).toBe("plan");
169
170 await newService.cleanup();
171 });
172 });
173
174 describe("error handling", () => {
175 it("should handle constructSystemMessage errors gracefully", async () => {
176 constructSystemMessageMock.mockRejectedValue(
177 new Error("Failed to construct"),
178 );
179
180 await service.initialize({});
181
182 await expect(service.getSystemMessage()).rejects.toThrow(
183 "Failed to construct",
184 );
185 });
186 });
187});