droboticflow/internal/api/annotation_blank_delivery_test.go

220 lines
9.3 KiB
Go

package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"droboticflow/internal/config"
"droboticflow/internal/domain"
)
// Exercise the authenticated request, the real durable processor, review readback
// and the published JSON bytes. No completed-materialization shortcut is seeded.
type blankActionExpectation struct {
id, status, description string
invalid bool
start, end, startUS, endUS float64
}
func TestContinuousBlankActionsReachReviewAndPublishedJSON(t *testing.T) {
cases := []struct {
name string
segments []domain.AnnotationSegment
want []blankActionExpectation
}{
{"whole-blank", nil, []blankActionExpectation{{"", "pending", "", false, 0, 4, 1000000, 5000000}}},
{"partitioned-blank", []domain.AnnotationSegment{{ID: "blank-left", Start: 0, End: 1, Status: "pending"}, {ID: "blank-right", Start: 1, End: 4, Status: "pending"}},
[]blankActionExpectation{{"blank-left", "pending", "", false, 0, 1, 1000000, 2000000}, {"blank-right", "pending", "", false, 1, 4, 2000000, 5000000}}},
{"mixed", []domain.AnnotationSegment{{ID: "blank", Start: 0, End: 1, Status: "pending"}, {ID: "described", Start: 1, End: 3, Status: "described", Description: "拿取商品"}, {ID: "invalid", Start: 3, End: 4, Status: "invalid", IsInvalid: true, Description: "无效片段"}},
[]blankActionExpectation{{"blank", "pending", "", false, 0, 1, 1000000, 2000000}, {"described", "described", "拿取商品", false, 1, 3, 2000000, 4000000}, {"invalid", "invalid", "无效片段", true, 3, 4, 4000000, 5000000}}},
{"legacy-gaps", []domain.AnnotationSegment{{ID: "old", Start: 1, End: 3, Description: "拿取商品"}},
[]blankActionExpectation{{"", "pending", "", false, 0, 1, 1000000, 2000000}, {"old", "described", "拿取商品", false, 1, 3, 2000000, 4000000}, {"", "pending", "", false, 3, 4, 4000000, 5000000}}},
}
for _, reuse := range []bool{false, true} {
for _, tc := range cases {
t.Run(fmt.Sprintf("%s/reuse=%v", tc.name, reuse), func(t *testing.T) {
checkBlankActionsDelivery(t, reuse, tc.segments, tc.want)
})
}
}
}
func checkBlankActionsDelivery(t *testing.T, reuse bool, segments []domain.AnnotationSegment, want []blankActionExpectation, consolidate ...bool) {
t.Helper()
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
f, root := annotationProcessorFixture(t)
batch, err := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
if err != nil {
t.Fatal(err)
}
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
if err = f.store.UpsertHumanBatch(f.ctx, *batch); err != nil {
t.Fatal(err)
}
base := f.handler
f.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Prefer", "respond-async")
base.ServeHTTP(w, r)
})
ctx, cancel := context.WithCancel(f.ctx)
done := make(chan error, 1)
go func() {
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: root}, 1, false)
}()
t.Cleanup(func() {
cancel()
if e := <-done; e != nil && !errors.Is(e, context.Canceled) {
t.Error(e)
}
})
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID
waitFor := func(submit bool) int {
t.Helper()
until := time.Now().Add(20 * time.Second)
for {
response := f.request(http.MethodGet, endpoint+"?view=status", f.annotator, nil)
var status struct {
Stage string `json:"stage"`
SliceStatus string `json:"sliceStatus"`
Version int `json:"version"`
Error string `json:"error"`
}
if err := json.Unmarshal(response.Body.Bytes(), &status); err != nil {
t.Fatal(err)
}
if response.Code != http.StatusOK || status.Error != "" {
t.Fatalf("processing: %d %s", response.Code, response.Body.String())
}
if (submit && status.Stage == domain.FanyuV58StageQualityReview) || (!submit && status.SliceStatus == "completed") {
return status.Version
}
if time.Now().After(until) {
t.Fatalf("processor timed out: %s", response.Body.String())
}
time.Sleep(25 * time.Millisecond)
}
}
slices := []domain.SliceSegment{{ID: "whole", Name: "测试切片", Start: 0, End: 4}}
var selected []string
if len(consolidate) > 0 && consolidate[0] {
slices = []domain.SliceSegment{{ID: "left", Name: "测试切片", Start: 0, End: 1}, {ID: "invalid", Name: "测试切片", Start: 1, End: 2}, {ID: "right", Name: "测试切片", Start: 2, End: 4}}
selected = []string{"invalid"}
}
version := f.item.Version
if reuse {
response := f.request(http.MethodPost, endpoint+"/slice-confirmations", f.annotator, map[string]any{"version": version, "sliceSegments": slices})
if response.Code != http.StatusAccepted {
t.Fatalf("slice confirmation: %d %s", response.Code, response.Body.String())
}
version = waitFor(false)
}
priorFiles := map[string]string{}
if reuse && len(selected) > 0 {
priorRoot := filepath.Join(root, "system", "runs", f.task.RunID, "human-materializations", f.item.ID, f.task.ID, "revision-000001")
err := filepath.Walk(priorRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
data, e := os.ReadFile(path)
if e != nil {
return e
}
priorFiles[path] = string(data)
}
return nil
})
if err != nil || len(priorFiles) == 0 {
t.Fatalf("prior evidence unavailable: %v", err)
}
}
response := f.request(http.MethodPost, endpoint+"/combined-submissions", f.annotator, map[string]any{
"version": version, "requireSliceCompletion": true,
"sliceSegments": slices, "segments": segments, "consolidateInvalidSliceIds": selected,
})
if response.Code != http.StatusAccepted {
t.Fatalf("blank submission: %d %s", response.Code, response.Body.String())
}
waitFor(true)
for path, original := range priorFiles {
data, e := os.ReadFile(path)
if e != nil || string(data) != original {
t.Fatalf("old revision overwritten: %s %v", path, e)
}
}
response = f.request(http.MethodGet, endpoint, f.reviewer, nil)
if response.Code != http.StatusOK {
t.Fatalf("quality read: %d %s", response.Code, response.Body.String())
}
var detail struct {
Revisions []struct {
Segments []domain.AnnotationSegment `json:"segments"`
} `json:"revisions"`
HumanItem struct {
Subject struct {
Payload struct {
Materialization struct {
Revision int `json:"revision"`
MetadataOnly bool `json:"metadataOnly"`
} `json:"materialization"`
} `json:"payload"`
} `json:"subject"`
} `json:"humanItem"`
}
if err := json.Unmarshal(response.Body.Bytes(), &detail); err != nil {
t.Fatal(err)
}
if len(detail.Revisions) != 1 || len(detail.Revisions[0].Segments) != len(want) {
t.Fatalf("quality partition lost: %s", response.Body.String())
}
generation := detail.HumanItem.Subject.Payload.Materialization.Revision
expectedGeneration := 1
if reuse {
expectedGeneration = 2
}
if generation != expectedGeneration || detail.HumanItem.Subject.Payload.Materialization.MetadataOnly != (reuse && len(selected) == 0) {
t.Fatalf("expected generation %d reuse %v: %s", expectedGeneration, reuse, response.Body.String())
}
path := filepath.Join(root, "system", "runs", f.task.RunID, "human-materializations", f.item.ID, f.task.ID, fmt.Sprintf("revision-%06d", generation), "slices", "slice-000001", "annotation.json")
encoded, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var exported struct {
Actions []map[string]any `json:"actions"`
Descriptions []map[string]any `json:"descriptions"`
}
if err = json.Unmarshal(encoded, &exported); err != nil {
t.Fatal(err)
}
if len(exported.Actions) != len(want) || !reflect.DeepEqual(exported.Actions, exported.Descriptions) {
t.Fatalf("exported partition/alias lost: %s", encoded)
}
identities := map[string]bool{}
for index, expected := range want {
action := detail.Revisions[0].Segments[index]
if action.ID == "" || identities[action.ID] || (expected.id != "" && action.ID != expected.id) || action.Start != expected.start || action.End != expected.end || action.Status != expected.status || action.Description != expected.description || action.IsInvalid != expected.invalid {
t.Fatalf("quality record %d: %+v", index, action)
}
identities[action.ID] = true
entry := exported.Actions[index]
if entry["id"] != action.ID || entry["status"] != expected.status || entry["description"] != expected.description || entry["isInvalid"] != expected.invalid || entry["clipStartMs"] != expected.start*1000 || entry["clipEndMs"] != expected.end*1000 || entry["startTimestampUs"] != expected.startUS || entry["endTimestampUs"] != expected.endUS {
t.Fatalf("exported record %d judgment/range lost: %s", index, encoded)
}
}
}
func TestInvalidSliceConsolidationRebuildsMediaAndPreservesPublishedActions(t *testing.T) {
actions := []domain.AnnotationSegment{{ID: "left-action", Start: 0, End: 1, Status: "described", Description: "拿起"}, {ID: "invalid-action", Start: 1, End: 2, Status: "invalid", IsInvalid: true, Description: "无效片段"}, {ID: "right-action", Start: 2, End: 4, Status: "described", Description: "放下"}}
want := []blankActionExpectation{{"left-action", "described", "拿起", false, 0, 1, 1000000, 2000000}, {"invalid-action", "invalid", "无效片段", true, 1, 2, 2000000, 3000000}, {"right-action", "described", "放下", false, 2, 4, 3000000, 5000000}}
checkBlankActionsDelivery(t, true, actions, want, true)
}