droboticflow/internal/api/discard_reason_dictionary_test.go

199 lines
7.2 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"encoding/json"
"go.mongodb.org/mongo-driver/bson"
"strings"
"testing"
)
func TestDiscardReasonDictionaryValidationAndSnapshots(t *testing.T) {
for _, mode := range []string{"missing", "disabled", "wrong_kind", "foreign", "old_version", "other_without_note", "success"} {
t.Run(mode, func(t *testing.T) {
f := randomClaimFixture(t, 0)
body := dispositionFixturePayload(t, f)
label, _ := f.store.GetAnnotationLabel(f.ctx, body.ReasonLabelID)
switch mode {
case "missing":
body.ReasonLabelID = "unknown"
case "disabled":
label.Status = domain.AnnotationLabelInactive
case "wrong_kind":
label.Kind = "slice"
case "foreign":
label.OrganizationID = "other"
case "old_version":
body.ReasonVersion = 0
case "other_without_note":
label.RequiresNote = true
case "success":
label.Tags = []string{"不可用"}
body.Reason = "forged"
body.ReasonNote = "遮挡持续两秒"
}
if err := f.store.UpsertAnnotationLabel(f.ctx, *label); err != nil {
t.Fatal(err)
}
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
if mode != "success" {
if r.Code < 400 {
t.Fatalf("accepted %s", mode)
}
return
}
if r.Code != 200 {
t.Fatalf("%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)
}
slice := got.Workspace.SliceSegments[1]
if slice.DiscardReasonDictionary == nil || len(slice.DiscardReasonDictionary.Tags) != 1 || slice.DiscardReasonDictionary.Tags[0] != "不可用" {
t.Fatal("discard tags missing")
}
if slice.DiscardReasonName != "相机被遮挡" || slice.DiscardReasonVersion != 1 || slice.DiscardReasonLabelID != label.ID || slice.DiscardReasonNote != body.ReasonNote || strings.Contains(slice.DiscardReason, "forged") {
t.Fatalf("snapshot %+v", slice)
}
label.Name = "改名之后"
label.Version = 2
label.Status = domain.AnnotationLabelInactive
if err := f.store.UpsertAnnotationLabel(f.ctx, *label); err != nil {
t.Fatal(err)
}
proposed := append([]domain.SliceSegment{}, got.Workspace.SliceSegments...)
proposed[1].DiscardReasonName = "forged"
proposed[1].DiscardReasonVersion = 99
canonical, issues := canonicalDispositions(got.Workspace.SliceSegments, proposed)
if len(issues) > 0 || canonical[1].DiscardReasonName != "相机被遮挡" || canonical[1].DiscardReasonVersion != 1 {
t.Fatal("historical snapshot overwritten")
}
body.Version = got.Item.Version
body.SliceSegments = got.Workspace.SliceSegments
body.Segments = got.Workspace.Segments
body.Disposition = "keep"
body.ReasonLabelID = ""
body.ReasonVersion = 0
r = f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
if r.Code != 200 {
t.Fatalf("restore depends on dictionary: %s", r.Body.String())
}
})
}
}
func TestDiscardReasonProjectIsolationAndIndependentSelections(t *testing.T) {
f := randomClaimFixture(t, 0)
dispositionFixturePayload(t, f)
base := "/api/projects/" + f.task.ProjectID
for _, kind := range []string{"action", "slice", "discard_reason"} {
body := map[string]any{"kind": kind, "code": "code-" + kind, "name": "同名原因", "requiresNote": true}
r := f.request("POST", "/api/annotation-labels", f.admin, body)
if r.Code != 201 {
t.Fatalf("create %s: %s", kind, r.Body.String())
}
var label domain.AnnotationLabelRecord
json.Unmarshal(r.Body.Bytes(), &label)
suffix := "annotation-labels"
if kind == "slice" {
suffix = "slice-labels"
}
if kind == "discard_reason" {
suffix = "discard-reason-labels"
}
r = f.request("PUT", base+"/"+suffix, f.admin, map[string]any{"labelIds": []string{label.ID}})
if r.Code != 200 {
t.Fatal(r.Body.String())
}
body["code"] = "duplicate-" + kind
if r = f.request("POST", "/api/annotation-labels", f.admin, body); r.Code != 409 {
t.Fatalf("duplicate accepted %s", kind)
}
}
selection, e := f.store.GetProjectAnnotationLabels(f.ctx, f.task.ProjectID)
if e != nil || len(selection.LabelIDs) != 1 || len(selection.SliceLabelIDs) != 1 || len(selection.DiscardReasonLabelIDs) != 1 {
t.Fatalf("lost selection %+v %v", selection, e)
}
r := f.request("GET", base+"/discard-reason-labels", f.annotator, nil)
if r.Code != 200 || !strings.Contains(r.Body.String(), "同名原因") {
t.Fatalf("project read %s", r.Body.String())
}
if r = f.request("GET", "/api/annotation-labels?kind=discard_reason", f.annotator, nil); r.Code != 403 {
t.Fatal("organization directory exposed")
}
if r = f.request("PUT", base+"/discard-reason-labels", f.annotator, map[string]any{"labelIds": []string{}}); r.Code != 403 {
t.Fatal("annotator modified dictionary")
}
}
func TestDiscardReasonLegacyNameCompatibility(t *testing.T) {
f := randomClaimFixture(t, 0)
body := dispositionFixturePayload(t, f)
for i := range body.SliceSegments {
body.SliceSegments[i].Name = "历史自由名称"
}
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": body.SliceSegments}
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
t.Fatal(err)
}
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
if r.Code != 200 {
t.Fatalf("unchanged historical names blocked: %s", r.Body.String())
}
}
// Mongo decodes nested arrays as bson.A; exercise both HTTP operations with
// the same decode shape instead of relying only on MemoryStore's JSON clone.
type bsonDispositionStore struct{ store.Store }
func (s bsonDispositionStore) GetHumanItem(ctx context.Context, id string) (*domain.HumanItemRecord, error) {
item, err := s.Store.GetHumanItem(ctx, id)
if err != nil || item == nil {
return item, err
}
raw, err := bson.Marshal(item)
if err != nil {
return nil, err
}
var result domain.HumanItemRecord
err = bson.Unmarshal(raw, &result)
return &result, err
}
func TestDiscardReasonBSONHistorySurvivesRestore(t *testing.T) {
f := randomClaimFixture(t, 0)
f.handler = NewHandler(bsonDispositionStore{Store: f.store}, nil)
body := dispositionFixturePayload(t, f)
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/slice-dispositions"
r := f.request("POST", path, f.annotator, body)
if r.Code != 200 {
t.Fatal(r.Body.String())
}
var got struct {
Item domain.HumanItemRecord `json:"item"`
Workspace combinedAnnotationDraftInput `json:"workspace"`
}
json.Unmarshal(r.Body.Bytes(), &got)
body.Version = got.Item.Version
body.SliceSegments = got.Workspace.SliceSegments
body.Segments = got.Workspace.Segments
body.Disposition = "keep"
r = f.request("POST", path, f.annotator, body)
if r.Code != 200 {
t.Fatal(r.Body.String())
}
json.Unmarshal(r.Body.Bytes(), &got)
history, _ := got.Item.Subject.Payload["sliceDispositionHistory"].([]any)
if len(history) != 2 {
t.Fatalf("Mongo history lost: %d", len(history))
}
first := mapValue(history[0])
snapshot := mapValue(first["after"])
if snapshot["discardReasonLabelId"] != "discard-fixture" {
t.Fatal("original reason evidence lost")
}
}