Track workspace size and age, expose an admin list and cleanup route, and refuse TTL or quota deletion until a frozen manifest matches verified receipts.
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
package workspace
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func TestPGRegistryRoundTrip(t *testing.T) {
|
|
url := os.Getenv("TEST_DATABASE_URL")
|
|
if url == "" {
|
|
t.Skip("set TEST_DATABASE_URL to run the workspace registry postgres test")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
schema := fmt.Sprintf("agentx_reg_%d", time.Now().UnixNano())
|
|
boot, err := pgxpool.New(ctx, url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer boot.Close()
|
|
if _, err := boot.Exec(ctx, "CREATE SCHEMA "+schema); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() {
|
|
cctx, ccancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer ccancel()
|
|
_, _ = boot.Exec(cctx, "DROP SCHEMA "+schema+" CASCADE")
|
|
})
|
|
cfg, err := pgxpool.ParseConfig(url)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cfg.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
|
|
_, err := conn.Exec(ctx, "SET search_path TO "+schema)
|
|
return err
|
|
}
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer pool.Close()
|
|
reg := NewPGRegistry(pool)
|
|
if err := reg.Ensure(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
frozen, receipts := sampleFrozen("ws-pg")
|
|
rec := Record{
|
|
ID: "ws-pg", RunID: "run-pg", Bytes: 12,
|
|
CreatedAt: time.Now().UTC().Add(-time.Hour),
|
|
Frozen: &frozen, Receipts: receipts,
|
|
}
|
|
if err := reg.Upsert(ctx, rec); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, ok, err := reg.Get(ctx, "ws-pg")
|
|
if err != nil || !ok {
|
|
t.Fatalf("get %#v %v %v", got, ok, err)
|
|
}
|
|
if got.RunID != "run-pg" || got.Frozen == nil || got.Frozen.SHA256 != "abc" || len(got.Receipts) != 1 || !got.Receipts[0].Verified {
|
|
t.Fatalf("round trip %#v", got)
|
|
}
|
|
}
|