forked from longtao.wu/droboticflow
60 lines
2.2 KiB
Go
60 lines
2.2 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// Undo/redo replays the explicit disposition endpoint, never forges a saved
|
|
// disposition through ordinary annotation writes. Verify persisted readback.
|
|
func TestDispositionHistoryUndoRedoThroughHTTP(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, f, "测试切片")
|
|
body := dispositionFixturePayload(t, f)
|
|
for i := range body.SliceSegments {
|
|
body.SliceSegments[i].Name = "切片 001"
|
|
}
|
|
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
|
|
for _, action := range []string{"discard", "keep", "discard", "keep"} {
|
|
body.Disposition = action
|
|
r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("%s: %d %s", action, r.Code, r.Body.String())
|
|
}
|
|
r = f.request(http.MethodGet, path, f.annotator, nil)
|
|
if r.Code != http.StatusOK {
|
|
t.Fatal(r.Body.String())
|
|
}
|
|
var detail struct {
|
|
HumanItem domain.HumanItemRecord `json:"humanItem"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &detail); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
workspace := annotationWorkspacePayload(&detail.HumanItem)
|
|
slices := decodeMaterializationValue[domain.SliceSegment](workspace["sliceSegments"])
|
|
segments := decodeMaterializationValue[domain.AnnotationSegment](workspace["segments"])
|
|
if len(slices) != 3 || slices[1].IsDiscarded() != (action == "discard") || slices[1].Start != 3 || slices[1].End != 7 {
|
|
t.Fatalf("incorrect persisted slices: %+v", slices)
|
|
}
|
|
if action == "discard" {
|
|
if len(segments) != 0 || len(slices[1].DiscardedAnnotations) != 1 || slices[1].DiscardReason != "相机被遮挡" {
|
|
t.Fatal("redo lost discard evidence")
|
|
}
|
|
} else if len(segments) != 1 || segments[0].Description != "old text" {
|
|
t.Fatal("undo lost previous description")
|
|
}
|
|
// Replaying a stale history write must not silently overwrite the new state.
|
|
stale := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body)
|
|
if stale.Code != http.StatusConflict {
|
|
t.Fatalf("stale replay: %d", stale.Code)
|
|
}
|
|
body.Version = detail.HumanItem.Version
|
|
body.SliceSegments = slices
|
|
body.Segments = segments
|
|
}
|
|
}
|