droboticflow/internal/api/slice_disposition_test.go
d-robotics 58e0e50570 🧪 Retro 2026-10-12: CI gate, known-failure baselines, devboot regression test, workbench smoke, review standards
Six follow-ups from the round8-12 hotfix retrospective:

- web-ci.yml: lint + Go/frontend tests on every push and PR (nothing ran
  app checks before; the round10 dev-mode regression shipped green)
- tests/known-failures.json + check-test-baseline.mjs: gate by exact
  failing-test id instead of hand-counted failure totals (14 frontend,
  70 Go upstream debts recorded from the dev tip)
- devboot.test.mjs: boots the real Vite dev server and pins the movi
  loader contract that broke round10 (query-less DEV url, /vendor stays
  off the module pipeline)
- scripts/local/smoke-workbench.mjs: pre-release smoke covering the
  seven recurring acceptance points (login/pool/claim/play-while-loading/
  captions/4x/playhead), 3x green against the local replica
- docs/CODING_STANDARDS.md + AGENTS pointer: eligibility flags are
  server-shipped, the UI renders them (rounds 9+12 lesson)
- vendor README host-element quirks section, HANDOFF gate pointers and
  movi-path verification rule, perf-audit report pinned with the
  post-round12 production 500 observation
- husky pre-commit: lint-staged (repo prettier scope) + tsc + gofmt
- gofmt: restore lint:go green on the dev tip (drift shipped silently
  in the perf merge and round12 because nothing gated it)
2026-10-12 18:07:02 +05:30

161 lines
7.4 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 string(wire.Workspace["segments"]) != "[]" {
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)
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")
}
}