Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
package signalbot
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func (f *fakeAPI) groups(path, body string) {
|
|
f.mux.HandleFunc("GET "+path, func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(body))
|
|
})
|
|
}
|
|
|
|
func TestGroupsMapping(t *testing.T) {
|
|
f := newFakeAPI(t)
|
|
f.accounts(`[{"number":"+331","aci":"a-1"}]`)
|
|
f.groups("/v1/groups/+331", `[
|
|
{"id":"group.QQ==","internal_id":"A","name":"News","admins":["+331"],"members":["+331","+332"],
|
|
"permissions":{"add_members":"only-admins","edit_group":"only-admins","send_messages":"only-admins"}},
|
|
{"id":"group.Qg==","internal_id":"B","name":"Chat","admins":["+332"],
|
|
"permissions":{"send_messages":"every-member"}}
|
|
]`)
|
|
got, err := mustNew(t, f.URL).Groups(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Groups: %v", err)
|
|
}
|
|
want := []Group{
|
|
{ID: "group.QQ==", Name: "News", IsAdmin: true, AdminsOnly: true},
|
|
{ID: "group.Qg==", Name: "Chat", IsAdmin: false, AdminsOnly: false},
|
|
}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %+v, want %+v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestGroupsIsAdminByNumberOrACI(t *testing.T) {
|
|
f := newFakeAPI(t)
|
|
f.accounts(`[{"number":"+331","aci":"a-1"}]`)
|
|
f.groups("/v1/groups/+331", `[
|
|
{"id":"group.A","name":"A","admins":["+331"]},
|
|
{"id":"group.B","name":"B","admins":["a-1"]},
|
|
{"id":"group.C","name":"C","admins":["+339"]}
|
|
]`)
|
|
got, err := mustNew(t, f.URL).Groups(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("Groups: %v", err)
|
|
}
|
|
for i, want := range []bool{true, true, false} {
|
|
if got[i].IsAdmin != want {
|
|
t.Errorf("group %s IsAdmin = %v, want %v", got[i].ID, got[i].IsAdmin, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGroupsEmpty(t *testing.T) {
|
|
f := newFakeAPI(t)
|
|
f.accounts(`[{"number":"+331"}]`)
|
|
f.groups("/v1/groups/+331", `[]`)
|
|
got, err := mustNew(t, f.URL).Groups(context.Background())
|
|
if err != nil || got == nil || len(got) != 0 {
|
|
t.Fatalf("got %#v, %v; want empty non-nil slice", got, err)
|
|
}
|
|
}
|