forked from longtao.wu/droboticflow
378 lines
17 KiB
Go
378 lines
17 KiB
Go
package api
|
||
|
||
import (
|
||
"droboticflow/internal/domain"
|
||
"droboticflow/internal/store"
|
||
"encoding/json"
|
||
"net/http"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
// ADR 0008(spec slice-edit-ux-rework §2 R1-Q8/R2-Q7,seam ②):
|
||
// 再退回生成的新意见基线 = 未解决旧意见 + 本轮新增;已解决意见不进基线;
|
||
// "仍有问题"的意见保持同一条目(备注并入原文本,不产生新条目 ID);
|
||
// 旧多轮数据缺失核对结论(意见状态)时按"未解决"兜底。
|
||
|
||
func TestReworkBaselineProblemsCarriesUnresolvedOnly(t *testing.T) {
|
||
problem := func(id, text string) domain.ReviewProblem {
|
||
return domain.ReviewProblem{ID: id, Scope: "slice", SliceID: "left", Text: text}
|
||
}
|
||
confirmation := func(id string, resolved bool, note string) domain.ReviewConfirmation {
|
||
return domain.ReviewConfirmation{OpinionID: id, Resolved: resolved, Note: note}
|
||
}
|
||
cases := []struct {
|
||
name string
|
||
prior []domain.ReviewProblem
|
||
confirmations []domain.ReviewConfirmation
|
||
fresh []domain.ReviewProblem
|
||
wantIDs []string
|
||
wantTexts map[string]string
|
||
}{
|
||
{
|
||
name: "首轮退回:无旧基线,仅本轮新增",
|
||
fresh: []domain.ReviewProblem{problem("op-a", "甲"), problem("op-b", "乙"), problem("op-c", "丙")},
|
||
wantIDs: []string{"op-a", "op-b", "op-c"},
|
||
wantTexts: map[string]string{"op-a": "甲", "op-b": "乙", "op-c": "丙"},
|
||
},
|
||
{
|
||
name: "再退回:已解决不进基线,未解决在前新增在后",
|
||
prior: []domain.ReviewProblem{problem("op-a", "甲"), problem("op-b", "乙"), problem("op-c", "丙")},
|
||
confirmations: []domain.ReviewConfirmation{confirmation("op-a", true, "已核对"), confirmation("op-b", true, ""), confirmation("op-c", false, "改后区间仍多 2 秒")},
|
||
fresh: []domain.ReviewProblem{problem("op-d", "丁"), problem("op-e", "戊")},
|
||
wantIDs: []string{"op-c", "op-d", "op-e"},
|
||
wantTexts: map[string]string{
|
||
"op-c": "丙\n【质检备注】改后区间仍多 2 秒",
|
||
"op-d": "丁",
|
||
"op-e": "戊",
|
||
},
|
||
},
|
||
{
|
||
name: "兜底:缺失核对结论按未解决延续(旧多轮数据)",
|
||
prior: []domain.ReviewProblem{problem("op-a", "甲"), problem("op-b", "乙")},
|
||
fresh: []domain.ReviewProblem{problem("op-c", "丙")},
|
||
wantIDs: []string{"op-a", "op-b", "op-c"},
|
||
wantTexts: map[string]string{"op-a": "甲", "op-b": "乙", "op-c": "丙"},
|
||
},
|
||
{
|
||
name: "部分核对:只确认了一条已解决,其余延续",
|
||
prior: []domain.ReviewProblem{problem("op-a", "甲"), problem("op-b", "乙")},
|
||
confirmations: []domain.ReviewConfirmation{confirmation("op-a", true, "已核对")},
|
||
fresh: []domain.ReviewProblem{problem("op-c", "丙")},
|
||
wantIDs: []string{"op-b", "op-c"},
|
||
wantTexts: map[string]string{"op-b": "乙", "op-c": "丙"},
|
||
},
|
||
{
|
||
name: "仍有问题无备注:原文不变仍延续(防御分支)",
|
||
prior: []domain.ReviewProblem{problem("op-a", "甲")},
|
||
confirmations: []domain.ReviewConfirmation{confirmation("op-a", false, "")},
|
||
fresh: nil,
|
||
wantIDs: []string{"op-a"},
|
||
wantTexts: map[string]string{"op-a": "甲"},
|
||
},
|
||
{
|
||
name: "多轮备注累积:旧备注块保留,新备注块追加",
|
||
prior: []domain.ReviewProblem{problem("op-c", "丙\n【质检备注】第一轮仍多 2 秒")},
|
||
confirmations: []domain.ReviewConfirmation{
|
||
confirmation("op-c", false, " 第二轮仍多 1 秒 "),
|
||
},
|
||
fresh: []domain.ReviewProblem{},
|
||
wantIDs: []string{"op-c"},
|
||
wantTexts: map[string]string{"op-c": "丙\n【质检备注】第一轮仍多 2 秒\n【质检备注】第二轮仍多 1 秒"},
|
||
},
|
||
}
|
||
for _, tc := range cases {
|
||
t.Run(tc.name, func(t *testing.T) {
|
||
got := reworkBaselineProblems(tc.prior, tc.confirmations, tc.fresh)
|
||
if len(got) != len(tc.wantIDs) {
|
||
t.Fatalf("条目数 = %d, want %d (%+v)", len(got), len(tc.wantIDs), got)
|
||
}
|
||
for i, id := range tc.wantIDs {
|
||
if got[i].ID != id {
|
||
t.Fatalf("第 %d 条 ID = %q, want %q(条目 ID 不得漂移)", i, got[i].ID, id)
|
||
}
|
||
if got[i].Text != tc.wantTexts[id] {
|
||
t.Fatalf("意见 %q 文本 = %q, want %q", id, got[i].Text, tc.wantTexts[id])
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// —— HTTP seam:退回路径端到端(rework 退回路径构造 + 基线查询收窄)——
|
||
|
||
// reworkRejectRound 走真实退回路径:开审查会话 → 存意见草稿(model 2,含核对
|
||
// 结论)→ 提交退回决定。正式效果落库后返回。
|
||
func reworkRejectRound(t *testing.T, f combinedDraftFixture, operationID string, problems, confirmations []map[string]any) {
|
||
t.Helper()
|
||
version, headers := openReviewDraftSession(t, f)
|
||
draftVersion := 0
|
||
if problems != nil || confirmations != nil {
|
||
payload := map[string]any{"version": 0, "resultVersion": version, "operationId": operationID + "-draft",
|
||
"modelVersion": 2, "problems": problems}
|
||
if confirmations != nil {
|
||
payload["confirmations"] = confirmations
|
||
}
|
||
w := reviewSessionRequest(f, http.MethodPut, "/api/human-items/"+f.item.ID+"/review-draft", f.reviewer, payload, 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": "退回基线契约验证", "problemSliceIds": []string{"left"}, "operationId": operationID,
|
||
"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 && w.Code != http.StatusAccepted {
|
||
t.Fatalf("rejection %s: %d %s", operationID, w.Code, w.Body.String())
|
||
}
|
||
}
|
||
|
||
// reworkAdvanceToNextReview 模拟标注员返工送检后回到质检:阶段/归属/修订号就位
|
||
// (不驱动完整送检端点,本票只考察退回路径的基线构造)。
|
||
func reworkAdvanceToNextReview(t *testing.T, f combinedDraftFixture) {
|
||
t.Helper()
|
||
item, e := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
if e != nil || item == nil {
|
||
t.Fatal(e)
|
||
}
|
||
item.CurrentStageID = domain.FanyuV58StageQualityReview
|
||
item.Status = domain.HumanItemClaimed
|
||
item.AssignedUserID = f.reviewer.ID
|
||
item.CurrentRevision++
|
||
item.Version++
|
||
if e := f.store.UpsertHumanItem(f.ctx, *item); e != nil {
|
||
t.Fatal(e)
|
||
}
|
||
task, e := f.store.GetAnnotationTask(f.ctx, f.task.ID)
|
||
if e != nil || task == nil {
|
||
t.Fatal(e)
|
||
}
|
||
task.CurrentRevision = item.CurrentRevision
|
||
task.Status = domain.AnnotationTaskStatusPendingQuality
|
||
if e := f.store.UpsertAnnotationTask(f.ctx, *task); e != nil {
|
||
t.Fatal(e)
|
||
}
|
||
}
|
||
|
||
func reworkContractFixture(t *testing.T) combinedDraftFixture {
|
||
t.Helper()
|
||
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)
|
||
}
|
||
return f
|
||
}
|
||
|
||
func TestReworkRejectWithOnlyUnresolvedPriorOpinions(t *testing.T) {
|
||
for _, legacyConfirmations := range []bool{false, true} {
|
||
t.Run(map[bool]string{false: "explicit-unresolved", true: "legacy-missing-verdict"}[legacyConfirmations], func(t *testing.T) {
|
||
f := reworkContractFixture(t)
|
||
f.item.ReviewRound = 0
|
||
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
reworkRejectRound(t, f, "first-return", []map[string]any{{"id": "prior", "scope": "slice", "sliceId": "left", "text": "原问题"}}, nil)
|
||
for _, operation := range []string{"second-return", "third-return"} {
|
||
reworkAdvanceToNextReview(t, f)
|
||
var confirmations []map[string]any
|
||
if !legacyConfirmations {
|
||
confirmations = []map[string]any{{"opinionId": "prior", "resolved": false, "note": "仍需修正"}}
|
||
}
|
||
reworkRejectRound(t, f, operation, []map[string]any{}, confirmations)
|
||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
if item.AssignedUserID != f.annotator.ID || item.CurrentStageID != domain.FanyuV58StageAnnotationCombined || item.Status != domain.HumanItemClaimed {
|
||
t.Fatalf("repeat return lost original annotator: %+v", item)
|
||
}
|
||
w := f.request(http.MethodGet, "/api/human-items?pool=mine&annotationView=active&projectId="+item.ProjectID, f.annotator, nil)
|
||
var pool struct {
|
||
Items []domain.HumanItemRecord `json:"items"`
|
||
}
|
||
json.Unmarshal(w.Body.Bytes(), &pool)
|
||
if w.Code != 200 || len(pool.Items) != 1 || pool.Items[0].ID != item.ID {
|
||
t.Fatalf("returned item missing from active pool: %d %s", w.Code, w.Body.String())
|
||
}
|
||
baseline, _ := f.store.GetReworkBaseline(f.ctx, store.ReworkBaselineID(item.ID, item.ReviewRound))
|
||
if baseline == nil || len(baseline.Problems) != 1 || baseline.Problems[0].ID != "prior" {
|
||
t.Fatalf("old opinion identity lost: %+v", baseline)
|
||
}
|
||
reviews, _ := f.store.ListAnnotationReviews(f.ctx, f.item.OrganizationID, f.task.ID)
|
||
var latest *domain.AnnotationReviewRecord
|
||
for i := range reviews {
|
||
if reviews[i].ReviewRound == item.ReviewRound-1 {
|
||
latest = &reviews[i]
|
||
}
|
||
}
|
||
if latest == nil || !strings.Contains(latest.Comment, "原问题") || len(latest.ProblemSliceIDs) != 1 || latest.ProblemSliceIDs[0] != "left" {
|
||
t.Fatalf("formal rejection omitted prior problem: %+v", latest)
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func TestReworkRejectDoesNotCarryResolvedOrTaskScopeOpinions(t *testing.T) {
|
||
for _, scope := range []string{"slice", "task"} {
|
||
t.Run(scope, func(t *testing.T) {
|
||
f := reworkContractFixture(t)
|
||
if err := f.store.UpsertReworkBaseline(f.ctx, domain.ReworkBaselineRecord{ID: store.ReworkBaselineID(f.item.ID, f.item.ReviewRound), ItemID: f.item.ID, Round: f.item.ReviewRound, Problems: []domain.ReviewProblem{{ID: "old", Scope: scope, SliceID: "left", Text: "旧意见"}}}); err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
version, headers := openReviewDraftSession(t, f)
|
||
save := reviewSessionRequest(f, http.MethodPut, "/api/human-items/"+f.item.ID+"/review-draft", f.reviewer, map[string]any{"version": 0, "resultVersion": version, "operationId": "resolved-draft", "modelVersion": 2, "problems": []map[string]any{}, "confirmations": []map[string]any{{"opinionId": "old", "resolved": scope != "task", "note": "历史意见"}}}, headers)
|
||
if save.Code != 200 {
|
||
t.Fatalf("save %d %s", save.Code, save.Body.String())
|
||
}
|
||
w := reviewSessionRequest(f, http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/reviews", f.reviewer, map[string]any{"stage": "quality", "decision": "rejected", "reasonCodes": []string{"manual_review_reject"}, "operationId": "empty-reject", "draftVersion": 1, "resultVersion": version}, headers)
|
||
if w.Code != http.StatusBadRequest {
|
||
t.Fatalf("empty effective rejection accepted: %d %s", w.Code, w.Body.String())
|
||
}
|
||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
if item.ReviewRound != f.item.ReviewRound || item.AssignedUserID != f.item.AssignedUserID {
|
||
t.Fatal("failed decision changed task")
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
func readBaselineProblems(t *testing.T, f combinedDraftFixture, rawRound string) (int, []domain.ReviewProblem) {
|
||
t.Helper()
|
||
endpoint := "/api/human-items/" + f.item.ID + "/rework-baseline"
|
||
if rawRound != "" {
|
||
endpoint += "?round=" + rawRound
|
||
}
|
||
w := f.request(http.MethodGet, endpoint, f.annotator, nil)
|
||
if w.Code != 200 {
|
||
t.Fatalf("baseline read %q: %d %s", rawRound, w.Code, w.Body.String())
|
||
}
|
||
var body struct {
|
||
Baseline *domain.ReworkBaselineRecord `json:"baseline"`
|
||
Round int `json:"round"`
|
||
}
|
||
json.Unmarshal(w.Body.Bytes(), &body)
|
||
if body.Baseline == nil {
|
||
return body.Round, nil
|
||
}
|
||
return body.Baseline.Round, body.Baseline.Problems
|
||
}
|
||
|
||
func problemIDs(problems []domain.ReviewProblem) []string {
|
||
ids := make([]string, 0, len(problems))
|
||
for _, p := range problems {
|
||
ids = append(ids, p.ID)
|
||
}
|
||
return ids
|
||
}
|
||
|
||
func TestReworkReturnBaselineUnresolvedOnlyAcrossRounds(t *testing.T) {
|
||
f := reworkContractFixture(t)
|
||
// round-1 退回:三条意见 → 基线(fixture 轮次 2)= 3 条。
|
||
reworkRejectRound(t, f, "contract-reject-1", []map[string]any{
|
||
{"id": "op-a", "scope": "slice", "sliceId": "left", "text": "甲意见"},
|
||
{"id": "op-b", "scope": "action", "sliceId": "left", "actionId": "left-action", "text": "乙意见", "atSeconds": 1},
|
||
{"id": "op-c", "scope": "slice", "sliceId": "right", "text": "丙意见"},
|
||
}, nil)
|
||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
if item.ReviewRound != 2 {
|
||
t.Fatalf("round-1 rejection must advance ReviewRound to 2: %d", item.ReviewRound)
|
||
}
|
||
frozen, _ := f.store.GetReworkBaseline(f.ctx, store.ReworkBaselineID(item.ID, 2))
|
||
if frozen == nil || len(frozen.Problems) != 3 {
|
||
t.Fatalf("round-1 baseline must freeze 3 opinions: %+v", frozen)
|
||
}
|
||
|
||
// 标注员返工送检 → round-2 复审:op-a/op-b 判已解决,op-c 判"仍有问题"(含
|
||
// 质检备注),另写两条新意见 → 再退回。
|
||
reworkAdvanceToNextReview(t, f)
|
||
reworkRejectRound(t, f, "contract-reject-2", []map[string]any{
|
||
{"id": "op-d", "scope": "slice", "sliceId": "left", "text": "丁意见"},
|
||
{"id": "op-e", "scope": "slice", "sliceId": "right", "text": "戊意见"},
|
||
}, []map[string]any{
|
||
{"opinionId": "op-a", "resolved": true, "note": "已核对修正"},
|
||
{"opinionId": "op-b", "resolved": true},
|
||
{"opinionId": "op-c", "resolved": false, "note": "改后区间仍多 2 秒"},
|
||
})
|
||
item, _ = f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
if item.ReviewRound != 3 {
|
||
t.Fatalf("round-2 rejection must advance ReviewRound to 3: %d", item.ReviewRound)
|
||
}
|
||
|
||
// 新基线(轮次 3)= 1 条未解决旧意见(同条目、备注并入)+ 2 条新增。
|
||
next, _ := f.store.GetReworkBaseline(f.ctx, store.ReworkBaselineID(item.ID, 3))
|
||
if next == nil {
|
||
t.Fatal("round-2 baseline missing")
|
||
}
|
||
if got, want := problemIDs(next.Problems), []string{"op-c", "op-d", "op-e"}; strings.Join(got, ",") != strings.Join(want, ",") {
|
||
t.Fatalf("round-2 baseline = %v, want %v(未解决在前、新增在后)", got, want)
|
||
}
|
||
if next.Problems[0].Text != "丙意见\n【质检备注】改后区间仍多 2 秒" {
|
||
t.Fatalf("仍有问题意见必须同条目并入备注(R2-Q7): %q", next.Problems[0].Text)
|
||
}
|
||
|
||
// 消费接口(标注员侧默认当前轮):字段不变,只收窄 problems 内容。
|
||
round, problems := readBaselineProblems(t, f, "")
|
||
if round != 3 || len(problems) != 3 || problems[0].ID != "op-c" {
|
||
t.Fatalf("current-round read: round=%d problems=%+v", round, problems)
|
||
}
|
||
|
||
// 历史轮次查询:已解决意见在任何基线查询接口都不再返回。
|
||
round, problems = readBaselineProblems(t, f, "2")
|
||
if round != 2 || len(problems) != 1 || problems[0].ID != "op-c" || problems[0].Text != "丙意见" {
|
||
t.Fatalf("historical round-2 read must hide resolved op-a/op-b: round=%d %+v", round, problems)
|
||
}
|
||
round, problems = readBaselineProblems(t, f, "1")
|
||
if round != 1 || problems != nil {
|
||
t.Fatalf("never-created round-1 baseline must stay null: round=%d %+v", round, problems)
|
||
}
|
||
|
||
// 冻结记录不动:round-2 基线在存储中仍保完整 3 条(审计轨迹,B02 保持)。
|
||
frozen, _ = f.store.GetReworkBaseline(f.ctx, store.ReworkBaselineID(item.ID, 2))
|
||
if frozen == nil || len(frozen.Problems) != 3 {
|
||
t.Fatalf("frozen baseline record must keep the full audit trail: %+v", frozen)
|
||
}
|
||
}
|
||
|
||
func TestReworkBaselineLegacyRoundsDefaultUnresolved(t *testing.T) {
|
||
f := reworkContractFixture(t)
|
||
// 旧多轮数据:直接冻结的 round-1 基线(无任何核对结论记录)。
|
||
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
item.ReviewRound = 1
|
||
item.Version++
|
||
if e := f.store.UpsertHumanItem(f.ctx, *item); e != nil {
|
||
t.Fatal(e)
|
||
}
|
||
if e := f.store.UpsertReworkBaseline(f.ctx, domain.ReworkBaselineRecord{
|
||
ID: store.ReworkBaselineID(item.ID, 1), OrganizationID: item.OrganizationID, ProjectID: item.ProjectID,
|
||
ItemID: item.ID, BatchID: item.BatchID, Round: 1, StageID: domain.FanyuV58StageAnnotationCombined,
|
||
Problems: []domain.ReviewProblem{
|
||
{ID: "lg-a", Scope: "slice", SliceID: "left", Text: "旧意见一"},
|
||
{ID: "lg-b", Scope: "task", Text: "旧意见二"},
|
||
},
|
||
}); e != nil {
|
||
t.Fatal(e)
|
||
}
|
||
// 旧客户端再退回:不送核对结论,只写新意见 → 兜底按未解决全量延续 + 新增。
|
||
reworkRejectRound(t, f, "legacy-reject", []map[string]any{
|
||
{"id": "lg-c", "scope": "slice", "sliceId": "left", "text": "新意见"},
|
||
}, nil)
|
||
item, _ = f.store.GetHumanItem(f.ctx, f.item.ID)
|
||
next, _ := f.store.GetReworkBaseline(f.ctx, store.ReworkBaselineID(item.ID, 2))
|
||
if next == nil {
|
||
t.Fatal("legacy rejection must still freeze a baseline")
|
||
}
|
||
if got, want := problemIDs(next.Problems), []string{"lg-a", "lg-b", "lg-c"}; strings.Join(got, ",") != strings.Join(want, ",") {
|
||
t.Fatalf("legacy fallback must carry all prior opinions unresolved: %v, want %v", got, want)
|
||
}
|
||
// 历史轮查询无核对结论 → 未解决意见照常返回;ADR 0009:整体意见(lg-b)仍隐藏。
|
||
if _, problems := readBaselineProblems(t, f, "1"); len(problems) != 1 || problems[0].ID != "lg-a" {
|
||
t.Fatalf("legacy round without confirmations must stay visible: %+v", problems)
|
||
}
|
||
}
|