Files
archie/internal/domain/workflow/definition_repair_test.go
T

83 lines
2.6 KiB
Go

package workflow
import (
"encoding/json"
"slices"
"strings"
"testing"
)
// A stored collection that stopped validating keeps every entry it can: valid
// entries stay, a broken shipped entry is restored, a broken custom one goes.
func TestRepairDefinitionCollection(t *testing.T) {
shipped, _ := ShippedDefinitions().DefinitionByID("triage")
custom := strings.Replace(shipped.YAML, "id: triage", "id: mine", 1)
edited := shipped.YAML + "\n# edited by the operator\n"
broken := "id: broken\nsteps:\n - id: a\n type: removed.step\n"
// with is the shipped set with triage's YAML swapped and extra appended.
with := func(triage string, extra ...WorkflowDefinitionEntry) ([]WorkflowDefinitionEntry, map[string]string) {
entries, byID := []WorkflowDefinitionEntry{}, map[string]string{}
for _, entry := range ShippedDefinitions().Definitions {
if entry.ID == "triage" {
entry.YAML = triage
}
entries = append(entries, entry)
byID[entry.ID] = entry.YAML
}
for _, entry := range extra {
entries = append(entries, entry)
byID[entry.ID] = entry.YAML
}
return entries, byID
}
restoredStored, _ := with(broken, WorkflowDefinitionEntry{ID: "mine", YAML: custom})
_, restoredWant := with(shipped.YAML, WorkflowDefinitionEntry{ID: "mine", YAML: custom})
editedStored, _ := with(edited, WorkflowDefinitionEntry{ID: "broken", YAML: broken})
_, editedWant := with(edited)
tests := []struct {
name string
stored []WorkflowDefinitionEntry
want map[string]string
wantRemoved []string
}{
{
name: "broken shipped entry is restored, custom kept",
stored: restoredStored,
want: restoredWant,
},
{
name: "broken custom entry is removed, edited shipped kept",
stored: editedStored,
want: editedWant,
wantRemoved: []string{"broken"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
value, err := json.Marshal(WorkflowDefinitionCollection{Definitions: tt.stored})
if err != nil {
t.Fatal(err)
}
got, removed, err := RepairDefinitionCollection(value, triageRegistry())
if err != nil {
t.Fatal(err)
}
byID := map[string]string{}
for _, entry := range got.Definitions {
byID[entry.ID] = entry.YAML
}
if len(byID) != len(tt.want) {
t.Fatalf("kept %d entries, want %d", len(byID), len(tt.want))
}
for id, yaml := range tt.want {
if byID[id] != yaml {
t.Errorf("entry %q not kept as expected", id)
}
}
if !slices.Equal(removed, tt.wantRemoved) {
t.Errorf("removed %v, want %v", removed, tt.wantRemoved)
}
})
}
}