51 lines
1.9 KiB
Go
51 lines
1.9 KiB
Go
package configkit_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
configkit "go.maximumdirect.net/configkit"
|
|
)
|
|
|
|
func TestTierHistoryRecordsEachContributionOnce(t *testing.T) {
|
|
t.Parallel()
|
|
for _, middle := range []string{"mid", "low"} {
|
|
t.Run(middle, func(t *testing.T) {
|
|
result, err := configkit.LoadLayered[map[string]any](
|
|
configkit.Input{Source: configkit.Bytes("main", []byte("x: high\n"))},
|
|
configkit.LayeredOptions[map[string]any]{Underlays: []configkit.Underlay{
|
|
{Inputs: []configkit.Input{{Source: configkit.Bytes("low", []byte("x: low\n"))}}},
|
|
{Inputs: []configkit.Input{{Source: configkit.Bytes("middle", []byte("x: "+middle+"\n"))}}},
|
|
}},
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
history := result.Metadata().History(configkit.Keys("x"))
|
|
if len(history) != 3 {
|
|
t.Fatalf("history has %d records; want the three independent contributions", len(history))
|
|
}
|
|
explanation, found := result.Explain(configkit.Keys("x"))
|
|
if !found || len(explanation.History) != len(history) {
|
|
t.Fatal("explanation lost ordered tier contributions")
|
|
}
|
|
for index, sourceID := range []string{"source-2", "source-3", "source-1"} {
|
|
change := history[index]
|
|
if len(change.Origin.Locations) != 1 || change.Origin.Locations[0].SourceID != sourceID {
|
|
t.Fatalf("contribution %d has origin %#v", index, change.Origin)
|
|
}
|
|
if len(explanation.History[index].Origin.Locations) != 1 || explanation.History[index].Origin.Locations[0] != change.Origin.Locations[0] {
|
|
t.Fatalf("explanation contribution %d has a different source origin", index)
|
|
}
|
|
if index == 0 {
|
|
if len(change.Previous) != 0 {
|
|
t.Fatal("first source contribution has a previous origin")
|
|
}
|
|
} else if len(change.Previous) != 1 || len(change.Previous[0].Locations) != 1 ||
|
|
change.Previous[0].Locations[0] != history[index-1].Origin.Locations[0] {
|
|
t.Fatalf("contribution %d has incorrect previous origin %#v", index, change.Previous)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|