173 lines
7.9 KiB
Go
173 lines
7.9 KiB
Go
package api
|
|
|
|
import (
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func dispositionFixturePayload(t *testing.T, f combinedDraftFixture) sliceDispositionInput {
|
|
t.Helper()
|
|
if err := f.store.UpsertAnnotationLabel(f.ctx, domain.AnnotationLabelRecord{ID: "discard-fixture", OrganizationID: f.item.OrganizationID, Kind: "discard_reason", Code: "discard-fixture", Name: "相机被遮挡", Status: domain.AnnotationLabelActive, Version: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertProjectDiscardReasonLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.item.ProjectID, OrganizationID: f.item.OrganizationID, DiscardReasonLabelIDs: []string{"discard-fixture"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return sliceDispositionInput{ReasonLabelID: "discard-fixture", ReasonVersion: 1, combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: f.item.Version, SliceSegments: []domain.SliceSegment{{ID: "a", Start: 0, End: 3}, {ID: "b", Start: 3, End: 7}, {ID: "c", Start: 7, End: 10}}, Segments: []domain.AnnotationSegment{{ID: "old-description", Start: 3, End: 7, Description: "old text", Source: "manual"}}}, SliceID: "b", Disposition: "discard", Reason: "相机被遮挡"}
|
|
}
|
|
|
|
func TestSliceDiscardRetainsRangeHistoryAndRestoresDescriptions(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
body := dispositionFixturePayload(t, f)
|
|
body.SliceSegments[1].DiscardedByUserID = "forged-user"
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("discard: %d %s", r.Code, r.Body.String())
|
|
}
|
|
var got struct {
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
if e := json.Unmarshal(r.Body.Bytes(), &got); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
slices := got.Workspace.SliceSegments
|
|
if len(slices) != 3 || slices[1].Start != 3 || slices[1].End != 7 || !slices[1].IsDiscarded() || len(got.Workspace.Segments) != 0 {
|
|
t.Fatal("discard changed coverage or retained active annotations")
|
|
}
|
|
if slices[1].DiscardedByUserID != f.annotator.ID || slices[1].DiscardedAt == "" || len(slices[1].DiscardedAnnotations) != 1 {
|
|
t.Fatal("missing server-owned audit evidence")
|
|
}
|
|
if len(validateCompleteSliceCoverage(slices, f.task)) != 0 {
|
|
t.Fatal("review coverage broken")
|
|
}
|
|
if got.Item.AssignedUserID != f.annotator.ID || got.Item.CurrentStageID != f.item.CurrentStageID {
|
|
t.Fatal("discard released or advanced task")
|
|
}
|
|
if stale := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body); stale.Code != 409 {
|
|
t.Fatal("stale mutation was accepted")
|
|
}
|
|
body.combinedAnnotationDraftInput = got.Workspace
|
|
body.Version = got.Item.Version
|
|
body.Disposition = "keep"
|
|
body.Reason = ""
|
|
r = f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("restore: %d %s", r.Code, r.Body.String())
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &got)
|
|
if got.Workspace.SliceSegments[1].IsDiscarded() || len(got.Workspace.Segments) != 1 || got.Workspace.Segments[0].ID != "old-description" {
|
|
t.Fatal("restore lost previous description")
|
|
}
|
|
if history, ok := got.Item.Subject.Payload["sliceDispositionHistory"].([]any); !ok || len(history) != 2 {
|
|
t.Fatal("discard/restore audit was not retained")
|
|
}
|
|
}
|
|
|
|
func TestSliceRestoreEmptyAnnotationsReturnsJSONArray(t *testing.T) {
|
|
for _, events := range [][]domain.AnnotationSegment{nil, {}} {
|
|
f := randomClaimFixture(t, 0)
|
|
body := dispositionFixturePayload(t, f)
|
|
body.Segments = events
|
|
for _, action := range []string{"discard", "keep", "keep"} {
|
|
body.Disposition = action
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%s: %d %s", action, r.Code, r.Body.String())
|
|
}
|
|
var wire struct {
|
|
Workspace map[string]json.RawMessage `json:"workspace"`
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
}
|
|
if e := json.Unmarshal(r.Body.Bytes(), &wire); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if raw := string(wire.Workspace["segments"]); len(raw) == 0 || raw[0] != '[' {
|
|
t.Fatalf("%s returned segments=%s; browser progress.filter requires an array", action, wire.Workspace["segments"])
|
|
}
|
|
var got struct {
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &got)
|
|
if action == "discard" && len(got.Workspace.Segments) != 0 {
|
|
t.Fatal("discard retained active annotations")
|
|
}
|
|
if action == "keep" {
|
|
if len(got.Workspace.Segments) != 1 {
|
|
t.Fatal("empty archive restore must create one pending interval")
|
|
}
|
|
pending := got.Workspace.Segments[0]
|
|
if pending.Start != 3 || pending.End != 7 || pending.Status != "pending" || pending.Description != "" {
|
|
t.Fatalf("restore did not create a full pending interval: %+v", pending)
|
|
}
|
|
}
|
|
body.combinedAnnotationDraftInput = got.Workspace
|
|
body.Version = wire.Item.Version
|
|
if wire.Item.AssignedUserID != f.annotator.ID || wire.Item.CurrentStageID != f.item.CurrentStageID {
|
|
t.Fatal("restore changed owner or stage")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSliceDiscardRejectsMissingReasonNonOwnerAndReviewStage(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
body := dispositionFixturePayload(t, f)
|
|
body.ReasonLabelID = ""
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if r.Code < 400 {
|
|
t.Fatal("empty reason accepted")
|
|
}
|
|
body.ReasonLabelID = "discard-fixture"
|
|
for _, actor := range []domain.UserRecord{f.reviewer, f.admin} {
|
|
r = f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", actor, body)
|
|
if r.Code != 403 {
|
|
t.Fatalf("nonowner %s: %d", actor.Role, r.Code)
|
|
}
|
|
}
|
|
item := f.item
|
|
item.CurrentStageID = domain.FanyuV58StageQualityReview
|
|
f.store.UpsertHumanItem(f.ctx, item)
|
|
r = f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if r.Code < 400 {
|
|
t.Fatal("submitted decision editable")
|
|
}
|
|
}
|
|
|
|
func TestDiscardPolicyCannotBeForgedRemovedOrMergedByOrdinarySave(t *testing.T) {
|
|
old := []domain.SliceSegment{{ID: "a", Start: 0, End: 4, DeliveryDisposition: "discard", DiscardReason: "遮挡", DiscardedByUserID: "author"}, {ID: "b", Start: 4, End: 10}}
|
|
for _, next := range [][]domain.SliceSegment{{{ID: "a", Start: 0, End: 4}, {ID: "b", Start: 4, End: 10}}, {{ID: "b", Start: 0, End: 10}}} {
|
|
if _, issues := canonicalDispositions(old, next); len(issues) == 0 {
|
|
t.Fatal("implicit policy change accepted")
|
|
}
|
|
}
|
|
next := append([]domain.SliceSegment(nil), old...)
|
|
next[0].DiscardedByUserID = "forged"
|
|
saved, issues := canonicalDispositions(old, next)
|
|
if len(issues) > 0 || saved[0].DiscardedByUserID != "author" {
|
|
t.Fatal("trusted identity not restored")
|
|
}
|
|
}
|
|
|
|
func TestDiscardCoverageSeparatesHandledAndLabeled(t *testing.T) {
|
|
slices := []domain.SliceSegment{{ID: "a", Start: 0, End: 3, AnnotationStatus: domain.SliceAnnotationLabeled}, {ID: "b", Start: 3, End: 7, DeliveryDisposition: "discard", DiscardReason: "遮挡"}, {ID: "c", Start: 7, End: 10, AnnotationStatus: domain.SliceAnnotationLabeled}}
|
|
actions := []domain.AnnotationSegment{{ID: "a", Start: 0, End: 3, Description: "动作"}, {ID: "c", Start: 7, End: 10, Description: "无效片段", IsInvalid: true}}
|
|
if issues := validateDescriptionIntervals(slices, actions, true); len(issues) > 0 {
|
|
t.Fatal(issues)
|
|
}
|
|
if issues := validateRequiredSliceCompletion(slices, actions, true); len(issues) > 0 {
|
|
t.Fatal(issues)
|
|
}
|
|
if issues := validateDescriptionIntervals(slices, actions[:1], true); len(issues) == 0 {
|
|
t.Fatal("kept unannotated interval passed")
|
|
}
|
|
for i := range slices {
|
|
slices[i].DeliveryDisposition = "discard"
|
|
slices[i].DiscardReason = "无可用内容"
|
|
}
|
|
if !allSlicesDiscarded(slices, nil) || len(validateDescriptionIntervals(slices, nil, true)) != 0 || len(validateRequiredSliceCompletion(slices, nil, true)) != 0 {
|
|
t.Fatal("all-discard cannot finish review")
|
|
}
|
|
}
|