package dockerclient

import (
	"strings"
	"testing"
)

func TestPullPolicyIsKnown(t *testing.T) {
	cases := map[PullPolicy]bool{
		PullPolicyIfMissing: true,
		PullPolicyAlways:    true,
		PullPolicyNever:     true,
		PullPolicy(""):      false,
		PullPolicy("other"): false,
	}
	for p, want := range cases {
		if got := p.IsKnown(); got != want {
			t.Errorf("PullPolicy(%q).IsKnown() = %v, want %v", p, got, want)
		}
	}
}

func TestParseMemoryString(t *testing.T) {
	cases := []struct {
		raw  string
		want int64
	}{
		{"", 0},
		{"  ", 0},
		{"512", 512},
		{"512b", 512},
		{"4k", 4 * 1024},
		{"1m", 1 * 1024 * 1024},
		{"512M", 512 * 1024 * 1024},
		{"2g", 2 * 1024 * 1024 * 1024},
	}
	for _, c := range cases {
		got, err := parseMemoryString(c.raw)
		if err != nil {
			t.Errorf("parseMemoryString(%q) returned error: %v", c.raw, err)
			continue
		}
		if got != c.want {
			t.Errorf("parseMemoryString(%q) = %d, want %d", c.raw, got, c.want)
		}
	}
}

func TestParseMemoryStringRejectsInvalid(t *testing.T) {
	cases := []string{"abc", "1x", "-1m"}
	for _, c := range cases {
		if _, err := parseMemoryString(c); err == nil {
			t.Errorf("parseMemoryString(%q) expected error, got nil", c)
		}
	}
}

func TestParseLogOpts(t *testing.T) {
	got, err := parseLogOpts("max-size=10m,max-file=3")
	if err != nil {
		t.Fatalf("parseLogOpts unexpected error: %v", err)
	}
	if got["max-size"] != "10m" || got["max-file"] != "3" {
		t.Errorf("parseLogOpts produced %v", got)
	}
}

func TestParseLogOptsRejectsMissingValue(t *testing.T) {
	if _, err := parseLogOpts("solo,foo=bar"); err == nil || !strings.Contains(err.Error(), "key=value") {
		t.Errorf("expected key=value error, got %v", err)
	}
}

func TestCloneStringMapNilSafe(t *testing.T) {
	if got := cloneStringMap(nil); got != nil {
		t.Errorf("cloneStringMap(nil) = %v, want nil", got)
	}
	src := map[string]string{"a": "1"}
	got := cloneStringMap(src)
	got["a"] = "mutated"
	if src["a"] != "1" {
		t.Errorf("cloneStringMap leaks mutation: %v", src)
	}
}
