|
| 1 | +import { describe, it, beforeEach } from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | + |
| 4 | +import { SandboxManager, parseDuration } from '../sandbox-manager.js'; |
| 5 | +import type { SandboxConfig } from '../types.js'; |
| 6 | + |
| 7 | +describe('parseDuration', () => { |
| 8 | + it('parses minutes', () => { |
| 9 | + assert.strictEqual(parseDuration('30m'), 30 * 60 * 1000); |
| 10 | + }); |
| 11 | + |
| 12 | + it('parses hours', () => { |
| 13 | + assert.strictEqual(parseDuration('1h'), 60 * 60 * 1000); |
| 14 | + assert.strictEqual(parseDuration('24h'), 24 * 60 * 60 * 1000); |
| 15 | + }); |
| 16 | + |
| 17 | + it('parses days', () => { |
| 18 | + assert.strictEqual(parseDuration('3d'), 3 * 24 * 60 * 60 * 1000); |
| 19 | + }); |
| 20 | + |
| 21 | + it('parses fractional values', () => { |
| 22 | + assert.strictEqual(parseDuration('0.5h'), 0.5 * 60 * 60 * 1000); |
| 23 | + }); |
| 24 | + |
| 25 | + it('trims whitespace', () => { |
| 26 | + assert.strictEqual(parseDuration(' 1h '), 60 * 60 * 1000); |
| 27 | + }); |
| 28 | + |
| 29 | + it('throws for invalid format', () => { |
| 30 | + assert.throws(() => parseDuration('abc'), /Invalid duration/); |
| 31 | + assert.throws(() => parseDuration('1x'), /Invalid duration/); |
| 32 | + assert.throws(() => parseDuration(''), /Invalid duration/); |
| 33 | + assert.throws(() => parseDuration('1'), /Invalid duration/); |
| 34 | + }); |
| 35 | +}); |
| 36 | + |
| 37 | +describe('SandboxManager', () => { |
| 38 | + let manager: SandboxManager; |
| 39 | + |
| 40 | + const localConfig: SandboxConfig = { |
| 41 | + env: 'local', |
| 42 | + local: { cwd: '/tmp' }, |
| 43 | + }; |
| 44 | + |
| 45 | + const sessionConfig: SandboxConfig = { |
| 46 | + ...localConfig, |
| 47 | + scope: 'session', |
| 48 | + }; |
| 49 | + |
| 50 | + beforeEach(() => { |
| 51 | + manager = new SandboxManager('worker-1', 'test-project'); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('setWorkerId', () => { |
| 55 | + it('updates the worker ID', () => { |
| 56 | + manager.setWorkerId('worker-2'); |
| 57 | + // Verify by creating a sandbox (workerId propagates to sandbox) |
| 58 | + const sandbox = manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 59 | + assert.ok(sandbox); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe('getOrCreateSandbox — execution scope', () => { |
| 64 | + it('creates a new sandbox for each call', async () => { |
| 65 | + const s1 = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 66 | + const s2 = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-2' }); |
| 67 | + |
| 68 | + assert.notStrictEqual(s1.id, s2.id); |
| 69 | + }); |
| 70 | + |
| 71 | + it('attaches execution to sandbox', async () => { |
| 72 | + const sandbox = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 73 | + assert.ok(sandbox.activeExecutionIds.has('exec-1')); |
| 74 | + }); |
| 75 | + |
| 76 | + it('sandbox is retrievable by ID', async () => { |
| 77 | + const sandbox = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 78 | + const found = manager.getSandbox(sandbox.id); |
| 79 | + assert.strictEqual(found, sandbox); |
| 80 | + }); |
| 81 | + |
| 82 | + it('defaults scope to execution', async () => { |
| 83 | + const sandbox = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 84 | + assert.strictEqual(sandbox.scope, 'execution'); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('getOrCreateSandbox — session scope', () => { |
| 89 | + it('throws when sessionId is missing', async () => { |
| 90 | + await assert.rejects( |
| 91 | + () => manager.getOrCreateSandbox(sessionConfig, { executionId: 'exec-1' }), |
| 92 | + /sessionId is required/ |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it('creates a session sandbox', async () => { |
| 97 | + const sandbox = await manager.getOrCreateSandbox(sessionConfig, { |
| 98 | + executionId: 'exec-1', |
| 99 | + sessionId: 'sess-1', |
| 100 | + }); |
| 101 | + |
| 102 | + assert.strictEqual(sandbox.scope, 'session'); |
| 103 | + assert.ok(sandbox.activeExecutionIds.has('exec-1')); |
| 104 | + }); |
| 105 | + |
| 106 | + it('reuses existing session sandbox', async () => { |
| 107 | + const s1 = await manager.getOrCreateSandbox(sessionConfig, { |
| 108 | + executionId: 'exec-1', |
| 109 | + sessionId: 'sess-1', |
| 110 | + }); |
| 111 | + const s2 = await manager.getOrCreateSandbox(sessionConfig, { |
| 112 | + executionId: 'exec-2', |
| 113 | + sessionId: 'sess-1', |
| 114 | + }); |
| 115 | + |
| 116 | + assert.strictEqual(s1.id, s2.id); |
| 117 | + assert.ok(s2.activeExecutionIds.has('exec-1')); |
| 118 | + assert.ok(s2.activeExecutionIds.has('exec-2')); |
| 119 | + }); |
| 120 | + |
| 121 | + it('creates new sandbox for different sessions', async () => { |
| 122 | + const s1 = await manager.getOrCreateSandbox(sessionConfig, { |
| 123 | + executionId: 'exec-1', |
| 124 | + sessionId: 'sess-1', |
| 125 | + }); |
| 126 | + const s2 = await manager.getOrCreateSandbox(sessionConfig, { |
| 127 | + executionId: 'exec-2', |
| 128 | + sessionId: 'sess-2', |
| 129 | + }); |
| 130 | + |
| 131 | + assert.notStrictEqual(s1.id, s2.id); |
| 132 | + }); |
| 133 | + |
| 134 | + it('is retrievable by session ID', async () => { |
| 135 | + const sandbox = await manager.getOrCreateSandbox(sessionConfig, { |
| 136 | + executionId: 'exec-1', |
| 137 | + sessionId: 'sess-1', |
| 138 | + }); |
| 139 | + const found = manager.getSessionSandbox('sess-1'); |
| 140 | + assert.strictEqual(found, sandbox); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + describe('onExecutionComplete', () => { |
| 145 | + it('detaches execution from sandbox', async () => { |
| 146 | + const sandbox = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 147 | + assert.ok(sandbox.activeExecutionIds.has('exec-1')); |
| 148 | + |
| 149 | + await manager.onExecutionComplete('exec-1'); |
| 150 | + assert.ok(!sandbox.activeExecutionIds.has('exec-1')); |
| 151 | + }); |
| 152 | + |
| 153 | + it('destroys execution-scoped sandbox on completion', async () => { |
| 154 | + const sandbox = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 155 | + const sandboxId = sandbox.id; |
| 156 | + |
| 157 | + await manager.onExecutionComplete('exec-1'); |
| 158 | + |
| 159 | + // Sandbox should be removed from manager |
| 160 | + assert.strictEqual(manager.getSandbox(sandboxId), undefined); |
| 161 | + assert.strictEqual(sandbox.destroyed, true); |
| 162 | + }); |
| 163 | + |
| 164 | + it('does not destroy session-scoped sandbox on execution complete', async () => { |
| 165 | + const sandbox = await manager.getOrCreateSandbox(sessionConfig, { |
| 166 | + executionId: 'exec-1', |
| 167 | + sessionId: 'sess-1', |
| 168 | + }); |
| 169 | + |
| 170 | + await manager.onExecutionComplete('exec-1'); |
| 171 | + |
| 172 | + assert.strictEqual(sandbox.destroyed, false); |
| 173 | + assert.ok(manager.getSandbox(sandbox.id)); |
| 174 | + assert.ok(manager.getSessionSandbox('sess-1')); |
| 175 | + }); |
| 176 | + |
| 177 | + it('is safe for unknown execution ID', async () => { |
| 178 | + await manager.onExecutionComplete('nonexistent'); |
| 179 | + // Should not throw |
| 180 | + }); |
| 181 | + }); |
| 182 | + |
| 183 | + describe('destroySandbox', () => { |
| 184 | + it('destroys and removes a sandbox', async () => { |
| 185 | + const sandbox = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 186 | + const sandboxId = sandbox.id; |
| 187 | + |
| 188 | + await manager.destroySandbox(sandboxId); |
| 189 | + |
| 190 | + assert.strictEqual(sandbox.destroyed, true); |
| 191 | + assert.strictEqual(manager.getSandbox(sandboxId), undefined); |
| 192 | + }); |
| 193 | + |
| 194 | + it('removes session index for session-scoped sandbox', async () => { |
| 195 | + const sandbox = await manager.getOrCreateSandbox(sessionConfig, { |
| 196 | + executionId: 'exec-1', |
| 197 | + sessionId: 'sess-1', |
| 198 | + }); |
| 199 | + |
| 200 | + await manager.destroySandbox(sandbox.id); |
| 201 | + |
| 202 | + assert.strictEqual(manager.getSessionSandbox('sess-1'), undefined); |
| 203 | + }); |
| 204 | + |
| 205 | + it('is safe for unknown sandbox ID', async () => { |
| 206 | + await manager.destroySandbox('nonexistent'); |
| 207 | + // Should not throw |
| 208 | + }); |
| 209 | + }); |
| 210 | + |
| 211 | + describe('destroyAll', () => { |
| 212 | + it('destroys all sandboxes', async () => { |
| 213 | + const s1 = await manager.getOrCreateSandbox(localConfig, { executionId: 'exec-1' }); |
| 214 | + const s2 = await manager.getOrCreateSandbox(sessionConfig, { |
| 215 | + executionId: 'exec-2', |
| 216 | + sessionId: 'sess-1', |
| 217 | + }); |
| 218 | + |
| 219 | + await manager.destroyAll(); |
| 220 | + |
| 221 | + assert.strictEqual(s1.destroyed, true); |
| 222 | + assert.strictEqual(s2.destroyed, true); |
| 223 | + assert.strictEqual(manager.getSandbox(s1.id), undefined); |
| 224 | + assert.strictEqual(manager.getSandbox(s2.id), undefined); |
| 225 | + assert.strictEqual(manager.getSessionSandbox('sess-1'), undefined); |
| 226 | + }); |
| 227 | + |
| 228 | + it('is safe when no sandboxes exist', async () => { |
| 229 | + await manager.destroyAll(); |
| 230 | + // Should not throw |
| 231 | + }); |
| 232 | + }); |
| 233 | + |
| 234 | + describe('startSweep / stopSweep', () => { |
| 235 | + it('starts and stops without error', () => { |
| 236 | + manager.startSweep(60000); |
| 237 | + manager.stopSweep(); |
| 238 | + }); |
| 239 | + |
| 240 | + it('stop is idempotent', () => { |
| 241 | + manager.stopSweep(); |
| 242 | + manager.stopSweep(); |
| 243 | + }); |
| 244 | + |
| 245 | + it('start replaces existing sweep', () => { |
| 246 | + manager.startSweep(60000); |
| 247 | + manager.startSweep(60000); // should not throw or leak |
| 248 | + manager.stopSweep(); |
| 249 | + }); |
| 250 | + }); |
| 251 | + |
| 252 | + describe('getSandbox / getSessionSandbox', () => { |
| 253 | + it('returns undefined for unknown IDs', () => { |
| 254 | + assert.strictEqual(manager.getSandbox('unknown'), undefined); |
| 255 | + assert.strictEqual(manager.getSessionSandbox('unknown'), undefined); |
| 256 | + }); |
| 257 | + }); |
| 258 | +}); |
0 commit comments