forked from longtao.wu/droboticflow
78 lines
3.1 KiB
Go
78 lines
3.1 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestDiscardAllowsUnfinishedKeptNamesUntilConfirmation(t *testing.T) {
|
|
for _, invalidKept := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "discard default name", true: "save with unfinished kept name"}[invalidKept], func(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, f, "测试切片")
|
|
body := dispositionFixturePayload(t, f)
|
|
for i := range body.SliceSegments {
|
|
body.SliceSegments[i].Name = "测试切片"
|
|
}
|
|
body.SliceSegments[1].Name = "切片 001"
|
|
if invalidKept {
|
|
body.SliceSegments[0].Name = "切片 002"
|
|
}
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("discard default name: %d %s", r.Code, r.Body.String())
|
|
}
|
|
var got struct {
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !got.Workspace.SliceSegments[1].IsDiscarded() || got.Workspace.SliceSegments[1].Name != "" {
|
|
t.Fatal("discard must clear the ordinary slice name")
|
|
}
|
|
r = f.request("GET", "/api/plugins/annotation-qa/tasks/"+f.task.ID, f.annotator, nil)
|
|
if r.Code != 200 {
|
|
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)
|
|
}
|
|
saved := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(&detail.HumanItem)["sliceSegments"])
|
|
if len(saved) != 3 || !saved[1].IsDiscarded() || saved[1].DiscardReason != "相机被遮挡" || saved[1].Name != "" {
|
|
t.Fatal("discard not retained on readback")
|
|
}
|
|
server := &server{store: f.store}
|
|
if issues := server.validateProjectSliceNames(f.ctx, &got.Item, got.Workspace.SliceSegments, "confirm"); (len(issues) > 0) != invalidKept {
|
|
t.Fatalf("confirmation must reject unfinished kept names only: %v", issues)
|
|
}
|
|
if invalidKept && saved[0].Name != "切片 002" {
|
|
t.Fatal("discard changed another slice's draft name")
|
|
}
|
|
restore := body
|
|
restore.Version = got.Item.Version
|
|
restore.SliceSegments = got.Workspace.SliceSegments
|
|
restore.Segments = got.Workspace.Segments
|
|
restore.Disposition = "keep"
|
|
r = f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, restore)
|
|
if r.Code != 200 {
|
|
t.Fatalf("restore unfinished names: %d %s", r.Code, r.Body.String())
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.Workspace.SliceSegments[1].IsDiscarded() || got.Workspace.SliceSegments[1].Name != "" {
|
|
t.Fatal("ordinary restore must return an unnamed kept slice")
|
|
}
|
|
if issues := server.validateProjectSliceNames(f.ctx, &got.Item, got.Workspace.SliceSegments, "confirm"); len(issues) == 0 {
|
|
t.Fatal("restored slice skipped name validation")
|
|
}
|
|
})
|
|
}
|
|
}
|