package confmgr

import "testing"

func TestDiffSets(t *testing.T) {
	left := ConfigSet{Parameters: []Parameter{
		{Key: "a", DS: "d", Signal: "A", Type: TypeFloat, Default: 1.0},
		{Key: "b", DS: "d", Signal: "B", Type: TypeFloat},
	}}
	right := ConfigSet{Parameters: []Parameter{
		{Key: "a", DS: "d", Signal: "A", Type: TypeFloat, Default: 2.0}, // changed default
		{Key: "c", DS: "d", Signal: "C", Type: TypeFloat},               // added
	}}
	diff := DiffSets(left, right)
	got := map[string]ChangeStatus{}
	for _, e := range diff {
		got[e.Key] = e.Status
	}
	if got["a"] != StatusChanged {
		t.Errorf("a: want changed, got %s", got["a"])
	}
	if got["b"] != StatusRemoved {
		t.Errorf("b: want removed, got %s", got["b"])
	}
	if got["c"] != StatusAdded {
		t.Errorf("c: want added, got %s", got["c"])
	}
}

func TestDiffSetsUnchanged(t *testing.T) {
	p := Parameter{Key: "a", DS: "d", Signal: "A", Type: TypeFloat, Default: 1.0}
	diff := DiffSets(ConfigSet{Parameters: []Parameter{p}}, ConfigSet{Parameters: []Parameter{p}})
	if len(diff) != 1 || diff[0].Status != StatusUnchanged {
		t.Errorf("want single unchanged entry, got %+v", diff)
	}
}

func TestDiffInstances(t *testing.T) {
	left := ConfigInstance{Values: map[string]any{"a": 1.0, "b": "x"}}
	right := ConfigInstance{Values: map[string]any{"a": 2.0, "c": true}}
	diff := DiffInstances(left, right)
	got := map[string]ChangeStatus{}
	for _, e := range diff {
		got[e.Key] = e.Status
	}
	if got["a"] != StatusChanged {
		t.Errorf("a: want changed, got %s", got["a"])
	}
	if got["b"] != StatusRemoved {
		t.Errorf("b: want removed, got %s", got["b"])
	}
	if got["c"] != StatusAdded {
		t.Errorf("c: want added, got %s", got["c"])
	}
}
