package api import ( "bytes" "context" "net/http" "net/http/httptest" "testing" "time" "droboticflow/internal/domain" "droboticflow/internal/store" ) func TestDashboardResponseCacheExpiryAndInvalidation(t *testing.T) { current := time.Unix(1700000000, 0) cache := dashboardResponseCache{now: func() time.Time { return current }} cache.set("k", http.StatusOK, "application/json", []byte(`{"a":1}`)) if entry := cache.get("k"); entry == nil || string(entry.body) != `{"a":1}` || entry.status != http.StatusOK { t.Fatalf("expected fresh entry, got %+v", entry) } cache.set("k2", http.StatusOK, "application/json", []byte(`{"b":2}`)) cache.invalidateAll() if cache.get("k") != nil || cache.get("k2") != nil { t.Fatal("invalidateAll must drop every entry") } cache.set("k", http.StatusOK, "application/json", []byte(`{"a":1}`)) current = current.Add(dashboardResponseCacheTTL + time.Second) if entry := cache.get("k"); entry != nil { t.Fatalf("expected TTL expiry, got %+v", entry) } } func TestDashboardResponseCacheBoundsDropped(t *testing.T) { current := time.Unix(1700000000, 0) cache := dashboardResponseCache{now: func() time.Time { return current }} for i := 0; i < 200; i++ { cache.set(string(rune('a'+i%26))+string(rune('a'+i/26)), http.StatusOK, "", []byte{byte(i)}) } if entry := cache.get("aa"); entry != nil { t.Fatalf("bounded cache should have dropped the oldest entries, got %+v", entry) } } func TestDashboardHumanWorkStatsMatchesLegacyEnrichOnMemoryStore(t *testing.T) { ctx := context.Background() dataStore := store.NewMemoryStore() seedDashboardHumanWorkFixtures(t, dataStore) legacy := &domain.DashboardSummary{} server := &server{store: dataStore} request := httptest.NewRequest(http.MethodGet, "/api/dashboard", nil) server.enrichDashboardHumanWork(ctx, request, legacy) counted := &domain.DashboardSummary{} stats, err := dataStore.DashboardHumanWorkStats(ctx, domain.DefaultOrganizationID) if err != nil { t.Fatal(err) } applyDashboardHumanWorkStats(counted, stats) if legacy.Tasks.PendingAnnotation != counted.Tasks.PendingAnnotation || legacy.Tasks.PendingQuality != counted.Tasks.PendingQuality || legacy.Tasks.InAnnotation != counted.Tasks.InAnnotation || legacy.Tasks.PendingAcceptance != counted.Tasks.PendingAcceptance || legacy.Tasks.Rejected != counted.Tasks.Rejected || legacy.Tasks.Escalated != counted.Tasks.Escalated || legacy.Tasks.QualityPassRate != counted.Tasks.QualityPassRate || legacy.Tasks.Waiting != counted.Tasks.Waiting || legacy.Data.VideoCount != counted.Data.VideoCount || legacy.Data.PackageCount != counted.Data.PackageCount || legacy.Data.CaptureDurationSeconds != counted.Data.CaptureDurationSeconds { t.Fatalf("counters diverge:\nlegacy=%+v/%+v\ncounted=%+v/%+v", legacy.Tasks, legacy.Data, counted.Tasks, counted.Data) } if counted.Tasks.PendingAnnotation != 1 || counted.Tasks.PendingQuality != 2 || counted.Tasks.InAnnotation != 1 || counted.Tasks.PendingAcceptance != 2 || counted.Tasks.Rejected != 1 || counted.Tasks.Escalated != 1 || counted.Tasks.QualityPassRate != 50 { t.Fatalf("unexpected counters: %+v", counted.Tasks) } if counted.Data.VideoCount != 3 || counted.Data.PackageCount != 2 || counted.Data.CaptureDurationSeconds != 100 { t.Fatalf("unexpected data counters: %+v", counted.Data) } } func seedDashboardHumanWorkFixtures(t *testing.T, dataStore store.Store) { t.Helper() ctx := context.Background() org := domain.DefaultOrganizationID item := func(id string, status domain.HumanItemStatus, stage string, key string, duration any) domain.HumanItemRecord { return domain.HumanItemRecord{ ID: id, OrganizationID: org, CurrentStageID: stage, Status: status, Subject: domain.HumanSubjectRef{ItemKey: key, Payload: map[string]any{"durationSeconds": duration}}, } } fixtures := []domain.HumanItemRecord{ item("ready-annotation", domain.HumanItemReady, "annotation", "video-a", 30.0), item("ready-quality", domain.HumanItemRework, "quality_review", "video-a", 40.0), item("claimed", domain.HumanItemClaimed, "annotation", "video-b", 20.0), item("submitted-quality", domain.HumanItemSubmitted, domain.FanyuV5StageCutReview, "video-b", nil), item("submitted-acceptance", domain.HumanItemSubmitted, "acceptance", "video-c", 0), item("accepted", domain.HumanItemAccepted, "done", "video-c", 10.0), item("rejected", domain.HumanItemRejected, "done", "", -5), item("escalated", domain.HumanItemEscalated, "annotation", "", "not-a-number"), {ID: "foreign", OrganizationID: "other-org", CurrentStageID: "annotation", Status: domain.HumanItemReady, Subject: domain.HumanSubjectRef{ItemKey: "video-foreign", Payload: map[string]any{"durationSeconds": 999.0}}}, } for _, fixture := range fixtures { if err := dataStore.UpsertHumanItem(ctx, fixture); err != nil { t.Fatal(err) } } packages := []domain.DeliveryPackageRecord{ {ID: "pkg-pending", OrganizationID: org, ProjectID: "p", Status: domain.DeliveryPackagePendingAcceptance}, {ID: "pkg-accepted", OrganizationID: org, ProjectID: "p", Status: domain.DeliveryPackageAccepted}, {ID: "pkg-debug", OrganizationID: org, ProjectID: "p", Mode: domain.RunModeDebug, Status: domain.DeliveryPackagePendingAcceptance}, {ID: "pkg-foreign", OrganizationID: "other-org", ProjectID: "p", Status: domain.DeliveryPackagePendingAcceptance}, } for _, pkg := range packages { if err := dataStore.UpsertDeliveryPackage(ctx, pkg); err != nil { t.Fatal(err) } } } func TestDashboardEndpointServesCachedBodyAndInvalidatesOnWrite(t *testing.T) { dataStore := store.NewMemoryStore() handler := NewHandler(dataStore, nil) first := httptest.NewRecorder() handler.ServeHTTP(first, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)) if first.Code != http.StatusOK { t.Fatalf("first dashboard request = %d body=%s", first.Code, first.Body.String()) } second := httptest.NewRecorder() handler.ServeHTTP(second, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)) if second.Code != http.StatusOK || !bytes.Equal(first.Body.Bytes(), second.Body.Bytes()) { t.Fatalf("cached body must be byte-identical: %d vs %d", first.Code, second.Code) } // A write done outside the API stays hidden while the TTL window is open. if err := dataStore.UpsertRun(context.Background(), domain.RunRecord{ID: "cached-run", OrganizationID: domain.DefaultOrganizationID, Status: domain.RunStatusRunning, CreatedAt: time.Now().UTC().Format(time.RFC3339Nano)}); err != nil { t.Fatal(err) } stillCached := httptest.NewRecorder() handler.ServeHTTP(stillCached, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)) if !bytes.Equal(second.Body.Bytes(), stillCached.Body.Bytes()) { t.Fatal("expected the TTL cache to keep serving the stale body before any API write") } // Any non-GET /api request flushes the dashboard caches. flush := httptest.NewRecorder() handler.ServeHTTP(flush, httptest.NewRequest(http.MethodPost, "/api/auth/logout", nil)) refreshed := httptest.NewRecorder() handler.ServeHTTP(refreshed, httptest.NewRequest(http.MethodGet, "/api/dashboard", nil)) if refreshed.Code != http.StatusOK { t.Fatalf("refreshed dashboard = %d body=%s", refreshed.Code, refreshed.Body.String()) } if bytes.Equal(stillCached.Body.Bytes(), refreshed.Body.Bytes()) { t.Fatal("expected cache invalidation to recompute the dashboard body") } } func TestDashboardCacheSurvivesDraftAndSessionSavesOnly(t *testing.T) { server := &server{dashboardResponses: dashboardResponseCache{now: time.Now}} handler := server.invalidateDashboardCachesOnWrite(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {})) for _, tc := range []struct { method, path string keeps bool }{ {http.MethodPut, "/api/plugins/annotation-qa/tasks/task-1/drafts", true}, {http.MethodPut, "/api/human-items/item-1/review-draft", true}, {http.MethodPost, "/api/human-items/item-1/review-session", true}, {http.MethodPut, "/api/plugins/annotation-qa/work-buffer", true}, {http.MethodPost, "/api/plugins/annotation-qa/tasks/task-1/combined-submissions", false}, {http.MethodPost, "/api/plugins/annotation-qa/tasks/task-1/drafts", false}, {http.MethodPost, "/api/human-items/item-1/release", false}, {http.MethodPut, "/api/dashboard/production/scope", false}, } { server.dashboardResponses.set("k", http.StatusOK, "application/json", []byte(`{}`)) handler.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(tc.method, tc.path, bytes.NewReader(nil))) if kept := server.dashboardResponses.get("k") != nil; kept != tc.keeps { t.Errorf("%s %s: cache kept=%v, want %v", tc.method, tc.path, kept, tc.keeps) } } }