droboticflow-new/internal/api/annotation_work_buffer_display_cache_test.go
d-robotics b2a4f16a1f ⚡ Cache work-buffer display labels and keep dashboards cached across autosaves
- The work-buffer poll resolved each item's project, workflow and capture
  labels by reading the full run document (up to 6MB) every 3s. Cache the
  resolved labels per item for 2 minutes.
- Slots whose item the active scan already counted keep their slot without
  rereading the item and its claim receipt (same outcome as before).
- Draft, review-draft, review-session and work-buffer saves no longer flush
  the dashboard response cache; they change nothing a dashboard counts.

Local perf DB with a 6MB run: poll total p50 141ms -> 7ms (enrich 131 -> 0),
byte-identical response. Admin dashboard stays cached across draft and
session saves and is still recomputed after other writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 00:49:09 +08:00

31 lines
1.1 KiB
Go

package api
import (
"droboticflow/internal/domain"
"testing"
"time"
)
func TestWorkBufferDisplayCacheCopiesAndExpires(t *testing.T) {
var cache workBufferDisplayCache
now := time.Unix(1700000000, 0)
item := domain.HumanItemRecord{OrganizationID: "org", ID: "item", RunID: "run", ProjectID: "project", WorkflowID: "workflow", LegacyTaskID: "task"}
key := workBufferDisplayKey(item)
cache.set(key, &domain.HumanTaskDisplayContext{ProjectName: "P", CaptureDirectories: []string{"A"}}, now)
got := cache.get(key, now.Add(time.Minute))
if got == nil || got.ProjectName != "P" || len(got.CaptureDirectories) != 1 {
t.Fatalf("cached display = %+v", got)
}
got.CaptureDirectories[0] = "mutated"
if again := cache.get(key, now); again.CaptureDirectories[0] != "A" {
t.Fatal("callers must not share the cached slice")
}
item.RunID = "other-run"
if cache.get(workBufferDisplayKey(item), now) != nil {
t.Fatal("a different run must not reuse the display")
}
if cache.get(key, now.Add(workBufferDisplayTTL+time.Second)) != nil {
t.Fatal("display must expire")
}
}