Elffuss BraidHub

kikocis/continue public

6d66081d22d3 · 3.9 KiBRawPermalink
1// core/util/GlobalContext.test.ts
2import { GlobalContext, GlobalContextType } from "./GlobalContext";
3import fs from "node:fs";
4import path from "path";
5import { getGlobalContextFilePath } from "./paths";
6
7describe("GlobalContext", () => {
8 let globalContext: GlobalContext;
9 const globalContextFilePath = getGlobalContextFilePath();
10
11 beforeEach(() => {
12 // Remove the global context file if it exists
13 if (fs.existsSync(globalContextFilePath)) {
14 fs.unlinkSync(globalContextFilePath);
15 }
16 globalContext = new GlobalContext();
17 });
18
19 afterEach(() => {
20 // Clean up
21 if (fs.existsSync(globalContextFilePath)) {
22 fs.unlinkSync(globalContextFilePath);
23 }
24 });
25
26 it("should update and get a boolean value correctly", () => {
27 globalContext.update("indexingPaused", true);
28 const value = globalContext.get("indexingPaused");
29 expect(value).toBe(true);
30 });
31
32 it("should update and get an object value correctly", () => {
33 const workspaceIdentifier = "workspace-1";
34 globalContext.update("lastSelectedProfileForWorkspace", {
35 [workspaceIdentifier]: "profile-1",
36 });
37
38 const value = globalContext.get("lastSelectedProfileForWorkspace");
39 expect(value).toEqual({ "workspace-1": "profile-1" });
40 });
41
42 it("should handle non-existent global context file gracefully on get", () => {
43 const value = globalContext.get("indexingPaused");
44 expect(value).toBeUndefined();
45 });
46
47 it("should handle non-existent global context file gracefully on update", () => {
48 globalContext.update("indexingPaused", true);
49 const value = globalContext.get("indexingPaused");
50 expect(value).toBe(true);
51 });
52
53 it("should overwrite existing values", () => {
54 globalContext.update("indexingPaused", true);
55 expect(globalContext.get("indexingPaused")).toBe(true);
56
57 globalContext.update("indexingPaused", false);
58 expect(globalContext.get("indexingPaused")).toBe(false);
59 });
60
61 it("should handle JSON parsing errors gracefully on get", () => {
62 // Write invalid JSON to the file
63 fs.writeFileSync(globalContextFilePath, "{ invalid json }");
64
65 const consoleWarnMock = jest
66 .spyOn(console, "warn")
67 .mockImplementation(() => {});
68
69 const value = globalContext.get("indexingPaused");
70 expect(value).toBeUndefined();
71 expect(consoleWarnMock).toHaveBeenCalledWith(
72 expect.stringContaining("Error parsing global context"),
73 );
74
75 // Clean up
76 consoleWarnMock.mockRestore();
77 });
78
79 it("should handle JSON parsing errors gracefully on update", () => {
80 // Write invalid JSON to the file
81 fs.writeFileSync(globalContextFilePath, "{ invalid json }");
82
83 const consoleWarnMock = jest
84 .spyOn(console, "warn")
85 .mockImplementation(() => {});
86
87 // Attempt to update
88 globalContext.update("indexingPaused", true);
89
90 // The update should have been skipped due to JSON parsing error
91 // So attempting to get the value should return undefined
92 const value = globalContext.get("indexingPaused");
93 expect(value).toBeUndefined();
94
95 expect(consoleWarnMock).toHaveBeenCalledWith(
96 expect.stringContaining("Error updating global context"),
97 );
98
99 // Clean up
100 consoleWarnMock.mockRestore();
101 });
102
103 it("should update and retrieve multiple values correctly", () => {
104 globalContext.update("indexingPaused", true);
105 globalContext.update("hasDismissedConfigTsNoticeJetBrains", false);
106
107 expect(globalContext.get("indexingPaused")).toBe(true);
108 expect(globalContext.get("hasDismissedConfigTsNoticeJetBrains")).toBe(
109 false,
110 );
111 });
112
113 it("should handle updating hasAlreadyCreatedAPromptFile correctly", () => {
114 globalContext.update("hasAlreadyCreatedAPromptFile", true);
115 expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBe(true);
116 });
117
118 it("should not crash or throw when getting a key that hasn't been set", () => {
119 expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBeUndefined();
120 });
121});