Files
Agent-System/internal/server/project_create_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

60 lines
1.5 KiB
Go

package server_test
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"unicode/utf8"
"github.com/jackc/pgx/v5/pgxpool"
"chat-agent-system/internal/projectworkflow"
)
func TestProjectCreate_PG_ChineseNameViaHTTP(t *testing.T) {
pool := openPGPoolWithMigrations(t)
if pool == nil {
return
}
defer pool.Close()
handler, _ := setupPGGovernanceServer(t, pool)
token := loginTestUser(t, handler, "zhanggong", "123456")
body, _ := json.Marshal(projectworkflow.CreateProjectInput{
Name: fmt.Sprintf("PG Phase5 治理链中文名称测试 %d", time.Now().UnixNano()),
DeviceType: "超声手术设备",
IntendedUse: "软组织切除",
TargetMarket: "NMPA 三类",
})
req := httptest.NewRequest(http.MethodPost, "/api/v1/projects", bytes.NewReader(body))
withBearerAuth(req, token)
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Code != http.StatusCreated {
t.Fatalf("create project with Chinese name: %d %s", rec.Code, rec.Body.String())
}
var proj projectworkflow.Project
if err := json.Unmarshal(rec.Body.Bytes(), &proj); err != nil {
t.Fatalf("decode project: %v", err)
}
if !utf8.ValidString(proj.Code) {
t.Fatalf("project code invalid UTF-8: %q", proj.Code)
}
t.Cleanup(func() {
cleanupProject(t, pool, proj.ID)
})
}
func cleanupProject(t *testing.T, pool *pgxpool.Pool, projectID int64) {
t.Helper()
if pool == nil || projectID <= 0 {
return
}
_, _ = pool.Exec(t.Context(), `DELETE FROM projects WHERE id = $1`, projectID)
}