162 lines
6.4 KiB
Go
162 lines
6.4 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// Fixtures set server lifecycle states; assertions use only the public buffer
|
|
// and claim routes. This complements actual company processor handoff tests.
|
|
func TestAnnotationBufferProcessingAndFailureKeepCapacityUntilHandoff(t *testing.T) {
|
|
f := randomClaimFixture(t, 2)
|
|
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
|
|
if err := f.store.UpsertHumanBatch(f.ctx, *batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claimed := claimRandom(t, f, "lifecycle-reserved-task")
|
|
path := "/api/plugins/annotation-qa/work-buffer?projectId=" + f.task.ProjectID
|
|
for _, kind := range []string{"slice", "submit"} {
|
|
for _, status := range []string{"queued", "running", "failed", "completed"} {
|
|
claimed.ProcessingJob = &domain.AnnotationProcessingJob{ID: "lifecycle-job", Kind: kind, Status: status, Error: "fixture failure"}
|
|
if err := f.store.UpsertHumanItem(f.ctx, claimed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := f.request(http.MethodGet, path, f.annotator, nil)
|
|
var result struct {
|
|
Occupied int `json:"occupied"`
|
|
Processing map[string]struct {
|
|
Status string `json:"status"`
|
|
Kind string `json:"kind"`
|
|
} `json:"processing"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Code != 200 || result.Occupied != 2 || result.Processing[claimed.ID].Status != status || result.Processing[claimed.ID].Kind != kind {
|
|
t.Fatalf("%s/%s: %d %s", kind, status, r.Code, r.Body.String())
|
|
}
|
|
r = f.request(http.MethodPost, "/api/plugins/annotation-qa/random-claims", f.annotator, map[string]any{"projectId": f.task.ProjectID, "requestId": fmt.Sprintf("blocked-%s-%s", kind, status)})
|
|
if r.Code != 409 {
|
|
t.Fatalf("processing freed capacity: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
}
|
|
claimed.CurrentStageID = "quality_review"
|
|
claimed.Status = domain.HumanItemSubmitted
|
|
claimed.Version++
|
|
if err := f.store.UpsertHumanItem(f.ctx, claimed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
next := claimRandom(t, f, "lifecycle-refill-after-handoff")
|
|
if next.ID == claimed.ID || next.ID == "" {
|
|
t.Fatal("handoff did not admit another task")
|
|
}
|
|
}
|
|
|
|
func TestAnnotationBufferEmptyPoolAndAccountIsolation(t *testing.T) {
|
|
f := randomClaimFixture(t, 0)
|
|
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
|
|
if err := f.store.UpsertHumanBatch(f.ctx, *batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
other := f.annotator
|
|
other.ID = "buffer-other-account"
|
|
if err := f.store.UpsertUser(f.ctx, other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertProjectMember(f.ctx, domain.ProjectMemberRecord{ID: "buffer-other-member", OrganizationID: other.OrganizationID, ProjectID: f.task.ProjectID, UserID: other.ID, SupplierID: "random-supplier", Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := "/api/plugins/annotation-qa/work-buffer?projectId=" + f.task.ProjectID
|
|
if r := f.request(http.MethodPut, path, f.annotator, map[string]any{"capacity": 8}); r.Code != 200 {
|
|
t.Fatal(r.Body.String())
|
|
}
|
|
for _, user := range []domain.UserRecord{f.annotator, other} {
|
|
for i := 0; i < 3; i++ {
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/random-claims", user, map[string]any{"projectId": f.task.ProjectID, "requestId": fmt.Sprintf("empty-%s-%d", user.ID, i), "bufferOnly": true})
|
|
var claim struct {
|
|
Item *domain.HumanItemRecord `json:"item"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &claim); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Code != 200 || claim.Item != nil {
|
|
t.Fatalf("empty pool: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
r := f.request(http.MethodGet, path, user, nil)
|
|
var result struct {
|
|
Capacity int `json:"capacity"`
|
|
Occupied int `json:"occupied"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
wantCap, wantCount := 2, 0
|
|
if user.ID == f.annotator.ID {
|
|
wantCap, wantCount = 8, 1
|
|
}
|
|
if r.Code != 200 || result.Capacity != wantCap || result.Occupied != wantCount {
|
|
t.Fatalf("account isolation: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
|
|
// A second trusted empty project has its own default, despite this user's
|
|
// capacity 8 in the original project; neither occupancy nor settings leak.
|
|
project, err := f.store.GetProject(f.ctx, f.task.ProjectID)
|
|
if err != nil || project == nil {
|
|
t.Fatal("missing fixture project")
|
|
}
|
|
project.ID = "buffer-isolated-project"
|
|
if err := f.store.UpsertProject(f.ctx, *project); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
secondBatch := *batch
|
|
secondBatch.ID = "buffer-isolated-batch"
|
|
secondBatch.ProjectID = project.ID
|
|
if err := f.store.UpsertHumanBatch(f.ctx, secondBatch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertProjectMember(f.ctx, domain.ProjectMemberRecord{ID: "buffer-isolated-project-member", OrganizationID: f.annotator.OrganizationID, ProjectID: project.ID, UserID: f.annotator.ID, SupplierID: "random-supplier", Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
otherPath := "/api/plugins/annotation-qa/work-buffer?projectId=" + project.ID
|
|
for _, query := range []struct {
|
|
path string
|
|
capacity, occupied int
|
|
}{{otherPath, 2, 0}, {path, 8, 1}} {
|
|
r := f.request(http.MethodGet, query.path, f.annotator, nil)
|
|
var result struct {
|
|
Capacity int `json:"capacity"`
|
|
Occupied int `json:"occupied"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &result); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Code != 200 || result.Capacity != query.capacity || result.Occupied != query.occupied {
|
|
t.Fatalf("project isolation: %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
supplier, _ := f.store.GetSupplier(f.ctx, "random-supplier")
|
|
supplier.Status = "disabled"
|
|
if err := f.store.UpsertSupplier(f.ctx, *supplier); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, method := range []string{http.MethodGet, http.MethodPut} {
|
|
r := f.request(method, path, f.annotator, map[string]any{"capacity": 2})
|
|
if r.Code != 403 {
|
|
t.Fatalf("revoked permission %s: %d %s", method, r.Code, r.Body.String())
|
|
}
|
|
}
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/random-claims", f.annotator, map[string]any{"projectId": f.task.ProjectID, "requestId": "revoked-after-settings"})
|
|
if r.Code != 403 {
|
|
t.Fatalf("revoked claim %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|