| 1 | import { describe, expect, it, vi } from "vitest"; |
| 2 | |
| 3 | // Use the actual implementation instead of the mocked one |
| 4 | vi.unmock("./systemMessage.js"); |
| 5 | const { constructSystemMessage } = await import("./systemMessage.js"); |
| 6 | |
| 7 | // Mock the service container to avoid "No factory registered for service 'config'" error |
| 8 | vi.mock("./services/ServiceContainer.js", () => ({ |
| 9 | serviceContainer: { |
| 10 | get: vi.fn().mockResolvedValue({ config: { rules: [] } }), |
| 11 | }, |
| 12 | })); |
| 13 | |
| 14 | // Mock processPromptOrRule to avoid file system operations in tests |
| 15 | vi.mock("./args.js", () => ({ |
| 16 | processPromptOrRule: vi |
| 17 | .fn() |
| 18 | .mockImplementation((spec: string) => Promise.resolve(spec)), |
| 19 | })); |
| 20 | |
| 21 | describe("constructSystemMessage", () => { |
| 22 | it("should return base system message with rules when additionalRules is provided", async () => { |
| 23 | const rules = ["These are the rules for the assistant."]; |
| 24 | const result = await constructSystemMessage(rules); |
| 25 | |
| 26 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 27 | expect(result).toContain('<context name="userRules">'); |
| 28 | expect(result).toContain(rules[0]); |
| 29 | expect(result).toContain("</context>"); |
| 30 | }); |
| 31 | |
| 32 | it("should return base system message with agent content when no rules but agent file exists", async () => { |
| 33 | // The implementation checks for agent files like AGENTS.md which exists in this project |
| 34 | const result = await constructSystemMessage(); |
| 35 | |
| 36 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 37 | expect(result).toContain('<context name="userRules">'); |
| 38 | expect(result).toContain("AGENTS.md"); |
| 39 | }); |
| 40 | |
| 41 | it("should include base system message components", async () => { |
| 42 | const result = await constructSystemMessage(); |
| 43 | |
| 44 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 45 | expect(result).toContain("<env>"); |
| 46 | expect(result).toContain('<context name="directoryStructure">'); |
| 47 | expect(result).toContain('<context name="gitStatus">'); |
| 48 | }); |
| 49 | |
| 50 | it("should handle whitespace-only rules message", async () => { |
| 51 | const result = await constructSystemMessage([" "]); |
| 52 | |
| 53 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 54 | expect(result).toContain('<context name="userRules">'); |
| 55 | }); |
| 56 | |
| 57 | it("should include working directory information", async () => { |
| 58 | const result = await constructSystemMessage(); |
| 59 | |
| 60 | expect(result).toContain("Working directory:"); |
| 61 | expect(result).toContain("<env>"); |
| 62 | }); |
| 63 | |
| 64 | it("should include platform information", async () => { |
| 65 | const result = await constructSystemMessage(); |
| 66 | |
| 67 | expect(result).toContain("Platform:"); |
| 68 | }); |
| 69 | |
| 70 | it("should include current date", async () => { |
| 71 | const result = await constructSystemMessage(); |
| 72 | |
| 73 | expect(result).toContain("Today's date:"); |
| 74 | expect(result).toContain(new Date().toISOString().split("T")[0]); |
| 75 | }); |
| 76 | |
| 77 | it("should format rules section correctly", async () => { |
| 78 | const rulesMessage = "Rule 1: Do this\nRule 2: Do that"; |
| 79 | const result = await constructSystemMessage([rulesMessage]); |
| 80 | |
| 81 | expect(result).toContain('<context name="userRules">'); |
| 82 | expect(result).toContain(rulesMessage); |
| 83 | expect(result).toContain("</context>"); |
| 84 | }); |
| 85 | |
| 86 | it("should handle multiline rules message", async () => { |
| 87 | const rulesMessage = `Rule 1: First rule |
| 88 | Rule 2: Second rule |
| 89 | Rule 3: Third rule`; |
| 90 | const result = await constructSystemMessage([rulesMessage]); |
| 91 | |
| 92 | expect(result).toContain(rulesMessage); |
| 93 | expect(result).toContain('<context name="userRules">'); |
| 94 | expect(result).toContain("</context>"); |
| 95 | }); |
| 96 | |
| 97 | it("should handle special characters in rules message", async () => { |
| 98 | const rulesMessage = "Rule with <special> characters & symbols!"; |
| 99 | const result = await constructSystemMessage([rulesMessage]); |
| 100 | |
| 101 | expect(result).toContain(rulesMessage); |
| 102 | expect(result).toContain('<context name="userRules">'); |
| 103 | }); |
| 104 | |
| 105 | it("should handle very long rules message", async () => { |
| 106 | const rulesMessage = "A".repeat(1000); |
| 107 | const result = await constructSystemMessage([rulesMessage]); |
| 108 | |
| 109 | expect(result).toContain(rulesMessage); |
| 110 | expect(result).toContain('<context name="userRules">'); |
| 111 | }); |
| 112 | |
| 113 | it("should combine rules and agent content when both are present", async () => { |
| 114 | const rulesMessage = "These are the rules."; |
| 115 | const result = await constructSystemMessage([rulesMessage]); |
| 116 | |
| 117 | expect(result).toContain('<context name="userRules">'); |
| 118 | expect(result).toContain(rulesMessage); |
| 119 | expect(result).toContain("AGENTS.md"); |
| 120 | expect(result).toContain("</context>"); |
| 121 | }); |
| 122 | |
| 123 | it("should add headless mode instructions when headless is true", async () => { |
| 124 | const result = await constructSystemMessage(undefined, undefined, true); |
| 125 | |
| 126 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 127 | expect(result).toContain("IMPORTANT: You are running in headless mode"); |
| 128 | expect(result).toContain("Provide ONLY your final answer"); |
| 129 | expect(result).toContain( |
| 130 | "Do not include explanations, reasoning, or additional commentary", |
| 131 | ); |
| 132 | }); |
| 133 | |
| 134 | it("should not add headless mode instructions when headless is false", async () => { |
| 135 | const result = await constructSystemMessage(undefined, undefined, false); |
| 136 | |
| 137 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 138 | expect(result).not.toContain("IMPORTANT: You are running in headless mode"); |
| 139 | expect(result).not.toContain("Provide ONLY your final answer"); |
| 140 | }); |
| 141 | |
| 142 | it("should not add headless mode instructions when headless is undefined", async () => { |
| 143 | const result = await constructSystemMessage(); |
| 144 | |
| 145 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 146 | expect(result).not.toContain("IMPORTANT: You are running in headless mode"); |
| 147 | expect(result).not.toContain("Provide ONLY your final answer"); |
| 148 | }); |
| 149 | |
| 150 | it("should add JSON format instructions when format is json", async () => { |
| 151 | const result = await constructSystemMessage(undefined, "json"); |
| 152 | |
| 153 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 154 | expect(result).toContain( |
| 155 | "IMPORTANT: You are operating in JSON output mode", |
| 156 | ); |
| 157 | expect(result).toContain("Your final response MUST be valid JSON"); |
| 158 | expect(result).toContain("JSON.parse()"); |
| 159 | }); |
| 160 | |
| 161 | it("should add plan mode instructions when mode is plan", async () => { |
| 162 | const result = await constructSystemMessage( |
| 163 | undefined, |
| 164 | undefined, |
| 165 | false, |
| 166 | "plan", |
| 167 | ); |
| 168 | |
| 169 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 170 | expect(result).toContain("PLAN MODE: You are operating in plan mode"); |
| 171 | expect(result).toContain( |
| 172 | "which means that your goal is to help the user investigate their ideas", |
| 173 | ); |
| 174 | expect(result).toContain("develop a plan before taking action"); |
| 175 | expect(result).toContain("read-only tools"); |
| 176 | expect(result).toContain( |
| 177 | "should not attempt to circumvent them to write / delete / create files", |
| 178 | ); |
| 179 | }); |
| 180 | |
| 181 | it("should not add plan mode instructions when mode is not plan", async () => { |
| 182 | const result = await constructSystemMessage( |
| 183 | undefined, |
| 184 | undefined, |
| 185 | false, |
| 186 | "normal", |
| 187 | ); |
| 188 | |
| 189 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 190 | expect(result).not.toContain("PLAN MODE: You are operating in plan mode"); |
| 191 | expect(result).not.toContain( |
| 192 | "which means that your goal is to help the user investigate their ideas", |
| 193 | ); |
| 194 | }); |
| 195 | |
| 196 | it("should not add plan mode instructions when mode is undefined", async () => { |
| 197 | const result = await constructSystemMessage(); |
| 198 | |
| 199 | expect(result).toContain("You are an agent in the Continue CLI"); |
| 200 | expect(result).not.toContain("PLAN MODE: You are operating in plan mode"); |
| 201 | expect(result).not.toContain( |
| 202 | "which means that your goal is to help the user investigate their ideas", |
| 203 | ); |
| 204 | }); |
| 205 | |
| 206 | it("should include basic plan mode description", async () => { |
| 207 | const result = await constructSystemMessage( |
| 208 | undefined, |
| 209 | undefined, |
| 210 | false, |
| 211 | "plan", |
| 212 | ); |
| 213 | |
| 214 | expect(result).toContain("PLAN MODE: You are operating in plan mode"); |
| 215 | expect(result).toContain("read-only tools"); |
| 216 | expect(result).toContain("write / delete / create files"); |
| 217 | }); |
| 218 | |
| 219 | it("should combine plan mode with headless mode instructions", async () => { |
| 220 | const result = await constructSystemMessage( |
| 221 | undefined, |
| 222 | undefined, |
| 223 | true, |
| 224 | "plan", |
| 225 | ); |
| 226 | |
| 227 | expect(result).toContain("IMPORTANT: You are running in headless mode"); |
| 228 | expect(result).toContain("PLAN MODE: You are operating in plan mode"); |
| 229 | expect(result).toContain( |
| 230 | "which means that your goal is to help the user investigate their ideas", |
| 231 | ); |
| 232 | }); |
| 233 | |
| 234 | it("should combine plan mode with JSON format instructions", async () => { |
| 235 | const result = await constructSystemMessage( |
| 236 | undefined, |
| 237 | "json", |
| 238 | false, |
| 239 | "plan", |
| 240 | ); |
| 241 | |
| 242 | expect(result).toContain( |
| 243 | "IMPORTANT: You are operating in JSON output mode", |
| 244 | ); |
| 245 | expect(result).toContain("PLAN MODE: You are operating in plan mode"); |
| 246 | expect(result).toContain( |
| 247 | "which means that your goal is to help the user investigate their ideas", |
| 248 | ); |
| 249 | }); |
| 250 | }); |