package docgov_test import ( "context" "fmt" "testing" "time" "chat-agent-system/internal/config" "chat-agent-system/internal/docgov" "github.com/jackc/pgx/v5/pgxpool" ) func TestDocGov_DatabaseIntegration(t *testing.T) { cfg, err := config.Load(".env") if err != nil || cfg == nil || cfg.ContextDbHost == "" { t.Skip("Skipping DB integration test: context DB not configured") } ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() pool, err := pgxpool.New(ctx, cfg.ContextDSN()) if err != nil || pool.Ping(ctx) != nil { t.Skipf("Skipping DB integration test: cannot connect to %s: %v", cfg.ContextDSN(), err) } defer pool.Close() engine := docgov.NewGovernanceEngine(pool) testDocID := fmt.Sprintf("doc_test_db_%d", time.Now().UnixNano()) // 1. 创建受控文档 doc, err := engine.CreateOrGetDocument( ctx, testDocID, 1, "feasibility_reg", "DB集成测试文档", "V0.5_feasibility", "data/dev/workspaces/test_db", 1, "张工", 2, "李审核", 3, "王总监", ) if err != nil { t.Fatalf("CreateOrGetDocument in DB failed: %v", err) } if doc.DocID != testDocID { t.Fatalf("unexpected docID: %s", doc.DocID) } // 2. 标记正文非模板并认领 _ = engine.MarkDocumentContentUpdated(ctx, testDocID, "db_test_sha256", false) doc, err = engine.RecordDocumentViewed(ctx, testDocID, 1, docgov.RoleAuthor, "张工") if err != nil { t.Fatalf("RecordDocumentViewed failed: %v", err) } if !doc.AuthorClaimed { t.Fatalf("expected author_claimed in DB") } // 3. 生成变更提案 prop, err := engine.ProposeModification( ctx, testDocID, "agent_squad", "测试修改提要", "测试影响分析", "测试潜在风险", ) if err != nil { t.Fatalf("ProposeModification in DB failed: %v", err) } // 4. 拍板同意提案 prop, err = engine.DecideModificationProposal(ctx, prop.ProposalID, 1, true, "DB测试同意") if err != nil { t.Fatalf("DecideModificationProposal in DB failed: %v", err) } if prop.Status != docgov.ProposalApprovedByAuthor { t.Fatalf("expected proposal approved, got %s", prop.Status) } // 5. 批注与送审流程 _, err = engine.AddComment(ctx, testDocID, 2, "李审核", docgov.RoleReviewer, "## 结论", docgov.CommentSuggestion, "请增加法规出处") if err != nil { t.Fatalf("AddComment in DB failed: %v", err) } doc, err = engine.SubmitForReview(ctx, testDocID, 1, docgov.RoleAuthor, "张工") if err != nil { t.Fatalf("SubmitForReview in DB failed: %v", err) } if !doc.IsLocked { t.Fatalf("expected doc locked in reviewing") } } // TestDocGov_CreateOrGetDocument_OptionalDutiesNullFK ensures reviewer/approver=0 // persist as SQL NULL (optional DutyMode) instead of FK-violating 0. func TestDocGov_CreateOrGetDocument_OptionalDutiesNullFK(t *testing.T) { cfg, err := config.Load(".env") if err != nil || cfg == nil || cfg.ContextDbHost == "" { t.Skip("Skipping DB integration test: context DB not configured") } ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() pool, err := pgxpool.New(ctx, cfg.ContextDSN()) if err != nil || pool.Ping(ctx) != nil { t.Skipf("Skipping DB integration test: cannot connect to %s: %v", cfg.ContextDSN(), err) } defer pool.Close() engine := docgov.NewGovernanceEngine(pool) testDocID := fmt.Sprintf("doc_opt_duty_%d", time.Now().UnixNano()) suffix := time.Now().UnixNano() var authorID int64 if err := pool.QueryRow(ctx, ` INSERT INTO users (account_id, name, role) VALUES ($1, $2, 'engineer') RETURNING id `, fmt.Sprintf("opt_duty_%d", suffix), "系统管理员").Scan(&authorID); err != nil { t.Fatalf("insert author: %v", err) } var projectID int64 if err := pool.QueryRow(ctx, ` INSERT INTO projects (code, name, status, creator_user_id) VALUES ($1, $2, 'active', $3) RETURNING id `, fmt.Sprintf("opt-duty-%d", suffix), fmt.Sprintf("optional duties %d", suffix), authorID).Scan(&projectID); err != nil { t.Fatalf("insert project: %v", err) } t.Cleanup(func() { _, _ = pool.Exec(context.Background(), `DELETE FROM document_records WHERE doc_id = $1`, testDocID) _, _ = pool.Exec(context.Background(), `DELETE FROM projects WHERE id = $1`, projectID) _, _ = pool.Exec(context.Background(), `DELETE FROM users WHERE id = $1`, authorID) }) doc, err := engine.CreateOrGetDocument( ctx, testDocID, projectID, "intake", "可选职责空审核人", "V0.5_feasibility", "", authorID, "系统管理员", 0, "", 0, "", ) if err != nil { t.Fatalf("CreateOrGetDocument with reviewer/approver=0 failed: %v", err) } if doc.DocID != testDocID { t.Fatalf("unexpected docID: %s", doc.DocID) } var reviewerNull, approverNull bool var gotAuthor int64 err = pool.QueryRow(ctx, ` SELECT author_id, reviewer_id IS NULL, approver_id IS NULL FROM document_records WHERE doc_id = $1 `, testDocID).Scan(&gotAuthor, &reviewerNull, &approverNull) if err != nil { t.Fatalf("query document_records: %v", err) } if gotAuthor != authorID { t.Fatalf("author_id=%d want %d", gotAuthor, authorID) } if !reviewerNull || !approverNull { t.Fatalf("expected NULL reviewer/approver, got reviewerNull=%v approverNull=%v", reviewerNull, approverNull) } }