package pnyx

import (
	"crypto/ed25519"
	"crypto/rand"
	"net"
	"slices"
	"testing"
)

func fatalErr(t *testing.T, err error) {
  if err != nil {
    t.Fatal(err)
  }
}

func TestSessionOpen(t *testing.T) {
  client_pubkey, client_key, err := ed25519.GenerateKey(rand.Reader)
  fatalErr(t, err)
  client_id := ID[PeerID](client_pubkey)

  server_pubkey, server_key, err := ed25519.GenerateKey(rand.Reader)
  fatalErr(t, err)
  server_id := ID[PeerID](server_pubkey)

  client_so, client_ecdh, err := NewSessionOpen(client_key)
  fatalErr(t, err)
  if client_so[0] != byte(SESSION_OPEN) {
    t.Fatalf("Session open command byte mismatch(%x != %x)", client_so[0], SESSION_OPEN)
  }

  server_so, server_ecdh, err := NewSessionOpen(server_key)
  fatalErr(t, err)

  server_session, err := ParseSessionOpen(server_ecdh, client_so[COMMAND_LENGTH:])
  fatalErr(t, err)

  client_session, err := ParseSessionOpen(client_ecdh, server_so[COMMAND_LENGTH:])
  fatalErr(t, err)

  if client_id != server_session.Peer {
    t.Fatalf("Server session(%s) has wrong peer ID(%s)", server_session.Peer, client_id)
  }
  
  if server_id != client_session.Peer {
    t.Fatalf("Client session(%s) has wrong peer ID(%s)", server_session.Peer, client_id)
  }

  if slices.Compare(server_session.secret, client_session.secret) != 0 {
    t.Fatalf("Client secret(%x) and server secret(%x) do not match", client_session.secret, server_session.secret)
  }
}

func TestECDH(t *testing.T) {
  client_public, client_private, err := ed25519.GenerateKey(rand.Reader)
  fatalErr(t, err)

  server_public, server_private, err := ed25519.GenerateKey(rand.Reader)
  fatalErr(t, err)

  server_secret, err := ECDH(client_public, server_private)
  fatalErr(t, err)

  client_secret, err := ECDH(server_public, client_private)
  fatalErr(t, err)

  if slices.Compare(server_secret, client_secret) != 0 {
    t.Fatalf("Server and Client secrets do not match")
  }
}

func TestSessionConnect(t *testing.T) {
  secret := make([]byte, 32)
  test_addr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8080")
  fatalErr(t, err)

  session_id := ID[SessionID](secret)
  session_connect := NewSessionConnect(test_addr, secret)
  if session_connect[0] != byte(SESSION_CONNECT) {
    t.Fatalf("Session open command byte mismatch(%x != %x)", session_connect[0], SESSION_CONNECT)
  } else if slices.Compare(session_connect[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH], session_id[:]) != 0 {
    t.Fatal("Session open ID mismatch")
  }

  parsed_addr, err := ParseSessionConnect(session_connect[COMMAND_LENGTH + ID_LENGTH:], secret)
  fatalErr(t, err)

  if parsed_addr.String() != test_addr.String() {
    t.Fatalf("Parsed address(%s) does not match test address(%s)", parsed_addr, test_addr)
  }
}

func TestSessionData(t *testing.T) {
  _, client_key, err := ed25519.GenerateKey(rand.Reader)
  fatalErr(t, err)

  _, server_key, err := ed25519.GenerateKey(rand.Reader)
  fatalErr(t, err)

  client_so, client_ecdh, err := NewSessionOpen(client_key)
  fatalErr(t, err)

  server_so, server_ecdh, err := NewSessionOpen(server_key)
  fatalErr(t, err)

  server_session, err := ParseSessionOpen(server_ecdh, client_so[COMMAND_LENGTH:])
  fatalErr(t, err)

  client_session, err := ParseSessionOpen(client_ecdh, server_so[COMMAND_LENGTH:])
  fatalErr(t, err)

  message := []byte("hello")
  session_data, err := NewSessionData(&server_session, message)
  fatalErr(t, err)

  if session_data[0] != byte(SESSION_DATA) {
    t.Fatalf("Session data command byte mismatch(%x != %x)", session_data[0], SESSION_DATA)
  } else if slices.Compare(session_data[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH], server_session.ID[:]) != 0 {
    t.Fatal("Session data ID mismatch")
  }

  parsed_message, err := ParseSessionData(&client_session, session_data[COMMAND_LENGTH+ID_LENGTH:])
  fatalErr(t, err)

  if slices.Compare(message, parsed_message) != 0 {
    t.Fatalf("Parsed message(%s) != test message(%s)", parsed_message, message)
  }
}

func TestSessionClose(t *testing.T) {
  _, client_key, err := ed25519.GenerateKey(rand.Reader)
  fatalErr(t, err)

  _, server_key, err := ed25519.GenerateKey(rand.Reader)
  fatalErr(t, err)

  client_so, client_ecdh, err := NewSessionOpen(client_key)
  fatalErr(t, err)

  server_so, server_ecdh, err := NewSessionOpen(server_key)
  fatalErr(t, err)

  server_session, err := ParseSessionOpen(server_ecdh, client_so[COMMAND_LENGTH:])
  fatalErr(t, err)

  client_session, err := ParseSessionOpen(client_ecdh, server_so[COMMAND_LENGTH:])
  fatalErr(t, err)

  session_close := NewSessionClose(&client_session)

  if session_close[0] != byte(SESSION_CLOSE) {
    t.Fatalf("Session close command byte mismatch(%x != %x)", session_close[0], SESSION_CLOSE)
  } else if slices.Compare(session_close[COMMAND_LENGTH:COMMAND_LENGTH+ID_LENGTH], server_session.ID[:]) != 0 {
    t.Fatal("Session close ID mismatch")
  }

  fatalErr(t, ParseSessionClose(&server_session, session_close[COMMAND_LENGTH+ID_LENGTH:]))
}
