|
10 | 10 |
|
11 | 11 | import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
12 | 12 | import { isValidWalletAddress, spawnChild } from "../replication/spawn.js"; |
| 13 | +import { SandboxCleanup } from "../replication/cleanup.js"; |
| 14 | +import { ChildLifecycle } from "../replication/lifecycle.js"; |
| 15 | +import { pruneDeadChildren } from "../replication/lineage.js"; |
13 | 16 | import { |
14 | 17 | MockConwayClient, |
15 | 18 | createTestDb, |
16 | 19 | createTestIdentity, |
17 | 20 | } from "./mocks.js"; |
18 | 21 | import type { AutomatonDatabase, GenesisConfig } from "../types.js"; |
| 22 | +import { MIGRATION_V7 } from "../state/schema.js"; |
19 | 23 |
|
20 | 24 | // Mock fs for constitution propagation |
21 | 25 | vi.mock("fs", async (importOriginal) => { |
@@ -194,3 +198,110 @@ describe("spawnChild", () => { |
194 | 198 | expect(deleteSpy).not.toHaveBeenCalled(); |
195 | 199 | }); |
196 | 200 | }); |
| 201 | + |
| 202 | +// ─── SandboxCleanup ────────────────────────────────────────── |
| 203 | + |
| 204 | +describe("SandboxCleanup", () => { |
| 205 | + let conway: MockConwayClient; |
| 206 | + let db: AutomatonDatabase; |
| 207 | + let lifecycle: ChildLifecycle; |
| 208 | + |
| 209 | + beforeEach(() => { |
| 210 | + conway = new MockConwayClient(); |
| 211 | + db = createTestDb(); |
| 212 | + // Apply lifecycle events migration |
| 213 | + db.raw.exec(MIGRATION_V7); |
| 214 | + lifecycle = new ChildLifecycle(db.raw); |
| 215 | + }); |
| 216 | + |
| 217 | + afterEach(() => { |
| 218 | + vi.restoreAllMocks(); |
| 219 | + }); |
| 220 | + |
| 221 | + it("does not transition to cleaned_up when sandbox deletion fails", async () => { |
| 222 | + // Create a child and transition to stopped |
| 223 | + lifecycle.initChild("child-1", "test-child", "sandbox-1", "test prompt"); |
| 224 | + lifecycle.transition("child-1", "sandbox_created", "created"); |
| 225 | + lifecycle.transition("child-1", "runtime_ready", "ready"); |
| 226 | + lifecycle.transition("child-1", "wallet_verified", "verified"); |
| 227 | + lifecycle.transition("child-1", "funded", "funded"); |
| 228 | + lifecycle.transition("child-1", "starting", "starting"); |
| 229 | + lifecycle.transition("child-1", "healthy", "healthy"); |
| 230 | + lifecycle.transition("child-1", "stopped", "stopped"); |
| 231 | + |
| 232 | + // Make deleteSandbox fail |
| 233 | + vi.spyOn(conway, "deleteSandbox").mockRejectedValue(new Error("API unavailable")); |
| 234 | + |
| 235 | + const cleanup = new SandboxCleanup(conway, lifecycle, db.raw); |
| 236 | + |
| 237 | + await expect(cleanup.cleanup("child-1")).rejects.toThrow("API unavailable"); |
| 238 | + |
| 239 | + // Child should still be in "stopped" state, NOT "cleaned_up" |
| 240 | + const state = lifecycle.getCurrentState("child-1"); |
| 241 | + expect(state).toBe("stopped"); |
| 242 | + }); |
| 243 | + |
| 244 | + it("transitions to cleaned_up when sandbox deletion succeeds", async () => { |
| 245 | + lifecycle.initChild("child-2", "test-child", "sandbox-2", "test prompt"); |
| 246 | + lifecycle.transition("child-2", "sandbox_created", "created"); |
| 247 | + lifecycle.transition("child-2", "runtime_ready", "ready"); |
| 248 | + lifecycle.transition("child-2", "wallet_verified", "verified"); |
| 249 | + lifecycle.transition("child-2", "funded", "funded"); |
| 250 | + lifecycle.transition("child-2", "starting", "starting"); |
| 251 | + lifecycle.transition("child-2", "healthy", "healthy"); |
| 252 | + lifecycle.transition("child-2", "stopped", "stopped"); |
| 253 | + |
| 254 | + const cleanup = new SandboxCleanup(conway, lifecycle, db.raw); |
| 255 | + await cleanup.cleanup("child-2"); |
| 256 | + |
| 257 | + const state = lifecycle.getCurrentState("child-2"); |
| 258 | + expect(state).toBe("cleaned_up"); |
| 259 | + }); |
| 260 | +}); |
| 261 | + |
| 262 | +// ─── pruneDeadChildren ────────────────────────────────────── |
| 263 | + |
| 264 | +describe("pruneDeadChildren", () => { |
| 265 | + let db: AutomatonDatabase; |
| 266 | + let conway: MockConwayClient; |
| 267 | + |
| 268 | + beforeEach(() => { |
| 269 | + db = createTestDb(); |
| 270 | + db.raw.exec(MIGRATION_V7); |
| 271 | + conway = new MockConwayClient(); |
| 272 | + }); |
| 273 | + |
| 274 | + afterEach(() => { |
| 275 | + vi.restoreAllMocks(); |
| 276 | + }); |
| 277 | + |
| 278 | + function insertChild(id: string, name: string, status: string, createdAt: string): void { |
| 279 | + db.raw.prepare( |
| 280 | + `INSERT INTO children (id, name, address, sandbox_id, genesis_prompt, status, created_at) |
| 281 | + VALUES (?, ?, '0xabc', 'sandbox-${id}', 'prompt', ?, ?)`, |
| 282 | + ).run(id, name, status, createdAt); |
| 283 | + } |
| 284 | + |
| 285 | + it("attempts sandbox cleanup for children with dead status", async () => { |
| 286 | + // Insert 7 dead children (exceeds keepLast=5, so 2 should be pruned) |
| 287 | + for (let i = 0; i < 7; i++) { |
| 288 | + insertChild(`dead-${i}`, `child-${i}`, "dead", `2020-01-0${i + 1} 00:00:00`); |
| 289 | + } |
| 290 | + |
| 291 | + // Create a mock cleanup that tracks calls |
| 292 | + const cleanupCalls: string[] = []; |
| 293 | + const mockCleanup = { |
| 294 | + cleanup: vi.fn(async (childId: string) => { |
| 295 | + cleanupCalls.push(childId); |
| 296 | + }), |
| 297 | + } as any; |
| 298 | + |
| 299 | + const removed = await pruneDeadChildren(db, mockCleanup, 5); |
| 300 | + |
| 301 | + // 2 oldest should be removed (dead-0 and dead-1) |
| 302 | + expect(removed).toBe(2); |
| 303 | + // cleanup.cleanup should have been called for "dead" children |
| 304 | + expect(cleanupCalls).toContain("dead-0"); |
| 305 | + expect(cleanupCalls).toContain("dead-1"); |
| 306 | + }); |
| 307 | +}); |
0 commit comments