fix: clear globalContext.json if corrupt (#6683)
This commit is contained in:
parent
25ebca018f
commit
2fb04eb2db
2 changed files with 98 additions and 11 deletions
|
|
@ -1,7 +1,6 @@
|
|||
// core/util/GlobalContext.test.ts
|
||||
import { GlobalContext, GlobalContextType } from "./GlobalContext";
|
||||
import fs from "node:fs";
|
||||
import path from "path";
|
||||
import { GlobalContext } from "./GlobalContext";
|
||||
import { getGlobalContextFilePath } from "./paths";
|
||||
|
||||
describe("GlobalContext", () => {
|
||||
|
|
@ -69,13 +68,40 @@ describe("GlobalContext", () => {
|
|||
const value = globalContext.get("indexingPaused");
|
||||
expect(value).toBeUndefined();
|
||||
expect(consoleWarnMock).toHaveBeenCalledWith(
|
||||
expect.stringContaining("Error parsing global context"),
|
||||
expect.stringContaining(
|
||||
"Error parsing global context, deleting corrupted file",
|
||||
),
|
||||
);
|
||||
|
||||
// Clean up
|
||||
consoleWarnMock.mockRestore();
|
||||
});
|
||||
|
||||
it("should delete corrupted file on get and allow fresh start", () => {
|
||||
// Write invalid JSON to the file
|
||||
fs.writeFileSync(globalContextFilePath, "{ invalid json }");
|
||||
expect(fs.existsSync(globalContextFilePath)).toBe(true);
|
||||
|
||||
const consoleWarnMock = jest
|
||||
.spyOn(console, "warn")
|
||||
.mockImplementation(() => {});
|
||||
|
||||
// Try to get a value - this should delete the corrupted file
|
||||
const value = globalContext.get("indexingPaused");
|
||||
expect(value).toBeUndefined();
|
||||
|
||||
// File should be deleted after corruption is detected
|
||||
expect(fs.existsSync(globalContextFilePath)).toBe(false);
|
||||
|
||||
// Now we should be able to update and get values normally
|
||||
globalContext.update("indexingPaused", true);
|
||||
const newValue = globalContext.get("indexingPaused");
|
||||
expect(newValue).toBe(true);
|
||||
|
||||
// Clean up
|
||||
consoleWarnMock.mockRestore();
|
||||
});
|
||||
|
||||
it("should handle JSON parsing errors gracefully on update", () => {
|
||||
// Write invalid JSON to the file
|
||||
fs.writeFileSync(globalContextFilePath, "{ invalid json }");
|
||||
|
|
@ -87,15 +113,45 @@ describe("GlobalContext", () => {
|
|||
// Attempt to update
|
||||
globalContext.update("indexingPaused", true);
|
||||
|
||||
// The update should have been skipped due to JSON parsing error
|
||||
// So attempting to get the value should return undefined
|
||||
const value = globalContext.get("indexingPaused");
|
||||
expect(value).toBeUndefined();
|
||||
|
||||
expect(consoleWarnMock).toHaveBeenCalledWith(
|
||||
expect.stringContaining("Error updating global context"),
|
||||
expect.stringContaining(
|
||||
"Error updating global context, deleting corrupted file",
|
||||
),
|
||||
);
|
||||
|
||||
// The update should have recreated the file with just the new value
|
||||
const value = globalContext.get("indexingPaused");
|
||||
expect(value).toBe(true);
|
||||
|
||||
// Clean up
|
||||
consoleWarnMock.mockRestore();
|
||||
});
|
||||
|
||||
it("should delete and recreate corrupted file on update", () => {
|
||||
// Write invalid JSON to the file
|
||||
fs.writeFileSync(globalContextFilePath, "{ invalid json }");
|
||||
expect(fs.existsSync(globalContextFilePath)).toBe(true);
|
||||
|
||||
const consoleWarnMock = jest
|
||||
.spyOn(console, "warn")
|
||||
.mockImplementation(() => {});
|
||||
|
||||
// Try to update - this should delete the corrupted file and recreate it
|
||||
globalContext.update("indexingPaused", true);
|
||||
|
||||
// File should still exist but with valid JSON now
|
||||
expect(fs.existsSync(globalContextFilePath)).toBe(true);
|
||||
|
||||
// Should be able to read the value
|
||||
const value = globalContext.get("indexingPaused");
|
||||
expect(value).toBe(true);
|
||||
|
||||
// Should be able to add more values
|
||||
globalContext.update("hasAlreadyCreatedAPromptFile", false);
|
||||
expect(globalContext.get("hasAlreadyCreatedAPromptFile")).toBe(false);
|
||||
// Original value should still be there
|
||||
expect(globalContext.get("indexingPaused")).toBe(true);
|
||||
|
||||
// Clean up
|
||||
consoleWarnMock.mockRestore();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -68,7 +68,28 @@ export class GlobalContext {
|
|||
try {
|
||||
parsed = JSON.parse(data);
|
||||
} catch (e: any) {
|
||||
console.warn(`Error updating global context: ${e}`);
|
||||
console.warn(
|
||||
`Error updating global context, deleting corrupted file: ${e}`,
|
||||
);
|
||||
// Delete the corrupted file and recreate it fresh
|
||||
try {
|
||||
fs.unlinkSync(filepath);
|
||||
} catch (deleteError) {
|
||||
console.warn(
|
||||
`Error deleting corrupted global context file: ${deleteError}`,
|
||||
);
|
||||
}
|
||||
// Recreate the file with just the new value
|
||||
fs.writeFileSync(
|
||||
filepath,
|
||||
JSON.stringify(
|
||||
{
|
||||
[key]: value,
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -90,7 +111,17 @@ export class GlobalContext {
|
|||
const parsed = JSON.parse(data);
|
||||
return parsed[key];
|
||||
} catch (e: any) {
|
||||
console.warn(`Error parsing global context: ${e}`);
|
||||
console.warn(
|
||||
`Error parsing global context, deleting corrupted file: ${e}`,
|
||||
);
|
||||
// Delete the corrupted file so it can be recreated fresh
|
||||
try {
|
||||
fs.unlinkSync(filepath);
|
||||
} catch (deleteError) {
|
||||
console.warn(
|
||||
`Error deleting corrupted global context file: ${deleteError}`,
|
||||
);
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue