|
| 1 | +// cd src && npx vitest run core/task-persistence/__tests__/apiMessages.spec.ts |
| 2 | + |
| 3 | +import * as os from "os" |
| 4 | +import * as path from "path" |
| 5 | +import * as fs from "fs/promises" |
| 6 | + |
| 7 | +import { readApiMessages } from "../apiMessages" |
| 8 | + |
| 9 | +let tmpBaseDir: string |
| 10 | + |
| 11 | +beforeEach(async () => { |
| 12 | + tmpBaseDir = await fs.mkdtemp(path.join(os.tmpdir(), "roo-test-api-")) |
| 13 | +}) |
| 14 | + |
| 15 | +describe("apiMessages.readApiMessages", () => { |
| 16 | + it("returns empty array when api_conversation_history.json contains invalid JSON", async () => { |
| 17 | + const taskId = "task-corrupt-api" |
| 18 | + const taskDir = path.join(tmpBaseDir, "tasks", taskId) |
| 19 | + await fs.mkdir(taskDir, { recursive: true }) |
| 20 | + const filePath = path.join(taskDir, "api_conversation_history.json") |
| 21 | + await fs.writeFile(filePath, "<<<corrupt data>>>", "utf8") |
| 22 | + |
| 23 | + const result = await readApiMessages({ |
| 24 | + taskId, |
| 25 | + globalStoragePath: tmpBaseDir, |
| 26 | + }) |
| 27 | + |
| 28 | + expect(result).toEqual([]) |
| 29 | + }) |
| 30 | + |
| 31 | + it("returns empty array when claude_messages.json fallback contains invalid JSON", async () => { |
| 32 | + const taskId = "task-corrupt-fallback" |
| 33 | + const taskDir = path.join(tmpBaseDir, "tasks", taskId) |
| 34 | + await fs.mkdir(taskDir, { recursive: true }) |
| 35 | + |
| 36 | + // Only write the old fallback file (claude_messages.json), NOT the new one |
| 37 | + const oldPath = path.join(taskDir, "claude_messages.json") |
| 38 | + await fs.writeFile(oldPath, "not json at all {[!", "utf8") |
| 39 | + |
| 40 | + const result = await readApiMessages({ |
| 41 | + taskId, |
| 42 | + globalStoragePath: tmpBaseDir, |
| 43 | + }) |
| 44 | + |
| 45 | + expect(result).toEqual([]) |
| 46 | + |
| 47 | + // The corrupted fallback file should NOT be deleted |
| 48 | + const stillExists = await fs |
| 49 | + .access(oldPath) |
| 50 | + .then(() => true) |
| 51 | + .catch(() => false) |
| 52 | + expect(stillExists).toBe(true) |
| 53 | + }) |
| 54 | + |
| 55 | + it("returns [] when file contains valid JSON that is not an array", async () => { |
| 56 | + const taskId = "task-non-array-api" |
| 57 | + const taskDir = path.join(tmpBaseDir, "tasks", taskId) |
| 58 | + await fs.mkdir(taskDir, { recursive: true }) |
| 59 | + const filePath = path.join(taskDir, "api_conversation_history.json") |
| 60 | + await fs.writeFile(filePath, JSON.stringify("hello"), "utf8") |
| 61 | + |
| 62 | + const result = await readApiMessages({ |
| 63 | + taskId, |
| 64 | + globalStoragePath: tmpBaseDir, |
| 65 | + }) |
| 66 | + |
| 67 | + expect(result).toEqual([]) |
| 68 | + }) |
| 69 | + |
| 70 | + it("returns [] when fallback file contains valid JSON that is not an array", async () => { |
| 71 | + const taskId = "task-non-array-fallback" |
| 72 | + const taskDir = path.join(tmpBaseDir, "tasks", taskId) |
| 73 | + await fs.mkdir(taskDir, { recursive: true }) |
| 74 | + |
| 75 | + // Only write the old fallback file, NOT the new one |
| 76 | + const oldPath = path.join(taskDir, "claude_messages.json") |
| 77 | + await fs.writeFile(oldPath, JSON.stringify({ key: "value" }), "utf8") |
| 78 | + |
| 79 | + const result = await readApiMessages({ |
| 80 | + taskId, |
| 81 | + globalStoragePath: tmpBaseDir, |
| 82 | + }) |
| 83 | + |
| 84 | + expect(result).toEqual([]) |
| 85 | + }) |
| 86 | +}) |
0 commit comments