package pocketbase import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" ) func TestGetBookmarkURLs_Success(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/collections/users/auth-with-password" { w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(authResponse{Token: "test-token"}) return } if r.URL.Path == "/api/collections/bookmarks/records" { if r.Header.Get("Authorization") != "test-token" { t.Errorf("Expected Authorization header 'test-token', got '%s'", r.Header.Get("Authorization")) } w.WriteHeader(http.StatusOK) json.NewEncoder(w).Encode(recordsResponse{ Items: []record{ {URL: "https://royalroad.com/fiction/1"}, {URL: "https://royalroad.com/fiction/2"}, }, }) return } t.Errorf("Unexpected request to %s", r.URL.Path) })) defer server.Close() client := New(server.URL, "user", "pass", "Geschichten") urls, err := client.GetBookmarkURLs(context.Background()) if err != nil { t.Fatalf("Unexpected error: %v", err) } if len(urls) != 2 { t.Errorf("Expected 2 URLs, got %d", len(urls)) } if urls[0] != "https://royalroad.com/fiction/1" { t.Errorf("Unexpected first URL: %s", urls[0]) } } func TestGetBookmarkURLs_AuthFailure(t *testing.T) { server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/api/collections/users/auth-with-password" { w.WriteHeader(http.StatusUnauthorized) return } t.Errorf("Unexpected request to %s", r.URL.Path) })) defer server.Close() client := New(server.URL, "user", "pass", "Geschichten") _, err := client.GetBookmarkURLs(context.Background()) if err == nil { t.Error("Expected error on auth failure, got nil") } }