package seatlist import ( "encoding/json" "errors" "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) } }) } }