forked from longtao.wu/droboticflow
208 lines
7.4 KiB
Go
208 lines
7.4 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/outputsnapshot"
|
|
"droboticflow/internal/productionstats"
|
|
"droboticflow/internal/store"
|
|
"droboticflow/internal/workforcestats"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type countingOutputStore struct {
|
|
store.Store
|
|
loads atomic.Int32
|
|
gate chan struct{}
|
|
}
|
|
|
|
func (s *countingOutputStore) LoadAnnotationOutput(ctx context.Context, org string, projects []string) (workforcestats.OutputSnapshot, error) {
|
|
s.loads.Add(1)
|
|
if s.gate != nil {
|
|
select {
|
|
case <-s.gate:
|
|
case <-ctx.Done():
|
|
return workforcestats.OutputSnapshot{}, ctx.Err()
|
|
}
|
|
}
|
|
return s.Store.LoadAnnotationOutput(ctx, org, projects)
|
|
}
|
|
func outputReady(t *testing.T, f combinedDraftFixture, target string, user domain.UserRecord) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
for {
|
|
r := f.request("GET", target, user, nil)
|
|
if r.Code != 202 {
|
|
if r.Code != 200 {
|
|
t.Fatalf("read %d: %s", r.Code, r.Body.String())
|
|
}
|
|
return r
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatal("snapshot never became ready")
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
}
|
|
func TestAnnotationOutputSnapshotSharedAcrossHandlersAndPages(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
if _, err := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wrapped := &countingOutputStore{Store: f.store}
|
|
f.handler = NewHandler(wrapped, nil)
|
|
outputReady(t, f, "/api/dashboard/annotation-output?tasksPage=1", f.admin)
|
|
// A second API handler must use the already published shared snapshot.
|
|
f.handler = NewHandler(wrapped, nil)
|
|
outputReady(t, f, "/api/dashboard/annotation-output?tasksPage=2", f.annotator)
|
|
f.request("POST", "/api/nonexistent-output-test", f.admin, nil)
|
|
outputReady(t, f, "/api/dashboard/annotation-output?tasksPage=3", f.annotator)
|
|
if n := wrapped.loads.Load(); n != 1 {
|
|
t.Fatalf("two handlers/pages rebuilt history %d times; want one shared build", n)
|
|
}
|
|
}
|
|
|
|
func TestAnnotationOutputSnapshotPinAndCurrentPermissions(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
_, e := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
r := outputReady(t, f, "/api/dashboard/annotation-output", f.admin)
|
|
var data struct {
|
|
Snapshot struct {
|
|
ID string `json:"id"`
|
|
} `json:"snapshot"`
|
|
}
|
|
if e = json.Unmarshal(r.Body.Bytes(), &data); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
pin := data.Snapshot.ID
|
|
if pin == "" {
|
|
t.Fatal("missing snapshot version")
|
|
}
|
|
export := f.request("GET", "/api/dashboard/annotation-output?format=csv&snapshotId="+pin, f.admin, nil)
|
|
if export.Code != 200 || export.Header().Get("X-Output-Snapshot") != pin {
|
|
t.Fatal("CSV did not use selected generation")
|
|
}
|
|
if r := f.request("GET", "/api/dashboard/annotation-output?snapshotId=expired", f.admin, nil); r.Code != 409 {
|
|
t.Fatal("unknown generation silently replaced")
|
|
}
|
|
changed := f.admin
|
|
changed.Role = domain.UserRoleQualityReviewer
|
|
if r := f.request("GET", "/api/dashboard/annotation-output?snapshotId="+pin, changed, nil); r.Code != 403 {
|
|
t.Fatal("cached generation bypassed current role")
|
|
}
|
|
if _, e = f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{}, Revision: 2}, 1); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if r := f.request("GET", "/api/dashboard/annotation-output?snapshotId="+pin, f.admin, nil); r.Code != 409 {
|
|
t.Fatal("scope removal kept old cached coverage")
|
|
}
|
|
}
|
|
|
|
func TestAnnotationOutputColdSharedResponseAndLegacyCompatibility(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
_, e := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
gate := make(chan struct{})
|
|
var release sync.Once
|
|
defer release.Do(func() { close(gate) })
|
|
wrapped := &countingOutputStore{Store: f.store, gate: gate}
|
|
f.handler = NewHandler(wrapped, nil)
|
|
started := time.Now()
|
|
cold := f.request("GET", "/api/dashboard/annotation-output?snapshotMode=shared", f.admin, nil)
|
|
if cold.Code != 202 || time.Since(started) > 250*time.Millisecond {
|
|
t.Fatalf("cold request blocked or returned fake numbers: %d", cold.Code)
|
|
}
|
|
legacy := make(chan *httptest.ResponseRecorder, 1)
|
|
go func() { legacy <- f.request("GET", "/api/dashboard/annotation-output", f.admin, nil) }()
|
|
select {
|
|
case r := <-legacy:
|
|
t.Fatalf("legacy client received premature %d", r.Code)
|
|
case <-time.After(20 * time.Millisecond):
|
|
}
|
|
release.Do(func() { close(gate) })
|
|
select {
|
|
case r := <-legacy:
|
|
if r.Code != 200 {
|
|
t.Fatalf("legacy client failed %d", r.Code)
|
|
}
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("legacy client not released")
|
|
}
|
|
if wrapped.loads.Load() != 1 {
|
|
t.Fatal("legacy and shared requests duplicated source read")
|
|
}
|
|
}
|
|
|
|
func TestAnnotationOutputPrivateCollectionsRejectBrowserAndWrites(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
for _, name := range []string{outputsnapshot.StateCollection, outputsnapshot.SlotsCollection} {
|
|
for _, operation := range []struct {
|
|
method, target string
|
|
body any
|
|
}{
|
|
{"GET", "/api/database/collections/" + name + "/documents?target=platform", nil},
|
|
{"POST", "/api/database/collections?target=business", map[string]any{"name": name}},
|
|
} {
|
|
r := f.request(operation.method, operation.target, f.admin, operation.body)
|
|
if r.Code < 400 {
|
|
t.Fatalf("internal collection exposed through %s %s: %d", operation.method, operation.target, r.Code)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAnnotationOutputHealthIsAdminScopedAndReadOnly(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
_, err := f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{f.task.ProjectID}, Revision: 1}, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wrapped := &countingOutputStore{Store: f.store}
|
|
f.handler = NewHandler(wrapped, nil)
|
|
url := "/api/dashboard/annotation-output/health"
|
|
for _, user := range []domain.UserRecord{f.annotator, f.reviewer} {
|
|
if r := f.request("GET", url, user, nil); r.Code != 403 {
|
|
t.Fatalf("non-admin read diagnostics: %d", r.Code)
|
|
}
|
|
}
|
|
r := f.request("GET", url, f.admin, nil)
|
|
if r.Code != 200 {
|
|
t.Fatal(r.Body.String())
|
|
}
|
|
var h outputsnapshot.Health
|
|
if err = json.Unmarshal(r.Body.Bytes(), &h); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if h.Status != "not_built" || wrapped.loads.Load() != 0 || r.Header().Get("Cache-Control") != "no-store" {
|
|
t.Fatalf("inspection unexpectedly built data: %+v", h)
|
|
}
|
|
outputReady(t, f, "/api/dashboard/annotation-output", f.admin)
|
|
r = f.request("GET", url, f.admin, nil)
|
|
if err = json.Unmarshal(r.Body.Bytes(), &h); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if h.Status != "healthy" || !h.Available || h.Metrics == nil || wrapped.loads.Load() != 1 {
|
|
t.Fatalf("missing shared health %+v", h)
|
|
}
|
|
_, err = f.store.SaveProductionStatsScope(f.ctx, domain.DefaultOrganizationID, productionstats.Scope{ProjectIDs: []string{}, Revision: 2}, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r = f.request("GET", url+"?projectId="+f.task.ProjectID, f.admin, nil)
|
|
h = outputsnapshot.Health{}
|
|
_ = json.Unmarshal(r.Body.Bytes(), &h)
|
|
if h.Status != "not_built" || h.Metrics != nil || wrapped.loads.Load() != 1 {
|
|
t.Fatal("health exposed excluded scope or rebuilt")
|
|
}
|
|
}
|