Something went wrong. Try again.
Go client library for the Semble API
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142package semble
import ( "context" "encoding/json" "errors" "net/http" "net/http/httptest" "testing")
func newTestClient(t *testing.T, h http.HandlerFunc) (*Client, func()) { t.Helper() ts := httptest.NewServer(h) c, err := New(WithBaseURL(ts.URL+"/xrpc"), WithAPIKey("secret"), WithHTTPClient(ts.Client())) if err != nil { t.Fatal(err) }
return c, ts.Close}
func TestGETBuildsXRPCURLAndAuth(t *testing.T) { c, close := newTestClient(t, func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { t.Fatalf("method=%s", r.Method) } if r.URL.Path != "/xrpc/network.cosmik.feed.getGlobal" { t.Fatalf("path=%s", r.URL.Path) } if got := r.Header.Get("x-api-key"); got != "secret" { t.Fatalf("x-api-key=%q", got) } q := r.URL.Query() if q.Get("page") != "2" || q.Get("limit") != "10" || q.Get("urlType") != "article" { t.Fatalf("query=%s", r.URL.RawQuery) } vals := q["activityTypes"] if len(vals) != 2 || vals[0] != "CARD_COLLECTED" || vals[1] != "CONNECTION_CREATED" { t.Fatalf("activityTypes=%v", vals) } _, _ = w.Write([]byte(`{"items":[],"pagination":{"currentPage":2}}`)) }) defer close() if _, err := c.GetGlobalFeed(context.Background(), FeedQuery{ PageQuery: PageQuery{Page: 2, Limit: 10}, URLType: URLTypeArticle, ActivityTypes: []string{"CARD_COLLECTED", "CONNECTION_CREATED"}, }); err != nil { t.Fatal(err) }}
func TestPOSTSendsJSONBodyAndAuth(t *testing.T) { c, close := newTestClient(t, func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { t.Fatalf("method=%s", r.Method) } if r.URL.Path != "/xrpc/network.cosmik.card.addUrl" { t.Fatalf("path=%s", r.URL.Path) } if r.Header.Get("Content-Type") != "application/json" { t.Fatalf("content-type=%q", r.Header.Get("Content-Type")) } var body map[string]any if err := json.NewDecoder(r.Body).Decode(&body); err != nil { t.Fatal(err) } if body["url"] != "https://example.com" || body["note"] != "n" { t.Fatalf("body=%v", body) } ids, ok := body["collectionIds"].([]any) if !ok || len(ids) != 2 || ids[0] != "c1" { t.Fatalf("collectionIds=%v", body["collectionIds"]) } _, _ = w.Write([]byte(`{"urlCardId":"card1"}`)) }) defer close() out, err := c.AddURLToLibrary(context.Background(), AddURLRequest{ URL: "https://example.com", Note: "n", CollectionIDs: []string{"c1", "c2"}, }) if err != nil { t.Fatal(err) } if out.URLCardID != "card1" { t.Fatalf("out=%+v", out) }}
func TestAPIErrorDecoding(t *testing.T) { c, close := newTestClient(t, func(w http.ResponseWriter, r *http.Request) { http.Error(w, `{"message":"bad"}`, http.StatusBadRequest) }) defer close() _, err := c.GetCard(context.Background(), "x") var apiErr *APIError if !errors.As(err, &apiErr) { t.Fatalf("err=%T %v", err, err) } if apiErr.StatusCode != http.StatusBadRequest { t.Fatalf("status=%d", apiErr.StatusCode) }}
func TestGetURLMetadataDecodesWrapper(t *testing.T) { c, close := newTestClient(t, func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/xrpc/network.cosmik.card.getUrlMetadata" { t.Fatalf("path=%s", r.URL.Path) } if r.URL.Query().Get("url") != "https://example.com" { t.Fatalf("query=%s", r.URL.RawQuery) } _, _ = w.Write([]byte(`{ "metadata":{"url":"https://example.com","title":"Example"}, "stats":{"libraryCount":3} }`)) }) defer close() out, err := c.GetURLMetadata(context.Background(), "https://example.com", true) if err != nil { t.Fatal(err) } if out.Metadata.Title != "Example" || out.Stats.LibraryCount != 3 { t.Fatalf("out=%+v", out) }}
func TestConnectionViewDecodesSourceAndTarget(t *testing.T) { var v ConnectionView if err := json.Unmarshal([]byte(`{ "source":{"url":"https://a.example"}, "target":{"url":"https://b.example"} }`), &v); err != nil { t.Fatal(err) } if v.Source.URL != "https://a.example" || v.Target.URL != "https://b.example" { t.Fatalf("connection view=%+v", v) }}