package ci

import "testing"

func TestExpandVariables(t *testing.T) {
	env := map[string]string{
		"HOST": "db.local", "PORT": "5432", "CI_PROJECT_NAME": "app",
		"URL":  "postgres://$HOST:${PORT}/${CI_PROJECT_NAME}",
		"PASS": "pa$$w0rd$HOST",
		"COST": "$$5 en $ONBEKEND en ${ONBEKEND2}",
		"A":    "x$B", "B": "y$A",
		"NEST": "<$URL>",
	}
	got := ExpandVariables(env, map[string]bool{"URL": true, "COST": true, "A": true, "B": true, "NEST": true, "PASS": false})
	if got["URL"] != "postgres://db.local:5432/app" {
		t.Fatalf("URL=%q", got["URL"])
	}
	if got["NEST"] != "<postgres://db.local:5432/app>" {
		t.Fatalf("NEST=%q", got["NEST"])
	}
	if got["COST"] != "$5 en $ONBEKEND en ${ONBEKEND2}" {
		t.Fatalf("COST=%q", got["COST"])
	}
	if _, ok := got["PASS"]; ok {
		t.Fatal("PASS zonder expand mag niet veranderen")
	}
	if env["PASS"] != "pa$$w0rd$HOST" {
		t.Fatal("env aangepast")
	}
	// cyclus: geen hang, verwijzing blijft letterlijk
	if got["A"] == "" || got["B"] == "" {
		t.Fatalf("cyclus: %q %q", got["A"], got["B"])
	}
}

func TestExpandVariablesOff(t *testing.T) {
	env := map[string]string{"P": "a$b$$c", "b": "X"}
	if got := ExpandVariables(env, nil); len(got) != 0 {
		t.Fatalf("%v", got)
	}
}
