forked from longtao.wu/droboticflow
331 lines
15 KiB
Go
331 lines
15 KiB
Go
package api
|
||
|
||
import (
|
||
"droboticflow/internal/domain"
|
||
"droboticflow/internal/store"
|
||
"encoding/json"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// B02:退回基线冻结、处理状态(跳过留痕)读写契约、复审核对结论(D7)校验与正式冻结。
|
||
|
||
func rejectWithDraft(t *testing.T, f combinedDraftFixture, problems []map[string]any) map[string]any {
|
||
t.Helper()
|
||
version, headers := openReviewDraftSession(t, f)
|
||
draftVersion := 0
|
||
if problems != nil {
|
||
w := putReviewDraft(t, f, "/api/human-items/"+f.item.ID+"/review-draft", version, "b02-draft", 0, problems, headers)
|
||
if w.Code != 200 {
|
||
t.Fatalf("draft save: %d %s", w.Code, w.Body.String())
|
||
}
|
||
draftVersion = readReviewDraft(t, f, "/api/human-items/"+f.item.ID+"/review-draft").Version
|
||
}
|
||
input := map[string]any{"stage": "quality", "decision": "rejected", "reasonCodes": []string{"manual_review_reject"},
|
||
"comment": "B02基线验证", "problemSliceIds": []string{"left"}, "operationId": "b02-reject", "draftVersion": draftVersion, "resultVersion": version}
|
||
w := reviewSessionRequest(f, http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/reviews", f.reviewer, input, headers)
|
||
if w.Code != http.StatusCreated {
|
||
t.Fatalf("rejection: %d %s", w.Code, w.Body.String())
|
||
}
|
||
out := map[string]any{}
|
||
json.Unmarshal(w.Body.Bytes(), &out)
|
||
return out
|
||
}
|
||
|
||
func TestQualityRejectionFreezesReworkBaseline(t *testing.T) {
|
||
f := decisionFixture(t)
|
||
f.item.LatestAuthorUserID = f.annotator.ID
|
||
f.item.SupplierID = f.annotator.SupplierID
|
||
f.task.SupplierID = f.annotator.SupplierID
|
||
f.item.MaxReworkRounds = 5
|
||
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := f.store.UpsertAnnotationTask(f.ctx, f.task); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
rejectWithDraft(t, f, []map[string]any{
|
||
{"id": "op-ts", "scope": "timestamp", "sliceId": "left", "atSeconds": 2.5, "timeBasis": "capture-seconds", "text": "时间点问题"},
|
||
{"id": "op-slice", "scope": "slice", "sliceId": "left", "text": "切片问题"},
|
||
})
|
||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
if item.Status != domain.HumanItemClaimed || item.AssignedUserID != f.annotator.ID {
|
||
t.Fatalf("directed return missing: %s %s", item.Status, item.AssignedUserID)
|
||
}
|
||
// 定向归属人读取基线:原意见 + 被审版本切片/动作摘要全部内嵌。
|
||
w := f.request(http.MethodGet, "/api/human-items/"+f.item.ID+"/rework-baseline", f.annotator, nil)
|
||
if w.Code != 200 {
|
||
t.Fatalf("baseline read: %d %s", w.Code, w.Body.String())
|
||
}
|
||
var body struct {
|
||
Baseline *domain.ReworkBaselineRecord `json:"baseline"`
|
||
}
|
||
json.Unmarshal(w.Body.Bytes(), &body)
|
||
b := body.Baseline
|
||
if b == nil || b.Round != item.ReviewRound || b.ReviewedRevision != f.item.CurrentRevision {
|
||
t.Fatalf("baseline header: %+v", b)
|
||
}
|
||
if len(b.Problems) != 2 || b.Problems[0].ID != "op-ts" || b.Problems[0].Scope != "timestamp" {
|
||
t.Fatalf("frozen opinions: %+v", b.Problems)
|
||
}
|
||
if len(b.Slices) != 2 || len(b.Actions) != 2 {
|
||
t.Fatalf("embedded evidence must survive revision pruning: %d slices %d actions", len(b.Slices), len(b.Actions))
|
||
}
|
||
if b.Comment != "B02基线验证" || b.RejectID == "" || b.ReviewOperationID == "" {
|
||
t.Fatalf("baseline provenance: %+v", b)
|
||
}
|
||
}
|
||
|
||
func TestReworkHandlingContractPermissionsAndCAS(t *testing.T) {
|
||
f := decisionFixture(t)
|
||
f.item.LatestAuthorUserID = f.annotator.ID
|
||
f.item.SupplierID = f.annotator.SupplierID
|
||
f.task.SupplierID = f.annotator.SupplierID
|
||
f.item.MaxReworkRounds = 5
|
||
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := f.store.UpsertAnnotationTask(f.ctx, f.task); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
rejectWithDraft(t, f, []map[string]any{{"id": "op-1", "scope": "slice", "sliceId": "left", "text": "切片意见"}, {"id": "op-2", "scope": "slice", "sliceId": "right", "text": "右段意见"}})
|
||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
endpoint := "/api/human-items/" + f.item.ID + "/rework-handling"
|
||
|
||
put := func(user domain.UserRecord, payload map[string]any) *httptest.ResponseRecorder {
|
||
return f.request(http.MethodPut, endpoint, user, payload)
|
||
}
|
||
// 非归属人不可写。
|
||
stranger := domain.UserRecord{ID: "stranger-annotator", OrganizationID: f.item.OrganizationID, Role: domain.UserRoleAnnotator, SupplierID: f.annotator.SupplierID, Status: domain.UserStatusActive}
|
||
if err := f.store.UpsertUser(f.ctx, stranger); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if w := put(stranger, map[string]any{"version": 0, "skippedOpinionIds": []string{"op-1"}}); w.Code != http.StatusForbidden {
|
||
t.Fatalf("stranger write must 403: %d %s", w.Code, w.Body.String())
|
||
}
|
||
// 引用不存在的意见被拒。
|
||
if w := put(f.annotator, map[string]any{"version": 0, "skippedOpinionIds": []string{"ghost"}}); w.Code != http.StatusBadRequest {
|
||
t.Fatalf("ghost opinion must 400: %d %s", w.Code, w.Body.String())
|
||
}
|
||
// 同一意见重复标记被拒。
|
||
if w := put(f.annotator, map[string]any{"version": 0, "skippedOpinionIds": []string{"op-1", "op-1"}}); w.Code != http.StatusBadRequest {
|
||
t.Fatalf("duplicate opinion must 400: %d", w.Code)
|
||
}
|
||
// 归属人写入成功(一键"不改了"留痕),复审员可读。
|
||
saved := put(f.annotator, map[string]any{"version": 0, "skippedOpinionIds": []string{"op-2"}})
|
||
if saved.Code != 200 {
|
||
t.Fatalf("owner save: %d %s", saved.Code, saved.Body.String())
|
||
}
|
||
var first struct {
|
||
Handling *domain.ReworkHandlingRecord `json:"handling"`
|
||
}
|
||
json.Unmarshal(saved.Body.Bytes(), &first)
|
||
if first.Handling == nil || first.Handling.Version != 1 || len(first.Handling.SkippedOpinionIDs) != 1 || first.Handling.SkippedOpinionIDs[0] != "op-2" {
|
||
t.Fatalf("handling record: %+v", first.Handling)
|
||
}
|
||
// CAS:旧版本冲突。
|
||
if w := put(f.annotator, map[string]any{"version": 0, "skippedOpinionIds": first.Handling.SkippedOpinionIDs}); w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), "rework_handling_version_conflict") {
|
||
t.Fatalf("stale version must conflict: %d %s", w.Code, w.Body.String())
|
||
}
|
||
// 撤销跳过(清空列表)也是合法更新。
|
||
updated := put(f.annotator, map[string]any{"version": 1, "skippedOpinionIds": []string{}})
|
||
if updated.Code != 200 {
|
||
t.Fatalf("cas clear: %d %s", updated.Code, updated.Body.String())
|
||
}
|
||
// 复审员读取(复审期对象访问沿用条目权限):看到的是清空后的最新状态。
|
||
read := f.request(http.MethodGet, endpoint, f.reviewer, nil)
|
||
if read.Code != 200 || !strings.Contains(read.Body.String(), `"skippedOpinionIds":[]`) {
|
||
t.Fatalf("reviewer read: %d %s", read.Code, read.Body.String())
|
||
}
|
||
_ = item
|
||
}
|
||
|
||
func TestReworkHandlingLocksDuringSubmissionAndAfterRoundChange(t *testing.T) {
|
||
for _, scenario := range []string{"submitting", "submitted", "stale-round"} {
|
||
t.Run(scenario, func(t *testing.T) {
|
||
f := reworkContractFixture(t)
|
||
reworkRejectRound(t, f, "lock-return", []map[string]any{{"id": "old", "scope": "slice", "sliceId": "left", "text": "原问题"}}, nil)
|
||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
round := item.ReviewRound
|
||
switch scenario {
|
||
case "submitting":
|
||
item.PendingCombinedSubmissionID = "pending"
|
||
case "submitted":
|
||
item.CurrentStageID = domain.FanyuV58StageQualityReview
|
||
item.Status = domain.HumanItemReady
|
||
case "stale-round":
|
||
round--
|
||
}
|
||
f.store.UpsertHumanItem(f.ctx, *item)
|
||
w := f.request(http.MethodPut, "/api/human-items/"+item.ID+"/rework-handling", f.annotator, map[string]any{"round": round, "version": 0, "skippedOpinionIds": []string{"old"}})
|
||
if w.Code != http.StatusConflict || !strings.Contains(w.Body.String(), "rework_handling_locked") {
|
||
t.Fatalf("locked handling accepted: %d %s", w.Code, w.Body.String())
|
||
}
|
||
handling, _ := f.store.GetReworkHandling(f.ctx, store.ReworkHandlingID(item.ID, item.ReviewRound))
|
||
if handling != nil {
|
||
t.Fatal("locked write persisted")
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestReviewConfirmationsValidateAgainstBaselineAndFreeze(t *testing.T) {
|
||
f := decisionFixture(t)
|
||
// 直接冻结一条 round-1 基线(模拟上一轮退回),验证草稿校验与正式冻结。
|
||
if err := f.store.UpsertReworkBaseline(f.ctx, domain.ReworkBaselineRecord{
|
||
ID: store.ReworkBaselineID(f.item.ID, 1), OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID,
|
||
ItemID: f.item.ID, Round: 1, StageID: domain.FanyuV58StageAnnotationCombined,
|
||
Problems: []domain.ReviewProblem{{ID: "op-old", Scope: "slice", SliceID: "left", Text: "旧问题"}},
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
f.item.ReviewRound = 1
|
||
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
version, headers := openReviewDraftSession(t, f)
|
||
endpoint := "/api/human-items/" + f.item.ID + "/review-draft"
|
||
save := func(op string, ver int, confirmations []map[string]any) *httptest.ResponseRecorder {
|
||
payload := map[string]any{"version": ver, "resultVersion": version, "operationId": op, "modelVersion": 2,
|
||
"problems": []map[string]any{}, "confirmations": confirmations}
|
||
return reviewSessionRequest(f, http.MethodPut, endpoint, f.reviewer, payload, headers)
|
||
}
|
||
if w := save("c-ghost", 0, []map[string]any{{"opinionId": "ghost", "resolved": true}}); w.Code != http.StatusBadRequest {
|
||
t.Fatalf("ghost opinion: %d %s", w.Code, w.Body.String())
|
||
}
|
||
if w := save("c-dup", 0, []map[string]any{{"opinionId": "op-old", "resolved": true}, {"opinionId": "op-old", "resolved": false}}); w.Code != http.StatusBadRequest {
|
||
t.Fatalf("duplicate: %d", w.Code)
|
||
}
|
||
if w := save("c-unresolved", 0, []map[string]any{{"opinionId": "op-old", "resolved": false}}); w.Code != http.StatusBadRequest {
|
||
t.Fatalf("unresolved without note or opinion: %d %s", w.Code, w.Body.String())
|
||
}
|
||
ok := save("c-ok", 0, []map[string]any{{"opinionId": "op-old", "resolved": true, "note": "已核对修正"}})
|
||
if ok.Code != 200 {
|
||
t.Fatalf("valid confirmations: %d %s", ok.Code, ok.Body.String())
|
||
}
|
||
var savedDraft struct {
|
||
Draft domain.ReviewDraft `json:"draft"`
|
||
}
|
||
json.Unmarshal(ok.Body.Bytes(), &savedDraft)
|
||
if len(savedDraft.Draft.Confirmations) != 1 || !savedDraft.Draft.Confirmations[0].Resolved {
|
||
t.Fatalf("draft confirmations: %+v", savedDraft.Draft.Confirmations)
|
||
}
|
||
// 无基线轮次:确认必须为空。
|
||
f2 := decisionFixture(t)
|
||
v2, h2 := openReviewDraftSession(t, f2)
|
||
payload := map[string]any{"version": 0, "resultVersion": v2, "operationId": "c-nobaseline", "modelVersion": 2,
|
||
"problems": []map[string]any{}, "confirmations": []map[string]any{{"opinionId": "op-old", "resolved": true}}}
|
||
if w := reviewSessionRequest(f2, http.MethodPut, "/api/human-items/"+f2.item.ID+"/review-draft", f2.reviewer, payload, h2); w.Code != http.StatusBadRequest {
|
||
t.Fatalf("no-baseline round must reject confirmations: %d %s", w.Code, w.Body.String())
|
||
}
|
||
// 随正式决定冻结(approved 路径也冻结核对结论)。
|
||
freeze := reviewSessionRequest(f, http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/reviews", f.reviewer, map[string]any{
|
||
"stage": "quality", "decision": "approved", "operationId": "c-freeze", "draftVersion": savedDraft.Draft.Version, "resultVersion": version,
|
||
}, headers)
|
||
if freeze.Code != http.StatusCreated {
|
||
t.Fatalf("approve freeze: %d %s", freeze.Code, freeze.Body.String())
|
||
}
|
||
reviews, err := f.store.ListAnnotationReviews(f.ctx, f.item.OrganizationID, f.task.ID)
|
||
if err != nil || len(reviews) == 0 {
|
||
t.Fatalf("reviews: %v %d", err, len(reviews))
|
||
}
|
||
frozen := false
|
||
for _, r := range reviews {
|
||
for _, c := range r.Confirmations {
|
||
if c.OpinionID == "op-old" && c.Resolved {
|
||
frozen = true
|
||
}
|
||
}
|
||
}
|
||
if !frozen {
|
||
t.Fatal("confirmations must freeze into the formal review record")
|
||
}
|
||
}
|
||
|
||
func TestReworkBaselineMissingDegradesGracefully(t *testing.T) {
|
||
f := decisionFixture(t)
|
||
w := f.request(http.MethodGet, "/api/human-items/"+f.item.ID+"/rework-baseline", f.reviewer, nil)
|
||
if w.Code != 200 || !strings.Contains(w.Body.String(), `"baseline":null`) {
|
||
t.Fatalf("missing baseline must return null not error: %d %s", w.Code, w.Body.String())
|
||
}
|
||
}
|
||
|
||
func TestQualityApprovalRequiresResolvedPriorOpinions(t *testing.T) {
|
||
for _, verdict := range []string{"missing", "unresolved", "resolved"} {
|
||
t.Run(verdict, func(t *testing.T) {
|
||
f := decisionFixture(t)
|
||
f.item.ReviewRound = 1
|
||
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := f.store.UpsertReworkBaseline(f.ctx, domain.ReworkBaselineRecord{
|
||
ID: store.ReworkBaselineID(f.item.ID, 1), OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID,
|
||
ItemID: f.item.ID, Round: 1, StageID: domain.FanyuV58StageAnnotationCombined,
|
||
Problems: []domain.ReviewProblem{{ID: "prior", Scope: "slice", SliceID: "left", Text: "尚需核对"}},
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
version, headers := openReviewDraftSession(t, f)
|
||
confirmations := []map[string]any{}
|
||
if verdict != "missing" {
|
||
confirmations = append(confirmations, map[string]any{"opinionId": "prior", "resolved": verdict == "resolved", "note": "已核对"})
|
||
}
|
||
saved := reviewSessionRequest(f, http.MethodPut, "/api/human-items/"+f.item.ID+"/review-draft", f.reviewer,
|
||
map[string]any{"version": 0, "resultVersion": version, "operationId": "save-verdict", "modelVersion": 2, "problems": []map[string]any{}, "confirmations": confirmations}, headers)
|
||
if saved.Code != 200 {
|
||
t.Fatalf("save: %d %s", saved.Code, saved.Body.String())
|
||
}
|
||
var body struct {
|
||
Draft domain.ReviewDraft `json:"draft"`
|
||
}
|
||
json.Unmarshal(saved.Body.Bytes(), &body)
|
||
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
reply := reviewSessionRequest(f, http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/reviews", f.reviewer,
|
||
map[string]any{"stage": "quality", "decision": "approved", "operationId": "approve-prior", "draftVersion": body.Draft.Version, "resultVersion": version}, headers)
|
||
expected := http.StatusBadRequest
|
||
if verdict == "resolved" {
|
||
expected = http.StatusCreated
|
||
}
|
||
if reply.Code != expected {
|
||
t.Fatalf("%s: got %d want %d: %s", verdict, reply.Code, expected, reply.Body.String())
|
||
}
|
||
if verdict != "resolved" {
|
||
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
if after.Version != before.Version || after.CurrentStageID != before.CurrentStageID {
|
||
t.Fatal("blocked approval changed task")
|
||
}
|
||
reviews, _ := f.store.ListAnnotationReviews(f.ctx, f.item.OrganizationID, f.task.ID)
|
||
if len(reviews) != 0 {
|
||
t.Fatal("blocked approval wrote formal review")
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestGenericLegacyQualityApprovalRequiresResolvedPriorOpinions(t *testing.T) {
|
||
f := decisionFixture(t)
|
||
f.item.ReviewRound = 1
|
||
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
if err := f.store.UpsertReworkBaseline(f.ctx, domain.ReworkBaselineRecord{
|
||
ID: store.ReworkBaselineID(f.item.ID, 1), OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID,
|
||
ItemID: f.item.ID, Round: 1, StageID: domain.FanyuV58StageAnnotationCombined,
|
||
Problems: []domain.ReviewProblem{{ID: "prior-unresolved", Scope: "slice", SliceID: "left", Text: "Unresolved"}},
|
||
}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
w := f.request(http.MethodPost, "/api/human-items/"+f.item.ID+"/decisions", f.reviewer, map[string]any{"version": f.item.Version, "decision": "approved"})
|
||
if w.Code != http.StatusBadRequest {
|
||
t.Fatalf("generic route bypass: status=%d body=%s", w.Code, w.Body.String())
|
||
}
|
||
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
if after.Version != before.Version || after.Status != before.Status || after.CurrentStageID != before.CurrentStageID {
|
||
t.Fatal("blocked generic approval changed task")
|
||
}
|
||
}
|