| 1 | // core/util/GlobalContext.test.ts |
| 2 | import fs from "node:fs"; |
| 3 | import { GlobalContext } from "./GlobalContext"; |
| 4 | import { getGlobalContextFilePath } from "./paths"; |
| 5 | |
| 6 | describe("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, deleting corrupted file", |
| 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 | }); |