forked from longtao.wu/droboticflow
185 lines
8.3 KiB
Go
185 lines
8.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/config"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// Actual page -> actual HTTP handler -> isolated memory store. Neither a
|
|
// production endpoint nor the local snapshot database is reachable here.
|
|
func TestReworkBoundaryBrowserReplica(t *testing.T) {
|
|
if os.Getenv("REWORK_BOUNDARY_BROWSER_TEST") != "1" {
|
|
t.Skip("local browser opt-in")
|
|
}
|
|
f := continuousFixture(t)
|
|
reason := dispositionFixturePayload(t, f)
|
|
selection, err := f.store.GetProjectAnnotationLabels(f.ctx, f.task.ProjectID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
label := domain.AnnotationLabelRecord{ID: "browser-extra-slice-name", OrganizationID: f.task.OrganizationID, Kind: "slice", Code: "browser-extra", Name: "补充切片名称", Status: domain.AnnotationLabelActive}
|
|
if err := f.store.UpsertAnnotationLabel(f.ctx, label); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
selection.SliceLabelIDs = append(selection.SliceLabelIDs, label.ID)
|
|
if err := f.store.UpsertProjectSliceLabels(f.ctx, *selection); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertOrganizationPlugin(f.ctx, domain.OrganizationPluginRecord{OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", Enabled: true, Config: defaultAnnotationPluginConfig()}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
reset := func(side, status string, round int) error {
|
|
var before []domain.SliceSegment
|
|
d := domain.SliceSegment{ID: "d", Start: 0, End: 5, DeliveryDisposition: "discard", DiscardReason: "相机被遮挡", DiscardReasonLabelID: reason.ReasonLabelID, DiscardReasonVersion: reason.ReasonVersion}
|
|
k := domain.SliceSegment{ID: "k", Name: "测试切片", Start: 5, End: 10}
|
|
if side == "unnamed" {
|
|
k.Name = ""
|
|
}
|
|
if side == "right" {
|
|
k.Start, k.End = 0, 5
|
|
d.Start, d.End = 5, 10
|
|
before = []domain.SliceSegment{k, d}
|
|
} else {
|
|
before = []domain.SliceSegment{d, k}
|
|
}
|
|
a := domain.AnnotationSegment{ID: "a", Start: k.Start, End: k.End, Status: status}
|
|
if status == "invalid" {
|
|
a.IsInvalid = true
|
|
a.Description = "无效片段"
|
|
}
|
|
if status == "described" {
|
|
a.Description = "合成有效动作"
|
|
}
|
|
item := f.item
|
|
item.Subject.ArtifactID = "left-video"
|
|
item.ReviewRound = round
|
|
actions := []domain.AnnotationSegment{a}
|
|
item.Subject.Payload = map[string]any{"rightArtifactId": "right-video", "sliceWorkflow": map[string]any{"status": "completed", "phase": "annotate"}, "annotationWorkspace": map[string]any{"userId": f.annotator.ID, "savedAt": nowISO(), "sliceSegments": before, "segments": actions, "statuses": map[string]string{"k": "pending", "d": "discarded"}}}
|
|
if side == "legacy" {
|
|
delete(item.Subject.Payload, "sliceWorkflow")
|
|
draft := mapValue(item.Subject.Payload["annotationWorkspace"])
|
|
draft["segments"] = []domain.AnnotationSegment{{ID: "newer-described", Start: 5, End: 7, Status: "described", Description: "最新草稿描述"}, {ID: "newer-pending", Start: 7, End: 10, Status: "pending"}}
|
|
draft["statuses"] = map[string]string{"k": "pending", "d": "pending"}
|
|
draft["savedAt"] = nowISO()
|
|
draft["savedVersion"] = item.Version
|
|
item.Subject.Payload["annotationDraft"] = draft
|
|
}
|
|
return f.store.UpsertHumanItem(f.ctx, item)
|
|
}
|
|
root := t.TempDir()
|
|
f.task.SourceVideo = "/api/runs/" + f.task.RunID + "/artifacts/left-video/content"
|
|
f.task.RightSourceVideo = "/api/runs/" + f.task.RunID + "/artifacts/right-video/content"
|
|
f.task.NodeID = "human"
|
|
f.task.ArtifactID = "left-video"
|
|
f.task.RightArtifactID = "right-video"
|
|
f.task.SourceIsFullVideo = true
|
|
f.task.SourceDurationSeconds = 10
|
|
f.task.CoverageEndSecond = 10
|
|
pts := "timestamp_us\n"
|
|
for i := 0; i < 100; i++ {
|
|
pts += fmt.Sprintf("%d\n", 1000000+i*100000)
|
|
}
|
|
bindings := []domain.NodeInputBinding{}
|
|
mediaArtifacts := []domain.ExecutionArtifact{}
|
|
for _, side := range []string{"left", "right"} {
|
|
video := filepath.Join(root, side+".mp4")
|
|
if out, err := exec.Command("ffmpeg", "-v", "error", "-y", "-f", "lavfi", "-i", "color=c=navy:s=160x90:r=10:d=10", "-c:v", "libx264", "-pix_fmt", "yuv420p", video).CombinedOutput(); err != nil {
|
|
t.Fatalf("video: %v: %s", err, out)
|
|
}
|
|
timestamps := filepath.Join(root, side+"_pts.csv")
|
|
mediaArtifacts = append(mediaArtifacts, domain.ExecutionArtifact{ID: side + "-video", Name: side + ".mp4", Path: video, Type: domain.PortTypeVideo})
|
|
if err := os.WriteFile(timestamps, []byte(pts), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
port := "fullLeftVideos"
|
|
if side == "right" {
|
|
port = "fullRightVideos"
|
|
}
|
|
bindings = append(bindings, domain.NodeInputBinding{TargetPortID: port, Artifacts: []domain.ExecutionArtifact{{ID: side + "-video", Name: side + ".mp4", Path: video, Data: map[string]any{"collectionId": "capture"}}}}, domain.NodeInputBinding{TargetPortID: side + "Timestamps", Artifacts: []domain.ExecutionArtifact{{ID: side + "-pts", Name: side + "_pts.csv", Path: timestamps, Data: map[string]any{"collectionId": "capture"}}}})
|
|
}
|
|
imu := "timestamp_us,x,y,z,type\n"
|
|
for i := 0; i < 100; i++ {
|
|
stamp := 1000000 + i*100000
|
|
imu += fmt.Sprintf("%d,1,2,3,accel\n%d,4,5,6,gyro\n", stamp, stamp)
|
|
}
|
|
imuPath := filepath.Join(root, "imu.csv")
|
|
if err := os.WriteFile(imuPath, []byte(imu), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bindings = append(bindings, domain.NodeInputBinding{TargetPortID: "imu", Artifacts: []domain.ExecutionArtifact{{ID: "imu", Name: "imu.csv", Path: imuPath, Data: map[string]any{"collectionId": "capture"}}}})
|
|
if err := f.store.UpsertRun(f.ctx, domain.RunRecord{ID: f.task.RunID, OrganizationID: f.task.OrganizationID, ProjectID: f.task.ProjectID, WorkflowID: f.task.WorkflowID, Artifacts: mediaArtifacts, NodeExecutions: []domain.NodeExecutionRecord{{NodeID: f.task.NodeID, InputBindings: bindings}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertAnnotationTask(f.ctx, f.task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := reset("left", "described", 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
processorContext, stopProcessor := context.WithCancel(t.Context())
|
|
processorDone := make(chan struct{})
|
|
go func() {
|
|
defer close(processorDone)
|
|
_ = RunAnnotationProcessor(processorContext, f.store, config.AppConfig{WorkspaceMountPath: root}, 1, false)
|
|
}()
|
|
defer func() { stopProcessor(); <-processorDone }()
|
|
handler := NewHandlerWithConfig(f.store, nil, config.AppConfig{WorkspaceMountPath: root})
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path == "/api/auth/me" {
|
|
renderJSON(w, 200, map[string]any{"user": sanitizeUser(&f.annotator), "organization": domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}})
|
|
return
|
|
}
|
|
if r.URL.Path == "/__boundary/reset" {
|
|
round := 1
|
|
if r.URL.Query().Get("round") == "0" {
|
|
round = 0
|
|
}
|
|
if err := reset(r.URL.Query().Get("side"), r.URL.Query().Get("status"), round); err != nil {
|
|
renderError(w, 500, err)
|
|
return
|
|
}
|
|
renderJSON(w, 200, map[string]any{"ok": true})
|
|
return
|
|
}
|
|
if r.URL.Path == "/__boundary/state" {
|
|
item, _ := f.store.GetHumanItem(r.Context(), f.item.ID)
|
|
renderJSON(w, 200, item)
|
|
return
|
|
}
|
|
if r.Method != "GET" && r.Method != "HEAD" && !strings.HasSuffix(r.URL.Path, "/slice-edit-applications") && !strings.HasSuffix(r.URL.Path, "/slice-edit-restorations") && !strings.HasSuffix(r.URL.Path, "/slice-confirmations") && !strings.HasSuffix(r.URL.Path, "/annotation-confirmations") && !strings.HasSuffix(r.URL.Path, "/drafts") {
|
|
renderJSON(w, 403, map[string]string{"error": "fixture write not permitted"})
|
|
return
|
|
}
|
|
r.AddCookie(&http.Cookie{Name: csrfCookieName, Value: "boundary-local-csrf"})
|
|
r.Header.Set(csrfHeaderName, "boundary-local-csrf")
|
|
r = r.WithContext(context.WithValue(r.Context(), authContextKey{}, &authenticatedPrincipal{User: f.annotator, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}}))
|
|
handler.ServeHTTP(w, r)
|
|
}))
|
|
defer server.Close()
|
|
ctx, cancel := context.WithTimeout(t.Context(), 4*time.Minute)
|
|
defer cancel()
|
|
script := os.Getenv("ANNOTATION_FEEDBACK_BROWSER_SCRIPT")
|
|
if script == "" {
|
|
script = "tests/browser/rework-boundary-replica.mjs"
|
|
}
|
|
cmd := exec.CommandContext(ctx, "node", script)
|
|
cmd.Dir = "../.."
|
|
cmd.Env = append(os.Environ(), "REWORK_BOUNDARY_API="+server.URL)
|
|
out, err := cmd.CombinedOutput()
|
|
t.Log(string(out))
|
|
if err != nil {
|
|
t.Fatal(fmt.Errorf("browser: %w", err))
|
|
}
|
|
}
|