60 lines
2.8 KiB
Go
60 lines
2.8 KiB
Go
package seatlist
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestHolderOf(t *testing.T) {
|
|
answer := `{"seats":[{"seat":"other","holders":[{"module":"x"}]},{"seat":"mesh-provisioner","holders":[{"module":"prov"},{"module":"second"}]}]}`
|
|
for _, row := range []struct {
|
|
name, seat, want, err string
|
|
call HolderCaller
|
|
}{
|
|
{"the first holder on record", "mesh-provisioner", "prov", "", func(k string, b any) (json.RawMessage, error) {
|
|
if k != "seat:mesh-module-assigner.assignments" || b.(map[string]string)["seats"] != "true" {
|
|
t.Errorf("asked %q with %v", k, b)
|
|
}
|
|
return json.RawMessage(answer), nil
|
|
}},
|
|
{"no holder", "absent", "", "no module holds the seat absent", func(string, any) (json.RawMessage, error) { return json.RawMessage(answer), nil }},
|
|
{"the ask fails", "x", "", "who holds the seat x is not asked", func(string, any) (json.RawMessage, error) { return nil, errors.New("down") }},
|
|
{"not the holders' answer", "x", "", "is not the seat holders'", func(string, any) (json.RawMessage, error) { return json.RawMessage(`nope`), nil }},
|
|
} {
|
|
t.Run(row.name, func(t *testing.T) {
|
|
got, err := HolderOf(row.call, row.seat)
|
|
if row.err == "" && (err != nil || got != row.want) || row.err != "" && (err == nil || !strings.Contains(err.Error(), row.err)) {
|
|
t.Errorf("HolderOf = %q, %v; want %q, %q", got, err, row.want, row.err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Design 47 H2 H-A-742: HolderNodesIn answers each holder of a seat with its module and its node, from the keys `<seat>.<node>`
|
|
// of the seat-holders view; a seat held once has no node; another seat's key is no holder; HoldersIn still answers the modules.
|
|
func TestHolderNodesInAnswersEachHolderWithItsNode(t *testing.T) {
|
|
view := map[string][]byte{
|
|
"go-package-registry.ace": []byte(`{"holders":[{"module":"registry"}]}`),
|
|
"go-package-registry.shanks": []byte(`{"holders":[{"module":"registry"},{"module":"mirror"}]}`),
|
|
"go-package-registry-extra.x": []byte(`{"holders":[{"module":"other"}]}`),
|
|
"mesh-delivery": []byte(`{"holders":[{"module":"gate"}]}`),
|
|
}
|
|
values := func() (map[string][]byte, error) { return view, nil }
|
|
want := []HolderNode{{"registry", "ace"}, {"mirror", "shanks"}, {"registry", "shanks"}}
|
|
if got := HolderNodesIn(values)("go-package-registry"); !slices.Equal(got, want) {
|
|
t.Errorf("holders with their nodes %v, want %v", got, want)
|
|
}
|
|
if got := HolderNodesIn(values)("mesh-delivery"); !slices.Equal(got, []HolderNode{{"gate", ""}}) {
|
|
t.Errorf("a seat held once: %v", got)
|
|
}
|
|
if got := HoldersIn(values)("go-package-registry"); len(got) != 2 {
|
|
t.Errorf("HoldersIn answers %v, want the two modules", got)
|
|
}
|
|
if got := HolderNodesIn(func() (map[string][]byte, error) { return nil, errors.New("unread") })("go-package-registry"); got != nil {
|
|
t.Errorf("an unread view answers %v", got)
|
|
}
|
|
}
|