forked from longtao.wu/droboticflow
72 lines
3.7 KiB
Go
72 lines
3.7 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// Opt-in loopback preview for real UI interaction against the same claim
|
|
// handlers with isolated in-memory data. Never connects to a live datastore.
|
|
func TestRandomClaimLocalPreview(t *testing.T) {
|
|
address := os.Getenv("RANDOM_CLAIM_PREVIEW_ADDRESS")
|
|
if address == "" {
|
|
t.Skip("opt-in local UI preview")
|
|
}
|
|
if !strings.HasPrefix(address, "127.0.0.1:") {
|
|
t.Fatal("preview must bind loopback")
|
|
}
|
|
f := randomClaimFixture(t, 6)
|
|
seedSliceDictionary(t, f, "测试切片", "拿取商品", "检查商品")
|
|
f.annotator.Name = "验收标注员"
|
|
items, _ := f.store.ListHumanItems(f.ctx, domain.DefaultOrganizationID, domain.HumanItemQuery{})
|
|
for _, item := range items {
|
|
item.Subject.Payload = map[string]any{"captureDirectory": "preview-capture/" + item.ID, "sliceWorkflow": map[string]any{"status": "editing", "phase": "slice"}}
|
|
if os.Getenv("SLICE_DISCARD_PREVIEW") == "1" {
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"userId": f.annotator.ID, "operations": []domain.SliceOperation{}, "savedAt": nowISO(), "sliceSegments": []domain.SliceSegment{{ID: "a", Name: "保留前段", Start: 0, End: 3}, {ID: "b", Name: "舍弃测试中段", Start: 3, End: 7}, {ID: "c", Name: "保留后段", Start: 7, End: 10}}, "segments": []domain.AnnotationSegment{{ID: "old", Start: 3, End: 7, Description: "舍弃前的描述", Source: "manual"}}, "statuses": map[string]string{"b": "labeled"}}
|
|
if os.Getenv("ANNOTATION_EMPTY_PREVIEW") == "1" {
|
|
item.Subject.Payload["annotationWorkspace"].(map[string]any)["segments"] = []domain.AnnotationSegment{}
|
|
}
|
|
}
|
|
f.store.UpsertHumanItem(f.ctx, item)
|
|
}
|
|
h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r = r.WithContext(context.WithValue(r.Context(), authContextKey{}, &authenticatedPrincipal{User: f.annotator, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}}))
|
|
if r.URL.Path == "/api/auth/me" {
|
|
http.SetCookie(w, &http.Cookie{Name: csrfCookieName, Value: "local-claim-preview", Path: "/", SameSite: http.SameSiteLaxMode})
|
|
renderJSON(w, 200, map[string]any{"user": f.annotator, "organization": map[string]any{"id": domain.DefaultOrganizationID, "status": "active"}})
|
|
return
|
|
}
|
|
if r.URL.Path == "/api/plugins" {
|
|
renderJSON(w, 200, map[string]any{"items": []any{map[string]any{"enabled": true, "manifest": map[string]any{"id": "annotation-qa", "name": "标注中心", "version": "6", "navigation": []any{map[string]any{"id": "annotation", "label": "标注中心", "path": "/plugins/annotation-qa/workflows/all/annotate", "icon": "tags", "roles": []string{"annotator"}}}}}}})
|
|
return
|
|
}
|
|
if r.URL.Path == "/api/health" || r.URL.Path == "/api/ready" || r.URL.Path == "/worker-api/ready" {
|
|
renderJSON(w, 200, map[string]any{"status": "ok", "ready": true, "buildCommit": "local-random-claim"})
|
|
return
|
|
}
|
|
if strings.HasSuffix(r.URL.Path, "/frame-timeline") {
|
|
edges := make([]float64, 301)
|
|
for i := range edges {
|
|
edges[i] = float64(i) / 30
|
|
}
|
|
renderJSON(w, 200, map[string]any{"boundariesSeconds": edges, "rightBoundariesSeconds": edges, "frameCount": 300, "rangeStartSecond": 0, "rangeEndSecond": 10, "boundaryContract": "hardware-frame-v1"})
|
|
return
|
|
}
|
|
if strings.HasSuffix(r.URL.Path, "/subject-content") {
|
|
http.ServeFile(w, r, os.Getenv("RANDOM_CLAIM_PREVIEW_VIDEO"))
|
|
return
|
|
}
|
|
f.handler.ServeHTTP(w, r)
|
|
})
|
|
fmt.Println("Isolated in-memory random-claim preview listening on", address)
|
|
if err := http.ListenAndServe(address, h); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|