package api import ( "context" "droboticflow/internal/domain" "droboticflow/internal/store" "encoding/json" "errors" "fmt" "math" "net/http" "net/http/httptest" "os" "path/filepath" "strings" "sync" "sync/atomic" "testing" "time" ) func TestHumanEvidenceCacheSharesBuildAndInvalidates(t *testing.T) { var c humanEvidenceCache var builds atomic.Int32 entered, release := make(chan struct{}), make(chan struct{}) build := func(ctx context.Context) runArtifactManifestDocument { if builds.Add(1) == 1 { close(entered) } <-release return runArtifactManifestDocument{Recognized: true, Summary: map[string]any{"count": 1}, Members: []runArtifactManifestMember{{ID: "a", Name: "original"}}} } var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done(); _, _, _ = c.get(context.Background(), "version-1", build) }() <-entered cancelled, cancel := context.WithCancel(context.Background()) cancel() if _, _, err := c.get(cancelled, "version-1", build); !errors.Is(err, context.Canceled) { t.Fatalf("waiter cancellation=%v", err) } for i := 0; i < 12; i++ { wg.Add(1) go func() { defer wg.Done(); _, _, _ = c.get(context.Background(), "version-1", build) }() } close(release) wg.Wait() if builds.Load() != 1 { t.Fatalf("repeated builds=%d", builds.Load()) } d, hit, err := c.get(context.Background(), "version-1", build) if err != nil || !hit { t.Fatal("missing hit", err) } d.Members[0].Name = "mutated" d.Summary["count"] = 99 d, _, _ = c.get(context.Background(), "version-1", build) if d.Members[0].Name != "original" || d.Summary["count"] != 1 { t.Fatal("cache mutated by reader") } c.mu.Lock() e := c.entries["version-1"] e.expires = time.Now().Add(-time.Second) c.entries["version-1"] = e c.mu.Unlock() _, hit, _ = c.get(context.Background(), "version-1", build) if hit || builds.Load() != 2 { t.Fatal("expired index reused") } item := &domain.HumanItemRecord{ID: "item", OrganizationID: "org", ProjectID: "project", Version: 1} run := &domain.RunRecord{ID: "run"} artifact := &domain.ExecutionArtifact{ID: "e", Data: map[string]any{"manifestSha256": "one"}} key := humanEvidenceKey(item, run, artifact) item.Version++ if key == humanEvidenceKey(item, run, artifact) { t.Fatal("version not fenced") } item.Version-- artifact.Data["manifestSha256"] = "two" if key == humanEvidenceKey(item, run, artifact) { t.Fatal("changed manifest reused") } artifact.Data["manifestSha256"] = "one" item.OrganizationID = "other" if key == humanEvidenceKey(item, run, artifact) { t.Fatal("tenant not fenced") } artifact.Data["invalid"] = math.NaN() if humanEvidenceKey(item, run, artifact) != "" { t.Fatal("invalid source must bypass caching") } for i := 0; i < 20; i++ { _, _, _ = c.get(context.Background(), fmt.Sprint(i), func(context.Context) runArtifactManifestDocument { return runArtifactManifestDocument{Recognized: true, Members: []runArtifactManifestMember{{ID: "m"}}} }) } if len(c.entries) > 16 { t.Fatal("unbounded cache") } badBuilds := 0 for i := 0; i < 2; i++ { _, _, _ = c.get(context.Background(), "failed", func(context.Context) runArtifactManifestDocument { badBuilds++ return runArtifactManifestDocument{Recognized: true, Message: "storage failure"} }) } if badBuilds != 2 { t.Fatal("storage failure cached") } } func TestHumanEvidenceHTTPReusesIndexAndRechecksAccessAndFiles(t *testing.T) { ctx := context.Background() workspace := t.TempDir() runID, projectID := "run-evidence-cache", "project-evidence-cache" root := filepath.Join(workspace, "system", "runs", runID, "review") if err := os.MkdirAll(root, 0700); err != nil { t.Fatal(err) } for i := 0; i < 450; i++ { if err := os.WriteFile(filepath.Join(root, fmt.Sprintf("%04d.json", i)), []byte(`{"ok":true}`), 0600); err != nil { t.Fatal(err) } } ds := store.NewMemoryStore() must := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } must(ds.UpsertProject(ctx, domain.ProjectRecord{ID: projectID, OrganizationID: domain.DefaultOrganizationID})) manifest := domain.ExecutionArtifact{ID: "evidence", RunID: runID, Name: "review.directory.index", Path: root, Data: map[string]any{"schemaVersion": "droboticflow.directory-index/v1", "rootPath": root, "datasetType": "directory", "presentation": "collection"}} must(ds.UpsertRun(ctx, domain.RunRecord{ID: runID, OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, Artifacts: []domain.ExecutionArtifact{manifest}})) supplierID := domain.DefaultSupplierID(domain.DefaultOrganizationID) must(ds.UpsertSupplier(ctx, domain.SupplierRecord{ID: supplierID, OrganizationID: domain.DefaultOrganizationID, Name: "Evidence fixture supplier", Status: "active"})) user := domain.UserRecord{ID: "reviewer", OrganizationID: domain.DefaultOrganizationID, SupplierID: supplierID, Role: domain.UserRoleQualityReviewer, Status: domain.UserStatusActive} must(ds.UpsertUser(ctx, user)) must(ds.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ID: "evidence-reviewer-member", OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, UserID: user.ID, SupplierID: supplierID, Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleQualityReviewer}})) must(ds.UpsertProjectAnnotationMembership(ctx, domain.ProjectAnnotationMembershipRecord{OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, UserID: user.ID, WorkgroupID: "wg", AssignmentRole: domain.AnnotationRoleQualityReviewer})) batch := domain.HumanBatchRecord{ID: "batch", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", ProjectID: projectID, RunID: runID, Stages: domain.DefaultFanyuV58HumanStages()} must(ds.UpsertHumanBatch(ctx, batch)) item := domain.HumanItemRecord{ID: "item", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", ProjectID: projectID, RunID: runID, BatchID: batch.ID, CurrentStageID: "quality_review", Status: domain.HumanItemClaimed, AssignedUserID: user.ID, WorkgroupID: "wg", Version: 1, Subject: domain.HumanSubjectRef{Payload: map[string]any{"materialization": map[string]any{"artifactId": manifest.ID, "runId": runID}}}} must(ds.UpsertHumanItem(ctx, item)) handler := runArtifactMembersHandler(t, ds, workspace) principal := &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID}} base := "/api/human-items/item/evidence/evidence/members" get := func(path string, p *authenticatedPrincipal) *httptest.ResponseRecorder { rec := httptest.NewRecorder() handler.ServeHTTP(rec, runArtifactMembersRequest("GET", path, p)) return rec } started := time.Now() first := get(base+"?limit=200", principal) cold := time.Since(started) if first.Code != 200 || !strings.Contains(first.Header().Get("Server-Timing"), "desc=load") { t.Fatalf("first %d %s", first.Code, first.Body.String()) } var payload struct { Items []runArtifactManifestMember `json:"items"` Total int `json:"total"` } must(json.Unmarshal(first.Body.Bytes(), &payload)) if payload.Total != 450 { t.Fatalf("count %d", payload.Total) } started = time.Now() for _, offset := range []int{200, 400} { r := get(fmt.Sprintf("%s?limit=200&offset=%d", base, offset), principal) if r.Code != 200 || !strings.Contains(r.Header().Get("Server-Timing"), "desc=hit") { t.Fatal("page rebuilt", r.Code, r.Body.String()) } } warm := time.Since(started) member := payload.Items[0] r := get(base+"/"+member.ID+"/content", principal) if r.Code != 200 || !strings.Contains(r.Header().Get("Server-Timing"), "desc=hit") { t.Fatalf("content %d %s", r.Code, r.Body.String()) } // A warmed cache must not expose either the list or its files to another actor. foreign := *principal foreign.User.ID = "not-a-member" if r = get(base, &foreign); r.Code == 200 { t.Fatal("cached authorization bypass") } must(os.Remove(filepath.Join(root, member.Name))) outside := filepath.Join(t.TempDir(), "secret") must(os.WriteFile(outside, []byte("private"), 0600)) must(os.Symlink(outside, filepath.Join(root, member.Name))) if r = get(base+"/"+member.ID+"/content", principal); r.Code != http.StatusNotFound || strings.Contains(r.Body.String(), "private") { t.Fatal("cached symlink escaped", r.Code, r.Body.String()) } item.Version++ must(ds.UpsertHumanItem(ctx, item)) if r = get(base, principal); !strings.Contains(r.Header().Get("Server-Timing"), "desc=load") { t.Fatal("new item version reused old index") } t.Logf("450-file directory: first index %s; two cached pages %s; 3 subsequent requests reused one scan", cold, warm) } func TestHumanEvidenceFailedBuilderDoesNotStrandWaiters(t *testing.T) { var c humanEvidenceCache if _, _, err := c.get(context.Background(), "panic", func(context.Context) runArtifactManifestDocument { panic("bad source") }); err == nil { t.Fatal("panic accepted") } if _, hit, err := c.get(context.Background(), "panic", func(context.Context) runArtifactManifestDocument { return runArtifactManifestDocument{Recognized: true} }); err != nil || hit { t.Fatal("failed flight retained", err) } }