feat: client core, accounts, link URI, errors
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c78dd3bd1
commit
a3136305d3
7 files changed
+583
No files matched your search
+177
@@ -0,0 +1,177 @@
|
||||
package signalbot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewDoesNoNetwork(t *testing.T) {
|
||||
if _, err := New("http://127.0.0.1:1"); err != nil {
|
||||
t.Fatalf("New returned %v, want nil", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRejectsBadURL(t *testing.T) {
|
||||
for _, u := range []string{"ftp://x", "::", ""} {
|
||||
if _, err := New(u); err == nil {
|
||||
t.Errorf("New(%q) returned nil error", u)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountsObjectAndLegacyFormats(t *testing.T) {
|
||||
cases := []struct {
|
||||
body string
|
||||
want []Account
|
||||
}{
|
||||
{`[{"number":"+331","aci":"a-1"},{"number":"","aci":"a-2"}]`, []Account{{"+331", "a-1"}, {"", "a-2"}}},
|
||||
{`[{"number":"+331","aci":null}]`, []Account{{"+331", ""}}},
|
||||
{`["+331"]`, []Account{{"+331", ""}}},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
f := newFakeAPI(t)
|
||||
f.accounts(tc.body)
|
||||
got, err := mustNew(t, f.URL).Accounts(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", tc.body, err)
|
||||
}
|
||||
if !reflect.DeepEqual(got, tc.want) {
|
||||
t.Errorf("%s: got %v, want %v", tc.body, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountAutoDetect(t *testing.T) {
|
||||
cases := []struct {
|
||||
body string
|
||||
want Account
|
||||
wantErr error
|
||||
}{
|
||||
{`[{"number":"+331","aci":"a-1"}]`, Account{"+331", "a-1"}, nil},
|
||||
{`[]`, Account{}, ErrNotLinked},
|
||||
{`[{"number":"+331"},{"number":"+332"}]`, Account{}, ErrMultipleAccounts},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
f := newFakeAPI(t)
|
||||
f.accounts(tc.body)
|
||||
got, err := mustNew(t, f.URL).account(context.Background())
|
||||
if !errors.Is(err, tc.wantErr) {
|
||||
t.Errorf("%s: err = %v, want %v", tc.body, err, tc.wantErr)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("%s: got %v, want %v", tc.body, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountWithNumber(t *testing.T) {
|
||||
f := newFakeAPI(t)
|
||||
f.accounts(`[{"number":"+331","aci":"a-1"},{"number":"+332","aci":"a-2"}]`)
|
||||
|
||||
got, err := mustNew(t, f.URL, WithNumber("+332")).account(context.Background())
|
||||
if err != nil || got != (Account{"+332", "a-2"}) {
|
||||
t.Fatalf("got %v, %v; want {+332 a-2}, nil", got, err)
|
||||
}
|
||||
|
||||
_, err = mustNew(t, f.URL, WithNumber("+339")).account(context.Background())
|
||||
if !errors.Is(err, ErrNotLinked) {
|
||||
t.Fatalf("unknown number: err = %v, want ErrNotLinked", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountCachedAfterSuccess(t *testing.T) {
|
||||
f := newFakeAPI(t)
|
||||
f.health()
|
||||
f.accounts(`[{"number":"+331"}]`)
|
||||
c := mustNew(t, f.URL)
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := c.Ping(context.Background()); err != nil {
|
||||
t.Fatalf("Ping %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
if n := f.hitCount("GET /v1/accounts"); n != 1 {
|
||||
t.Fatalf("/v1/accounts hit %d times, want 1", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPing(t *testing.T) {
|
||||
f := newFakeAPI(t)
|
||||
f.health()
|
||||
f.accounts(`[{"number":"+331"}]`)
|
||||
if err := mustNew(t, f.URL).Ping(context.Background()); err != nil {
|
||||
t.Fatalf("Ping: %v", err)
|
||||
}
|
||||
|
||||
closed := newFakeAPI(t)
|
||||
url := closed.URL
|
||||
closed.Close()
|
||||
err := mustNew(t, url).Ping(context.Background())
|
||||
if !errors.Is(err, ErrUnavailable) {
|
||||
t.Fatalf("Ping on closed server: err = %v, want ErrUnavailable", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAPIErrorMapping(t *testing.T) {
|
||||
cases := []struct {
|
||||
status int
|
||||
body string
|
||||
wantIs error
|
||||
wantNotIs []error
|
||||
wantMsg string
|
||||
}{
|
||||
{503, `{"error":"down"}`, ErrUnavailable, []error{ErrRateLimited}, "down"},
|
||||
{429, `{"error":"slow down","challenge_tokens":["x"]}`, ErrRateLimited, []error{ErrUnavailable}, "slow down"},
|
||||
{400, `{"error":"bad"}`, nil, []error{ErrUnavailable, ErrRateLimited}, "bad"},
|
||||
{400, "plain text oops\n", nil, []error{ErrUnavailable, ErrRateLimited}, "plain text oops"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
f := newFakeAPI(t)
|
||||
f.mux.HandleFunc("GET /v1/accounts", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(tc.status)
|
||||
_, _ = w.Write([]byte(tc.body))
|
||||
})
|
||||
_, err := mustNew(t, f.URL).Accounts(context.Background())
|
||||
var apiErr *APIError
|
||||
if !errors.As(err, &apiErr) {
|
||||
t.Fatalf("%d: err %v is not *APIError", tc.status, err)
|
||||
}
|
||||
if apiErr.StatusCode != tc.status || apiErr.Message != tc.wantMsg {
|
||||
t.Errorf("%d: got %+v, want status %d message %q", tc.status, apiErr, tc.status, tc.wantMsg)
|
||||
}
|
||||
if tc.wantIs != nil && !errors.Is(err, tc.wantIs) {
|
||||
t.Errorf("%d: errors.Is(%v) = false", tc.status, tc.wantIs)
|
||||
}
|
||||
for _, not := range tc.wantNotIs {
|
||||
if errors.Is(err, not) {
|
||||
t.Errorf("%d: errors.Is(%v) = true, want false", tc.status, not)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinkURI(t *testing.T) {
|
||||
f := newFakeAPI(t)
|
||||
f.mux.HandleFunc("GET /v1/qrcodelink/raw", func(w http.ResponseWriter, r *http.Request) {
|
||||
if got := r.URL.Query().Get("device_name"); got != "imdbotg" {
|
||||
writeJSON(w, 400, map[string]string{"error": "device_name=" + got})
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]string{"device_link_uri": "sgnl://linkdevice?uuid=x"})
|
||||
})
|
||||
uri, err := mustNew(t, f.URL).LinkURI(context.Background(), "imdbotg")
|
||||
if err != nil || uri != "sgnl://linkdevice?uuid=x" {
|
||||
t.Fatalf("got %q, %v", uri, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEndpointJoin(t *testing.T) {
|
||||
for _, base := range []string{"http://h/signal/", "http://h/signal"} {
|
||||
got := mustNew(t, base).endpoint("/v1/accounts")
|
||||
if got != "http://h/signal/v1/accounts" {
|
||||
t.Errorf("base %q: got %q", base, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user