package api import ( "context" "droboticflow/internal/config" "droboticflow/internal/domain" "encoding/json" "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 TestSourceIndexLocalBrowserReplica(t *testing.T) { parent := os.Getenv("FANYU_SOURCE_INDEX_REPLICA_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=64x48:r=8:d=4", "-c:v", "libx265", "-threads", "1", "-x265-params", "pools=1:frame-threads=1:log-level=error", "-tag:v", "hvc1", "-pix_fmt", "yuv420p", filepath.Join(root, side+".mp4")) if output, err := command.CombinedOutput(); err != nil { t.Fatalf("HEVC browser fixture: %v %s", err, output) } } s := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true} slices := []domain.SliceSegment{{ID: "first", Name: "测试切片", Start: 0, End: 1}, {ID: "discard", Start: 1, End: 3, DeliveryDisposition: "discard", DiscardReason: "合成舍弃测试"}, {ID: "last", Name: "测试切片", Start: 3, End: 4}} result, e := s.materializeCombinedSliceSubmission(f.ctx, f.task, f.item, combinedAnnotationSubmissionInput{sliceOnly: true, SliceSegments: slices}, 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() if e = f.store.UpsertHumanItem(f.ctx, source); e != nil { t.Fatal(e) } t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true") listener, e := net.Listen("tcp", "127.0.0.1:3012") if e != nil { t.Fatal(e) } web := &http.Server{Handler: NewHandlerWithConfig(f.store, nil, config.AppConfig{WorkspaceMountPath: root, RequireSessionAuth: true}), ReadHeaderTimeout: 5 * time.Second} go web.Serve(listener) defer web.Shutdown(context.Background()) b, _ := json.Marshal(map[string]any{"phone": admin.Phone, "password": password, "api": "http://127.0.0.1:3012", "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:3012; credentials in private local browser-context.json") deadline := time.NewTimer(20 * time.Minute) 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 } } } }