package mdns

import (
	"fmt"
	"net"
	"testing"

	"golib/v2/pkg/mdns"
)

const (
	LocalName = "simanc-test.local"
)

func TestListenAndServe(t *testing.T) {
	err := ListenAndServe(LocalName)
	if err != nil {
		t.Error(err)
	}
}

func TestLookup(t *testing.T) {
	ips, err := Lookup(LocalName)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log(ips)
}

func TestLookups(t *testing.T) {
	names := []string{
		Fqdn("a.simanc-test"),
		Fqdn("b.simanc-test"),
		Fqdn("c.simanc-test"),
	}
	go func() {
		if err := ListenAndServeNames(names); err != nil {
			panic(err)
		}
	}()
	ips, err := Lookups(names)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log(ips)
}

func TestClient_ListenAndServe(t *testing.T) {
	client := &Client{
		Name:    []string{LocalName},
		Address: mdns.Address,
		Handle:  clientHandler,
	}
	if err := client.ListenAndServe(); err != nil {
		t.Error(err)
	}
}

func clientHandler(name string, addr net.IP) {
	fmt.Println("Name:", name, "Addr:", addr)
}
