package api import ( "context" "errors" "net/http" "testing" "time" "droboticflow/internal/domain" "droboticflow/internal/store" ) // A real sidebar request must start both independent reads before waiting for // either result. The rendezvous tests the critical path without timing budgets. type rendezvousProgressStore struct { store.Store sliceStarted, annotationStarted chan struct{} sliceErr, annotationErr error } func (s *rendezvousProgressStore) ListLatestSliceProgressByTaskIDs(ctx context.Context, org string, ids []string) ([]domain.SliceRevisionRecord, error) { close(s.sliceStarted) select { case <-s.annotationStarted: case <-ctx.Done(): return nil, ctx.Err() case <-time.After(time.Second): return nil, errors.New("annotation read did not start while slice read was pending") } if s.sliceErr != nil { return nil, s.sliceErr } return s.Store.ListLatestSliceProgressByTaskIDs(ctx, org, ids) } func (s *rendezvousProgressStore) ListLatestAnnotationProgressByTaskIDs(ctx context.Context, org string, ids []string) ([]domain.AnnotationRevisionRecord, error) { close(s.annotationStarted) select { case <-s.sliceStarted: case <-ctx.Done(): return nil, ctx.Err() case <-time.After(time.Second): return nil, errors.New("slice read did not start while annotation read was pending") } if s.annotationErr != nil { return nil, s.annotationErr } return s.Store.ListLatestAnnotationProgressByTaskIDs(ctx, org, ids) } func TestAnnotationSidebarProgressReadsOverlap(t *testing.T) { for _, tc := range []struct { name string sliceErr, annotationErr error status int }{ {"both-success", nil, nil, 200}, {"slice-failure", errors.New("synthetic slice failure"), nil, 500}, {"annotation-failure", nil, errors.New("synthetic annotation failure"), 500}, {"both-fail", errors.New("synthetic slice failure"), errors.New("synthetic annotation failure"), 500}, {"canceled-read", context.Canceled, nil, 500}, } { t.Run(tc.name, func(t *testing.T) { f := newFanyuV5FourStageFixture(t, domain.FanyuV5StageSemanticAnnotation, domain.HumanItemInProgress, "annotator-v5", 1) s := &rendezvousProgressStore{Store: f.store, sliceStarted: make(chan struct{}), annotationStarted: make(chan struct{}), sliceErr: tc.sliceErr, annotationErr: tc.annotationErr} f.handler = NewHandler(s, nil) got := f.request(http.MethodGet, "/api/plugins/annotation-qa/sidebar?projectId="+f.task.ProjectID, f.annotator, nil) if got.Code != tc.status { t.Fatalf("status=%d want=%d body=%s", got.Code, tc.status, got.Body) } }) } }