package service

import "testing"

func TestSettingsHelpers(t *testing.T) {
	t.Setenv("METIS_SAMPLE", "value")
	if got := getenvDefault("METIS_SAMPLE", "fallback"); got != "value" {
		t.Fatalf("getenvDefault = %q", got)
	}
	if got := getenvInt64("METIS_MAX_DEVICE_BYTES", 99); got != 99 {
		t.Fatalf("getenvInt64 fallback = %d", got)
	}
	t.Setenv("METIS_MAX_DEVICE_BYTES", "abc")
	if got := getenvInt64("METIS_MAX_DEVICE_BYTES", 99); got != 99 {
		t.Fatalf("getenvInt64 invalid = %d", got)
	}
	if got := splitList("a, ,b,,c"); len(got) != 3 || got[0] != "a" || got[2] != "c" {
		t.Fatalf("splitList = %#v", got)
	}
	if got := hostnameOr("fallback"); got == "" {
		t.Fatal("hostnameOr returned empty string")
	}
}

func TestHostnameFallbackAndSplitListBranches(t *testing.T) {
	orig := hostNameLookup
	hostNameLookup = func() (string, error) { return "", errStub{} }
	t.Cleanup(func() { hostNameLookup = orig })
	if got := hostnameOr("fallback"); got != "fallback" {
		t.Fatalf("hostnameOr fallback = %q", got)
	}
	if got := splitList(""); got != nil {
		t.Fatalf("splitList empty = %#v", got)
	}
	if got := splitList("  "); got != nil {
		t.Fatalf("splitList whitespace = %#v", got)
	}
}

type errStub struct{}

func (errStub) Error() string { return "stub" }
