package server

import "testing"

func TestMatchesAny(t *testing.T) {
	cases := []struct {
		name     string
		pattern  string
		expected bool
	}{
		{"tailscale0", "tailscale*", true},
		{"Tailscale", "tailscale*", true},
		{"ts0", "ts*", true},
		{"eth0", "tailscale*,ts*", false},
		{"tailscale1", "tailscale*,ts*", true},
		{"utun3", "tailscale*,ts*,utun*", true},
		{"", "tailscale*", false},
	}
	for _, tc := range cases {
		got := matchesAny(tc.name, splitPatterns(tc.pattern))
		if got != tc.expected {
			t.Errorf("matchesAny(%q, %q) = %v, want %v", tc.name, tc.pattern, got, tc.expected)
		}
	}
}

func TestFindTailscaleAddrEmptyPattern(t *testing.T) {
	if _, err := FindTailscaleAddr(""); err == nil {
		t.Error("expected error on empty pattern")
	}
}

// We can't reliably assert that FindTailscaleAddr succeeds on arbitrary CI
// hosts, but on the typical dev box without Tailscale it should return
// ErrNoTailscaleInterface rather than panic.
func TestFindTailscaleAddrNoMatchReturnsError(t *testing.T) {
	_, err := FindTailscaleAddr("definitely-no-such-iface-name-xyz*")
	if err == nil {
		t.Skip("a host happens to have an interface matching the unlikely pattern; skipping")
	}
}
