Files
Warp/server/internal/storage/pocketbase/pbtest/pbtest.go
T
tajniak81andClaude Opus 5.5 a47e15cc1c
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
CI / Admin panel (push) Canceled after 0s
CI / Web client (push) Canceled after 0s
CI / Desktop client (ubuntu-24.04) (push) Canceled after 0s
CI / Desktop client (windows-latest) (push) Canceled after 0s
PocketBase as a standalone database server
The pocketbase driver no longer embeds PocketBase: Warp keeps its data as
PocketBase collections on a server of its own and reaches it only through
the REST API, the way the postgres driver reaches PostgreSQL.
database.connection_string is the server's URL, and Warp signs in as the
superuser in WARP_PB_SUPERUSER_EMAIL / WARP_PB_SUPERUSER_PASSWORD (or a
WARP_PB_SUPERUSER_FILE of the two).

Every SQL table is a superuser-only collection of the same name and
columns, created by warp migrate. Reads go through PocketBase's SQL
endpoint with the SQL drivers' queries, via a read-only database/sql
driver that inlines arguments, moves long ones into warp_params and pages
past 1000 rows. Writes are record operations in batch requests under one
write lock in the Warp process. Media is a protected file field in
warp_media, local or in the S3 bucket Warp writes into PocketBase's
settings. Search matches word prefixes with LIKE. The storage tests run
against an in-process PocketBase and pass as on SQLite.

Docker: the PocketBase stacks get a pocketbase service running
tajniak81/pocketbase-docker-root (WARP_PB_IMAGE), with a pb-data volume
and its admin UI on loopback; the AIO images run that PocketBase binary
as a process beside the homeserver, making up a superuser when none is
set. The .env examples, READMEs, docs and warp.yaml.example follow.

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

65 lines
1.8 KiB
Go

// Package pbtest runs a real PocketBase server in-process for tests, so the
// pocketbase driver is tested against the same REST API it uses in
// production, with no container to start.
package pbtest
import (
"net/http/httptest"
"testing"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
_ "github.com/pocketbase/pocketbase/migrations" // PocketBase's system migrations; without them its bootstrap has no tables
)
// Superuser is the account the server is started with.
const (
SuperuserEmail = "warp-tests@example.com"
SuperuserPassword = "warp-tests-password"
)
// Server is a running PocketBase.
type Server struct {
// URL is the server's address, for database.connection_string.
URL string
// DataDir is its pb_data, where a test can look for what it stored.
DataDir string
}
// Start runs a fresh PocketBase with an empty data directory. It is stopped
// when the test ends.
func Start(tb testing.TB) Server {
tb.Helper()
dataDir := tb.TempDir()
app := core.NewBaseApp(core.BaseAppConfig{DataDir: dataDir})
if err := app.Bootstrap(); err != nil {
tb.Fatalf("pocketbase: bootstrap: %v", err)
}
tb.Cleanup(func() { _ = app.ResetBootstrapState() })
superusers, err := app.FindCollectionByNameOrId(core.CollectionNameSuperusers)
if err != nil {
tb.Fatalf("pocketbase: %v", err)
}
su := core.NewRecord(superusers)
su.SetEmail(SuperuserEmail)
su.SetPassword(SuperuserPassword)
if err := app.Save(su); err != nil {
tb.Fatalf("pocketbase: create the superuser: %v", err)
}
router, err := apis.NewRouter(app)
if err != nil {
tb.Fatalf("pocketbase: router: %v", err)
}
mux, err := router.BuildMux()
if err != nil {
tb.Fatalf("pocketbase: router: %v", err)
}
server := httptest.NewServer(mux)
tb.Cleanup(server.Close)
return Server{URL: server.URL, DataDir: dataDir}
}