Files
Agent-System/internal/workspace/registry_pg_test.go
T
agent-bot ee1e53b510
CI / go-vet (pull_request) Failing after 10s
CI / backup-restore-drill (pull_request) Failing after 25s
CI / web (pull_request) Successful in 33s
fix(tests): per-package test databases + legitimate approval fixtures (issue #2 close)
Root causes of the remaining 5 CI PG failures (and the hidden flaky set):
1. All PG-integration packages shared ONE database under parallel package
   execution - migrations/seeds/truncates raced (nondeterministic failure
   sets, locally and in CI).
   Fix: RequireTestDatabaseURL/SkipIfNoTestDatabaseURL now derive a
   per-package database (<base>_<pkgHash>, advisory-lock-guarded
   CREATE); 9 raw-env straggler tests funnelled through the helpers;
   db-package tests call it unqualified (no import cycle).
2. Approval-chain fixtures were not production-shaped: freezeReviewSnapshot
   only honors head_snapshot_id, fake content hashes never froze a
   snapshot, so ApproverApprove rejected with 'approved requires frozen
   review_snapshot_id'. Errors were swallowed (_, _ =) hiding this.
   Fix (ownership-respecting, arch-enforced):
   - docrevision.SeedContentSnapshotForTest seeds the ledger tables it owns
   - docgov GovernanceEngine.FreezeHeadSnapshotForTest freezes head/review
     pointers on document_records (docgov-owned) and clears engine cache
   - all four integration fixtures use the pair; swallowed errors checked
3. Fixed-name fixtures collide once a package shares one DB (projects
   uq_projects_name_global): uniquified in projectinstance + server tests.
4. CAS/ctxmgr tests never bootstrapped migrations (relied on other packages
   having migrated the shared DB): explicit RunMigrationsOnceForTest.

Verified: full parallel short suite = 75 packages, 0 failures against an
isolated throwaway PG. CI skip list removed - short tests fully required.
2026-09-29 09:54:43 +00:00

66 lines
1.7 KiB
Go

package workspace
import (
"chat-agent-system/internal/db"
"context"
"fmt"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
func TestPGRegistryRoundTrip(t *testing.T) {
dsn := db.SkipIfNoTestDatabaseURL(t)
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, dsn)
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(dsn)
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)
}
}