package governance import ( "slices" "testing" "time" "github.com/google/uuid" "github.com/samcharles93/archie-core/modules/governance/internal/store" ) // TestPathNeverDropsBelowTheAuthorityTheActionNeeds guards the escalation // path: an action that needs authority is never passed to a person without // it, however the path is set, every step keeps its own deadline, and a target's // known owner is asked before the default owner, which an unknown owner leaves first. func TestPathNeverDropsBelowTheAuthorityTheActionNeeds(t *testing.T) { owner := store.Person{ID: uuid.New(), Name: "Owner"} peer := store.Person{ID: uuid.New(), Name: "Peer"} lead := store.Person{ID: uuid.New(), Name: "Lead", Authority: true} set := Settings{ ApprovalDeadline: time.Hour, DefaultOwner: owner.ID.String(), Escalation: []Step{ {Person: peer.ID.String(), Deadline: 2 * time.Hour}, {Person: lead.ID.String(), Deadline: 3 * time.Hour}, }, } people := []store.Person{owner, peer, lead} tests := []struct { name string needAuthority bool // target is the person who answers for the action's target. target string want []string wantDeadlines []time.Duration }{ { name: "an ordinary action goes through every step", want: []string{"Owner", "Peer", "Lead"}, wantDeadlines: []time.Duration{time.Hour, 2 * time.Hour, 3 * time.Hour}, }, { name: "an action that needs authority skips a step without it", needAuthority: true, want: []string{"Owner", "Lead"}, wantDeadlines: []time.Duration{time.Hour, 3 * time.Hour}, }, { name: "the target's owner is asked first, then the default owner and the path", target: peer.ID.String(), want: []string{"Peer", "Owner", "Lead"}, wantDeadlines: []time.Duration{time.Hour, time.Hour, 3 * time.Hour}, }, { name: "a target owner without the authority an action needs is not asked", target: peer.ID.String(), needAuthority: true, want: []string{"Owner", "Lead"}, wantDeadlines: []time.Duration{time.Hour, 3 * time.Hour}, }, { name: "a target owner nobody knows leaves the default owner first", target: uuid.NewString(), want: []string{"Owner", "Peer", "Lead"}, wantDeadlines: []time.Duration{time.Hour, 2 * time.Hour, 3 * time.Hour}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var names []string var deadlines []time.Duration for _, st := range set.path(people, tt.target, tt.needAuthority) { names = append(names, st.Name) deadlines = append(deadlines, st.Deadline()) } if !slices.Equal(names, tt.want) || !slices.Equal(deadlines, tt.wantDeadlines) { t.Fatalf("path = %v %v, want %v %v", names, deadlines, tt.want, tt.wantDeadlines) } }) } }