const { WebSocketServer } = require('ws');
const { createServer } = require('http');

// Test WebSocket maxPayload parameter
const httpServer = createServer();
const wss = new WebSocketServer({
  port: 0,
  maxPayload: 65536, // 64KB
});

let testPassed = false;

wss.on('connection', (ws) => {
  console.log('Client connected');
  
  // Send message larger than 64KB
  const oversized = 'x'.repeat(70000);
  
  console.log('Sending 70KB message...');
  ws.send(oversized, (err) => {
    if (err) {
      console.log('✓ Error received (expected):', err.message);
      testPassed = true;
    } else {
      console.log('✗ No error - maxPayload NOT enforced!');
    }
    
    ws.close();
    httpServer.close();
    wss.close();
    
    if (testPassed) {
      console.log('✅ TEST PASSED: maxPayload (64KB) is enforced');
      process.exit(0);
    } else {
      console.log('❌ TEST FAILED');
      process.exit(1);
    }
  });
});

httpServer.listen(0, () => {
  console.log('Server listening on port', httpServer.address().port);
});
