@@ -268,28 +268,27 @@ describe("SessionManager.setSessionFile with corrupted files", () => {
|
||||
expect(header.id).toBe(sm.getSessionId());
|
||||
});
|
||||
|
||||
it("truncates and rewrites file without valid header", () => {
|
||||
it("throws and preserves non-empty file without valid header", () => {
|
||||
const noHeaderFile = join(tempDir, "no-header.jsonl");
|
||||
// File with messages but no session header (corrupted state)
|
||||
writeFileSync(
|
||||
noHeaderFile,
|
||||
'{"type":"message","id":"abc","parentId":"orphaned","timestamp":"2025-01-01T00:00:00Z","message":{"role":"assistant","content":"test"}}\n',
|
||||
const originalContent =
|
||||
'{"type":"message","id":"abc","parentId":"orphaned","timestamp":"2025-01-01T00:00:00Z","message":{"role":"assistant","content":"test"}}\n';
|
||||
writeFileSync(noHeaderFile, originalContent);
|
||||
|
||||
expect(() => SessionManager.open(noHeaderFile, tempDir)).toThrow(
|
||||
`Session file is not a valid pi session and was not modified: ${noHeaderFile}`,
|
||||
);
|
||||
expect(readFileSync(noHeaderFile, "utf-8")).toBe(originalContent);
|
||||
});
|
||||
|
||||
const sm = SessionManager.open(noHeaderFile, tempDir);
|
||||
it("throws and preserves non-session JSONL files", () => {
|
||||
const nonSessionFile = join(tempDir, "not-a-session.log");
|
||||
const originalContent = '{"type":"event","data":"not a session"}\n';
|
||||
writeFileSync(nonSessionFile, originalContent);
|
||||
|
||||
// Should have created a new session with valid header
|
||||
expect(sm.getSessionId()).toBeTruthy();
|
||||
expect(sm.getHeader()).toBeTruthy();
|
||||
expect(sm.getHeader()?.type).toBe("session");
|
||||
|
||||
// File should now contain only a valid header (old content truncated)
|
||||
const content = readFileSync(noHeaderFile, "utf-8");
|
||||
const lines = content.trim().split("\n").filter(Boolean);
|
||||
expect(lines.length).toBe(1);
|
||||
const header = JSON.parse(lines[0]);
|
||||
expect(header.type).toBe("session");
|
||||
expect(header.id).toBe(sm.getSessionId());
|
||||
expect(() => SessionManager.open(nonSessionFile, tempDir)).toThrow(
|
||||
`Session file is not a valid pi session and was not modified: ${nonSessionFile}`,
|
||||
);
|
||||
expect(readFileSync(nonSessionFile, "utf-8")).toBe(originalContent);
|
||||
});
|
||||
|
||||
it("preserves explicit session file path when recovering from corrupted file", () => {
|
||||
@@ -302,16 +301,14 @@ describe("SessionManager.setSessionFile with corrupted files", () => {
|
||||
expect(sm.getSessionFile()).toBe(explicitPath);
|
||||
});
|
||||
|
||||
it("subsequent loads of recovered file work correctly", () => {
|
||||
const corruptedFile = join(tempDir, "corrupted.jsonl");
|
||||
writeFileSync(corruptedFile, "garbage content\n");
|
||||
it("subsequent loads of initialized empty file work correctly", () => {
|
||||
const emptyFile = join(tempDir, "empty.jsonl");
|
||||
writeFileSync(emptyFile, "");
|
||||
|
||||
// First open recovers the file
|
||||
const sm1 = SessionManager.open(corruptedFile, tempDir);
|
||||
const sm1 = SessionManager.open(emptyFile, tempDir);
|
||||
const sessionId = sm1.getSessionId();
|
||||
|
||||
// Second open should load the recovered file successfully
|
||||
const sm2 = SessionManager.open(corruptedFile, tempDir);
|
||||
const sm2 = SessionManager.open(emptyFile, tempDir);
|
||||
expect(sm2.getSessionId()).toBe(sessionId);
|
||||
expect(sm2.getHeader()?.type).toBe("session");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user