Files
Docwell/Server/internal/store/pocketbase/client_test.go
T
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:11:07 +01:00

86 lines
2.5 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package pocketbase
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"
)
type record struct {
ID string `json:"id"`
Name string `json:"name"`
}
func fakePocketBase(t *testing.T, authCalls *atomic.Int32) *httptest.Server {
t.Helper()
mux := http.NewServeMux()
mux.HandleFunc("GET /api/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`{"code":200}`))
})
mux.HandleFunc("POST /api/collections/_superusers/auth-with-password", func(w http.ResponseWriter, r *http.Request) {
n := authCalls.Add(1)
json.NewEncoder(w).Encode(map[string]string{"token": "tok" + string(rune('0')+n)})
})
mux.HandleFunc("GET /api/collections/workspaces/records", func(w http.ResponseWriter, r *http.Request) {
// The first token is treated as expired to exercise the retry path.
if r.Header.Get("Authorization") != "tok2" {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(`{"status":401,"message":"expired"}`))
return
}
if got := r.URL.Query().Get("filter"); got != "slug='acme'" {
t.Errorf("filter = %q", got)
}
w.Write([]byte(`{"page":1,"perPage":30,"totalItems":1,"totalPages":1,"items":[{"id":"w1","name":"Acme"}]}`))
})
mux.HandleFunc("GET /api/collections/workspaces/records/missing", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte(`{"status":404,"message":"not found"}`))
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
return srv
}
func TestHealth(t *testing.T) {
var calls atomic.Int32
srv := fakePocketBase(t, &calls)
if err := New(srv.URL, "a", "b").Health(context.Background()); err != nil {
t.Fatal(err)
}
if calls.Load() != 0 {
t.Error("health check should not authenticate")
}
}
func TestListReauthenticatesOn401(t *testing.T) {
var calls atomic.Int32
srv := fakePocketBase(t, &calls)
c := New(srv.URL, "a", "b")
res, err := List[record](context.Background(), c, "workspaces", ListOptions{Filter: "slug='acme'"})
if err != nil {
t.Fatal(err)
}
if len(res.Items) != 1 || res.Items[0].Name != "Acme" {
t.Errorf("items = %+v", res.Items)
}
if calls.Load() != 2 {
t.Errorf("auth calls = %d, want 2", calls.Load())
}
}
func TestNotFound(t *testing.T) {
var calls atomic.Int32
srv := fakePocketBase(t, &calls)
_, err := Get[record](context.Background(), New(srv.URL, "a", "b"), "workspaces", "missing")
if !IsNotFound(err) {
t.Fatalf("err = %v, want not found", err)
}
}