|
| 1 | +import { describe, it, expect, beforeEach } from "bun:test" |
| 2 | +import { |
| 3 | + SubagentDatabase, |
| 4 | + AgentRepository, |
| 5 | + AgentTaskRepository, |
| 6 | + AgentMetricsRepository, |
| 7 | + MemorySegmentRepository, |
| 8 | +} from "../database.js" |
| 9 | + |
| 10 | +// Mock database connection |
| 11 | +class MockDatabaseConnection { |
| 12 | + private tables = new Map() |
| 13 | + private queries: Array<{ sql: string; params: any[] }> = [] |
| 14 | + |
| 15 | + async query(sql: string, params: any[] = []): Promise<any[]> { |
| 16 | + this.queries.push({ sql, params }) |
| 17 | + |
| 18 | + // Simple mock implementation for common queries |
| 19 | + if (sql.includes("SELECT 1")) { |
| 20 | + return [{ test: 1 }] |
| 21 | + } |
| 22 | + |
| 23 | + if (sql.includes("INSERT INTO agents")) { |
| 24 | + const [id, type, config, status] = params |
| 25 | + if (!this.tables.has("agents")) { |
| 26 | + this.tables.set("agents", []) |
| 27 | + } |
| 28 | + this.tables.get("agents").push({ id, type, config, status, created_at: new Date() }) |
| 29 | + return [] |
| 30 | + } |
| 31 | + |
| 32 | + if (sql.includes("SELECT * FROM agents")) { |
| 33 | + return this.tables.get("agents") || [] |
| 34 | + } |
| 35 | + |
| 36 | + return [] |
| 37 | + } |
| 38 | + |
| 39 | + async transaction<T>(callback: (db: any) => Promise<T>): Promise<T> { |
| 40 | + return callback(this) |
| 41 | + } |
| 42 | + |
| 43 | + async close(): Promise<void> { |
| 44 | + this.queries = [] |
| 45 | + this.tables.clear() |
| 46 | + } |
| 47 | + |
| 48 | + getQueries(): Array<{ sql: string; params: any[] }> { |
| 49 | + return this.queries |
| 50 | + } |
| 51 | + |
| 52 | + reset(): void { |
| 53 | + this.queries = [] |
| 54 | + this.tables.clear() |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +describe("SubagentDatabase", () => { |
| 59 | + let db: SubagentDatabase |
| 60 | + let mockConn: MockDatabaseConnection |
| 61 | + |
| 62 | + beforeEach(() => { |
| 63 | + mockConn = new MockDatabaseConnection() |
| 64 | + db = new SubagentDatabase(mockConn) |
| 65 | + }) |
| 66 | + |
| 67 | + describe("Initialization", () => { |
| 68 | + it("should initialize successfully", async () => { |
| 69 | + await db.initialize() |
| 70 | + |
| 71 | + const health = await db.getHealthStatus() |
| 72 | + expect(health.connected).toBe(true) |
| 73 | + expect(health.responseTime).toBeGreaterThan(0) |
| 74 | + }) |
| 75 | + |
| 76 | + it("should not initialize twice", async () => { |
| 77 | + await db.initialize() |
| 78 | + |
| 79 | + // Second initialization should not error, but should return early |
| 80 | + await db.initialize() |
| 81 | + |
| 82 | + const health = await db.getHealthStatus() |
| 83 | + expect(health.connected).toBe(true) |
| 84 | + }) |
| 85 | + }) |
| 86 | + |
| 87 | + describe("Repository Access", () => { |
| 88 | + beforeEach(async () => { |
| 89 | + await db.initialize() |
| 90 | + }) |
| 91 | + |
| 92 | + it("should provide agents repository", () => { |
| 93 | + const agents = db.getAgents() |
| 94 | + expect(agents).toBeDefined() |
| 95 | + expect(agents).toBeInstanceOf(AgentRepository) |
| 96 | + }) |
| 97 | + |
| 98 | + it("should provide tasks repository", () => { |
| 99 | + const tasks = db.getTasks() |
| 100 | + expect(tasks).toBeDefined() |
| 101 | + expect(tasks).toBeInstanceOf(AgentTaskRepository) |
| 102 | + }) |
| 103 | + |
| 104 | + it("should provide metrics repository", () => { |
| 105 | + const metrics = db.getMetrics() |
| 106 | + expect(metrics).toBeDefined() |
| 107 | + expect(metrics).toBeInstanceOf(AgentMetricsRepository) |
| 108 | + }) |
| 109 | + |
| 110 | + it("should provide memory segments repository", () => { |
| 111 | + const memorySegments = db.getMemorySegments() |
| 112 | + expect(memorySegments).toBeDefined() |
| 113 | + expect(memorySegments).toBeInstanceOf(MemorySegmentRepository) |
| 114 | + }) |
| 115 | + }) |
| 116 | + |
| 117 | + describe("Transaction Support", () => { |
| 118 | + beforeEach(async () => { |
| 119 | + await db.initialize() |
| 120 | + }) |
| 121 | + |
| 122 | + it("should execute transactions successfully", async () => { |
| 123 | + let transactionExecuted = false |
| 124 | + |
| 125 | + await db.transaction(async (transactionDb) => { |
| 126 | + transactionExecuted = true |
| 127 | + expect(transactionDb).toBeDefined() |
| 128 | + expect(transactionDb.getAgents()).toBeDefined() |
| 129 | + }) |
| 130 | + |
| 131 | + expect(transactionExecuted).toBe(true) |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + describe("Health Monitoring", () => { |
| 136 | + beforeEach(async () => { |
| 137 | + await db.initialize() |
| 138 | + }) |
| 139 | + |
| 140 | + it("should return accurate health status", async () => { |
| 141 | + const health = await db.getHealthStatus() |
| 142 | + |
| 143 | + expect(health.connected).toBe(true) |
| 144 | + expect(health.responseTime).toBeGreaterThan(0) |
| 145 | + expect(health.errorCount).toBe(0) |
| 146 | + expect(typeof health.responseTime).toBe("number") |
| 147 | + }) |
| 148 | + |
| 149 | + it("should handle connection failures", async () => { |
| 150 | + // Simulate connection failure |
| 151 | + mockConn.queries.push = () => { |
| 152 | + throw new Error("Connection failed") |
| 153 | + } |
| 154 | + |
| 155 | + const health = await db.getHealthStatus() |
| 156 | + |
| 157 | + expect(health.connected).toBe(false) |
| 158 | + expect(health.errorCount).toBe(1) |
| 159 | + }) |
| 160 | + }) |
| 161 | + |
| 162 | + describe("Resource Cleanup", () => { |
| 163 | + beforeEach(async () => { |
| 164 | + await db.initialize() |
| 165 | + }) |
| 166 | + |
| 167 | + it("should close connection properly", async () => { |
| 168 | + await db.close() |
| 169 | + |
| 170 | + const health = await db.getHealthStatus() |
| 171 | + // After close, health check should fail |
| 172 | + expect(health.connected).toBe(false) |
| 173 | + }) |
| 174 | + }) |
| 175 | +}) |
| 176 | + |
| 177 | +describe("AgentRepositoryImpl", () => { |
| 178 | + let repo: AgentRepository |
| 179 | + let mockConn: MockDatabaseConnection |
| 180 | + |
| 181 | + beforeEach(() => { |
| 182 | + mockConn = new MockDatabaseConnection() |
| 183 | + repo = new AgentRepositoryImpl(mockConn) |
| 184 | + }) |
| 185 | + |
| 186 | + describe("Agent CRUD Operations", () => { |
| 187 | + it("should create agent successfully", async () => { |
| 188 | + const agent = { |
| 189 | + id: "test-agent-1", |
| 190 | + type: "oracle", |
| 191 | + config: { model: "gpt-5", maxConcurrency: 3 }, |
| 192 | + status: "idle", |
| 193 | + } |
| 194 | + |
| 195 | + await repo.create(agent) |
| 196 | + |
| 197 | + const queries = mockConn.getQueries() |
| 198 | + expect(queries.some((q) => q.sql.includes("INSERT INTO agents"))).toBe(true) |
| 199 | + expect(queries.some((q) => q.params.includes("test-agent-1"))).toBe(true) |
| 200 | + }) |
| 201 | + |
| 202 | + it("should find agent by ID", async () => { |
| 203 | + // First create an agent |
| 204 | + const agent = { |
| 205 | + id: "find-test-agent", |
| 206 | + type: "coder", |
| 207 | + config: { model: "claude-sonnet", maxConcurrency: 5 }, |
| 208 | + status: "running", |
| 209 | + } |
| 210 | + |
| 211 | + await repo.create(agent) |
| 212 | + |
| 213 | + // Mock find by ID response |
| 214 | + mockConn.tables.set("agents", [ |
| 215 | + { |
| 216 | + id: "find-test-agent", |
| 217 | + type: "coder", |
| 218 | + config: '{"model":"claude-sonnet","maxConcurrency":5}', |
| 219 | + status: "running", |
| 220 | + }, |
| 221 | + ]) |
| 222 | + |
| 223 | + const found = await repo.findById("find-test-agent") |
| 224 | + |
| 225 | + expect(found).toBeDefined() |
| 226 | + expect(found.id).toBe("find-test-agent") |
| 227 | + expect(found.type).toBe("coder") |
| 228 | + expect(found.config).toEqual({ model: "claude-sonnet", maxConcurrency: 5 }) |
| 229 | + }) |
| 230 | + |
| 231 | + it("should return null for non-existent agent", async () => { |
| 232 | + const found = await repo.findById("non-existent-agent") |
| 233 | + expect(found).toBeNull() |
| 234 | + }) |
| 235 | + |
| 236 | + it("should find agents by type", async () => { |
| 237 | + // Mock agents of different types |
| 238 | + mockConn.tables.set("agents", [ |
| 239 | + { id: "oracle-1", type: "oracle", config: "{}", status: "idle" }, |
| 240 | + { id: "oracle-2", type: "oracle", config: "{}", status: "running" }, |
| 241 | + { id: "coder-1", type: "coder", config: "{}", status: "idle" }, |
| 242 | + ]) |
| 243 | + |
| 244 | + const oracleAgents = await repo.findByType("oracle") |
| 245 | + |
| 246 | + expect(oracleAgents).toHaveLength(2) |
| 247 | + expect(oracleAgents.every((a) => a.type === "oracle")).toBe(true) |
| 248 | + }) |
| 249 | + |
| 250 | + it("should update agent successfully", async () => { |
| 251 | + const updates = { |
| 252 | + status: "running", |
| 253 | + config: { model: "gpt-5-updated", maxConcurrency: 5 }, |
| 254 | + } |
| 255 | + |
| 256 | + await repo.update("test-agent", updates) |
| 257 | + |
| 258 | + const queries = mockConn.getQueries() |
| 259 | + expect(queries.some((q) => q.sql.includes("UPDATE agents"))).toBe(true) |
| 260 | + expect(queries.some((q) => q.params.includes("running"))).toBe(true) |
| 261 | + }) |
| 262 | + |
| 263 | + it("should delete agent successfully", async () => { |
| 264 | + await repo.delete("test-agent") |
| 265 | + |
| 266 | + const queries = mockConn.getQueries() |
| 267 | + expect(queries.some((q) => q.sql.includes("DELETE FROM agents"))).toBe(true) |
| 268 | + expect(queries.some((q) => q.params.includes("test-agent"))).toBe(true) |
| 269 | + }) |
| 270 | + |
| 271 | + it("should list all agents", async () => { |
| 272 | + // Mock agents list |
| 273 | + mockConn.tables.set("agents", [ |
| 274 | + { id: "agent-1", type: "oracle", config: '{"model":"gpt-5"}', status: "idle" }, |
| 275 | + { id: "agent-2", type: "coder", config: '{"model":"claude-sonnet"}', status: "running" }, |
| 276 | + ]) |
| 277 | + |
| 278 | + const agents = await repo.list() |
| 279 | + |
| 280 | + expect(agents).toHaveLength(2) |
| 281 | + expect(agents[0].config).toEqual({ model: "gpt-5" }) |
| 282 | + expect(agents[1].config).toEqual({ model: "claude-sonnet" }) |
| 283 | + }) |
| 284 | + }) |
| 285 | +}) |
0 commit comments