forked from longtao.wu/droboticflow
152 lines
7.0 KiB
Go
152 lines
7.0 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// History uses the normal confirmation endpoint. Observe restoration and
|
|
// rejected writes through GET, without a separate undo API or store assertions.
|
|
func TestAnnotationHistoryRestoresSavedDescriptionsThroughHTTP(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, f, "前段")
|
|
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)
|
|
}
|
|
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
|
|
read := func() domain.HumanItemRecord {
|
|
t.Helper()
|
|
r := f.request(http.MethodGet, path, f.admin, nil)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("GET %d: %s", r.Code, r.Body.String())
|
|
}
|
|
var response struct {
|
|
HumanItem domain.HumanItemRecord `json:"humanItem"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return response.HumanItem
|
|
}
|
|
payload := func(version int, descriptions []domain.AnnotationSegment) map[string]any {
|
|
return map[string]any{"version": version, "sliceSegments": []map[string]any{{"id": "slice-a", "name": "前段", "start": 0, "end": 10}}, "segments": descriptions}
|
|
}
|
|
saved := []domain.AnnotationSegment{{ID: "action", Start: 2, End: 4, Description: "拿起商品"}}
|
|
version := read().Version
|
|
for _, descriptions := range [][]domain.AnnotationSegment{saved, {}, saved} {
|
|
r := f.request(http.MethodPost, path+"/annotation-confirmations", f.annotator, payload(version, descriptions))
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("POST %d: %s", r.Code, r.Body.String())
|
|
}
|
|
current := read()
|
|
actual := decodeMaterializationValue[domain.AnnotationSegment](mapValue(current.Subject.Payload["annotationWorkspace"])["segments"])
|
|
if current.Version != version+1 || len(actual) != len(descriptions) {
|
|
t.Fatalf("restoration: version=%d descriptions=%+v", current.Version, actual)
|
|
}
|
|
if len(actual) > 0 && (actual[0].Start != 2 || actual[0].End != 4 || actual[0].Description != "拿起商品") {
|
|
t.Fatalf("description changed: %+v", actual)
|
|
}
|
|
version = current.Version
|
|
}
|
|
// A stale undo cannot erase newer saved content.
|
|
r := f.request(http.MethodPost, path+"/annotation-confirmations", f.annotator, payload(version-1, []domain.AnnotationSegment{}))
|
|
if r.Code != http.StatusConflict {
|
|
t.Fatalf("stale undo %d: %s", r.Code, r.Body.String())
|
|
}
|
|
if read().Version != version {
|
|
t.Fatal("stale undo advanced version")
|
|
}
|
|
for _, scenario := range []string{"ownership", "processing"} {
|
|
t.Run(scenario, func(t *testing.T) {
|
|
current := read()
|
|
if scenario == "ownership" {
|
|
current.AssignedUserID = f.admin.ID
|
|
} else {
|
|
current.AssignedUserID = f.annotator.ID
|
|
current.PendingCombinedSubmissionID = fanyuV5CombinedSubmissionID(current.ID, current.Version-1)
|
|
current.PendingCombinedPayloadSHA = "history-test-payload"
|
|
current.ProcessingJob = &domain.AnnotationProcessingJob{ID: current.PendingCombinedSubmissionID, TaskID: f.task.ID, Kind: "submit", Status: "queued"}
|
|
}
|
|
if err := f.store.UpsertHumanItem(f.ctx, current); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rejected := f.request(http.MethodPost, path+"/annotation-confirmations", f.annotator, payload(version, []domain.AnnotationSegment{}))
|
|
expectedCode := "human_item_claim_required"
|
|
if scenario == "processing" {
|
|
expectedCode = "combined_submission_in_progress"
|
|
}
|
|
if rejected.Code != http.StatusConflict || !bytes.Contains(rejected.Body.Bytes(), []byte(expectedCode)) {
|
|
t.Fatalf("rejected undo %d: %s", rejected.Code, rejected.Body.String())
|
|
}
|
|
latest := read()
|
|
actual := decodeMaterializationValue[domain.AnnotationSegment](mapValue(latest.Subject.Payload["annotationWorkspace"])["segments"])
|
|
if latest.Version != version || len(actual) != 1 || actual[0].Description != "拿起商品" {
|
|
t.Fatalf("rejected undo changed state: %+v", latest)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAnnotationHistoryFirstSaveRestoresRevisionBackedDescriptionsThroughHTTP(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, f, "前段")
|
|
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)
|
|
}
|
|
original := []domain.AnnotationSegment{{ID: "revision-action", Start: 2, End: 4, Description: "原始动作", Source: "manual"}}
|
|
slices := []domain.SliceSegment{{ID: "slice-a", Name: "前段", Start: 0, End: 10}}
|
|
if err := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "history-revision", OrganizationID: item.OrganizationID, TaskID: f.task.ID, Revision: 1, AuthorUserID: f.annotator.ID, Segments: original}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertSliceRevision(f.ctx, domain.SliceRevisionRecord{ID: "history-slice-revision", OrganizationID: item.OrganizationID, TaskID: f.task.ID, Revision: 1, AuthorUserID: f.annotator.ID, Segments: slices}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
|
|
type taskResponse struct {
|
|
HumanItem domain.HumanItemRecord `json:"humanItem"`
|
|
Revisions []domain.AnnotationRevisionRecord `json:"revisions"`
|
|
SliceRevisions []domain.SliceRevisionRecord `json:"sliceRevisions"`
|
|
}
|
|
read := func() taskResponse {
|
|
t.Helper()
|
|
r := f.request(http.MethodGet, path, f.annotator, nil)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("GET %d: %s", r.Code, r.Body.String())
|
|
}
|
|
var response taskResponse
|
|
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return response
|
|
}
|
|
loaded := read()
|
|
if loaded.HumanItem.Subject.Payload["annotationWorkspace"] != nil || len(loaded.Revisions) != 1 || len(loaded.SliceRevisions) != 1 {
|
|
t.Fatalf("revision-backed load missing: %+v", loaded)
|
|
}
|
|
confirmed := []domain.AnnotationSegment{{ID: "revision-action", Start: 2, End: 3, Description: "原始动作", Source: "manual"}, {ID: "new-child", Start: 3, End: 4, Description: "分割后的动作", Source: "manual"}}
|
|
version := loaded.HumanItem.Version
|
|
for _, descriptions := range [][]domain.AnnotationSegment{confirmed, loaded.Revisions[0].Segments} {
|
|
r := f.request(http.MethodPost, path+"/annotation-confirmations", f.annotator, map[string]any{"version": version, "sliceSegments": loaded.SliceRevisions[0].Segments, "segments": descriptions})
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("POST %d: %s", r.Code, r.Body.String())
|
|
}
|
|
latest := read()
|
|
if latest.HumanItem.Version != version+1 {
|
|
t.Fatalf("version=%d", latest.HumanItem.Version)
|
|
}
|
|
version = latest.HumanItem.Version
|
|
}
|
|
restored := decodeMaterializationValue[domain.AnnotationSegment](mapValue(read().HumanItem.Subject.Payload["annotationWorkspace"])["segments"])
|
|
if len(restored) != 1 || restored[0].ID != "revision-action" || restored[0].Start != 2 || restored[0].End != 4 || restored[0].Description != "原始动作" {
|
|
t.Fatalf("first save undo did not restore original revision: %+v", restored)
|
|
}
|
|
}
|