package plugin

import (
	"testing"
	"time"
)

func TestValidateStrategy(t *testing.T) {
	cases := []struct {
		name           string
		value          string
		allowBlueGreen bool
		wantErr        bool
	}{
		{"empty always ok (backward compat)", "", true, false},
		{"empty ok when blue-green disallowed", "", false, false},
		{"recreate ok", StrategyRecreate, true, false},
		{"recreate ok when blue-green disallowed", StrategyRecreate, false, false},
		{"blue-green ok when allowed", StrategyBlueGreen, true, false},
		{"blue-green rejected when disallowed (compose)", StrategyBlueGreen, false, true},
		{"reserved rolling rejected (allowed)", "rolling", true, true},
		{"reserved rolling rejected (disallowed)", "rolling", false, true},
		{"junk rejected", "banana", true, true},
	}
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			err := ValidateStrategy(c.value, c.allowBlueGreen)
			if (err != nil) != c.wantErr {
				t.Fatalf("ValidateStrategy(%q, %v) err=%v, wantErr=%v", c.value, c.allowBlueGreen, err, c.wantErr)
			}
		})
	}
}

func TestBuildGreenName_UniqueSuffixAndDistinct(t *testing.T) {
	base := "tf-build-app-1234abcd"
	a := BuildGreenName(base, time.Unix(1000, 0))
	b := BuildGreenName(base, time.Unix(2000, 0))
	if a == base || b == base {
		t.Fatal("green name must differ from the deterministic base")
	}
	if a == b {
		t.Fatal("different timestamps must yield different green names")
	}
	if len(a) <= len(base) {
		t.Fatalf("green name %q should extend the base %q", a, base)
	}
}
