Skip to content
Shell
3.7 KB
Raw Blame
/**
 * Test Scenario: Multi-Agent Browser Coordination
 *
 * Simulates 6 agents competing for browser contexts.
 */

import {
  reserveContext,
  releaseContext,
  shareContext,
  canUseContext,
  getStatus,
  heartbeat,
  forceReleaseAll,
} from './coordinator';

async function sleep(ms: number) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function runTest() {
  console.log('=== Playwright Coordinator Test Scenario ===\n');

  // Clean slate
  console.log('1. Cleaning up any existing state...');
  for (let i = 1; i <= 6; i++) {
    await forceReleaseAll(`test-agent-${i}`);
  }

  let status = await getStatus();
  console.log(`   Available: ${status.available}\n`);

  // Test: Multiple agents reserving
  console.log('2. Six agents try to reserve contexts...');
  const agents = ['alpha', 'beta', 'gamma', 'delta', 'epsilon', 'zeta'];
  const reservations = [];

  for (const agent of agents) {
    const result = await reserveContext(`agent-${agent}`, `Testing by ${agent}`);
    reservations.push({ agent, result });
    console.log(`   agent-${agent}: ${result.success ? `Got ${result.contextId}` : result.error}`);
  }

  status = await getStatus();
  console.log(`\n   Pool status: ${status.available} available, ${status.reserved} reserved\n`);

  // Test: 7th agent tries to reserve (should queue)
  console.log('3. Seventh agent tries to reserve (pool full)...');
  const overflow = await reserveContext('agent-overflow', 'Should queue');
  console.log(`   Result: ${overflow.success ? 'Got context' : `Queued at position ${overflow.waitPosition}`}\n`);

  // Test: Heartbeat
  console.log('4. Agent alpha sends heartbeat...');
  const hbResult = await heartbeat('agent-alpha');
  console.log(`   Heartbeat: ${hbResult ? 'OK' : 'Failed'}\n`);

  // Test: Sharing
  console.log('5. Agent alpha shares context with agent overflow...');
  const shareResult = await shareContext('agent-alpha', 'agent-overflow', 60000);
  console.log(`   Share: ${shareResult.success ? `Session ${shareResult.sessionId}` : shareResult.error}`);

  const overflowCheck = await canUseContext('agent-overflow');
  console.log(`   Overflow can now use: ${overflowCheck.canUse} (shared: ${overflowCheck.isShared})\n`);

  // Test: Release
  console.log('6. Releasing contexts...');
  for (const agent of agents.slice(0, 3)) {
    const result = await releaseContext(`agent-${agent}`);
    console.log(`   agent-${agent}: ${result.success ? 'Released' : result.error}`);
  }

  status = await getStatus();
  console.log(`\n   Pool status: ${status.available} available, ${status.reserved} reserved\n`);

  // Test: Overflow agent can now reserve their own
  console.log('7. Overflow agent now reserves own context...');
  const overflowReserve = await reserveContext('agent-overflow', 'Finally got my own');
  console.log(`   Result: ${overflowReserve.success ? `Got ${overflowReserve.contextId}` : overflowReserve.error}\n`);

  // Final status
  console.log('8. Final pool status:');
  status = await getStatus();
  console.log(`   Available: ${status.available}`);
  console.log(`   Reserved: ${status.reserved}`);
  console.log(`   Shared: ${status.shared}`);
  console.log(`   Waiting: ${status.waiting}`);

  console.log('\n   Contexts:');
  for (const ctx of status.contexts) {
    const icon = ctx.status === 'available' ? '🟒' : ctx.status === 'reserved' ? '🟑' : 'πŸ”΅';
    console.log(`   ${icon} ${ctx.id}: ${ctx.status}${ctx.agentId ? ` (${ctx.agentId})` : ''}`);
  }

  // Cleanup
  console.log('\n9. Cleaning up...');
  for (const agent of [...agents, 'overflow']) {
    await forceReleaseAll(`agent-${agent}`);
  }

  status = await getStatus();
  console.log(`   All cleaned: ${status.available} available\n`);

  console.log('=== Test Complete ===');
}

runTest().catch(console.error);