|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest' |
| 2 | + |
| 3 | +vi.mock('bun:sqlite', () => ({ |
| 4 | + Database: vi.fn().mockImplementation(() => ({})), |
| 5 | +})) |
| 6 | + |
| 7 | +const mockListKv = vi.fn() |
| 8 | + |
| 9 | +vi.mock('../../src/utils/logger', () => ({ |
| 10 | + logger: { |
| 11 | + info: vi.fn(), |
| 12 | + error: vi.fn(), |
| 13 | + warn: vi.fn(), |
| 14 | + }, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('../../src/services/project-id-resolver', () => ({ |
| 18 | + resolveProjectId: vi.fn(), |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('../../src/services/plugin-memory', () => ({ |
| 22 | + PluginMemoryService: vi.fn().mockImplementation(() => ({ |
| 23 | + listKv: mockListKv, |
| 24 | + })), |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock('../../src/db/queries', () => ({ |
| 28 | + getRepoById: vi.fn(), |
| 29 | +})) |
| 30 | + |
| 31 | +vi.mock('@opencode-manager/shared/config/env', () => ({ |
| 32 | + getWorkspacePath: vi.fn(() => '/tmp/test-workspace'), |
| 33 | + getReposPath: vi.fn(() => '/tmp/test-repos'), |
| 34 | + getOpenCodeConfigFilePath: vi.fn(() => '/tmp/test-workspace/.config/opencode.json'), |
| 35 | + getAgentsMdPath: vi.fn(() => '/tmp/test-workspace/AGENTS.md'), |
| 36 | + getDatabasePath: vi.fn(() => ':memory:'), |
| 37 | + getConfigPath: vi.fn(() => '/tmp/test-workspace/config'), |
| 38 | + ENV: { |
| 39 | + SERVER: { PORT: 5003, HOST: '0.0.0.0', NODE_ENV: 'test' }, |
| 40 | + AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173', SECRET: 'test-secret-for-encryption-key-32c' }, |
| 41 | + WORKSPACE: { BASE_PATH: '/tmp/test-workspace', REPOS_DIR: 'repos', CONFIG_DIR: 'config', AUTH_FILE: 'auth.json' }, |
| 42 | + OPENCODE: { PORT: 5551, HOST: '127.0.0.1' }, |
| 43 | + DATABASE: { PATH: ':memory:' }, |
| 44 | + FILE_LIMITS: { |
| 45 | + MAX_SIZE_BYTES: 1024 * 1024, |
| 46 | + MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024, |
| 47 | + }, |
| 48 | + }, |
| 49 | + FILE_LIMITS: { |
| 50 | + MAX_SIZE_BYTES: 1024 * 1024, |
| 51 | + MAX_UPLOAD_SIZE_BYTES: 10 * 1024 * 1024, |
| 52 | + }, |
| 53 | +})) |
| 54 | + |
| 55 | +vi.mock('@opencode-manager/shared/utils', () => ({ |
| 56 | + parseJsonc: vi.fn(), |
| 57 | +})) |
| 58 | + |
| 59 | +import { createMemoryRoutes } from '../../src/routes/memory' |
| 60 | +import { resolveProjectId } from '../../src/services/project-id-resolver' |
| 61 | +import { getRepoById } from '../../src/db/queries' |
| 62 | + |
| 63 | +const mockResolveProjectId = resolveProjectId as ReturnType<typeof vi.fn> |
| 64 | +const mockGetRepoById = getRepoById as ReturnType<typeof vi.fn> |
| 65 | + |
| 66 | +describe('Memory Routes - Ralph Status', () => { |
| 67 | + let memoryApp: ReturnType<typeof createMemoryRoutes> |
| 68 | + let testDb: any |
| 69 | + |
| 70 | + beforeEach(() => { |
| 71 | + vi.clearAllMocks() |
| 72 | + mockResolveProjectId.mockReset() |
| 73 | + mockListKv.mockReset() |
| 74 | + mockGetRepoById.mockReset() |
| 75 | + |
| 76 | + testDb = {} as any |
| 77 | + memoryApp = createMemoryRoutes(testDb) |
| 78 | + }) |
| 79 | + |
| 80 | + describe('GET /ralph/status', () => { |
| 81 | + it('should return 400 when repoId query param is missing', async () => { |
| 82 | + const req = new Request('http://localhost/ralph/status') |
| 83 | + const res = await memoryApp.fetch(req) |
| 84 | + |
| 85 | + expect(res.status).toBe(400) |
| 86 | + const json = await res.json() as Record<string, unknown> |
| 87 | + expect(json.error).toBe('Missing repoId') |
| 88 | + }) |
| 89 | + |
| 90 | + it('should return 400 when repoId is not a valid number', async () => { |
| 91 | + const req = new Request('http://localhost/ralph/status?repoId=abc') |
| 92 | + const res = await memoryApp.fetch(req) |
| 93 | + |
| 94 | + expect(res.status).toBe(400) |
| 95 | + const json = await res.json() as Record<string, unknown> |
| 96 | + expect(json.error).toBe('Invalid repoId') |
| 97 | + }) |
| 98 | + |
| 99 | + it('should return 200 with empty loops when repo is not found in DB', async () => { |
| 100 | + mockGetRepoById.mockReturnValue(null) |
| 101 | + |
| 102 | + const req = new Request('http://localhost/ralph/status?repoId=1') |
| 103 | + const res = await memoryApp.fetch(req) |
| 104 | + |
| 105 | + expect(res.status).toBe(200) |
| 106 | + const json = await res.json() as Record<string, unknown> |
| 107 | + expect(json.loops).toEqual([]) |
| 108 | + }) |
| 109 | + |
| 110 | + it('should return 200 with empty loops when resolveProjectId returns null', async () => { |
| 111 | + mockGetRepoById.mockReturnValue({ |
| 112 | + id: 1, |
| 113 | + fullPath: '/tmp/test-repo', |
| 114 | + repoUrl: 'https://github.com/test/repo.git', |
| 115 | + localPath: 'test-repo', |
| 116 | + sourcePath: '', |
| 117 | + branch: 'main', |
| 118 | + currentBranch: 'main', |
| 119 | + cloneStatus: 'ready', |
| 120 | + isWorktree: false, |
| 121 | + openCodeConfigName: 'default', |
| 122 | + }) |
| 123 | + mockResolveProjectId.mockResolvedValue(null) |
| 124 | + |
| 125 | + const req = new Request('http://localhost/ralph/status?repoId=1') |
| 126 | + const res = await memoryApp.fetch(req) |
| 127 | + |
| 128 | + expect(res.status).toBe(200) |
| 129 | + const json = await res.json() as Record<string, unknown> |
| 130 | + expect(json.loops).toEqual([]) |
| 131 | + }) |
| 132 | + |
| 133 | + it('should return 200 with loops array for a valid repo with active loops', async () => { |
| 134 | + const mockRepo = { |
| 135 | + id: 1, |
| 136 | + fullPath: '/tmp/test-repo', |
| 137 | + repoUrl: 'https://github.com/test/repo.git', |
| 138 | + localPath: 'test-repo', |
| 139 | + sourcePath: '', |
| 140 | + branch: 'main', |
| 141 | + currentBranch: 'main', |
| 142 | + cloneStatus: 'ready', |
| 143 | + isWorktree: false, |
| 144 | + openCodeConfigName: 'default', |
| 145 | + } |
| 146 | + mockGetRepoById.mockReturnValue(mockRepo) |
| 147 | + mockResolveProjectId.mockResolvedValue('test-project-id') |
| 148 | + mockListKv.mockReturnValue([ |
| 149 | + { |
| 150 | + key: 'ralph:session-123', |
| 151 | + data: { |
| 152 | + active: true, |
| 153 | + sessionId: 'session-123', |
| 154 | + worktreeName: 'test-worktree', |
| 155 | + worktreeDir: '/tmp/worktrees/test', |
| 156 | + iteration: 1, |
| 157 | + maxIterations: 10, |
| 158 | + startedAt: '2024-01-01T00:00:00.000Z', |
| 159 | + prompt: 'Test prompt', |
| 160 | + phase: 'coding', |
| 161 | + audit: false, |
| 162 | + errorCount: 0, |
| 163 | + auditCount: 0, |
| 164 | + }, |
| 165 | + createdAt: Date.now(), |
| 166 | + updatedAt: Date.now(), |
| 167 | + expiresAt: Date.now() + 86400000, |
| 168 | + }, |
| 169 | + ]) |
| 170 | + |
| 171 | + const req = new Request('http://localhost/ralph/status?repoId=1') |
| 172 | + const res = await memoryApp.fetch(req) |
| 173 | + |
| 174 | + expect(res.status).toBe(200) |
| 175 | + const json = await res.json() as Record<string, unknown> |
| 176 | + expect(json.loops).toHaveLength(1) |
| 177 | + expect((json.loops as Array<Record<string, unknown>>)[0]?.active).toBe(true) |
| 178 | + }) |
| 179 | + |
| 180 | + it('should filter out KV entries that do not have an active field', async () => { |
| 181 | + const mockRepo = { |
| 182 | + id: 1, |
| 183 | + fullPath: '/tmp/test-repo', |
| 184 | + repoUrl: 'https://github.com/test/repo.git', |
| 185 | + localPath: 'test-repo', |
| 186 | + sourcePath: '', |
| 187 | + branch: 'main', |
| 188 | + currentBranch: 'main', |
| 189 | + cloneStatus: 'ready', |
| 190 | + isWorktree: false, |
| 191 | + openCodeConfigName: 'default', |
| 192 | + } |
| 193 | + mockGetRepoById.mockReturnValue(mockRepo) |
| 194 | + mockResolveProjectId.mockResolvedValue('test-project-id') |
| 195 | + mockListKv.mockReturnValue([ |
| 196 | + { |
| 197 | + key: 'ralph:session-123', |
| 198 | + data: { active: true, sessionId: 'session-123' }, |
| 199 | + createdAt: Date.now(), |
| 200 | + updatedAt: Date.now(), |
| 201 | + expiresAt: Date.now() + 86400000, |
| 202 | + }, |
| 203 | + { |
| 204 | + key: 'ralph:session-456', |
| 205 | + data: null, |
| 206 | + createdAt: Date.now(), |
| 207 | + updatedAt: Date.now(), |
| 208 | + expiresAt: Date.now() + 86400000, |
| 209 | + }, |
| 210 | + { |
| 211 | + key: 'ralph:session-789', |
| 212 | + data: 'string-data', |
| 213 | + createdAt: Date.now(), |
| 214 | + updatedAt: Date.now(), |
| 215 | + expiresAt: Date.now() + 86400000, |
| 216 | + }, |
| 217 | + { |
| 218 | + key: 'ralph:session-abc', |
| 219 | + data: { sessionId: 'session-abc' }, |
| 220 | + createdAt: Date.now(), |
| 221 | + updatedAt: Date.now(), |
| 222 | + expiresAt: Date.now() + 86400000, |
| 223 | + }, |
| 224 | + ]) |
| 225 | + |
| 226 | + const req = new Request('http://localhost/ralph/status?repoId=1') |
| 227 | + const res = await memoryApp.fetch(req) |
| 228 | + |
| 229 | + expect(res.status).toBe(200) |
| 230 | + const json = await res.json() as Record<string, unknown> |
| 231 | + expect(json.loops).toHaveLength(1) |
| 232 | + expect((json.loops as Array<Record<string, unknown>>)[0]?.active).toBe(true) |
| 233 | + }) |
| 234 | + |
| 235 | + it('should return 500 when an unexpected error is thrown', async () => { |
| 236 | + mockGetRepoById.mockImplementation(() => { |
| 237 | + throw new Error('Database error') |
| 238 | + }) |
| 239 | + |
| 240 | + const req = new Request('http://localhost/ralph/status?repoId=1') |
| 241 | + const res = await memoryApp.fetch(req) |
| 242 | + |
| 243 | + expect(res.status).toBe(500) |
| 244 | + const json = await res.json() as Record<string, unknown> |
| 245 | + expect(json.error).toBe('Failed to get Ralph status') |
| 246 | + }) |
| 247 | + }) |
| 248 | +}) |
0 commit comments