192 lines
8.2 KiB
Go
192 lines
8.2 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
)
|
|
|
|
func continuousFixture(t *testing.T) combinedDraftFixture {
|
|
f := newCombinedDraftFixture(t)
|
|
b, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
b.ProcessingProfile = domain.ProcessingProfileRawHuman
|
|
if err := f.store.UpsertHumanBatch(f.ctx, *b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.task.ProcessingProfile = domain.ProcessingProfileRawHuman
|
|
if err := f.store.UpsertAnnotationTask(f.ctx, f.task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedSliceDictionary(t, f, "测试切片")
|
|
return f
|
|
}
|
|
|
|
func TestContinuousActionsSavePendingProgressAndReadBack(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
r := f.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/drafts", f.annotator, map[string]any{
|
|
"version": f.item.Version,
|
|
"sliceSegments": []map[string]any{{"id": "s", "name": "测试切片", "start": 0, "end": 10}},
|
|
"segments": []map[string]any{{"id": "a", "start": 0, "end": 4, "status": "pending"}, {"id": "b", "start": 4, "end": 10, "status": "described", "description": "拿取"}},
|
|
})
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("save: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID, f.annotator, nil)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("read: %d %s", r.Code, r.Body.String())
|
|
}
|
|
var response struct {
|
|
Task struct {
|
|
ContinuousActions bool `json:"continuousActions"`
|
|
} `json:"task"`
|
|
HumanItem struct {
|
|
Subject struct {
|
|
Payload struct {
|
|
Draft struct {
|
|
Segments []struct {
|
|
Status string `json:"status"`
|
|
} `json:"segments"`
|
|
} `json:"annotationDraft"`
|
|
} `json:"payload"`
|
|
} `json:"subject"`
|
|
CurrentStageID string `json:"currentStageId"`
|
|
} `json:"humanItem"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !response.Task.ContinuousActions || len(response.HumanItem.Subject.Payload.Draft.Segments) != 2 || response.HumanItem.Subject.Payload.Draft.Segments[0].Status != "pending" {
|
|
t.Fatalf("pending partition / scope missing: %s", r.Body.String())
|
|
}
|
|
if response.HumanItem.CurrentStageID != domain.FanyuV58StageAnnotationCombined {
|
|
t.Fatal("saving progress submitted task")
|
|
}
|
|
}
|
|
|
|
func TestContinuousActionsRejectUnconfirmedTextSubmissionWithoutClientFlag(t *testing.T) {
|
|
for name, segments := range map[string][]map[string]any{
|
|
"pending": {{"id": "a", "start": 0, "end": 10, "description": "未确认文字", "status": "pending"}},
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/combined-submissions", f.annotator, map[string]any{"version": f.item.Version, "sliceSegments": []map[string]any{{"id": "s", "name": "测试切片", "start": 0, "end": 10}}, "segments": segments})
|
|
if r.Code != http.StatusBadRequest {
|
|
t.Fatalf("unconfirmed text must reject before media work: %d %s", r.Code, r.Body.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestContinuousActionsConfirmedInvalidCanReachReview(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
seedCompletedMaterialization(t, f, 1)
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/combined-submissions", f.annotator, map[string]any{"version": f.item.Version, "sliceSegments": []map[string]any{{"id": "s", "name": "测试切片", "start": 0, "end": 10}}, "segments": []map[string]any{{"id": "a", "start": 0, "end": 10, "status": "invalid", "isInvalid": true}}})
|
|
if r.Code != http.StatusCreated {
|
|
t.Fatalf("confirmed invalid should reach quality: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID, f.annotator, nil)
|
|
var response struct {
|
|
Revisions []struct {
|
|
Segments []domain.AnnotationSegment `json:"segments"`
|
|
} `json:"revisions"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(response.Revisions) != 1 || len(response.Revisions[0].Segments) != 1 || !response.Revisions[0].Segments[0].IsInvalid {
|
|
t.Fatalf("invalid result lost: %s", r.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestContinuousActionsSaveProgressFillsLegacyGapsAndProtectsVersion(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
item := f.item
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
payload := map[string]any{"version": item.Version, "sliceSegments": []map[string]any{{"id": "s", "name": "测试切片", "start": 0, "end": 10}}, "segments": []map[string]any{{"id": "old", "start": 2, "end": 4, "description": "历史动作"}}}
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/annotation-confirmations"
|
|
r := f.request(http.MethodPost, endpoint, f.annotator, payload)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("save progress: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID, f.annotator, nil)
|
|
var response struct {
|
|
HumanItem struct {
|
|
Subject struct {
|
|
Payload struct {
|
|
Workspace struct {
|
|
Segments []domain.AnnotationSegment `json:"segments"`
|
|
} `json:"annotationWorkspace"`
|
|
} `json:"payload"`
|
|
} `json:"subject"`
|
|
} `json:"humanItem"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
segments := response.HumanItem.Subject.Payload.Workspace.Segments
|
|
if len(segments) != 3 || segments[0].Start != 0 || segments[0].End != 2 || segments[0].Status != "pending" || segments[1].ID != "old" || segments[1].Description != "历史动作" || segments[2].Start != 4 || segments[2].End != 10 || segments[2].Status != "pending" {
|
|
t.Fatalf("legacy gaps: %+v", segments)
|
|
}
|
|
r = f.request(http.MethodPost, endpoint, f.annotator, payload)
|
|
if r.Code != http.StatusConflict {
|
|
t.Fatalf("stale progress overwrote: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestContinuousActionsRejectContradictoryAndUnknownJudgments(t *testing.T) {
|
|
for _, segment := range []map[string]any{
|
|
{"id": "a", "start": 0, "end": 10, "status": "mystery", "description": "test"},
|
|
{"id": "a", "start": 0, "end": 10, "status": "described"},
|
|
{"id": "a", "start": 0, "end": 10, "status": "pending", "isInvalid": true},
|
|
{"id": "a", "start": 0, "end": 10, "status": "invalid"},
|
|
} {
|
|
f := continuousFixture(t)
|
|
r := f.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/drafts", f.annotator, map[string]any{"version": f.item.Version, "sliceSegments": []map[string]any{{"id": "s", "start": 0, "end": 10}}, "segments": []map[string]any{segment}})
|
|
if r.Code != http.StatusBadRequest {
|
|
t.Fatalf("contradictory judgment: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestContinuousActionsLegacyEndpointCannotBypassCombinedGate(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/revisions", f.annotator, map[string]any{"segments": []any{}})
|
|
if r.Code != http.StatusConflict {
|
|
t.Fatalf("legacy stage bypass: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestContinuousActionsSubmittedTaskCannotBeRewrittenByProgressSave(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
item := f.item
|
|
item.CurrentStageID = "quality_review"
|
|
item.Status = domain.HumanItemSubmitted
|
|
item.LatestAuthorUserID = f.annotator.ID
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := f.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/drafts", f.annotator, map[string]any{"version": f.item.Version, "sliceSegments": []map[string]any{{"id": "s", "start": 0, "end": 10}}, "segments": []any{}})
|
|
if r.Code != http.StatusConflict {
|
|
t.Fatalf("submitted progress mutation: %d %s", r.Code, r.Body.String())
|
|
}
|
|
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID, f.annotator, nil)
|
|
var response struct {
|
|
HumanItem struct {
|
|
Version int `json:"version"`
|
|
Subject struct {
|
|
Payload map[string]any `json:"payload"`
|
|
} `json:"subject"`
|
|
} `json:"humanItem"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.HumanItem.Version != item.Version || response.HumanItem.Subject.Payload["annotationDraft"] != nil {
|
|
t.Fatalf("read or rejected save rewrote old task: %s", r.Body.String())
|
|
}
|
|
}
|