package wgfunc

import (
	"github.com/stretchr/testify/assert"
	"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
	"testing"
	"time"
)

func TestConnection(t *testing.T) {
//sinnlos aber hey entiwkclung und so
	key, _ := wgtypes.GeneratePrivateKey()
	clientKey, _ := wgtypes.GeneratePrivateKey()

	serverConfig := WireGuardConfig{
		Name:       "wgUnit",
		IPRanges:   []string{"10.0.0.1/24"},
		Port:       54123,
		PrivateKey: key.String(),
	}
	server, err := NewWireGuard(serverConfig)
	assert.Nil(t, err, "cant create server")

	err = server.Up()
	defer server.Down()

	foundDevice := checkForNetworkDevice("wgUnit", t)

	assert.True(t, foundDevice, "should fount device with name wgUnit")



	peerConfigServer := PeerConfig{
		PublicKey:    clientKey.PublicKey().String(),
		PreSharedKey: "",
		AllowedIPs:   []string{"10.0.0.4/32"},
		Endpoint:     "",
	}
	server.AddPeer(peerConfigServer)


	//Client
	time.Sleep(10*time.Second)

	clientConfig := WireGuardConfig{
		Name: "wgclient",
		IPRanges: []string{"10.0.0.4/32"},
		PrivateKey: clientKey.String(),
	}

	client, err := NewWireGuard(clientConfig)
	assert.Nil(t, err, "should be able to create client config")
	client.Up()
	defer client.Down()

	peerConfigClient := PeerConfig{
		PublicKey:    key.PublicKey().String(),
		PreSharedKey: "",
		AllowedIPs:   []string{"10.0.0.1/24"},
		Endpoint:     "127.0.0.1:54123",
	}

	client.AddPeer(peerConfigClient)

}