-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathSystemMessageService.test.ts
More file actions
150 lines (121 loc) · 3.79 KB
/
SystemMessageService.test.ts
File metadata and controls
150 lines (121 loc) · 3.79 KB
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
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,
};
const mockBlocks = [{ type: "text", text: "Test system message" }];
constructSystemMessageMock.mockResolvedValue(mockBlocks);
await service.initialize(config);
const message = await service.getSystemMessage("normal");
expect(constructSystemMessageMock).toHaveBeenCalledWith(
"normal", // Default mode
["rule1"],
"json",
true,
);
expect(message).toEqual(mockBlocks);
});
});
describe("updateConfig", () => {
it("should update configuration partially", async () => {
await service.initialize({
additionalRules: ["rule1"],
headless: false,
});
service.updateConfig({
format: "json",
});
constructSystemMessageMock.mockResolvedValue([
{ type: "text", text: "Updated message" },
]);
await service.getSystemMessage("normal");
expect(constructSystemMessageMock).toHaveBeenCalledWith(
"normal",
["rule1"],
"json",
false,
);
});
it("should override existing configuration", async () => {
await service.initialize({
additionalRules: ["rule1"],
format: "json" as const,
});
service.updateConfig({
additionalRules: ["rule2", "rule3"],
});
constructSystemMessageMock.mockResolvedValue([
{ type: "text", text: "Updated message" },
]);
await service.getSystemMessage("normal");
expect(constructSystemMessageMock).toHaveBeenCalledWith(
"normal",
["rule2", "rule3"],
"json",
undefined,
);
});
});
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",
);
});
});
});