droboticflow/internal/api/acceptance_source_opinions_test.go
d-robotics 285f895d34 🔧 08: rework baseline carries unresolved opinions only + ADR 0008
R1-Q8/R2-Q7 (spec slice-edit-ux-rework, seam ②):
- planQualityReviewDecision now builds the next rework baseline as
  unresolved prior opinions (same entry IDs, QC still-problem notes
  merged into the original text) followed by this round's new opinions;
  resolved opinions never enter the new baseline
- rework-baseline reads (any round) and the acceptance source-opinions
  baseline projection narrow to unresolved-only via the frozen round's
  review confirmations; frozen records stay untouched (B02)
- legacy fallback: missing confirmations mean unresolved and opinions
  keep carrying (documented + tested); missing prior baseline keeps the
  first-round fresh-only behavior
- ADR 0008 records the decision, consequences and the rejected
  collapsed-display alternative
2026-10-13 20:10:49 +05:30

185 lines
12 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"context"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// B04:多来源完成协调(待完成来源明细)+ 意见来源投影(谁、哪一轮)。
func acceptanceProjectionFixture(t *testing.T) (dataStore store.Store, handler http.Handler, quality, acceptance domain.UserRecord, source, aggregate *domain.HumanItemRecord) {
t.Helper()
ctx := context.Background()
dataStore = store.NewMemoryStore()
now := nowISO()
if err := dataStore.UpsertSupplier(ctx, domain.SupplierRecord{ID: "vendor-v58b", OrganizationID: domain.DefaultOrganizationID, Name: "v58b vendor", Status: "active"}); err != nil {
t.Fatal(err)
}
quality = domain.UserRecord{ID: "quality-v58b", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleQualityReviewer, SupplierID: "vendor-v58b", Status: domain.UserStatusActive}
acceptance = domain.UserRecord{ID: "acceptance-v58b", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAcceptanceReviewer, SupplierID: "vendor-v58b", Status: domain.UserStatusActive}
for _, user := range []domain.UserRecord{quality, acceptance} {
if err := dataStore.UpsertUser(ctx, user); err != nil {
t.Fatal(err)
}
}
for _, membership := range []domain.ProjectAnnotationMembershipRecord{
{ID: "q-membership-v58b", OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-v58b", UserID: quality.ID, WorkgroupID: "workgroup-v58b", VendorID: "vendor-v58b", AssignmentRole: domain.AnnotationRoleQualityReviewer, CreatedAt: now, UpdatedAt: now},
{ID: "a-membership-v58b", OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-v58b", UserID: acceptance.ID, WorkgroupID: "workgroup-v58b", VendorID: "vendor-v58b", AssignmentRole: domain.AnnotationRoleAcceptanceReviewer, CreatedAt: now, UpdatedAt: now},
} {
if err := dataStore.UpsertProjectAnnotationMembership(ctx, membership); err != nil {
t.Fatal(err)
}
}
batch := domain.HumanBatchRecord{ID: "batch-v58b", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", ProjectID: "project-v58b", WorkflowID: "workflow-v58b", RunID: "run-v58b", GateNodeID: "human-v58b", Status: domain.HumanBatchActive, Stages: domain.DefaultFanyuV58HumanStages(), ReleasePolicy: domain.HumanReleaseAllAccepted, MaxReworkRounds: 5, RequirePrePackageAcceptance: true, CreatedAt: now, UpdatedAt: now}
if err := dataStore.UpsertHumanBatch(ctx, batch); err != nil {
t.Fatal(err)
}
task := domain.AnnotationTaskRecord{ID: "task-v58b", OrganizationID: domain.DefaultOrganizationID, ProjectID: batch.ProjectID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, NodeID: batch.GateNodeID, ArtifactID: "artifact-v58b", SourceVideo: "bos://fixture/video.mp4", ClipStart: 0, ClipEnd: 20, TemplateID: "template-v58b", TemplateVersion: 6, WorkgroupID: "workgroup-v58b", VendorID: "vendor-v58b", Status: domain.AnnotationTaskStatusAccepted, CurrentRevision: 1, CreatedAt: now, UpdatedAt: now}
if err := dataStore.UpsertAnnotationTask(ctx, task); err != nil {
t.Fatal(err)
}
if err := dataStore.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{ID: "slice-v58b", OrganizationID: domain.DefaultOrganizationID, TaskID: task.ID, Revision: 1, Segments: []domain.SliceSegment{{ID: "slice", Start: 0, End: 20}}, AuthorUserID: "annotator-v58b", CreatedAt: now}); err != nil {
t.Fatal(err)
}
if err := dataStore.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: "revision-v58b", OrganizationID: domain.DefaultOrganizationID, TaskID: task.ID, Revision: 1, TemplateVersion: 6, Segments: []domain.AnnotationSegment{{ID: "semantic", LabelID: "01", Label: "测试", Start: 0, End: 20}}, AuthorUserID: "annotator-v58b", CreatedAt: now}); err != nil {
t.Fatal(err)
}
item := domain.HumanItemRecord{ID: "item-v58b", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", BatchID: batch.ID, ProjectID: batch.ProjectID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, GateNodeID: batch.GateNodeID, LegacyTaskID: task.ID, Subject: domain.HumanSubjectRef{ArtifactID: task.ArtifactID, ItemKey: "clip-v58b"}, CurrentStageID: domain.FanyuV58StageQualityReview, Status: domain.HumanItemAccepted, WorkgroupID: task.WorkgroupID, VendorID: task.VendorID, CurrentRevision: 1, ReviewRound: 1, MaxReworkRounds: 5, Version: 2, AcceptedAt: now, AcceptedByUserID: quality.ID, CreatedAt: now, UpdatedAt: now}
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
t.Fatal(err)
}
server := &server{store: dataStore}
if _, err := server.ensureFanyuV5PrePackageAcceptance(ctx, &batch); err != nil {
t.Fatal(err)
}
source, _ = dataStore.GetHumanItem(ctx, item.ID)
aggregate, _ = dataStore.GetHumanItem(ctx, fanyuV5PrePackageAcceptanceItemID(batch.ID))
if aggregate == nil {
t.Fatal("aggregate acceptance item missing")
}
t.Logf("aggregate: plugin=%s scope=%s supplier=%q task=%q stage=%s", aggregate.PluginID, aggregate.ProcessingScope, aggregate.SupplierID, aggregate.LegacyTaskID, aggregate.CurrentStageID)
// 投影数据:一轮退回基线(含时间点意见 + 一条未确认的遗留意见)+ 一条质检
// 正式判定(含 D7 冻结确认;src-ts 已判解决)。
if err := dataStore.UpsertReworkBaseline(ctx, domain.ReworkBaselineRecord{
ID: store.ReworkBaselineID(source.ID, 1), OrganizationID: source.OrganizationID, ProjectID: source.ProjectID,
ItemID: source.ID, BatchID: source.BatchID, Round: 1, StageID: domain.FanyuV58StageAnnotationCombined,
Problems: []domain.ReviewProblem{{ID: "src-ts", Scope: "timestamp", SliceID: "slice", AtSeconds: 8, Start: 8, End: 8, Text: "来源时间点问题"}, {ID: "src-open", Scope: "slice", SliceID: "slice", Start: 0, End: 20, Text: "来源遗留问题"}},
Comment: "第一轮退回说明", CreatedAt: now,
}); err != nil {
t.Fatal(err)
}
if err := dataStore.UpsertAnnotationReview(ctx, domain.AnnotationReviewRecord{ID: "quality-review-v58b", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1, ReviewRound: 1, Stage: domain.AnnotationReviewStageQuality, Decision: domain.AnnotationReviewApproved, ReviewerUserID: quality.ID, Comment: "复核通过", Confirmations: []domain.ReviewConfirmation{{OpinionID: "src-ts", Resolved: true, Note: "已核对"}}, CreatedAt: now}); err != nil {
t.Fatal(err)
}
// 现代成员记录:聚合条目访问走 ProjectMember 口径(legacy 工作组成员不覆盖该路径)。
if err := dataStore.UpsertProjectMember(ctx, domain.ProjectMemberRecord{OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-v58b", UserID: quality.ID, SupplierID: "vendor-v58b", Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleQualityReviewer}, CreatedAt: now, UpdatedAt: now}); err != nil {
t.Fatal(err)
}
if err := dataStore.UpsertProjectMember(ctx, domain.ProjectMemberRecord{OrganizationID: domain.DefaultOrganizationID, ProjectID: "project-v58b", UserID: acceptance.ID, SupplierID: "vendor-v58b", Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleAcceptanceReviewer}, CreatedAt: now, UpdatedAt: now}); err != nil {
t.Fatal(err)
}
handler = NewHandler(dataStore, nil)
return dataStore, handler, quality, acceptance, source, aggregate
}
func acceptanceProjectionRequest(handler http.Handler, method, path string, user domain.UserRecord, body any) *httptest.ResponseRecorder {
encoded, _ := json.Marshal(body)
req := httptest.NewRequest(method, path, strings.NewReader(string(encoded)))
req.Header.Set("Content-Type", "application/json")
req.AddCookie(&http.Cookie{Name: csrfCookieName, Value: "b04-csrf"})
req.Header.Set(csrfHeaderName, "b04-csrf")
req = req.WithContext(context.WithValue(req.Context(), authContextKey{}, &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}}))
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
return recorder
}
func TestAcceptanceGateNamesPendingSources(t *testing.T) {
dataStore, handler, _, acceptance, source, aggregate := acceptanceProjectionFixture(t)
ctx := context.Background()
// 来源未重新通过质检:终审判定被拒且明确点名缺哪个来源。
notReady := *source
notReady.Status = domain.HumanItemSubmitted
notReady.Version++
if err := dataStore.UpsertHumanItem(ctx, notReady); err != nil {
t.Fatal(err)
}
claimed := acceptanceProjectionRequest(handler, http.MethodPost, "/api/human-items/"+aggregate.ID+"/claim", acceptance, map[string]any{"version": aggregate.Version})
if claimed.Code != http.StatusOK {
t.Fatalf("claim: %d %s", claimed.Code, claimed.Body.String())
}
current, _ := dataStore.GetHumanItem(ctx, aggregate.ID)
decision := acceptanceProjectionRequest(handler, http.MethodPost, "/api/human-items/"+aggregate.ID+"/decisions", acceptance, map[string]any{"version": current.Version, "decision": "approved"})
if decision.Code != http.StatusConflict {
t.Fatalf("pending gate must 409: %d %s", decision.Code, decision.Body.String())
}
if !strings.Contains(decision.Body.String(), "clip-v58b") || !strings.Contains(decision.Body.String(), "quality_review") {
t.Fatalf("gate must name the pending source and stage: %s", decision.Body.String())
}
// 来源重新通过后放行(再次终审分支)。
ready := notReady
ready.Status = domain.HumanItemAccepted
ready.Version++
if err := dataStore.UpsertHumanItem(ctx, ready); err != nil {
t.Fatal(err)
}
current, _ = dataStore.GetHumanItem(ctx, aggregate.ID)
approved := acceptanceProjectionRequest(handler, http.MethodPost, "/api/human-items/"+aggregate.ID+"/decisions", acceptance, map[string]any{"version": current.Version, "decision": "approved", "poseDelivery": map[string]any{"headpose": "include", "handpose": "include"}})
if approved.Code != http.StatusOK {
t.Fatalf("approved after sources ready: %d %s", approved.Code, approved.Body.String())
}
}
func TestAcceptanceSourceOpinionsProjection(t *testing.T) {
_, handler, quality, acceptance, _, aggregate := acceptanceProjectionFixture(t)
// 质检员只读可见(共享授权语义不扩权:标注员不可见由访问层兜底)。
w := acceptanceProjectionRequest(handler, http.MethodGet, "/api/human-items/"+aggregate.ID+"/source-opinions", acceptance, nil)
if w.Code != http.StatusOK {
t.Fatalf("projection read: %d %s", w.Code, w.Body.String())
}
var body struct {
Opinions []struct {
SourceItemID string `json:"sourceItemId"`
ItemKey string `json:"itemKey"`
Baselines []struct {
Round int `json:"round"`
Problems []domain.ReviewProblem `json:"problems"`
Comment string `json:"comment"`
} `json:"baselines"`
Reviews []struct {
Round int `json:"round"`
Decision string `json:"decision"`
ReviewerUserID string `json:"reviewerUserId"`
Confirmations []domain.ReviewConfirmation `json:"confirmations"`
} `json:"reviews"`
} `json:"opinions"`
}
json.Unmarshal(w.Body.Bytes(), &body)
if len(body.Opinions) != 1 {
t.Fatalf("one source expected: %s", w.Body.String())
}
opinion := body.Opinions[0]
// R1-Q8(ADR 0008):基线投影不返回已判解决的 src-ts,只余未确认的 src-open;
// 解决轨迹保留在 reviews 投影的冻结核对结论中(服务端正式审查记录)。
if opinion.ItemKey != "clip-v58b" || len(opinion.Baselines) != 1 || len(opinion.Baselines[0].Problems) != 1 || opinion.Baselines[0].Problems[0].ID != "src-open" {
t.Fatalf("baseline projection must hide resolved opinions: %+v", opinion)
}
if len(opinion.Reviews) != 1 || opinion.Reviews[0].ReviewerUserID != quality.ID || opinion.Reviews[0].Round != 1 {
t.Fatalf("review projection (who/which round): %+v", opinion.Reviews)
}
if len(opinion.Reviews[0].Confirmations) != 1 || !opinion.Reviews[0].Confirmations[0].Resolved {
t.Fatalf("frozen confirmations must project: %+v", opinion.Reviews[0].Confirmations)
}
// 非聚合条目不暴露该端点。
other := acceptanceProjectionRequest(handler, http.MethodGet, "/api/human-items/"+body.Opinions[0].SourceItemID+"/source-opinions", acceptance, nil)
if other.Code != http.StatusConflict {
t.Fatalf("non-aggregate item must not expose projection: %d", other.Code)
}
}