|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 2 | +import { promises as fs } from 'fs'; |
| 3 | +import path from 'path'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | +import { ensureMemoryFilePath, defaultMemoryPath } from '../index.js'; |
| 6 | + |
| 7 | +describe('ensureMemoryFilePath', () => { |
| 8 | + const testDir = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | + const oldMemoryPath = path.join(testDir, '..', 'memory.json'); |
| 10 | + const newMemoryPath = path.join(testDir, '..', 'memory.jsonl'); |
| 11 | + |
| 12 | + let originalEnv: string | undefined; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + // Save original environment variable |
| 16 | + originalEnv = process.env.MEMORY_FILE_PATH; |
| 17 | + // Delete environment variable |
| 18 | + delete process.env.MEMORY_FILE_PATH; |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(async () => { |
| 22 | + // Restore original environment variable |
| 23 | + if (originalEnv !== undefined) { |
| 24 | + process.env.MEMORY_FILE_PATH = originalEnv; |
| 25 | + } else { |
| 26 | + delete process.env.MEMORY_FILE_PATH; |
| 27 | + } |
| 28 | + |
| 29 | + // Clean up test files |
| 30 | + try { |
| 31 | + await fs.unlink(oldMemoryPath); |
| 32 | + } catch { |
| 33 | + // Ignore if file doesn't exist |
| 34 | + } |
| 35 | + try { |
| 36 | + await fs.unlink(newMemoryPath); |
| 37 | + } catch { |
| 38 | + // Ignore if file doesn't exist |
| 39 | + } |
| 40 | + }); |
| 41 | + |
| 42 | + describe('with MEMORY_FILE_PATH environment variable', () => { |
| 43 | + it('should return absolute path when MEMORY_FILE_PATH is absolute', async () => { |
| 44 | + const absolutePath = '/tmp/custom-memory.jsonl'; |
| 45 | + process.env.MEMORY_FILE_PATH = absolutePath; |
| 46 | + |
| 47 | + const result = await ensureMemoryFilePath(); |
| 48 | + |
| 49 | + expect(result).toBe(absolutePath); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should convert relative path to absolute when MEMORY_FILE_PATH is relative', async () => { |
| 53 | + const relativePath = 'custom-memory.jsonl'; |
| 54 | + process.env.MEMORY_FILE_PATH = relativePath; |
| 55 | + |
| 56 | + const result = await ensureMemoryFilePath(); |
| 57 | + |
| 58 | + expect(path.isAbsolute(result)).toBe(true); |
| 59 | + expect(result).toContain('custom-memory.jsonl'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should handle Windows absolute paths', async () => { |
| 63 | + const windowsPath = 'C:\\temp\\memory.jsonl'; |
| 64 | + process.env.MEMORY_FILE_PATH = windowsPath; |
| 65 | + |
| 66 | + const result = await ensureMemoryFilePath(); |
| 67 | + |
| 68 | + // On Windows, should return as-is; on Unix, will be treated as relative |
| 69 | + if (process.platform === 'win32') { |
| 70 | + expect(result).toBe(windowsPath); |
| 71 | + } else { |
| 72 | + expect(path.isAbsolute(result)).toBe(true); |
| 73 | + } |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + describe('without MEMORY_FILE_PATH environment variable', () => { |
| 78 | + it('should return default path when no files exist', async () => { |
| 79 | + const result = await ensureMemoryFilePath(); |
| 80 | + |
| 81 | + expect(result).toBe(defaultMemoryPath); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should migrate from memory.json to memory.jsonl when only old file exists', async () => { |
| 85 | + // Create old memory.json file |
| 86 | + await fs.writeFile(oldMemoryPath, '{"test":"data"}'); |
| 87 | + |
| 88 | + const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 89 | + |
| 90 | + const result = await ensureMemoryFilePath(); |
| 91 | + |
| 92 | + expect(result).toBe(defaultMemoryPath); |
| 93 | + |
| 94 | + // Verify migration happened |
| 95 | + const newFileExists = await fs.access(newMemoryPath).then(() => true).catch(() => false); |
| 96 | + const oldFileExists = await fs.access(oldMemoryPath).then(() => true).catch(() => false); |
| 97 | + |
| 98 | + expect(newFileExists).toBe(true); |
| 99 | + expect(oldFileExists).toBe(false); |
| 100 | + |
| 101 | + // Verify console messages |
| 102 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 103 | + expect.stringContaining('DETECTED: Found legacy memory.json file') |
| 104 | + ); |
| 105 | + expect(consoleErrorSpy).toHaveBeenCalledWith( |
| 106 | + expect.stringContaining('COMPLETED: Successfully migrated') |
| 107 | + ); |
| 108 | + |
| 109 | + consoleErrorSpy.mockRestore(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should use new file when both old and new files exist', async () => { |
| 113 | + // Create both files |
| 114 | + await fs.writeFile(oldMemoryPath, '{"old":"data"}'); |
| 115 | + await fs.writeFile(newMemoryPath, '{"new":"data"}'); |
| 116 | + |
| 117 | + const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 118 | + |
| 119 | + const result = await ensureMemoryFilePath(); |
| 120 | + |
| 121 | + expect(result).toBe(defaultMemoryPath); |
| 122 | + |
| 123 | + // Verify no migration happened (both files should still exist) |
| 124 | + const newFileExists = await fs.access(newMemoryPath).then(() => true).catch(() => false); |
| 125 | + const oldFileExists = await fs.access(oldMemoryPath).then(() => true).catch(() => false); |
| 126 | + |
| 127 | + expect(newFileExists).toBe(true); |
| 128 | + expect(oldFileExists).toBe(true); |
| 129 | + |
| 130 | + // Verify no console messages about migration |
| 131 | + expect(consoleErrorSpy).not.toHaveBeenCalled(); |
| 132 | + |
| 133 | + consoleErrorSpy.mockRestore(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should preserve file content during migration', async () => { |
| 137 | + const testContent = '{"entities": [{"name": "test", "type": "person"}]}'; |
| 138 | + await fs.writeFile(oldMemoryPath, testContent); |
| 139 | + |
| 140 | + await ensureMemoryFilePath(); |
| 141 | + |
| 142 | + const migratedContent = await fs.readFile(newMemoryPath, 'utf-8'); |
| 143 | + expect(migratedContent).toBe(testContent); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe('defaultMemoryPath', () => { |
| 148 | + it('should end with memory.jsonl', () => { |
| 149 | + expect(defaultMemoryPath).toMatch(/memory\.jsonl$/); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should be an absolute path', () => { |
| 153 | + expect(path.isAbsolute(defaultMemoryPath)).toBe(true); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments