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