|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest" |
| 2 | +import * as fs from "fs/promises" |
| 3 | +import * as path from "path" |
| 4 | +import { readApiMessages, saveApiMessages } from "../apiMessages" |
| 5 | + |
| 6 | +// Mock dependencies |
| 7 | +vi.mock("../../../utils/fs") |
| 8 | +vi.mock("../../../utils/storage") |
| 9 | +vi.mock("fs/promises") |
| 10 | +vi.mock("../../../utils/safeWriteJson") |
| 11 | + |
| 12 | +const { fileExistsAtPath } = await import("../../../utils/fs") |
| 13 | +const { getTaskDirectoryPath } = await import("../../../utils/storage") |
| 14 | +const { safeWriteJson } = await import("../../../utils/safeWriteJson") |
| 15 | + |
| 16 | +describe("apiMessages", () => { |
| 17 | + const mockTaskId = "test-task-id" |
| 18 | + const mockGlobalStoragePath = "/mock/storage" |
| 19 | + const mockTaskDir = "/mock/storage/tasks/test-task-id" |
| 20 | + const mockFilePath = path.join(mockTaskDir, "api_conversation_history.json") |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + vi.clearAllMocks() |
| 24 | + vi.mocked(getTaskDirectoryPath).mockResolvedValue(mockTaskDir) |
| 25 | + }) |
| 26 | + |
| 27 | + afterEach(() => { |
| 28 | + vi.resetAllMocks() |
| 29 | + }) |
| 30 | + |
| 31 | + describe("readApiMessages - validation", () => { |
| 32 | + it("should successfully read and return valid messages", async () => { |
| 33 | + const validMessages = [ |
| 34 | + { role: "user", content: "Hello" }, |
| 35 | + { role: "assistant", content: [{ type: "text", text: "Hi there!" }] }, |
| 36 | + ] |
| 37 | + |
| 38 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 39 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(validMessages)) |
| 40 | + |
| 41 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 42 | + |
| 43 | + expect(result).toHaveLength(2) |
| 44 | + expect(result[0]).toEqual({ role: "user", content: "Hello" }) |
| 45 | + expect(result[1]).toEqual({ role: "assistant", content: [{ type: "text", text: "Hi there!" }] }) |
| 46 | + }) |
| 47 | + |
| 48 | + it("should fix messages with undefined content", async () => { |
| 49 | + const messagesWithUndefinedContent = [ |
| 50 | + { role: "user", content: "Valid" }, |
| 51 | + { role: "assistant", content: undefined }, |
| 52 | + { role: "user", content: "Continue" }, |
| 53 | + ] |
| 54 | + |
| 55 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 56 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(messagesWithUndefinedContent)) |
| 57 | + |
| 58 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 59 | + |
| 60 | + expect(result).toHaveLength(3) |
| 61 | + expect(result[0].content).toBe("Valid") |
| 62 | + expect(result[1].content).toEqual([]) // Fixed from undefined to empty array |
| 63 | + expect(result[2].content).toBe("Continue") |
| 64 | + }) |
| 65 | + |
| 66 | + it("should fix messages with null content", async () => { |
| 67 | + const messagesWithNullContent = [ |
| 68 | + { role: "user", content: "Valid" }, |
| 69 | + { role: "assistant", content: null }, |
| 70 | + { role: "user", content: "Continue" }, |
| 71 | + ] |
| 72 | + |
| 73 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 74 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(messagesWithNullContent)) |
| 75 | + |
| 76 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 77 | + |
| 78 | + expect(result).toHaveLength(3) |
| 79 | + expect(result[0].content).toBe("Valid") |
| 80 | + expect(result[1].content).toEqual([]) // Fixed from null to empty array |
| 81 | + expect(result[2].content).toBe("Continue") |
| 82 | + }) |
| 83 | + |
| 84 | + it("should fix messages with non-array, non-string content", async () => { |
| 85 | + const messagesWithInvalidContent = [ |
| 86 | + { role: "user", content: "Valid" }, |
| 87 | + { role: "assistant", content: { invalid: "object" } }, |
| 88 | + { role: "user", content: "Continue" }, |
| 89 | + ] |
| 90 | + |
| 91 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 92 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(messagesWithInvalidContent)) |
| 93 | + |
| 94 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 95 | + |
| 96 | + expect(result).toHaveLength(3) |
| 97 | + expect(result[0].content).toBe("Valid") |
| 98 | + expect(result[1].content).toEqual([]) // Fixed from object to empty array |
| 99 | + expect(result[2].content).toBe("Continue") |
| 100 | + }) |
| 101 | + |
| 102 | + it("should filter out messages with invalid roles", async () => { |
| 103 | + const messagesWithInvalidRoles = [ |
| 104 | + { role: "user", content: "Valid" }, |
| 105 | + { role: "invalid_role", content: "Bad message" }, |
| 106 | + { role: "assistant", content: "Also valid" }, |
| 107 | + ] |
| 108 | + |
| 109 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 110 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(messagesWithInvalidRoles)) |
| 111 | + |
| 112 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 113 | + |
| 114 | + expect(result).toHaveLength(2) |
| 115 | + expect(result[0].role).toBe("user") |
| 116 | + expect(result[1].role).toBe("assistant") |
| 117 | + }) |
| 118 | + |
| 119 | + it("should filter out completely malformed messages (not objects)", async () => { |
| 120 | + const messagesWithNonObjects = [ |
| 121 | + { role: "user", content: "Valid" }, |
| 122 | + null, |
| 123 | + "string message", |
| 124 | + 123, |
| 125 | + { role: "assistant", content: "Also valid" }, |
| 126 | + ] |
| 127 | + |
| 128 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 129 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(messagesWithNonObjects)) |
| 130 | + |
| 131 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 132 | + |
| 133 | + expect(result).toHaveLength(2) |
| 134 | + expect(result[0].role).toBe("user") |
| 135 | + expect(result[1].role).toBe("assistant") |
| 136 | + }) |
| 137 | + |
| 138 | + it("should filter out invalid blocks from message content arrays", async () => { |
| 139 | + const messagesWithInvalidBlocks = [ |
| 140 | + { |
| 141 | + role: "assistant", |
| 142 | + content: [ |
| 143 | + { type: "text", text: "Valid text" }, |
| 144 | + null, // Invalid block |
| 145 | + { invalid: "no type field" }, // Invalid block |
| 146 | + { type: "text", text: "Another valid text" }, |
| 147 | + ], |
| 148 | + }, |
| 149 | + ] |
| 150 | + |
| 151 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 152 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(messagesWithInvalidBlocks)) |
| 153 | + |
| 154 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 155 | + |
| 156 | + expect(result).toHaveLength(1) |
| 157 | + expect(Array.isArray(result[0].content)).toBe(true) |
| 158 | + expect((result[0].content as any[]).length).toBe(2) |
| 159 | + expect((result[0].content as any[])[0]).toEqual({ type: "text", text: "Valid text" }) |
| 160 | + expect((result[0].content as any[])[1]).toEqual({ type: "text", text: "Another valid text" }) |
| 161 | + }) |
| 162 | + |
| 163 | + it("should return empty array when parsedData is not an array", async () => { |
| 164 | + const nonArrayData = { invalid: "not an array" } |
| 165 | + |
| 166 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 167 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(nonArrayData)) |
| 168 | + |
| 169 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 170 | + |
| 171 | + expect(result).toEqual([]) |
| 172 | + }) |
| 173 | + |
| 174 | + it("should preserve valid metadata fields (ts, isSummary, etc.)", async () => { |
| 175 | + const messagesWithMetadata = [ |
| 176 | + { |
| 177 | + role: "user", |
| 178 | + content: "Test", |
| 179 | + ts: 1234567890, |
| 180 | + isSummary: true, |
| 181 | + condenseId: "c-123", |
| 182 | + truncationId: "t-456", |
| 183 | + }, |
| 184 | + ] |
| 185 | + |
| 186 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 187 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(messagesWithMetadata)) |
| 188 | + |
| 189 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 190 | + |
| 191 | + expect(result).toHaveLength(1) |
| 192 | + expect(result[0].ts).toBe(1234567890) |
| 193 | + expect(result[0].isSummary).toBe(true) |
| 194 | + expect(result[0].condenseId).toBe("c-123") |
| 195 | + expect(result[0].truncationId).toBe("t-456") |
| 196 | + }) |
| 197 | + |
| 198 | + it("should handle old claude_messages.json format with validation", async () => { |
| 199 | + const oldFormatMessages = [ |
| 200 | + { role: "user", content: "Valid" }, |
| 201 | + { role: "assistant", content: undefined }, // Should be fixed |
| 202 | + ] |
| 203 | + |
| 204 | + // New format doesn't exist, old format exists |
| 205 | + vi.mocked(fileExistsAtPath) |
| 206 | + .mockResolvedValueOnce(false) // New format |
| 207 | + .mockResolvedValueOnce(true) // Old format |
| 208 | + |
| 209 | + vi.mocked(fs.readFile).mockResolvedValue(JSON.stringify(oldFormatMessages)) |
| 210 | + // Mock unlink to prevent errors |
| 211 | + ;(fs as any).unlink = vi.fn().mockResolvedValue(undefined) |
| 212 | + |
| 213 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 214 | + |
| 215 | + expect(result).toHaveLength(2) |
| 216 | + expect(result[0].content).toBe("Valid") |
| 217 | + expect(result[1].content).toEqual([]) // Fixed from undefined |
| 218 | + }) |
| 219 | + |
| 220 | + it("should return empty array when no file exists", async () => { |
| 221 | + vi.mocked(fileExistsAtPath).mockResolvedValue(false) |
| 222 | + |
| 223 | + const result = await readApiMessages({ taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 224 | + |
| 225 | + expect(result).toEqual([]) |
| 226 | + }) |
| 227 | + }) |
| 228 | + |
| 229 | + describe("saveApiMessages", () => { |
| 230 | + it("should call safeWriteJson with correct parameters", async () => { |
| 231 | + const messages = [ |
| 232 | + { role: "user", content: "Test" }, |
| 233 | + { role: "assistant", content: [{ type: "text", text: "Response" }] }, |
| 234 | + ] as any[] |
| 235 | + |
| 236 | + await saveApiMessages({ messages, taskId: mockTaskId, globalStoragePath: mockGlobalStoragePath }) |
| 237 | + |
| 238 | + expect(safeWriteJson).toHaveBeenCalledWith(mockFilePath, messages) |
| 239 | + }) |
| 240 | + }) |
| 241 | +}) |
0 commit comments