|
| 1 | +import { describe, it, expect, beforeEach, vi } from "vitest" |
| 2 | +import * as fs from "fs/promises" |
| 3 | +import * as path from "path" |
| 4 | +import { HierarchicalMemoryManager } from "../HierarchicalMemoryManager" |
| 5 | +import { fileExistsAtPath } from "../../../utils/fs" |
| 6 | + |
| 7 | +// Mock fs/promises |
| 8 | +vi.mock("fs/promises") |
| 9 | +// Mock fileExistsAtPath |
| 10 | +vi.mock("../../../utils/fs") |
| 11 | + |
| 12 | +describe("HierarchicalMemoryManager", () => { |
| 13 | + let manager: HierarchicalMemoryManager |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + vi.clearAllMocks() |
| 17 | + }) |
| 18 | + |
| 19 | + describe("constructor", () => { |
| 20 | + it("should initialize with enabled state and file names", () => { |
| 21 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md", "Roorules.md"]) |
| 22 | + expect(manager).toBeDefined() |
| 23 | + }) |
| 24 | + |
| 25 | + it("should initialize with disabled state", () => { |
| 26 | + manager = new HierarchicalMemoryManager(false, []) |
| 27 | + expect(manager).toBeDefined() |
| 28 | + }) |
| 29 | + }) |
| 30 | + |
| 31 | + describe("loadFor", () => { |
| 32 | + it("should return empty array when disabled", async () => { |
| 33 | + manager = new HierarchicalMemoryManager(false, ["CLAUDE.md"]) |
| 34 | + const result = await manager.loadFor("/project/src/file.ts", "/project") |
| 35 | + expect(result).toEqual([]) |
| 36 | + }) |
| 37 | + |
| 38 | + it("should return empty array when no file names configured", async () => { |
| 39 | + manager = new HierarchicalMemoryManager(true, []) |
| 40 | + const result = await manager.loadFor("/project/src/file.ts", "/project") |
| 41 | + expect(result).toEqual([]) |
| 42 | + }) |
| 43 | + |
| 44 | + it("should load memory files from parent directories", async () => { |
| 45 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 46 | + |
| 47 | + // Mock file system |
| 48 | + vi.mocked(fileExistsAtPath).mockImplementation(async (filePath) => { |
| 49 | + return ( |
| 50 | + filePath === path.join("/project/src", "CLAUDE.md") || |
| 51 | + filePath === path.join("/project", "CLAUDE.md") |
| 52 | + ) |
| 53 | + }) |
| 54 | + |
| 55 | + vi.mocked(fs.readFile).mockImplementation(async (filePath) => { |
| 56 | + if (filePath === path.join("/project/src", "CLAUDE.md")) { |
| 57 | + return "# Source Memory\nThis is source directory memory." |
| 58 | + } |
| 59 | + if (filePath === path.join("/project", "CLAUDE.md")) { |
| 60 | + return "# Project Memory\nThis is project root memory." |
| 61 | + } |
| 62 | + throw new Error("File not found") |
| 63 | + }) |
| 64 | + |
| 65 | + const result = await manager.loadFor("/project/src/components/file.ts", "/project") |
| 66 | + |
| 67 | + expect(result).toHaveLength(2) |
| 68 | + // Results are reversed (root → leaf), so project memory comes first |
| 69 | + expect(result[0]).toMatchObject({ |
| 70 | + role: "user", |
| 71 | + content: expect.stringContaining("Memory from /project/CLAUDE.md"), |
| 72 | + isHierarchicalMemory: true, |
| 73 | + }) |
| 74 | + expect(result[1]).toMatchObject({ |
| 75 | + role: "user", |
| 76 | + content: expect.stringContaining("Memory from /project/src/CLAUDE.md"), |
| 77 | + isHierarchicalMemory: true, |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it("should not load duplicate memory files", async () => { |
| 82 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 83 | + |
| 84 | + // Mock file system |
| 85 | + vi.mocked(fileExistsAtPath).mockImplementation(async (filePath) => { |
| 86 | + return filePath === path.join("/project", "CLAUDE.md") |
| 87 | + }) |
| 88 | + |
| 89 | + vi.mocked(fs.readFile).mockImplementation(async (filePath) => { |
| 90 | + if (filePath === path.join("/project", "CLAUDE.md")) { |
| 91 | + return "# Project Memory\nThis is project root memory." |
| 92 | + } |
| 93 | + throw new Error("File not found") |
| 94 | + }) |
| 95 | + |
| 96 | + // Load for the first file |
| 97 | + const result1 = await manager.loadFor("/project/src/file1.ts", "/project") |
| 98 | + expect(result1).toHaveLength(1) |
| 99 | + |
| 100 | + // Load for the second file in the same directory - should not reload the same memory |
| 101 | + const result2 = await manager.loadFor("/project/src/file2.ts", "/project") |
| 102 | + expect(result2).toHaveLength(0) |
| 103 | + }) |
| 104 | + |
| 105 | + it("should handle file read errors gracefully", async () => { |
| 106 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 107 | + |
| 108 | + // Mock file system |
| 109 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 110 | + vi.mocked(fs.readFile).mockRejectedValue(new Error("Permission denied")) |
| 111 | + |
| 112 | + const result = await manager.loadFor("/project/src/file.ts", "/project") |
| 113 | + expect(result).toEqual([]) |
| 114 | + }) |
| 115 | + |
| 116 | + it("should stop at root directory", async () => { |
| 117 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 118 | + |
| 119 | + // Mock file system |
| 120 | + vi.mocked(fileExistsAtPath).mockImplementation(async (filePath) => { |
| 121 | + return filePath === path.join("/project", "CLAUDE.md") |
| 122 | + }) |
| 123 | + |
| 124 | + vi.mocked(fs.readFile).mockImplementation(async (filePath) => { |
| 125 | + if (filePath === path.join("/project", "CLAUDE.md")) { |
| 126 | + return "# Project Memory" |
| 127 | + } |
| 128 | + throw new Error("File not found") |
| 129 | + }) |
| 130 | + |
| 131 | + const result = await manager.loadFor("/project/src/file.ts", "/project") |
| 132 | + expect(result).toHaveLength(1) |
| 133 | + |
| 134 | + // Should not try to read beyond root |
| 135 | + expect(fs.readFile).not.toHaveBeenCalledWith(path.join("/", "CLAUDE.md"), "utf-8") |
| 136 | + }) |
| 137 | + |
| 138 | + it("should handle multiple memory file names", async () => { |
| 139 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md", "Roorules.md", ".context.md"]) |
| 140 | + |
| 141 | + // Mock file system |
| 142 | + vi.mocked(fileExistsAtPath).mockImplementation(async (filePath) => { |
| 143 | + const fileName = path.basename(filePath.toString()) |
| 144 | + const dirName = path.dirname(filePath.toString()) |
| 145 | + |
| 146 | + return ( |
| 147 | + (fileName === "CLAUDE.md" && dirName === "/project") || |
| 148 | + (fileName === "Roorules.md" && dirName === "/project") || |
| 149 | + (fileName === ".context.md" && dirName === "/project/src") |
| 150 | + ) |
| 151 | + }) |
| 152 | + |
| 153 | + vi.mocked(fs.readFile).mockImplementation(async (filePath) => { |
| 154 | + const fileName = path.basename(filePath.toString()) |
| 155 | + const dirName = path.dirname(filePath.toString()) |
| 156 | + |
| 157 | + if (fileName === "CLAUDE.md" && dirName === "/project") { |
| 158 | + return "# CLAUDE Memory" |
| 159 | + } |
| 160 | + if (fileName === "Roorules.md" && dirName === "/project") { |
| 161 | + return "# Roo Rules" |
| 162 | + } |
| 163 | + if (fileName === ".context.md" && dirName === "/project/src") { |
| 164 | + return "# Context Memory" |
| 165 | + } |
| 166 | + throw new Error("File not found") |
| 167 | + }) |
| 168 | + |
| 169 | + const result = await manager.loadFor("/project/src/file.ts", "/project") |
| 170 | + expect(result).toHaveLength(3) |
| 171 | + |
| 172 | + // Check that all three files were loaded |
| 173 | + const contents = result.map((msg) => msg.content.toString()) |
| 174 | + expect(contents.some((c) => c.includes("Context Memory"))).toBe(true) |
| 175 | + expect(contents.some((c) => c.includes("CLAUDE Memory"))).toBe(true) |
| 176 | + expect(contents.some((c) => c.includes("Roo Rules"))).toBe(true) |
| 177 | + }) |
| 178 | + |
| 179 | + it("should handle empty memory files", async () => { |
| 180 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 181 | + |
| 182 | + // Mock file system - only one file exists |
| 183 | + vi.mocked(fileExistsAtPath).mockImplementation(async (filePath) => { |
| 184 | + return filePath === path.join("/project", "CLAUDE.md") |
| 185 | + }) |
| 186 | + vi.mocked(fs.readFile).mockResolvedValue("") |
| 187 | + |
| 188 | + const result = await manager.loadFor("/project/src/file.ts", "/project") |
| 189 | + expect(result).toHaveLength(1) // Empty files are still loaded |
| 190 | + }) |
| 191 | + |
| 192 | + it("should include content with whitespace", async () => { |
| 193 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 194 | + |
| 195 | + // Mock file system - only one file exists |
| 196 | + vi.mocked(fileExistsAtPath).mockImplementation(async (filePath) => { |
| 197 | + return filePath === path.join("/project", "CLAUDE.md") |
| 198 | + }) |
| 199 | + vi.mocked(fs.readFile).mockResolvedValue("\n\n # Memory Content \n\n") |
| 200 | + |
| 201 | + const result = await manager.loadFor("/project/src/file.ts", "/project") |
| 202 | + expect(result).toHaveLength(1) |
| 203 | + expect(result[0].content).toContain("# Memory Content") |
| 204 | + }) |
| 205 | + }) |
| 206 | + |
| 207 | + describe("edge cases", () => { |
| 208 | + it("should handle file path at root directory", async () => { |
| 209 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 210 | + |
| 211 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 212 | + vi.mocked(fs.readFile).mockResolvedValue("# Root Memory") |
| 213 | + |
| 214 | + const result = await manager.loadFor("/file.ts", "/") |
| 215 | + expect(result).toHaveLength(1) |
| 216 | + }) |
| 217 | + |
| 218 | + it.skip("should handle Windows-style paths", async () => { |
| 219 | + // Skip this test on Unix systems as path handling is OS-specific |
| 220 | + // The implementation uses path.resolve which behaves differently on Windows vs Unix |
| 221 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 222 | + |
| 223 | + // Mock file system - handle Windows paths |
| 224 | + vi.mocked(fileExistsAtPath).mockImplementation(async (filePath) => { |
| 225 | + const fp = filePath.toString() |
| 226 | + return fp.endsWith("CLAUDE.md") |
| 227 | + }) |
| 228 | + |
| 229 | + vi.mocked(fs.readFile).mockImplementation(async (filePath) => { |
| 230 | + const fp = filePath.toString() |
| 231 | + if (fp.endsWith("CLAUDE.md")) { |
| 232 | + return "# Windows Memory" |
| 233 | + } |
| 234 | + throw new Error("File not found") |
| 235 | + }) |
| 236 | + |
| 237 | + const result = await manager.loadFor("C:\\project\\src\\file.ts", "C:\\project") |
| 238 | + expect(result.length).toBeGreaterThanOrEqual(1) |
| 239 | + }) |
| 240 | + |
| 241 | + it("should handle relative file paths by converting to absolute", async () => { |
| 242 | + manager = new HierarchicalMemoryManager(true, ["CLAUDE.md"]) |
| 243 | + |
| 244 | + // For relative paths, the manager should still work correctly |
| 245 | + vi.mocked(fileExistsAtPath).mockResolvedValue(true) |
| 246 | + vi.mocked(fs.readFile).mockResolvedValue("# Memory") |
| 247 | + |
| 248 | + const result = await manager.loadFor("./src/file.ts", ".") |
| 249 | + // Should still attempt to check for memory files |
| 250 | + expect(fileExistsAtPath).toHaveBeenCalled() |
| 251 | + }) |
| 252 | + }) |
| 253 | +}) |
0 commit comments