forked from longtao.wu/droboticflow
233 lines
12 KiB
Go
233 lines
12 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestHumanEvidenceArtifactRunIDsUsesEvidenceRunAndSafeFallback(t *testing.T) {
|
|
type mongoStyleMap map[string]any
|
|
type mongoStyleArray []any
|
|
allowed := map[string]string{}
|
|
humanEvidenceArtifactRunIDs(mongoStyleMap{
|
|
"items": mongoStyleArray{
|
|
mongoStyleMap{"runId": "run-source", "clipArtifactId": "artifact-source"},
|
|
mongoStyleMap{"artifactId": "artifact-parent"},
|
|
},
|
|
}, "run-parent", allowed)
|
|
if allowed["artifact-source"] != "run-source" || allowed["artifact-parent"] != "run-parent" {
|
|
t.Fatalf("artifact evidence must retain its scoped source run: %#v", allowed)
|
|
}
|
|
if _, exists := allowed["artifact-unscoped"]; exists {
|
|
t.Fatalf("unreferenced artifacts must not gain evidence access: %#v", allowed)
|
|
}
|
|
}
|
|
|
|
func TestAcceptanceArtifactRefsAcceptsMongoStyleNamedArray(t *testing.T) {
|
|
type mongoStyleArray []any
|
|
refs := acceptanceArtifactRefs(mongoStyleArray{map[string]any{"artifactId": "headpose-1"}})
|
|
if len(refs) != 1 || refs[0]["artifactId"] != "headpose-1" {
|
|
t.Fatalf("refs=%#v", refs)
|
|
}
|
|
}
|
|
|
|
func TestFanyuV5PrePackageAcceptanceAggregatesMultipleTasksBeforeRelease(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
batch := domain.HumanBatchRecord{
|
|
ID: "batch-v5", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", ProjectID: "project-v5", WorkflowID: "workflow-v5", RunID: "run-v5", GateNodeID: "gate-v5",
|
|
ReleasePolicy: domain.HumanReleaseAllAccepted, PartitionMaxItems: 10, RequirePrePackageAcceptance: true, Status: domain.HumanBatchActive,
|
|
}
|
|
if err := dataStore.UpsertHumanBatch(ctx, batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
poseSummary := map[string]any{
|
|
"headposeEnabled": true, "headposeAvailable": true, "headposeFrameCount": 2,
|
|
"headposeArtifacts": []map[string]any{{"artifactId": "headpose-manifest", "frameCount": 2}},
|
|
"headposeRenderArtifacts": []map[string]any{{"artifactId": "headpose-render", "name": "headpose-annotated.mp4"}},
|
|
"handposeEnabled": true, "handposeAvailable": true, "handposeFrameCount": 3,
|
|
"handPrelabelEnabled": true, "handPrelabelAvailable": true, "handPrelabelFrameCount": 3,
|
|
"handPrelabelArtifacts": []map[string]any{{"artifactId": "hand-prelabel-manifest", "frameCount": 3}},
|
|
"handPrelabelRenderArtifacts": []map[string]any{{"artifactId": "hand-prelabel-render", "name": "wilor-annotated-video.mp4"}},
|
|
}
|
|
for index := 1; index <= 2; index++ {
|
|
task := domain.AnnotationTaskRecord{ID: "task-v5-" + string(rune('0'+index)), OrganizationID: domain.DefaultOrganizationID, ProjectID: batch.ProjectID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, NodeID: batch.GateNodeID, ArtifactID: "clip-artifact-" + string(rune('0'+index)), SourceVideo: "clip.mp4", ClipStart: 0, ClipEnd: 10, TemplateID: "tpl-v5", TemplateVersion: 5, WorkgroupID: "wg-v5", VendorID: "vendor-v5", Status: domain.AnnotationTaskStatusAccepted, CurrentRevision: 1, PoseSummary: poseSummary}
|
|
if err := dataStore.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{ID: "slice-v5-" + string(rune('0'+index)), OrganizationID: domain.DefaultOrganizationID, TaskID: task.ID, Revision: 1, Segments: []domain.SliceSegment{{ID: "final", Start: 0, End: 10}}, AuthorUserID: "annotator"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: "annotation-v5-" + string(rune('0'+index)), OrganizationID: domain.DefaultOrganizationID, TaskID: task.ID, Revision: 1, TemplateVersion: 5, Segments: []domain.AnnotationSegment{{ID: "label", LabelID: "01", Label: "鲜食处理", Start: 0, End: 10, Confidence: 1}}, AuthorUserID: "annotator"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item := domain.HumanItemRecord{ID: "item-v5-" + string(rune('0'+index)), OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", BatchID: batch.ID, ProjectID: batch.ProjectID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, GateNodeID: batch.GateNodeID, LegacyTaskID: task.ID, WorkgroupID: "wg-v5", VendorID: "vendor-v5", Status: domain.HumanItemAccepted, AcceptedAt: nowISO(), Version: 1, Subject: domain.HumanSubjectRef{ItemKey: task.ArtifactID, Payload: map[string]any{"materialization": map[string]any{"runId": task.RunID, "artifactId": "materialized-" + task.ID, "status": "completed"}}}}
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
server := &server{store: dataStore}
|
|
if err := server.flushHumanReleasePartitions(ctx, &batch, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
partitions, err := dataStore.ListReleasePartitions(ctx, batch.OrganizationID, batch.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(partitions) != 0 {
|
|
t.Fatalf("prepackage acceptance must block partitions, got %#v", partitions)
|
|
}
|
|
aggregateItem, err := dataStore.GetHumanItem(ctx, fanyuV5PrePackageAcceptanceItemID(batch.ID))
|
|
if err != nil || aggregateItem == nil {
|
|
t.Fatalf("aggregate item=%#v err=%v", aggregateItem, err)
|
|
}
|
|
if aggregateItem.CurrentStageID != fanyuV5PrePackageAcceptanceStageID || aggregateItem.Status != domain.HumanItemReady {
|
|
t.Fatalf("aggregate stage=%s status=%s", aggregateItem.CurrentStageID, aggregateItem.Status)
|
|
}
|
|
items, _ := aggregateItem.Subject.Payload["items"].([]any)
|
|
if len(items) != 2 {
|
|
t.Fatalf("aggregate should contain both clip tasks, got %#v", aggregateItem.Subject.Payload)
|
|
}
|
|
statistics, _ := aggregateItem.Subject.Payload["statistics"].(map[string]any)
|
|
if statistics["headposeEnabled"] != true || statistics["handPrelabelEnabled"] != true || acceptanceEvidenceCount(statistics["headposeFrameCount"]) != 2 || acceptanceEvidenceCount(statistics["handPrelabelFrameCount"]) != 3 {
|
|
t.Fatalf("prelabel aggregate statistics=%#v", statistics)
|
|
}
|
|
if len(acceptanceArtifactRefs(statistics["headposeRenderArtifacts"])) != 1 || len(acceptanceArtifactRefs(statistics["handPrelabelRenderArtifacts"])) != 1 {
|
|
t.Fatalf("rendered prelabel evidence must be de-duplicated: %#v", statistics)
|
|
}
|
|
firstEvidence, _ := items[0].(map[string]any)
|
|
firstPose, _ := firstEvidence["poseSummary"].(map[string]any)
|
|
firstMaterialization, _ := firstEvidence["materialization"].(map[string]any)
|
|
if firstEvidence == nil || firstPose == nil || firstMaterialization == nil || strings.TrimSpace(fmt.Sprint(firstMaterialization["artifactId"])) == "" || len(acceptanceArtifactRefs(firstPose["headposeArtifacts"])) != 1 || len(acceptanceArtifactRefs(firstPose["headposeRenderArtifacts"])) != 1 || len(acceptanceArtifactRefs(firstPose["handPrelabelArtifacts"])) != 1 || len(acceptanceArtifactRefs(firstPose["handPrelabelRenderArtifacts"])) != 1 {
|
|
t.Fatalf("per-task pose evidence must be preserved: %#v", items)
|
|
}
|
|
aggregateItem.Status = domain.HumanItemAccepted
|
|
aggregateItem.AcceptedAt = nowISO()
|
|
aggregateItem.AcceptedByUserID = "acceptance-reviewer"
|
|
if err := dataStore.UpsertHumanItem(ctx, *aggregateItem); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := server.flushHumanReleasePartitions(ctx, &batch, true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
partitions, err = dataStore.ListReleasePartitions(ctx, batch.OrganizationID, batch.ID)
|
|
if err != nil || len(partitions) != 1 || len(partitions[0].ItemIDs) != 2 {
|
|
t.Fatalf("confirmed aggregate must release one multi-clip partition: %#v err=%v", partitions, err)
|
|
}
|
|
}
|
|
|
|
func TestFanyuV5CombinedSubmissionReservationPreventsConcurrentOverwrite(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
item := domain.HumanItemRecord{ID: "combined-item", OrganizationID: domain.DefaultOrganizationID, BatchID: "batch", CurrentStageID: "annotation_combined", Status: domain.HumanItemClaimed, Version: 7}
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := &server{store: dataStore}
|
|
results := make(chan error, 2)
|
|
var group sync.WaitGroup
|
|
for _, candidate := range []struct{ id, sha string }{{"submission-a", "aaa"}, {"submission-b", "bbb"}} {
|
|
candidate := candidate
|
|
group.Add(1)
|
|
go func() {
|
|
defer group.Done()
|
|
_, _, err := server.reserveFanyuV5CombinedSubmission(ctx, item, 7, candidate.id, candidate.sha)
|
|
results <- err
|
|
}()
|
|
}
|
|
group.Wait()
|
|
close(results)
|
|
success := 0
|
|
for err := range results {
|
|
if err == nil {
|
|
success++
|
|
}
|
|
}
|
|
if success != 1 {
|
|
t.Fatalf("exactly one concurrent combined submission must reserve, successes=%d", success)
|
|
}
|
|
stored, err := dataStore.GetHumanItem(ctx, item.ID)
|
|
if err != nil || stored == nil || stored.Version != 8 || stored.PendingCombinedSubmissionID == "" || stored.PendingCombinedPayloadSHA == "" {
|
|
t.Fatalf("winner reservation missing: %#v err=%v", stored, err)
|
|
}
|
|
}
|
|
|
|
func TestFanyuV5CombinedReservationResumesAfterReloadWithIncrementedVersion(t *testing.T) {
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
item := domain.HumanItemRecord{ID: "retry-item", OrganizationID: domain.DefaultOrganizationID, CurrentStageID: "annotation_combined", Status: domain.HumanItemClaimed, Version: 3}
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := &server{store: dataStore}
|
|
reserved, resumed, err := server.reserveFanyuV5CombinedSubmission(ctx, item, 3, "submission-v3", "semantic-payload-sha")
|
|
if err != nil || resumed || reserved.Version != 4 {
|
|
t.Fatalf("initial reserve=%#v resumed=%v err=%v", reserved, resumed, err)
|
|
}
|
|
// Simulate a request that reloaded the pending item after a partial write.
|
|
loaded, _ := dataStore.GetHumanItem(ctx, item.ID)
|
|
retry, resumed, err := server.reserveFanyuV5CombinedSubmission(ctx, *loaded, 4, "submission-v4", "semantic-payload-sha")
|
|
if err != nil || !resumed || retry.PendingCombinedSubmissionID != "submission-v3" || retry.Version != 4 {
|
|
t.Fatalf("retry must resume original reservation: %#v resumed=%v err=%v", retry, resumed, err)
|
|
}
|
|
}
|
|
|
|
func TestFanyuV5CombinedPayloadSHAIgnoresCASVersionForRetry(t *testing.T) {
|
|
base := combinedAnnotationSubmissionInput{
|
|
Version: 3,
|
|
SliceSegments: []domain.SliceSegment{{ID: "slice", Start: 0, End: 5}},
|
|
Segments: []domain.AnnotationSegment{},
|
|
}
|
|
first, err := fanyuV5CombinedPayloadSHA(base)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
base.Version = 4 // Reload sees the reservation's incremented item version.
|
|
retry, err := fanyuV5CombinedPayloadSHA(base)
|
|
if err != nil || first != retry {
|
|
t.Fatalf("semantic retry must keep its reservation hash: first=%q retry=%q err=%v", first, retry, err)
|
|
}
|
|
}
|
|
|
|
func TestEarliestRejectTargetSelectsEarliestConfiguredStage(t *testing.T) {
|
|
batch := &domain.HumanBatchRecord{Stages: domain.DefaultAnnotationHumanStages()}
|
|
target, ok := earliestRejectTarget(
|
|
batch,
|
|
[]string{"split_annotation", "semantic_annotation"},
|
|
[]string{"semantic_annotation", "split_annotation"},
|
|
)
|
|
if !ok || target != "split_annotation" {
|
|
t.Fatalf("target=%q ok=%v", target, ok)
|
|
}
|
|
}
|
|
|
|
func TestEarliestRejectTargetRejectsUnknownStage(t *testing.T) {
|
|
batch := &domain.HumanBatchRecord{Stages: domain.DefaultAnnotationHumanStages()}
|
|
if target, ok := earliestRejectTarget(batch, []string{"semantic_annotation"}, []string{"training"}); ok || target != "" {
|
|
t.Fatalf("unexpected target=%q ok=%v", target, ok)
|
|
}
|
|
}
|
|
|
|
func TestAllowedHumanReopenStagesKeepsRejectZoneSeparateFromRework(t *testing.T) {
|
|
batch := &domain.HumanBatchRecord{Stages: domain.DefaultAnnotationHumanStages()}
|
|
splitQuality := allowedHumanReopenStages(batch, "split_quality_review")
|
|
if len(splitQuality) != 2 || splitQuality[0] != "split_annotation" || splitQuality[1] != "split_quality_review" {
|
|
t.Fatalf("split quality reopen stages=%#v", splitQuality)
|
|
}
|
|
quality := allowedHumanReopenStages(batch, "quality_review")
|
|
if len(quality) != 4 || quality[0] != "split_annotation" || quality[3] != "quality_review" {
|
|
t.Fatalf("quality reopen stages=%#v", quality)
|
|
}
|
|
legacyAcceptanceBatch := &domain.HumanBatchRecord{Stages: append(domain.DefaultAnnotationHumanStages(), domain.HumanStageDefinition{ID: "acceptance", Label: "最终验收", Kind: domain.HumanStageAcceptance, RequiredRole: "acceptance_reviewer"})}
|
|
acceptance := allowedHumanReopenStages(legacyAcceptanceBatch, "acceptance")
|
|
if len(acceptance) != 4 || containsHumanStage(acceptance, "acceptance") {
|
|
t.Fatalf("acceptance must reopen an earlier production stage: %#v", acceptance)
|
|
}
|
|
}
|