forked from longtao.wu/droboticflow
201 lines
8.4 KiB
Go
201 lines
8.4 KiB
Go
package api
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func TestLegacyAnnotationConfirmationReadinessAcceptsPersistedActionPartitions(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
key string
|
|
taskStatus domain.AnnotationTaskStatus
|
|
}{
|
|
{name: "first pass", key: "annotationWorkspace", taskStatus: domain.AnnotationTaskStatusPendingAnnotation},
|
|
{name: "legacy draft fallback", key: "annotationDraft", taskStatus: domain.AnnotationTaskStatusPendingAnnotation},
|
|
{name: "rework", key: "annotationWorkspace", taskStatus: domain.AnnotationTaskStatusRework},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
task := fixture.task
|
|
task.Status = tc.taskStatus
|
|
item := withLegacyActionPartition(fixture.item)
|
|
item.Subject.Payload[tc.key] = legacyActionPartition()
|
|
ready, err := (&server{store: fixture.store}).legacyAnnotationConfirmationReady(fixture.ctx, task, &item)
|
|
if err != nil || !ready {
|
|
t.Fatalf("legacy action partition ready=%v err=%v", ready, err)
|
|
}
|
|
stored, err := fixture.store.GetHumanItem(fixture.ctx, fixture.item.ID)
|
|
if err != nil || stored == nil || stored.Version != fixture.item.Version {
|
|
t.Fatalf("readiness check mutated persisted item: item=%#v err=%v", stored, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLegacyAnnotationConfirmationReadinessRejectsMissingOrInvalidActions(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
segments []domain.AnnotationSegment
|
|
}{
|
|
{name: "no actions"},
|
|
{name: "gap in action partition", segments: []domain.AnnotationSegment{{ID: "a", Start: 0, End: 4, Status: "pending"}}},
|
|
{name: "crosses slice boundary", segments: []domain.AnnotationSegment{{ID: "a", Start: 0, End: 10, Status: "pending"}}},
|
|
{name: "invalid action state", segments: []domain.AnnotationSegment{{ID: "a", Start: 0, End: 10, Status: "unknown"}}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
item := withLegacyActionPartition(fixture.item)
|
|
workspace := legacyActionPartition()
|
|
if tc.segments == nil {
|
|
delete(workspace, "segments")
|
|
} else {
|
|
workspace["segments"] = tc.segments
|
|
}
|
|
if tc.name == "crosses slice boundary" {
|
|
workspace["sliceSegments"] = []domain.SliceSegment{
|
|
{ID: "slice-1", Start: 0, End: 5, AnnotationStatus: domain.SliceAnnotationPending},
|
|
{ID: "slice-2", Start: 5, End: 10, AnnotationStatus: domain.SliceAnnotationPending},
|
|
}
|
|
workspace["statuses"] = map[string]domain.SliceAnnotationStatus{"slice-1": domain.SliceAnnotationPending, "slice-2": domain.SliceAnnotationPending}
|
|
}
|
|
item.Subject.Payload["annotationWorkspace"] = workspace
|
|
ready, err := (&server{store: fixture.store}).legacyAnnotationConfirmationReady(fixture.ctx, fixture.task, &item)
|
|
if err != nil || ready {
|
|
t.Fatalf("invalid legacy actions ready=%v err=%v", ready, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLegacyAnnotationConfirmationReadinessRequiresExplicitWorkflowCompletion(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
status string
|
|
phase string
|
|
}{
|
|
{name: "editing", status: "editing", phase: "slice"},
|
|
{name: "slice", status: "completed", phase: "slice"},
|
|
{name: "failed", status: "failed", phase: "materializing"},
|
|
{name: "processing", status: "processing", phase: "materializing"},
|
|
{name: "queued", status: "queued", phase: "materializing"},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
item := fixture.item
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": tc.status, "phase": tc.phase}
|
|
ready, err := (&server{store: fixture.store}).legacyAnnotationConfirmationReady(fixture.ctx, fixture.task, &item)
|
|
if err != nil || ready {
|
|
t.Fatalf("workflow %s/%s ready=%v err=%v", tc.status, tc.phase, ready, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
fixture := newCombinedDraftFixture(t)
|
|
item := fixture.item
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
ready, err := (&server{store: fixture.store}).legacyAnnotationConfirmationReady(fixture.ctx, fixture.task, &item)
|
|
if err != nil || !ready {
|
|
t.Fatalf("completed annotation workflow ready=%v err=%v", ready, err)
|
|
}
|
|
}
|
|
|
|
func TestLegacyAnnotationConfirmationReadinessRejectsPendingSubmissionAndActiveJob(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
mutate func(*domain.HumanItemRecord)
|
|
}{
|
|
{name: "pending submission", mutate: func(item *domain.HumanItemRecord) { item.PendingCombinedSubmissionID = "pending" }},
|
|
{name: "queued job", mutate: func(item *domain.HumanItemRecord) {
|
|
item.ProcessingJob = &domain.AnnotationProcessingJob{Status: "queued"}
|
|
}},
|
|
{name: "running job", mutate: func(item *domain.HumanItemRecord) {
|
|
item.ProcessingJob = &domain.AnnotationProcessingJob{Status: "running"}
|
|
}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
item := withLegacyActionPartition(fixture.item)
|
|
item.Subject.Payload["annotationWorkspace"] = legacyActionPartition()
|
|
tc.mutate(&item)
|
|
ready, err := (&server{store: fixture.store}).legacyAnnotationConfirmationReady(fixture.ctx, fixture.task, &item)
|
|
if err != nil || ready {
|
|
t.Fatalf("blocked state ready=%v err=%v", ready, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLegacyAnnotationConfirmationReadinessRequiresFullVideoGenerationEvidence(t *testing.T) {
|
|
for _, evidence := range []string{"materialization", "formal revision"} {
|
|
t.Run(evidence, func(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
task := fixture.task
|
|
task.SourceIsFullVideo = true
|
|
task.SourceDurationSeconds = 10
|
|
item := withLegacyActionPartition(fixture.item)
|
|
item.Subject.Payload["annotationWorkspace"] = legacyActionPartition()
|
|
ready, err := (&server{store: fixture.store}).legacyAnnotationConfirmationReady(fixture.ctx, task, &item)
|
|
if err != nil || ready {
|
|
t.Fatalf("ungenerated full-source task ready=%v err=%v", ready, err)
|
|
}
|
|
|
|
if evidence == "materialization" {
|
|
item.Subject.Payload["materialization"] = map[string]any{"status": "completed", "revision": 1, "artifactId": "materialized-legacy", "runId": task.RunID}
|
|
} else if err := fixture.store.UpsertSliceRevision(fixture.ctx, domain.SliceRevisionRecord{
|
|
ID: "legacy-slice-revision", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1,
|
|
Segments: []domain.SliceSegment{{ID: "slice-1", Start: 0, End: 10}},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ready, err = (&server{store: fixture.store}).legacyAnnotationConfirmationReady(fixture.ctx, task, &item)
|
|
if err != nil || !ready {
|
|
t.Fatalf("generated full-source task ready=%v err=%v", ready, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func withLegacyActionPartition(item domain.HumanItemRecord) domain.HumanItemRecord {
|
|
if item.Subject.Payload == nil {
|
|
item.Subject.Payload = map[string]any{}
|
|
}
|
|
return item
|
|
}
|
|
|
|
func legacyActionPartition() map[string]any {
|
|
return map[string]any{
|
|
"sliceSegments": []domain.SliceSegment{{ID: "slice-1", Start: 0, End: 10, AnnotationStatus: domain.SliceAnnotationPending}},
|
|
"segments": []domain.AnnotationSegment{
|
|
{ID: "action-1", Start: 0, End: 5, Status: "pending"},
|
|
{ID: "action-2", Start: 5, End: 10, Status: "pending"},
|
|
},
|
|
"statuses": map[string]domain.SliceAnnotationStatus{"slice-1": domain.SliceAnnotationPending},
|
|
}
|
|
}
|
|
|
|
func TestLegacyAnnotationConfirmationHTTPUsesPersistedDraftAndStillRejectsStaleWrites(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
item := f.item
|
|
slices := []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 10}}
|
|
actions := []domain.AnnotationSegment{{ID: "a", Start: 0, End: 10, Status: "pending"}}
|
|
item.Subject.Payload["annotationDraft"] = map[string]any{"sliceSegments": slices, "segments": actions, "userId": f.annotator.ID}
|
|
f.store.UpsertHumanItem(f.ctx, item)
|
|
confirmed := []domain.AnnotationSegment{{ID: "a", Start: 0, End: 10, Status: "invalid", IsInvalid: true, Description: "无效片段"}}
|
|
body := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: slices, Segments: confirmed}
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/annotation-confirmations"
|
|
r := f.request("POST", endpoint, f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("legacy G confirmation %d %s", r.Code, r.Body.String())
|
|
}
|
|
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if saved.Version != item.Version+1 || saved.Subject.Payload["sliceWorkflow"] != nil {
|
|
t.Fatal("legacy compatibility must not forge a completion marker")
|
|
}
|
|
r = f.request("POST", endpoint, f.annotator, body)
|
|
if r.Code != 409 {
|
|
t.Fatalf("stale G should conflict: %d", r.Code)
|
|
}
|
|
}
|