Elffuss BraidHub

kikocis/continue public

main · 10.7 KiBRawPermalink
1// core/util/GlobalContext.test.ts
2import fs from "node:fs";
3import { GlobalContext } from "./GlobalContext";
4import { getGlobalContextFilePath } from "./paths";
5
6describe("GlobalContext", () => {
7 let globalContext: GlobalContext;
8 const globalContextFilePath = getGlobalContextFilePath();
9
10 beforeEach(() => {
11 // Remove the global context file if it exists
12 if (fs.existsSync(globalContextFilePath)) {
13 fs.unlinkSync(globalContextFilePath);
14 }
15 globalContext = new GlobalContext();
16 });
17
18 afterEach(() => {
19 // Clean up
20 if (fs.existsSync(globalContextFilePath)) {
21 fs.unlinkSync(globalContextFilePath);
22 }
23 });
24
25 it("should update and get a boolean value correctly", () => {
26 globalContext.update("indexingPaused", true);
27 const value = globalContext.get("indexingPaused");
28 expect(value).toBe(true);
29 });
30
31 it("should update and get an object value correctly", () => {
32 const workspaceIdentifier = "workspace-1";
33 globalContext.update("lastSelectedProfileForWorkspace", {
34 [workspaceIdentifier]: "profile-1",
35 });
36
37 const value = globalContext.get("lastSelectedProfileForWorkspace");
38 expect(value).toEqual({ "workspace-1": "profile-1" });
39 });
40
41 it("should handle non-existent global context file gracefully on get", () => {
42 const value = globalContext.get("indexingPaused");
43 expect(value).toBeUndefined();
44 });
45
46 it("should handle non-existent global context file gracefully on update", () => {
47 globalContext.update("indexingPaused", true);
48 const value = globalContext.get("indexingPaused");
49 expect(value).toBe(true);
50 });
51
52 it("should overwrite existing values", () => {
53 globalContext.update("indexingPaused", true);
54 expect(globalContext.get("indexingPaused")).toBe(true);
55
56 globalContext.update("indexingPaused", false);
57 expect(globalContext.get("indexingPaused")).toBe(false);
58 });
59
60 it("should handle JSON parsing errors gracefully on get", () => {
61 // Write invalid JSON to the file
62 fs.writeFileSync(globalContextFilePath, "{ invalid json }");
63
64 const consoleWarnMock = jest
65 .spyOn(console, "warn")
66 .mockImplementation(() => {});
67
68 const value = globalContext.get("indexingPaused");
69 expect(value).toBeUndefined();
70 expect(consoleWarnMock).toHaveBeenCalledWith(
71 expect.stringContaining(
72 "Error parsing global context, deleting corrupted file",
73 ),
74 );
75
76 // Clean up
77 consoleWarnMock.mockRestore();
78 });
79
80 it("should delete corrupted file on get and allow fresh start", () => {
81 // Write invalid JSON to the file
82 fs.writeFileSync(globalContextFilePath, "{ invalid json }");
83 expect(fs.existsSync(globalContextFilePath)).toBe(true);
84
85 const consoleWarnMock = jest
86 .spyOn(console, "warn")
87 .mockImplementation(() => {});
88
89 // Try to get a value - this should delete the corrupted file
90 const value = globalContext.get("indexingPaused");
91 expect(value).toBeUndefined();
92
93 // File should be deleted after corruption is detected
94 expect(fs.existsSync(globalContextFilePath)).toBe(false);
95
96 // Now we should be able to update and get values normally
97 globalContext.update("indexingPaused", true);
98 const newValue = globalContext.get("indexingPaused");
99 expect(newValue).toBe(true);
100
101 // Clean up
102 consoleWarnMock.mockRestore();
103 });
104
105 it("should handle JSON parsing errors gracefully on update", () => {
106 // Write invalid JSON to the file
107 fs.writeFileSync(globalContextFilePath, "{ invalid json }");
108
109 const consoleWarnMock = jest
110 .spyOn(console, "warn")
111 .mockImplementation(() => {});
112
113 // Attempt to update
114 globalContext.update("indexingPaused", true);
115
116 expect(consoleWarnMock).toHaveBeenCalledWith(
117 expect.stringContaining(
118 "Error updating global context, attempting to salvage security-sensitive values",
119 ),
120 );
121
122 // The update should have recreated the file with just the new value
123 const value = globalContext.get("indexingPaused");
124 expect(value).toBe(true);
125
126 // Clean up
127 consoleWarnMock.mockRestore();
128 });
129
130 it("should delete and recreate corrupted file on update", () => {
131 // Write invalid JSON to the file
132 fs.writeFileSync(globalContextFilePath, "{ invalid json }");
133 expect(fs.existsSync(globalContextFilePath)).toBe(true);
134
135 const consoleWarnMock = jest
136 .spyOn(console, "warn")
137 .mockImplementation(() => {});
138
139 // Try to update - this should delete the corrupted file and recreate it
140 globalContext.update("indexingPaused", true);
141
142 // File should still exist but with valid JSON now
143 expect(fs.existsSync(globalContextFilePath)).toBe(true);
144
145 // Should be able to read the value
146 const value = globalContext.get("indexingPaused");
147 expect(value).toBe(true);
148
149 // Should be able to add more values
150 globalContext.update("hasAlreadyCreatedAPromptFile", false);
151 expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBe(false);
152 // Original value should still be there
153 expect(globalContext.get("indexingPaused")).toBe(true);
154
155 // Clean up
156 consoleWarnMock.mockRestore();
157 });
158
159 it("should update and retrieve multiple values correctly", () => {
160 globalContext.update("indexingPaused", true);
161 globalContext.update("hasDismissedConfigTsNoticeJetBrains", false);
162
163 expect(globalContext.get("indexingPaused")).toBe(true);
164 expect(globalContext.get("hasDismissedConfigTsNoticeJetBrains")).toBe(
165 false,
166 );
167 });
168
169 it("should handle updating hasAlreadyCreatedAPromptFile correctly", () => {
170 globalContext.update("hasAlreadyCreatedAPromptFile", true);
171 expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBe(true);
172 });
173
174 it("should not crash or throw when getting a key that hasn't been set", () => {
175 expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBeUndefined();
176 });
177
178 describe("salvage functionality", () => {
179 it("should salvage allowAnonymousTelemetry from corrupted sharedConfig", () => {
180 // Create a file with partially corrupted JSON that contains salvageable sharedConfig
181 const corruptedContent = `{
182 "indexingPaused": true,
183 "sharedConfig": {"allowAnonymousTelemetry": false},
184 "corrupted": { invalid json
185}`;
186 fs.writeFileSync(globalContextFilePath, corruptedContent);
187
188 const consoleWarnMock = jest
189 .spyOn(console, "warn")
190 .mockImplementation(() => {});
191
192 // Try to update - this should salvage the allowAnonymousTelemetry value
193 globalContext.update("indexingPaused", false);
194
195 expect(consoleWarnMock).toHaveBeenCalledWith(
196 expect.stringContaining(
197 "Error updating global context, attempting to salvage security-sensitive values",
198 ),
199 );
200
201 // Should have the new value
202 expect(globalContext.get("indexingPaused")).toBe(false);
203
204 // Should have salvaged the sharedConfig with allowAnonymousTelemetry
205 const salvaged = globalContext.get("sharedConfig");
206 expect(salvaged).toEqual({ allowAnonymousTelemetry: false });
207
208 consoleWarnMock.mockRestore();
209 });
210
211 it("should handle salvage when no sharedConfig is present", () => {
212 const corruptedContent = `{
213 "indexingPaused": true,
214 "corrupted": { invalid json
215}`;
216 fs.writeFileSync(globalContextFilePath, corruptedContent);
217
218 const consoleWarnMock = jest
219 .spyOn(console, "warn")
220 .mockImplementation(() => {});
221
222 globalContext.update("hasAlreadyCreatedAPromptFile", true);
223
224 expect(consoleWarnMock).toHaveBeenCalledWith(
225 expect.stringContaining(
226 "Error updating global context, attempting to salvage security-sensitive values",
227 ),
228 );
229
230 // Should have the new value
231 expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBe(true);
232
233 // Should not have any sharedConfig since none was salvageable
234 expect(globalContext.get("sharedConfig")).toBeUndefined();
235
236 consoleWarnMock.mockRestore();
237 });
238
239 it("should handle salvage when sharedConfig has invalid allowAnonymousTelemetry", () => {
240 const corruptedContent = `{
241 "indexingPaused": true,
242 "sharedConfig": {"allowAnonymousTelemetry": "not-a-boolean"},
243 "corrupted": { invalid json
244}`;
245 fs.writeFileSync(globalContextFilePath, corruptedContent);
246
247 const consoleWarnMock = jest
248 .spyOn(console, "warn")
249 .mockImplementation(() => {});
250
251 globalContext.update("indexingPaused", false);
252
253 // Should not have salvaged an invalid boolean value
254 const salvaged = globalContext.get("sharedConfig");
255 expect(salvaged).toBeUndefined();
256
257 consoleWarnMock.mockRestore();
258 });
259
260 it("should handle salvage with valid allowAnonymousTelemetry set to true", () => {
261 const corruptedContent = `{
262 "indexingPaused": true,
263 "sharedConfig": {"allowAnonymousTelemetry": true, "otherField": "value"},
264 "corrupted": { invalid json
265}`;
266 fs.writeFileSync(globalContextFilePath, corruptedContent);
267
268 const consoleWarnMock = jest
269 .spyOn(console, "warn")
270 .mockImplementation(() => {});
271
272 globalContext.update("hasAlreadyCreatedAPromptFile", true);
273
274 // Should have salvaged only the allowAnonymousTelemetry value
275 const salvaged = globalContext.get("sharedConfig");
276 expect(salvaged).toEqual({ allowAnonymousTelemetry: true });
277
278 consoleWarnMock.mockRestore();
279 });
280
281 it("should handle completely unparseable content gracefully", () => {
282 fs.writeFileSync(
283 globalContextFilePath,
284 "complete garbage not json at all",
285 );
286
287 const consoleWarnMock = jest
288 .spyOn(console, "warn")
289 .mockImplementation(() => {});
290
291 globalContext.update("indexingPaused", true);
292
293 // Should still work and create a new file
294 expect(globalContext.get("indexingPaused")).toBe(true);
295 expect(globalContext.get("sharedConfig")).toBeUndefined();
296
297 consoleWarnMock.mockRestore();
298 });
299
300 it("should preserve multiple salvageable values when updating different key", () => {
301 const corruptedContent = `{
302 "indexingPaused": true,
303 "sharedConfig": {"allowAnonymousTelemetry": false},
304 "hasAlreadyCreatedAPromptFile": true,
305 "corrupted": { invalid json
306}`;
307 fs.writeFileSync(globalContextFilePath, corruptedContent);
308
309 const consoleWarnMock = jest
310 .spyOn(console, "warn")
311 .mockImplementation(() => {});
312
313 // Update a different key - should salvage and preserve existing data
314 globalContext.update("showConfigUpdateToast", false);
315
316 // Should have the new value
317 expect(globalContext.get("showConfigUpdateToast")).toBe(false);
318
319 // Should have salvaged the security-sensitive value
320 expect(globalContext.get("sharedConfig")).toEqual({
321 allowAnonymousTelemetry: false,
322 });
323
324 consoleWarnMock.mockRestore();
325 });
326 });
327});