droboticflow/internal/api/client_movi_stats_test.go
d-robotics 54e47906c1 🎬 movi-player D-layer: vendor 0.4.1 slim dist, MoviVideo wrapper on E′ direct pipeline, telemetry endpoint, spikes pass
- vendor public/vendor/movi-player-0.4.1/ (element.slim.js 4.9MB + movi.wasm 5.4MB,
  Apache-2.0, SHA-256 pinned, upgrade = new versioned dir + MOVI_VERSION bump)
- src/lib/movi-player.ts: lazy loader (@vite-ignore dynamic import), runtime
  force flag (movi default / native fleet kill-switch / wasm canary), throttled
  best-effort telemetry reporter
- src/components/MoviVideo.tsx: engine="native wasm" + nohotkeys, HTMLMediaElement
  event bridge, #t= fragment split (startat + wrapper-managed end bound),
  in-place custom-element upgrade preserving node identity, wasmurl before connect
- mount points: LazyVideoPreview (evidence/artifact/object-storage previews),
  MaterializedSliceWorkbench dual-camera, CaptureAnnotationWorkbench main videos
  (moviElementReady gate: transport seek queue needs the element on first render;
  falls back to native E′ path until defined), page-level preload on
  AnnotationOperations
- POST /api/client/movi-stats: session-attributed, field-truncated, store errors
  swallowed (202); Mongo clientMoviStats collection w/ 14d TTL + org/kind indexes
- spikes (CDP, headless Edge 155, production hev1 480p30 sample): key conflicts
  PASS (17 keys all reach container dispatcher, zero playback-state interference,
  nohotkeys effective); frame-seek parity PASS (movi deltas identical to native
  <video>, ±1/30s steps both directions); deep probe: engine=native-wasm hands
  playback to shadow <video> with zero WASM fetches on HW-decode clients
- verification: tsc clean, gofmt/build/vet pass, frontend 644 tests net-zero new
  failures vs E′ baseline (6 new pass), Go API suite net-zero new failures
  (75 pre-existing; the one delta traced to a colleague's uncommitted part-index
  working-tree change), replica E2E: vendor assets served, module registers,
  telemetry lands in Mongo with TTL
2026-10-11 20:06:22 +05:30

128 lines
4.8 KiB
Go

package api
import (
"bytes"
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"droboticflow/internal/domain"
"droboticflow/internal/store"
)
type moviStatsFixture struct {
handler http.Handler
user domain.UserRecord
store *store.MemoryStore
}
func newMoviStatsFixture(t *testing.T) moviStatsFixture {
t.Helper()
dataStore := store.NewMemoryStore()
user := domain.UserRecord{ID: "movi-user", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAnnotator, Status: domain.UserStatusActive}
if err := dataStore.UpsertUser(context.Background(), user); err != nil {
t.Fatal(err)
}
return moviStatsFixture{handler: NewHandler(dataStore, nil), user: user, store: dataStore}
}
func (fixture moviStatsFixture) request(user domain.UserRecord, body any) *httptest.ResponseRecorder {
encoded, _ := json.Marshal(body)
request := httptest.NewRequest(http.MethodPost, "/api/client/movi-stats", bytes.NewReader(encoded))
request.Header.Set("Content-Type", "application/json")
csrf := "movi-stats-csrf"
request.AddCookie(&http.Cookie{Name: csrfCookieName, Value: csrf})
request.Header.Set(csrfHeaderName, csrf)
request = request.WithContext(withTestPrincipal(request.Context(), user))
recorder := httptest.NewRecorder()
fixture.handler.ServeHTTP(recorder, request)
return recorder
}
func withTestPrincipal(ctx context.Context, user domain.UserRecord) context.Context {
return context.WithValue(ctx, authContextKey{}, &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: user.OrganizationID, Status: domain.OrganizationStatusActive}})
}
func TestClientMoviStatsRequiresAuthentication(t *testing.T) {
fixture := newMoviStatsFixture(t)
encoded, _ := json.Marshal(map[string]any{"kind": "load"})
request := httptest.NewRequest(http.MethodPost, "/api/client/movi-stats", bytes.NewReader(encoded))
request.Header.Set("Content-Type", "application/json")
recorder := httptest.NewRecorder()
fixture.handler.ServeHTTP(recorder, request)
if recorder.Code != http.StatusUnauthorized {
t.Fatalf("unauthenticated status=%d body=%s", recorder.Code, recorder.Body.String())
}
}
func TestClientMoviStatsAcceptsAndAttributesSession(t *testing.T) {
fixture := newMoviStatsFixture(t)
recorder := fixture.request(fixture.user, map[string]any{
"kind": "qoe-heartbeat",
"decoder": "software",
"src": "/api/packages/pkg-1/content",
"message": strings.Repeat("x", 500),
"fatal": false,
"smooth": false,
"userID": "spoofed-user",
"orgID": "spoofed-org",
})
if recorder.Code != http.StatusAccepted {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
records, err := fixture.store.ListClientMoviStats(context.Background(), domain.DefaultOrganizationID, 10)
if err != nil || len(records) != 1 {
t.Fatalf("records=%#v err=%v", records, err)
}
record := records[0]
if record.UserID != fixture.user.ID || record.OrganizationID != domain.DefaultOrganizationID {
t.Fatalf("identity must come from the session, got user=%s org=%s", record.UserID, record.OrganizationID)
}
if record.Kind != "qoe-heartbeat" || record.Decoder != "software" {
t.Fatalf("payload fields not persisted: %#v", record)
}
if len(record.Message) != 300 {
t.Fatalf("message must be truncated to 300, got %d", len(record.Message))
}
if record.Smooth == nil || *record.Smooth {
t.Fatalf("smooth flag must persist as false, got %#v", record.Smooth)
}
}
func TestClientMoviStatsAcceptsEmptyAndOversizedPayloads(t *testing.T) {
fixture := newMoviStatsFixture(t)
empty := fixture.request(fixture.user, map[string]any{})
if empty.Code != http.StatusAccepted {
t.Fatalf("empty kind status=%d body=%s", empty.Code, empty.Body.String())
}
oversized := fixture.request(fixture.user, map[string]any{
"kind": strings.Repeat("k", 400),
"decoder": strings.Repeat("d", 400),
})
if oversized.Code != http.StatusAccepted {
t.Fatalf("oversized fields status=%d body=%s", oversized.Code, oversized.Body.String())
}
}
func TestClientMoviStatsListNewestFirst(t *testing.T) {
dataStore := store.NewMemoryStore()
first := domain.ClientMoviStatRecord{ID: "stat-1", OrganizationID: domain.DefaultOrganizationID, Kind: "load"}
second := domain.ClientMoviStatRecord{ID: "stat-2", OrganizationID: domain.DefaultOrganizationID, Kind: "qoe-startup"}
other := domain.ClientMoviStatRecord{ID: "stat-3", OrganizationID: "other-org", Kind: "load"}
for _, record := range []domain.ClientMoviStatRecord{first, second, other} {
if err := dataStore.InsertClientMoviStat(context.Background(), record); err != nil {
t.Fatal(err)
}
}
records, err := dataStore.ListClientMoviStats(context.Background(), domain.DefaultOrganizationID, 10)
if err != nil {
t.Fatal(err)
}
if len(records) != 2 || records[0].ID != "stat-2" || records[1].ID != "stat-1" {
t.Fatalf("expected newest-first filtered list, got %#v", records)
}
}