import { Checker } from './Checker'
import { Room } from 'livekit-client'

export class WebSocketCheck extends Checker {
  name = 'WebSocketCheck'

  get description(): string {
    return 'Connecting to signal connection via WebSocket'
  }

  protected async perform(): Promise<void> {
    if (this.url.startsWith('ws:') || this.url.startsWith('http:')) {
      this.appendWarning('Server is insecure, clients may block connections to it')
    }

    const room = new Room()
    let signalClient = room.engine.client
    const joinRes = await signalClient.join(this.url, this.token, {
      autoSubscribe: true,
      maxRetries: 0,
      e2eeEnabled: false,
      websocketTimeout: 15_000,
    })
    this.appendMessage(`Connected to server, version ${joinRes.serverVersion}.`)
    await signalClient.close()
  }
}
