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.
132 lines
3.9 KiB
Go
132 lines
3.9 KiB
Go
package storage
|
|
|
|
import (
|
|
"chat-agent-system/internal/db"
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func TestCAS_DatabaseIntegration(t *testing.T) {
|
|
// 隔离优先:TEST_DATABASE_URL 指向专用测试库;未提供时跳过,禁止默认连开发库。
|
|
dsn := db.SkipIfNoTestDatabaseURL(t)
|
|
if dsn == "" {
|
|
t.Skip("TEST_DATABASE_URL not set; skipping CAS DB integration test")
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
defer cancel()
|
|
|
|
pool, err := pgxpool.New(ctx, dsn)
|
|
if err != nil {
|
|
t.Skipf("cannot connect to context db (%v), skipping DB integration test", err)
|
|
return
|
|
}
|
|
defer pool.Close()
|
|
|
|
if err := db.RunMigrationsOnceForTest(ctx, pool, dsn); err != nil {
|
|
t.Fatalf("migration failed: %v", err)
|
|
}
|
|
|
|
if err := pool.Ping(ctx); err != nil {
|
|
t.Skipf("context db ping failed (%v), skipping DB integration test", err)
|
|
return
|
|
}
|
|
|
|
tempDir, err := os.MkdirTemp("", "cas_db_test_*")
|
|
if err != nil {
|
|
t.Fatalf("mkdir temp failed: %v", err)
|
|
}
|
|
defer os.RemoveAll(tempDir)
|
|
|
|
mockEmb := &mockEmbedder{}
|
|
cas, err := NewCAS(tempDir, pool, mockEmb)
|
|
if err != nil {
|
|
t.Fatalf("new cas failed: %v", err)
|
|
}
|
|
|
|
// 准备测试项目与用户
|
|
var projID int64
|
|
stamp := time.Now().Format("20060102150405.000000")
|
|
projCode := "TEST_PROJ_" + stamp
|
|
err = pool.QueryRow(ctx, `
|
|
INSERT INTO projects (code, name, customer_name, device_type)
|
|
VALUES ($1, '测试内窥镜系统 ' || $2, '北京水木东方', '内窥镜')
|
|
ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name
|
|
RETURNING id
|
|
`, projCode, stamp).Scan(&projID)
|
|
if err != nil {
|
|
t.Fatalf("insert project failed: %v", err)
|
|
}
|
|
|
|
var userID int64
|
|
accountID := "user_test_" + time.Now().Format("150405")
|
|
err = pool.QueryRow(ctx, `
|
|
INSERT INTO users (account_id, name, role)
|
|
VALUES ($1, '测试工程师', 'engineer')
|
|
ON CONFLICT (account_id) DO UPDATE SET name = EXCLUDED.name
|
|
RETURNING id
|
|
`, accountID).Scan(&userID)
|
|
if err != nil {
|
|
t.Fatalf("insert user failed: %v", err)
|
|
}
|
|
|
|
// 1. 上传文件 1 (使用动态内容确保本次测试为全新文件)
|
|
testData := []byte("这是写入真实数据库并在物理磁盘去重的测试申报文件内容: " + time.Now().Format(time.RFC3339Nano))
|
|
ref1, err := cas.StoreFile(ctx, projID, userID, "spec_v1.pdf", "standard", testData, "application/pdf")
|
|
if err != nil {
|
|
t.Fatalf("store file 1 to db failed: %v", err)
|
|
}
|
|
if ref1.IsDeduplicated {
|
|
t.Errorf("expected first upload to not be deduplicated")
|
|
}
|
|
|
|
// 2. 上传文件 2 (内容相同,触发 DB 去重)
|
|
ref2, err := cas.StoreFile(ctx, projID, userID, "spec_v2_dup.pdf", "standard", testData, "application/pdf")
|
|
if err != nil {
|
|
t.Fatalf("store file 2 to db failed: %v", err)
|
|
}
|
|
if !ref2.IsDeduplicated {
|
|
t.Errorf("expected second upload to be deduplicated in DB")
|
|
}
|
|
|
|
// 3. 存储并检索向量切片(内容带唯一后缀,避免跨次运行 chunk_hash 冲突)
|
|
chunk, err := cas.StoreMemoryChunk(ctx, MemoryChunkInput{
|
|
Domain: "project_doc",
|
|
ProjectID: projID,
|
|
UserID: userID,
|
|
Title: "光机指标要求",
|
|
Content: "内窥镜视场角不低于 100 度,光学畸变应严格控制在 5% 以内。 run=" + time.Now().Format(time.RFC3339Nano),
|
|
ImportanceWeight: 1.2,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("store memory chunk to db failed: %v", err)
|
|
}
|
|
if chunk.ID <= 0 {
|
|
t.Errorf("expected positive chunk ID from db")
|
|
}
|
|
|
|
// 4. 检索向量切片
|
|
hits, err := cas.SearchMemory(ctx, MemorySearchOptions{
|
|
Domain: "project_doc",
|
|
ProjectID: projID,
|
|
QueryText: "视场角",
|
|
ApplyTimeDecay: true,
|
|
DecayHalfLifeDays: 7.0,
|
|
TopK: 5,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("search memory in db failed: %v", err)
|
|
}
|
|
if len(hits) == 0 {
|
|
t.Fatalf("expected memory hits from db, got 0")
|
|
}
|
|
if hits[0].Chunk.Title != "光机指标要求" {
|
|
t.Errorf("unexpected top hit title from db: %s", hits[0].Chunk.Title)
|
|
}
|
|
}
|