droboticflow/internal/api/annotation_operations_test.go
d-robotics 58e0e50570
Some checks failed
web-ci / checks (push) Has been cancelled
web-ci / checks (pull_request) Has been cancelled
🧪 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

287 lines
14 KiB
Go

package api
import (
"testing"
"droboticflow/internal/domain"
)
func TestValidateAnnotationSegmentsRejectsOverlapAndOutOfRange(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 10, ClipEnd: 20}
issues := validateAnnotationSegments([]domain.AnnotationSegment{
{ID: "one", LabelID: "a", Label: "A", Start: 9, End: 12, Confidence: 1},
{ID: "two", LabelID: "b", Label: "B", Start: 11, End: 15, Confidence: 1},
}, task)
if len(issues) < 2 {
t.Fatalf("expected range and overlap issues, got %#v", issues)
}
}
func TestValidateAnnotationSegmentsAcceptsOrderedTimeline(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 0, ClipEnd: 30}
issues := validateAnnotationSegments([]domain.AnnotationSegment{
{ID: "one", LabelID: "static", Label: "静止观察", Start: 0, End: 10, Confidence: 0.9},
{ID: "two", LabelID: "steady", Label: "平稳活动", Start: 10, End: 30, Confidence: 0.8},
}, task)
if len(issues) != 0 {
t.Fatalf("unexpected issues: %#v", issues)
}
}
func TestValidateAnnotationSegmentsRequiresUniqueNonEmptyIDs(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 0, ClipEnd: 30}
issues := validateAnnotationSegments([]domain.AnnotationSegment{
{ID: "", LabelID: "01", Label: "鲜食处理", Start: 0, End: 5, Confidence: 1},
{ID: "same", LabelID: "02", Label: "商品整理", Start: 5, End: 10, Confidence: 1},
{ID: "same", LabelID: "03", Label: "清洁操作", Start: 10, End: 15, Confidence: 1},
}, task)
if len(issues) != 2 {
t.Fatalf("empty and duplicate annotation ids must fail: %#v", issues)
}
}
func TestAnnotationTaskProgressUsesExplicitStatusesAndLegacyEventFallback(t *testing.T) {
slices := &domain.SliceRevisionRecord{Segments: []domain.SliceSegment{
{ID: "labeled", Start: 0, End: 2, AnnotationStatus: domain.SliceAnnotationLabeled},
{ID: "empty", Start: 2, End: 4, AnnotationStatus: domain.SliceAnnotationEmptyConfirmed},
{ID: "pending", Start: 4, End: 6, AnnotationStatus: domain.SliceAnnotationPending},
{ID: "legacy-labeled", Start: 6, End: 8},
{ID: "legacy-pending", Start: 8, End: 10},
}}
annotations := &domain.AnnotationRevisionRecord{Segments: []domain.AnnotationSegment{
{ID: "pending-event", Start: 4.5, End: 5.5},
{ID: "legacy-event", Start: 6.5, End: 7.5},
}}
progress := annotationTaskProgress(slices, annotations)
if progress.SliceCount != 5 || progress.CompletedSliceCount != 3 || progress.PendingSliceCount != 2 || progress.AnnotationEventCount != 2 {
t.Fatalf("progress=%#v", progress)
}
}
func TestVirtualFullVideoTaskCanExpandRoughCutWithinSourceDuration(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 10, ClipEnd: 12, SourceIsFullVideo: true, SourceDurationSeconds: 30}
if issues := validateSliceSegments([]domain.SliceSegment{{ID: "expanded", Start: 8, End: 14}}, task); len(issues) != 0 {
t.Fatalf("full-video rough cut must be expandable: %#v", issues)
}
if issues := validateSliceSegments([]domain.SliceSegment{{ID: "outside", Start: 8, End: 31}}, task); len(issues) == 0 {
t.Fatal("full-video adjustment must remain within verified source duration")
}
if issues := validateAnnotationSegments([]domain.AnnotationSegment{{ID: "label", LabelID: "01", Label: "鲜食处理", Start: 8, End: 14, Confidence: 1}}, task); len(issues) != 0 {
t.Fatalf("semantic annotation must accept the reviewed expanded cut: %#v", issues)
}
}
func TestValidateCompleteSliceCoverageRequiresAContiguousFullPartition(t *testing.T) {
task := domain.AnnotationTaskRecord{SourceIsFullVideo: true, SourceDurationSeconds: 10}
complete := []domain.SliceSegment{{ID: "a", Start: 0, End: 2}, {ID: "b", Start: 2, End: 7}, {ID: "c", Start: 7, End: 10}}
if issues := validateCompleteSliceCoverage(complete, task); len(issues) != 0 {
t.Fatalf("complete partition rejected: %#v", issues)
}
gap := []domain.SliceSegment{{ID: "a", Start: 0, End: 2}, {ID: "b", Start: 3, End: 10}}
if issues := validateCompleteSliceCoverage(gap, task); len(issues) != 1 || issues[0].Code != "slice_coverage_gap" {
t.Fatalf("gap issues=%#v", issues)
}
overlap := []domain.SliceSegment{{ID: "a", Start: 0, End: 4}, {ID: "b", Start: 3, End: 10}}
if issues := validateCompleteSliceCoverage(overlap, task); len(issues) != 1 || issues[0].Code != "slice_coverage_overlap" {
t.Fatalf("overlap issues=%#v", issues)
}
trimmed := []domain.SliceSegment{{ID: "a", Start: 1, End: 9}}
if issues := validateCompleteSliceCoverage(trimmed, task); len(issues) != 2 {
t.Fatalf("trimmed coverage issues=%#v", issues)
}
}
func TestAnnotationTaskEditableRangeUsesMaterializedCommonCoverage(t *testing.T) {
task := domain.AnnotationTaskRecord{SourceIsFullVideo: true, SourceDurationSeconds: 25, CoverageStartSecond: 0.25, CoverageEndSecond: 24.75, CoverageMode: "full_common_range"}
start, end := annotationTaskEditableRange(task)
if start != 0.25 || end != 24.75 {
t.Fatalf("editable range=(%v,%v)", start, end)
}
}
func TestValidateFanyuV5CombinedSegmentsRequiresExactTemplateAndSliceContainment(t *testing.T) {
template := &domain.AnnotationTemplateRecord{Labels: domain.ConvenienceStore22SemanticLabels()}
slices := []domain.SliceSegment{{ID: "a", Start: 0, End: 5}, {ID: "b", Start: 5, End: 10}}
if issues := validateFanyuV5CombinedSemanticSegments(nil, slices, template); len(issues) != 0 {
t.Fatalf("explicit no-action submission should be valid: %#v", issues)
}
slices[0].AnnotationStatus = domain.SliceAnnotationLabeled
slices[0].TaskLabelID = "01"
slices[0].TaskLabel = "错误名称"
if issues := validateFanyuV5CombinedSemanticSegments(nil, slices, template); len(issues) == 0 {
t.Fatal("mismatched slice task label must fail")
}
slices[0].TaskLabel = "鲜食处理"
if issues := validateFanyuV5CombinedSemanticSegments([]domain.AnnotationSegment{{ID: "cross", Description: "跨片动作", Handedness: "both", Start: 4, End: 6}}, slices, template); len(issues) == 0 {
t.Fatal("cross-slice annotation must fail")
}
}
func TestValidateSliceSegmentsRequiresUniqueIDsAndKnownAnnotationStatus(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 0, ClipEnd: 10}
issues := validateSliceSegments([]domain.SliceSegment{
{ID: "same", Start: 0, End: 5, AnnotationStatus: domain.SliceAnnotationLabeled},
{ID: "same", Start: 5, End: 10, AnnotationStatus: "unknown"},
}, task)
if len(issues) != 2 {
t.Fatalf("duplicate id and unknown status must both fail: %#v", issues)
}
}
func TestValidateRequiredSliceCompletionDistinguishesLabeledAndConfirmedEmpty(t *testing.T) {
slices := []domain.SliceSegment{
{ID: "labeled", Start: 0, End: 5, TaskLabelID: "01", TaskLabel: "鲜食处理", AnnotationStatus: domain.SliceAnnotationLabeled},
{ID: "empty", Start: 5, End: 10, AnnotationStatus: domain.SliceAnnotationEmptyConfirmed},
}
segments := []domain.AnnotationSegment{{ID: "semantic", LabelID: "01", Label: "鲜食处理", Start: 1, End: 2}}
if issues := validateRequiredSliceCompletion(slices, segments, false); len(issues) != 0 {
t.Fatalf("completed slices rejected: %#v", issues)
}
slices[0].AnnotationStatus = domain.SliceAnnotationPending
slices[1].AnnotationStatus = domain.SliceAnnotationEmptyConfirmed
segments = append(segments, domain.AnnotationSegment{ID: "unexpected", LabelID: "02", Label: "商品整理", Start: 6, End: 7})
issues := validateRequiredSliceCompletion(slices, segments, false)
if len(issues) != 2 || issues[0].Code != "slice_annotation_pending" || issues[1].Code != "empty_slice_has_annotations" {
t.Fatalf("pending and false-empty slices must fail independently: %#v", issues)
}
}
func TestValidateCombinedAnnotationEventsRequiresFreeTextAndHandedness(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 0, ClipEnd: 10}
valid := []domain.AnnotationSegment{{ID: "event", Description: "拿取商品", Handedness: "right", Start: 1, End: 2, Confidence: 1}}
if issues := validateCombinedAnnotationEvents(valid, task); len(issues) != 0 {
t.Fatalf("valid free-text event rejected: %#v", issues)
}
invalid := []domain.AnnotationSegment{{ID: "event", Start: 1, End: 2, Confidence: 1}}
issues := validateCombinedAnnotationEvents(invalid, task)
if len(issues) != 2 {
t.Fatalf("missing description and handedness must fail: %#v", issues)
}
}
func TestDescriptionOnlyCombinedAnnotationNeedsNoHandednessOrTaskLabel(t *testing.T) {
task := domain.AnnotationTaskRecord{ClipStart: 0, ClipEnd: 10, TemplateVersion: 6}
segments := []domain.AnnotationSegment{{ID: "description", LabelID: "dict-01", Description: "拿取商品", Start: 1, End: 2, Confidence: 1}}
if issues := validateCombinedAnnotationEvents(segments, task); len(issues) != 0 {
t.Fatalf("description-only event rejected: %#v", issues)
}
slices := []domain.SliceSegment{{ID: "slice", Name: "拿取", Start: 0, End: 10, AnnotationStatus: domain.SliceAnnotationLabeled}}
if issues := validateRequiredSliceCompletion(slices, segments, true); len(issues) != 0 {
t.Fatalf("description-only completion rejected: %#v", issues)
}
}
func TestClearCombinedAnnotationDraftPreservesOtherSubjectPayload(t *testing.T) {
item := &domain.HumanItemRecord{Subject: domain.HumanSubjectRef{Payload: map[string]any{"annotationDraft": map[string]any{"savedAt": "now"}, "materialization": map[string]any{"status": "completed"}}}}
clearCombinedAnnotationDraft(item)
if _, exists := item.Subject.Payload["annotationDraft"]; exists || item.Subject.Payload["materialization"] == nil {
t.Fatalf("final submission must clear only its draft: %#v", item.Subject.Payload)
}
}
func TestMembershipRequiresMatchingProjectAndSupplier(t *testing.T) {
task := domain.AnnotationTaskRecord{ProjectID: "project-a", SupplierID: "supplier-a"}
memberships := []annotationProjectRole{
{ProjectID: "project-a", SupplierID: "supplier-a", Role: domain.AnnotationRoleAnnotator},
}
if !membershipAllowsTask(memberships, task) {
t.Fatal("expected matching membership to allow task")
}
task.SupplierID = "supplier-b"
if membershipAllowsTask(memberships, task) {
t.Fatal("supplier mismatch must not allow task")
}
}
func TestAnnotationProductionRolesValid(t *testing.T) {
for _, role := range []domain.AnnotationAssignmentRole{
domain.AnnotationRoleAnnotator,
domain.AnnotationRoleQualityReviewer,
domain.AnnotationRoleAcceptanceReviewer,
} {
if !annotationRoleValid(role) {
t.Fatalf("role %s must be valid", role)
}
}
stages := domain.DefaultFanyuV58HumanStages()
if stages[0].RequiredRole != "annotator" || stages[1].RequiredRole != "quality_reviewer" {
t.Fatalf("unexpected stage model: %#v", stages)
}
if annotationRoleForHumanStage(stages[0]) != domain.AnnotationRoleAnnotator ||
annotationRoleForHumanStage(stages[1]) != domain.AnnotationRoleQualityReviewer {
t.Fatalf("stage role routing does not match configured roles: %#v", stages)
}
}
func TestQualityReviewerMembershipDoesNotCoverIndependentAcceptanceRole(t *testing.T) {
task := domain.AnnotationTaskRecord{ProjectID: "project-a", SupplierID: "supplier-a"}
memberships := []annotationProjectRole{{
ProjectID: task.ProjectID, SupplierID: task.SupplierID, Role: domain.AnnotationRoleQualityReviewer,
}}
if membershipHasRole(memberships, task, domain.AnnotationRoleAcceptanceReviewer) {
t.Fatal("quality reviewer must not cover independent final-acceptance membership checks")
}
memberships = append(memberships, annotationProjectRole{ProjectID: task.ProjectID, SupplierID: task.SupplierID, Role: domain.AnnotationRoleAcceptanceReviewer})
if !membershipHasRole(memberships, task, domain.AnnotationRoleAcceptanceReviewer) {
t.Fatal("explicit acceptance reviewer membership must grant final acceptance")
}
}
func TestFanyuV58UsesCombinedAnnotationAndIndependentReview(t *testing.T) {
stages := domain.DefaultFanyuV58HumanStages()
if !domain.IsFanyuV58ThreeRoleHumanLoop(stages) {
t.Fatalf("unexpected v5.8 stages: %#v", stages)
}
if annotationRoleForHumanStage(stages[0]) != domain.AnnotationRoleAnnotator || annotationRoleForHumanStage(stages[1]) != domain.AnnotationRoleQualityReviewer {
t.Fatalf("unexpected v5.8 stage role routing: %#v", stages)
}
acceptance := domain.HumanStageDefinition{ID: fanyuV5PrePackageAcceptanceStageID, Kind: domain.HumanStageAcceptance, RequiredRole: "acceptance_reviewer"}
if annotationRoleForHumanStage(acceptance) != domain.AnnotationRoleAcceptanceReviewer {
t.Fatal("v5.8 aggregate acceptance must require an independent acceptance reviewer")
}
}
func TestFanyuV58FirstHumanSubmissionIgnoresSystemSuggestionRevision(t *testing.T) {
systemSuggestion := domain.AnnotationRevisionRecord{Revision: 0, AuthorUserID: "system"}
if fanyuV5CombinedSubmissionIsRework([]domain.AnnotationRevisionRecord{systemSuggestion}, domain.AnnotationTaskStatusPendingAnnotation) {
t.Fatal("system revision zero must not turn the first human submission into rework")
}
humanRevision := domain.AnnotationRevisionRecord{Revision: 2, AuthorUserID: "annotator"}
if !fanyuV5CombinedSubmissionIsRework([]domain.AnnotationRevisionRecord{systemSuggestion, humanRevision}, domain.AnnotationTaskStatusPendingAnnotation) {
t.Fatal("a prior human revision must mark the next submission as rework")
}
}
func TestValidateHumanStagesAllowsDirectedReworkToEarlierStage(t *testing.T) {
if issues := validateHumanStages(domain.DefaultAnnotationHumanStages()); len(issues) != 0 {
t.Fatalf("default stages should be valid: %#v", issues)
}
}
func TestValidateHumanStagesRejectsForwardRejectTarget(t *testing.T) {
issues := validateHumanStages([]domain.HumanStageDefinition{
{ID: "review", Label: "审核", Kind: domain.HumanStageReview, RequiredRole: "quality_reviewer", RejectTargets: []string{"annotate"}},
{ID: "annotate", Label: "标注", Kind: domain.HumanStageAnnotation, RequiredRole: "annotator"},
})
if len(issues) == 0 {
t.Fatal("forward reject target must be rejected")
}
}
func TestSplitAndAnnotationQualityReviewFlow(t *testing.T) {
stages := domain.DefaultAnnotationHumanStages()
if len(stages) != 4 {
t.Fatalf("expected 4 stages, got %d", len(stages))
}
if stages[0].ID != "split_annotation" || stages[1].ID != "split_quality_review" ||
stages[2].ID != "semantic_annotation" || stages[3].ID != "quality_review" {
t.Fatalf("unexpected stage order: %#v", stages)
}
if len(stages[1].RejectTargets) != 1 || stages[1].RejectTargets[0] != "split_annotation" {
t.Fatalf("split quality review reject target must be split_annotation: %#v", stages[1].RejectTargets)
}
if len(stages[3].RejectTargets) != 2 || stages[3].RejectTargets[0] != "semantic_annotation" {
t.Fatalf("quality review reject targets must include semantic_annotation: %#v", stages[3].RejectTargets)
}
}