droboticflow/internal/api/annotation_contiguous_progress_acceptance_test.go

63 lines
2.4 KiB
Go

package api
import (
"encoding/json"
"net/http"
"testing"
"droboticflow/internal/domain"
)
func TestContinuousProgressPendingTextDoesNotConfirmAndMalformedHistoryIsNotClipped(t *testing.T) {
f := continuousFixture(t)
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID
payload := map[string]any{"version": f.item.Version, "sliceSegments": []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 10}}, "segments": []domain.AnnotationSegment{{ID: "unfinished", Start: 0, End: 10, Status: "pending", Description: "尚未确认的草稿"}}}
r := f.request(http.MethodPut, endpoint+"/drafts", f.annotator, payload)
if r.Code != 200 {
t.Fatalf("save pending text: %d %s", r.Code, r.Body.String())
}
read := func() (int, []domain.AnnotationSegment) {
t.Helper()
r := f.request(http.MethodGet, endpoint, f.annotator, nil)
var detail struct {
HumanItem struct {
Version int `json:"version"`
CurrentStageID string `json:"currentStageId"`
Subject struct {
Payload struct {
Draft struct {
Segments []domain.AnnotationSegment `json:"segments"`
} `json:"annotationDraft"`
} `json:"payload"`
} `json:"subject"`
} `json:"humanItem"`
}
if err := json.Unmarshal(r.Body.Bytes(), &detail); err != nil {
t.Fatal(err)
}
if r.Code != 200 || detail.HumanItem.CurrentStageID != domain.FanyuV58StageAnnotationCombined {
t.Fatalf("read progress advanced: %d %s", r.Code, r.Body.String())
}
return detail.HumanItem.Version, detail.HumanItem.Subject.Payload.Draft.Segments
}
version, segments := read()
if len(segments) != 1 || segments[0].Status != "pending" || segments[0].Description != "尚未确认的草稿" {
t.Fatalf("pending text confirmed or lost: %+v", segments)
}
for _, bad := range [][]domain.AnnotationSegment{
{{ID: "a", Start: -1, End: 10, Description: "outside"}},
{{ID: "a", Start: 0, End: 6, Description: "overlap"}, {ID: "b", Start: 5, End: 10, Description: "overlap"}},
} {
payload["version"] = version
payload["segments"] = bad
r = f.request(http.MethodPut, endpoint+"/drafts", f.annotator, payload)
if r.Code != 400 {
t.Fatalf("malformed saved rather than rejected: %d %s", r.Code, r.Body.String())
}
after, current := read()
if after != version || len(current) != 1 || current[0].ID != "unfinished" || current[0].Status != "pending" {
t.Fatalf("rejection changed authoritative draft: v%d %+v", after, current)
}
}
}