forked from longtao.wu/droboticflow
83 lines
4.1 KiB
Go
83 lines
4.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func TestInvalidSliceConsolidationPreservesActionsAndAudit(t *testing.T) {
|
|
slices := []domain.SliceSegment{{ID: "a", Name: "收银", Start: 0, End: 2, StartTimestampUS: 100}, {ID: "b", Name: "收银", Start: 2, End: 3}, {ID: "c", Name: "收银", Start: 3, End: 5, EndTimestampUS: 900}, {ID: "d", Name: "其他", Start: 5, End: 9}}
|
|
actions := []domain.AnnotationSegment{{ID: "x", Start: 0, End: 2, Description: "拿起商品", Phrases: []string{"细节"}}, {ID: "y", Start: 2, End: 3, IsInvalid: true, Description: "无效片段", Status: "invalid", StartTimestampUS: 200, EndTimestampUS: 300}, {ID: "z", Start: 3, End: 5, Description: "放下商品"}}
|
|
before, _ := json.Marshal(actions)
|
|
original, _ := json.Marshal(slices)
|
|
got, ops, err := consolidateInvalidActionSlices(slices, actions, []string{"b"})
|
|
if err != nil || len(got) != 2 || got[0].Start != 0 || got[0].End != 5 || got[0].Name != "收银" || got[0].StartTimestampUS != 100 || got[0].EndTimestampUS != 900 {
|
|
t.Fatalf("%+v %v", got, err)
|
|
}
|
|
after, _ := json.Marshal(actions)
|
|
if string(before) != string(after) {
|
|
t.Fatal("action JSON changed")
|
|
}
|
|
unchanged, _ := json.Marshal(slices)
|
|
if string(original) != string(unchanged) {
|
|
t.Fatal("input slices mutated")
|
|
}
|
|
if !reflect.DeepEqual(ops[0].Before, slices[:3]) || ops[0].Type != "consolidate_invalid_actions" {
|
|
t.Fatal("audit lost")
|
|
}
|
|
if !reflect.DeepEqual(got[1], slices[3]) {
|
|
t.Fatal("unrelated slice changed")
|
|
}
|
|
kept, ops, err := consolidateInvalidActionSlices(slices, actions, nil)
|
|
if err != nil || len(ops) != 0 || !reflect.DeepEqual(kept, slices) {
|
|
t.Fatal("implicit consolidation")
|
|
}
|
|
for name, change := range map[string]func([]domain.SliceSegment, []domain.AnnotationSegment){
|
|
"discard": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { s[1].DeliveryDisposition = "discard" },
|
|
"different-name": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { s[2].Name = "盘点" },
|
|
"gap": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { s[2].Start = 3.1 },
|
|
"valid-action": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { a[1].IsInvalid = false },
|
|
"partial-invalid": func(s []domain.SliceSegment, a []domain.AnnotationSegment) { a[1].End = 2.5 },
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
s := append([]domain.SliceSegment(nil), slices...)
|
|
a := append([]domain.AnnotationSegment(nil), actions...)
|
|
change(s, a)
|
|
if _, _, err := consolidateInvalidActionSlices(s, a, []string{"b"}); err == nil {
|
|
t.Fatal("unsafe consolidation accepted")
|
|
}
|
|
})
|
|
}
|
|
if _, _, err = consolidateInvalidActionSlices(slices, actions, []string{"b", "b"}); err == nil {
|
|
t.Fatal("duplicate accepted")
|
|
}
|
|
}
|
|
|
|
func TestInvalidSliceConsolidationOverlappingCandidates(t *testing.T) {
|
|
var ss []domain.SliceSegment
|
|
var aa []domain.AnnotationSegment
|
|
for i, id := range []string{"a", "b", "c", "d", "e"} {
|
|
ss = append(ss, domain.SliceSegment{ID: id, Name: "盘点", Start: float64(i), End: float64(i + 1)})
|
|
aa = append(aa, domain.AnnotationSegment{ID: id, Start: float64(i), End: float64(i + 1), IsInvalid: i == 1 || i == 3})
|
|
}
|
|
got, ops, err := consolidateInvalidActionSlices(ss, aa, []string{"b", "d"})
|
|
if err != nil || len(got) != 1 || got[0].End != 5 || len(ops) != 1 || len(ops[0].Before) != 5 {
|
|
t.Fatalf("%+v %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestInvalidSliceConsolidationRejectsStaleSelectionBeforeReservation(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/combined-submissions", f.annotator, map[string]any{"version": f.item.Version, "sliceSegments": []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 10}}, "segments": []domain.AnnotationSegment{{ID: "a", Start: 0, End: 10, Description: "动作"}}, "consolidateInvalidSliceIds": []string{"missing"}})
|
|
if r.Code != 400 {
|
|
t.Fatalf("%d %s", r.Code, r.Body.String())
|
|
}
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if item.Version != f.item.Version || item.PendingCombinedSubmissionID != "" {
|
|
t.Fatal("invalid request mutated task")
|
|
}
|
|
}
|