droboticflow/internal/api/slice_phase_browser_test.go
d-robotics 867ca69835 🐛 Keep initial slicing conflicts visible and repair live rework feedback
Slice-phase quick edits with pending actions were canceled before history recording because conflict rendering and teardown were annotation-only. Preserve the actions and use the existing atomic application and restoration flow in both editing phases. Compare complete live editor drafts without derived completion status, and keep discarded-slice evidence and opinions visible.
2026-10-21 14:43:36 +08:00

155 lines
8.0 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"
"droboticflow/internal/store"
)
// Actual page -> actual HTTP handler -> isolated memory store. Neither a
// production endpoint nor the local snapshot database is reachable here.
func TestSlicePhaseBrowserReplica(t *testing.T) {
if os.Getenv("REWORK_BOUNDARY_BROWSER_TEST") != "1" {
t.Skip("local browser opt-in")
}
f := continuousFixture(t)
reason := dispositionFixturePayload(t, f)
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(status, shape string) error {
before := []domain.SliceSegment{{ID: "first", Start: 0, End: 10}}
item := f.item
item.ReviewRound = 0
actions := []domain.AnnotationSegment{{ID: "pending-first", Start: 0, End: 10, Status: "pending"}}
if status == "empty" {
actions = []domain.AnnotationSegment{}
}
item.Subject.Payload = map[string]any{"sliceWorkflow": map[string]any{"status": "editing", "phase": "slice"}, "annotationWorkspace": map[string]any{"userId": f.annotator.ID, "savedAt": nowISO(), "sliceSegments": before, "segments": actions, "statuses": map[string]string{"first": "pending"}}}
if status == "draft" {
item.Subject.Payload["annotationDraft"] = map[string]any{"userId": f.annotator.ID, "savedAt": time.Now().Add(time.Second).UTC().Format(time.RFC3339Nano), "savedVersion": item.Version, "sliceSegments": before, "segments": actions, "statuses": map[string]string{"first": "pending"}}
item.Subject.Payload["annotationWorkspace"].(map[string]any)["segments"] = []domain.AnnotationSegment{}
}
if shape == "two" {
before = []domain.SliceSegment{{ID: "first", Name: "测试切片", Start: 0, End: 0.2}, {ID: "second", Start: 0.2, End: 10}}
actions = []domain.AnnotationSegment{{ID: "a", Start: 0, End: 0.2, Status: "pending"}, {ID: "b", Start: 0.2, End: 10, Status: "pending"}}
item.Subject.Payload["annotationWorkspace"].(map[string]any)["sliceSegments"] = before
item.Subject.Payload["annotationWorkspace"].(map[string]any)["segments"] = actions
}
if shape == "rework" {
before = []domain.SliceSegment{{ID: "first", Name: "测试切片", Start: 0, End: 5, AnnotationStatus: "pending"}, {ID: "discard", Start: 5, End: 10, DeliveryDisposition: "discard", DiscardReason: "相机被遮挡", DiscardReasonLabelID: reason.ReasonLabelID, DiscardReasonVersion: reason.ReasonVersion}}
actions = []domain.AnnotationSegment{{ID: "a", Start: 0, End: 5, Status: "described", Description: "原描述"}}
item.ReviewRound = 1
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
item.Subject.Payload["annotationWorkspace"].(map[string]any)["sliceSegments"] = before
item.Subject.Payload["annotationWorkspace"].(map[string]any)["segments"] = actions
frozen := append([]domain.SliceSegment(nil), before...)
frozen[0].AnnotationStatus = "labeled"
if err := f.store.UpsertReworkBaseline(f.ctx, domain.ReworkBaselineRecord{ID: store.ReworkBaselineID(item.ID, 1), OrganizationID: item.OrganizationID, ProjectID: item.ProjectID, ItemID: item.ID, Round: 1, StageID: domain.FanyuV58StageAnnotationCombined, Slices: frozen, Actions: actions, Problems: []domain.ReviewProblem{{ID: "p-slice", Scope: "slice", SliceID: "first", Text: "核对切片"}, {ID: "p-action", Scope: "action", SliceID: "first", ActionID: "a", Text: "核对描述"}}}); err != nil {
return err
}
}
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{}
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")
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, 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("pending", ""); err != nil {
t.Fatal(err)
}
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" {
if err := reset(r.URL.Query().Get("status"), r.URL.Query().Get("shape")); 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, "/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(), 3*time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, "node", "tests/browser/slice-phase-replica.mjs")
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))
}
}