package image

import (
	"encoding/json"
	"testing"

	"github.com/alexei/tinyforge/internal/workload/plugin"
)

func imageCfg(strategy string) json.RawMessage {
	m := map[string]any{"image": "registry.example.com/o/app", "port": 8080}
	if strategy != "" {
		m["deploy_strategy"] = strategy
	}
	b, _ := json.Marshal(m)
	return b
}

func TestValidate_Strategy(t *testing.T) {
	cases := []struct {
		strategy string
		wantErr  bool
	}{
		{"", false},
		{"recreate", false},
		{"blue-green", false},
		{"rolling", true},
		{"junk", true},
	}
	for _, c := range cases {
		t.Run("strategy="+c.strategy, func(t *testing.T) {
			err := (&source{}).Validate(imageCfg(c.strategy))
			if (err != nil) != c.wantErr {
				t.Fatalf("Validate(strategy=%q) err=%v, wantErr=%v", c.strategy, err, c.wantErr)
			}
		})
	}
}

func TestEffectiveStrategy_DefaultsToBlueGreen(t *testing.T) {
	// image's historical default is blue-green — empty must NOT flip it to
	// recreate (the load-bearing per-source default).
	if got := effectiveStrategy(Config{}); got != plugin.StrategyBlueGreen {
		t.Fatalf("empty strategy = %q, want blue-green (image default)", got)
	}
	if got := effectiveStrategy(Config{DeployStrategy: plugin.StrategyRecreate}); got != plugin.StrategyRecreate {
		t.Fatalf("explicit recreate = %q, want recreate", got)
	}
}
