droboticflow/internal/api/acceptance_manual_preview_test.go

284 lines
15 KiB
Go

package api
import (
"context"
"droboticflow/internal/config"
"droboticflow/internal/domain"
"droboticflow/internal/productionstats"
"droboticflow/internal/store"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
)
// Interactive local replica only; the memory store, synthesized media and
// random test password cannot reach production Mongo/BOS or customer records.
func TestAcceptanceManualPreview(t *testing.T) {
parent := os.Getenv("FANYU_ACCEPTANCE_MANUAL_DIR")
if parent == "" {
t.Skip("opt-in interactive synthetic browser replica")
}
if !filepath.IsAbs(parent) {
t.Fatal("absolute local output required")
}
if e := os.MkdirAll(parent, 0700); e != nil {
t.Fatal(e)
}
f, root := annotationProcessorFixture(t)
for _, side := range []string{"left", "right"} {
command := exec.Command("ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i", "testsrc2=s=640x360:r=30:d=12", "-c:v", "libx264", "-threads", "1", "-preset", "ultrafast", "-movflags", "+faststart", "-pix_fmt", "yuv420p", filepath.Join(root, side+".mp4"))
if output, err := command.CombinedOutput(); err != nil {
t.Fatalf("preview media: %v %s", err, output)
}
}
pts, imu := "timestamp_us\n", "timestamp_us,x,y,z,type\n"
for n := 0; n < 360; n++ {
stamp := 1000000 + int64(n)*1000000/30
pts += fmt.Sprintf("%d\n", stamp)
imu += fmt.Sprintf("%d,1,2,3,accel\n%d,4,5,6,gyro\n", stamp, stamp)
}
for _, side := range []string{"left", "right"} {
if e := os.WriteFile(filepath.Join(root, side+"_pts.csv"), []byte(pts), 0600); e != nil {
t.Fatal(e)
}
}
if e := os.WriteFile(filepath.Join(root, "imu.csv"), []byte(imu), 0600); e != nil {
t.Fatal(e)
}
f.task.ClipEnd, f.task.SourceDurationSeconds, f.task.CoverageEndSecond = 12, 12, 12
f.task.CurrentRevision = 1
f.task.SupplierID = f.annotator.SupplierID
if e := f.store.UpsertAnnotationTask(f.ctx, f.task); e != nil {
t.Fatal(e)
}
run, e := f.store.GetRun(f.ctx, f.task.RunID)
if e != nil || run == nil {
t.Fatal("preview run unavailable", e)
}
for _, execution := range run.NodeExecutions {
for _, binding := range execution.InputBindings {
run.Artifacts = append(run.Artifacts, binding.Artifacts...)
}
}
if e = f.store.UpsertRun(f.ctx, *run); e != nil {
t.Fatal(e)
}
seedSliceDictionary(t, f, "商品盘点", "门店巡视")
s := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true}
slices := []domain.SliceSegment{{ID: "first", Name: "商品盘点", Start: 0, End: 4}, {ID: "discard", Start: 4, End: 8, DeliveryDisposition: "discard", DiscardReason: "玩手机"}, {ID: "last", Name: "门店巡视", Start: 8, End: 12}}
actions := []domain.AnnotationSegment{{ID: "a1", Start: 0, End: 4, Description: "核对货架上的商品", Source: "manual"}, {ID: "a2", Start: 8, End: 12, Description: "巡视门店", Source: "manual"}}
result, e := s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, combinedAnnotationSubmissionInput{SliceSegments: slices, Segments: actions}, 1)
if e != nil {
t.Fatal(e)
}
password, e := randomURLToken(24)
if e != nil {
t.Fatal(e)
}
hash, e := hashPassword(password)
if e != nil {
t.Fatal(e)
}
admin := f.admin
admin.ID = "source-index-local-reviewer"
admin.Phone = "13900009111"
admin.Name = "本地验收员"
admin.PasswordHash = hash
admin.Role = domain.UserRoleAcceptanceReviewer
admin.SupplierID = "source-index-local-reviewer"
admin.AcceptanceAccessMode = "managed"
if e = f.store.UpsertSupplier(f.ctx, domain.SupplierRecord{ID: admin.SupplierID, OrganizationID: admin.OrganizationID, Name: "隔离源引用验收", Status: "active"}); e != nil {
t.Fatal(e)
}
if _, e = f.store.SetSupplierAcceptanceAuthorization(f.ctx, admin.OrganizationID, admin.SupplierID, true, 0, f.admin.ID); e != nil {
t.Fatal(e)
}
if e = f.store.UpsertUser(f.ctx, admin); e != nil {
t.Fatal(e)
}
if e = f.store.UpsertOrganizationPlugin(f.ctx, domain.OrganizationPluginRecord{OrganizationID: f.task.OrganizationID, PluginID: "annotation-qa", Enabled: true, Config: defaultAnnotationPluginConfig()}); e != nil {
t.Fatal(e)
}
sourceBatch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
sourceBatch.RequirePrePackageAcceptance = true
sourceBatch.ProcessingProfile = domain.ProcessingProfileRawHuman
if e = f.store.UpsertHumanBatch(f.ctx, *sourceBatch); e != nil {
t.Fatal(e)
}
batch := *sourceBatch
batch.ID = fanyuV5PrePackageAcceptanceBatchID(sourceBatch.ID)
batch.SourceBatchID = sourceBatch.ID
batch.GateNodeID = "source-demo-acceptance"
batch.RequirePrePackageAcceptance = false
batch.Stages = []domain.HumanStageDefinition{{ID: fanyuV5PrePackageAcceptanceStageID, Kind: domain.HumanStageAcceptance, RequiredRole: "acceptance_reviewer"}}
if e = f.store.UpsertHumanBatch(f.ctx, batch); e != nil {
t.Fatal(e)
}
item := f.item
item.ID = fanyuV5PrePackageAcceptanceItemID(sourceBatch.ID)
item.BatchID = batch.ID
item.AssignedUserID = ""
item.CurrentStageID = fanyuV5PrePackageAcceptanceStageID
item.Status = domain.HumanItemReady
item.ProcessingProfile = domain.ProcessingProfileRawHuman
item.Subject = domain.HumanSubjectRef{ItemKey: "source-index-demo", Payload: map[string]any{"processingProfile": "raw_human", "items": []map[string]any{{"taskId": f.task.ID, "runId": f.task.RunID, "sourceHumanItemId": f.item.ID, "captureDirectory": "source-index-demo", "materialization": map[string]any{"runId": f.task.RunID, "artifactId": result.Artifact.ID, "revision": 1, "sliceCount": 3, "coverage": result.Payload["coverage"]}, "finalSlices": result.Slices, "annotation": result.Segments}}}}
if e = f.store.UpsertHumanItem(f.ctx, item); e != nil {
t.Fatal(e)
}
source := f.item
source.CurrentStageID = domain.FanyuV58StageQualityReview
source.Status = domain.HumanItemAccepted
source.CurrentRevision = 1
source.AcceptedAt = nowISO()
source.SupplierID, source.LatestAuthorUserID, source.MaxReworkRounds = f.annotator.SupplierID, f.annotator.ID, 10
source.Subject.ArtifactID = f.task.ArtifactID
source.Subject.Payload = result.Payload
source.Subject.Payload["rightArtifactId"] = f.task.RightArtifactID
source.Subject.Payload["captureDirectory"] = "本地验收演示/part0001"
source.Subject.Payload["annotationWorkspace"] = map[string]any{"userId": f.annotator.ID, "sliceSegments": slices, "segments": actions, "statuses": map[string]string{"first": "labeled", "last": "labeled"}}
if e = f.store.UpsertSliceRevision(f.ctx, domain.SliceRevisionRecord{ID: "preview-slice", OrganizationID: source.OrganizationID, TaskID: f.task.ID, Revision: 1, AuthorUserID: f.annotator.ID, Segments: slices, CreatedAt: nowISO()}); e != nil {
t.Fatal(e)
}
if e = f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "preview-annotation", OrganizationID: source.OrganizationID, TaskID: f.task.ID, Revision: 1, AuthorUserID: f.annotator.ID, Segments: actions, CountsAsOutput: true, CreatedAt: nowISO()}); e != nil {
t.Fatal(e)
}
if e = f.store.UpsertHumanItem(f.ctx, source); e != nil {
t.Fatal(e)
}
credentials := map[string]map[string]string{}
for i, u := range []domain.UserRecord{f.admin, f.annotator, f.reviewer, admin} {
u.Phone = fmt.Sprintf("1390000920%d", i)
u.PasswordHash = hash
u.Status = domain.UserStatusActive
if i == 0 {
u.Name = "本地管理员"
}
if i == 1 {
u.Name = "本地标注员"
}
if i == 2 {
u.Name = "本地质检员"
}
if e = f.store.UpsertUser(f.ctx, u); e != nil {
t.Fatal(e)
}
credentials[[]string{"admin", "annotator", "quality", "acceptance"}[i]] = map[string]string{"phone": u.Phone, "password": password}
}
if _, e = f.store.SaveProductionStatsScope(f.ctx, source.OrganizationID, productionstats.Scope{ProjectIDs: []string{source.ProjectID}, Revision: 1}, 0); e != nil {
t.Fatal(e)
}
seedManualPreviewOutput(t, f)
// A separate returned source lets QC forwarding be tried before consuming the acceptance example.
qcSource := source
qcSource.ID = "manual-quality-item"
qcSource.LegacyTaskID = "manual-quality-task"
qcSource.BatchID = "manual-quality-batch"
qcSource.Status = domain.HumanItemSubmitted
qcSource.AssignedUserID = ""
qcSource.ReviewRound = 1
qcSource.ReviewOperationID = "manual-acceptance-return"
qcSource.AcceptedAt = ""
qcSource.Subject = domain.HumanSubjectRef{ItemKey: "本地返工演示/part0002", Payload: map[string]any{"captureDirectory": "本地返工演示/part0002", "finalSlices": slices, "annotation": actions, "sliceWorkflow": map[string]any{"phase": "annotate", "status": "completed"}}}
qcSource.Subject.ArtifactID = f.task.ArtifactID
qcSource.Subject.Payload["rightArtifactId"] = f.task.RightArtifactID
qcTask := f.task
qcTask.ID = qcSource.LegacyTaskID
qcTask.Status = domain.AnnotationTaskStatusPendingQuality
qcTask.AssignedUserID = ""
qcBatch := *sourceBatch
qcBatch.ID = qcSource.BatchID
qcBatch.GateNodeID = "manual-quality-gate"
for _, e := range []error{f.store.UpsertHumanBatch(f.ctx, qcBatch), f.store.UpsertAnnotationTask(f.ctx, qcTask), f.store.UpsertHumanItem(f.ctx, qcSource), f.store.UpsertSliceRevision(f.ctx, domain.SliceRevisionRecord{ID: "qc-slice", OrganizationID: source.OrganizationID, TaskID: qcTask.ID, Revision: 1, AuthorUserID: f.annotator.ID, Segments: slices, CreatedAt: nowISO()}), f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "qc-annotation", OrganizationID: source.OrganizationID, TaskID: qcTask.ID, Revision: 1, AuthorUserID: f.annotator.ID, Segments: actions, CountsAsOutput: true, CreatedAt: nowISO()})} {
if e != nil {
t.Fatal(e)
}
}
op := domain.ReviewOperation{ID: qcSource.ReviewOperationID, OrganizationID: source.OrganizationID, ProjectID: source.ProjectID, ItemID: "manual-previous-acceptance", StageID: "prepackage_acceptance", UserID: admin.ID, Status: "completed", Decision: "rejected", ReviewRound: 1, SourceItemIDs: []string{qcSource.ID}, CreatedAt: nowISO(), Effects: []domain.ReviewDecisionEffect{{Item: &qcSource}}, Draft: domain.ReviewDraft{Problems: []domain.ReviewProblem{{ID: "acceptance-note", SourceItemID: qcSource.ID, Scope: "timestamp", SliceID: "first", AtSeconds: 1, TimeBasis: "slice-seconds", Text: "验收示例:请核对这一帧的动作描述"}}}}
if ok, e := f.store.CompareAndSwapReviewOperation(f.ctx, op, 0); e != nil || !ok {
t.Fatal(ok, e)
}
frozen := item
frozen.Subject.Payload = map[string]any{"items": []map[string]any{{"sourceHumanItemId": qcSource.ID, "finalSlices": slices, "annotation": actions}}}
baseline := acceptanceSourceBaseline(&op, qcSource, frozen, 1)
baseline.ID = store.ReworkBaselineID(qcSource.ID, 1)
if e = f.store.UpsertReworkBaseline(f.ctx, *baseline); e != nil {
t.Fatal(e)
}
accountsJSON, _ := json.Marshal(credentials)
html := `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>本地人工测试</title><style>body{font:16px system-ui;background:#f4f7fa;color:#182234;max-width:850px;margin:70px auto;padding:24px}h1{font-size:30px}section{display:grid;grid-template-columns:1fr 1fr;gap:18px;margin:28px 0}button{background:white;border:1px solid #dde3eb;border-radius:12px;padding:25px;text-align:left;font:inherit;cursor:pointer;box-shadow:0 4px 18px #00000006}button:hover{border-color:#ff7800}strong{display:block;font-size:20px;margin-bottom:8px}p{color:#627087;line-height:1.7}#error{color:#c22}a{color:#d96700}</style><h1>Drobodata 本地人工测试</h1><p>使用本次修改的真实页面和后端。账号、任务、视频均为本机合成数据,可保存、提交和退回;重启预览后重建。</p><section><button onclick="enter('admin','/?view=dashboard')"><strong>管理员看板</strong>查看日期筛选、产出、质量和人员统计</button><button onclick="enter('acceptance','/plugins/annotation-qa/workflows/all/acceptance')"><strong>验收工作台</strong>领取样例,测试切片、备注、通过置灰和打回</button><button onclick="enter('quality','/plugins/annotation-qa/workflows/all/quality')"><strong>质检返工</strong>已有一条验收退回任务,可补充意见后退回标注员</button><button onclick="enter('annotator','/plugins/annotation-qa/workflows/all/annotate')"><strong>标注员</strong>先在质检侧退回,再检查合并意见、修改和保存</button></section><p>切换身份前请保存当前编辑。之后可返回此页继续切换;同一浏览器内按一个身份依次测试。</p><p id="error"></p><script>const accounts=` + string(accountsJSON) + `;async function enter(role,path){const r=await fetch('/api/auth/login',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(accounts[role])});if(!r.ok){document.querySelector('#error').textContent='登录失败,请联系我检查本地预览';return}location.href=path}</script>`
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
listener, e := net.Listen("tcp", "127.0.0.1:3039")
if e != nil {
t.Fatal(e)
}
handler := NewHandlerWithConfig(f.store, nil, config.AppConfig{WorkspaceMountPath: root, RequireSessionAuth: true})
web := &http.Server{Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/api/local-preview" && r.Method == http.MethodGet {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Cache-Control", "no-store")
fmt.Fprint(w, html)
return
}
handler.ServeHTTP(w, r)
}), ReadHeaderTimeout: 5 * time.Second}
go web.Serve(listener)
defer web.Shutdown(context.Background())
b, _ := json.Marshal(map[string]any{"accounts": credentials, "api": "http://127.0.0.1:3039", "acceptanceId": item.ID, "projectId": item.ProjectID, "workspace": root})
if e = os.WriteFile(filepath.Join(parent, "browser-context.json"), b, 0600); e != nil {
t.Fatal(e)
}
t.Log("synthetic API listening at 127.0.0.1:3039; credentials in private local browser-context.json")
deadline := time.NewTimer(24 * time.Hour)
defer deadline.Stop()
tick := time.NewTicker(time.Second)
defer tick.Stop()
for {
select {
case <-deadline.C:
return
case <-tick.C:
if _, e := os.Stat(filepath.Join(parent, "browser-done")); e == nil {
return
}
}
}
}
func seedManualPreviewOutput(t *testing.T, f combinedDraftFixture) {
t.Helper()
for i := 0; i < 12; i++ {
at := time.Now().UTC().Add(-time.Duration(2+i%7) * 24 * time.Hour).Format(time.RFC3339)
id := fmt.Sprintf("manual-history-%02d", i)
task := f.task
task.ID = id
task.RunID = ""
task.DataTaskID = ""
task.Status = domain.AnnotationTaskStatusAccepted
task.SourceDurationSeconds = 600 + float64(i*90)
task.CreatedAt = at
e := f.store.UpsertAnnotationTask(f.ctx, task)
if e != nil {
t.Fatal(e)
}
for _, e = range []error{f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: id + "-r", OrganizationID: task.OrganizationID, TaskID: id, Revision: 1, AuthorUserID: f.annotator.ID, CountsAsOutput: true}), f.store.UpsertAnnotationWorklogEvent(f.ctx, domain.AnnotationWorklogEventRecord{ID: id + "-s", OrganizationID: task.OrganizationID, ProjectID: task.ProjectID, TaskID: id, UserID: f.annotator.ID, SupplierID: f.annotator.SupplierID, EventType: "combined_submitted", Revision: 1, CreatedAt: at})} {
if e != nil {
t.Fatal(e)
}
}
decision := domain.AnnotationReviewApproved
if i%4 == 0 {
decision = domain.AnnotationReviewRejected
}
if e = f.store.UpsertAnnotationReview(f.ctx, domain.AnnotationReviewRecord{ID: id + "-q", OrganizationID: task.OrganizationID, TaskID: id, Revision: 1, Stage: domain.AnnotationReviewStageQuality, Decision: decision, ReviewerUserID: f.reviewer.ID, CreatedAt: at}); e != nil {
t.Fatal(e)
}
}
}