droboticflow-new/internal/api/annotation_five_rules_test.go

140 lines
5.7 KiB
Go

package api
import (
"bytes"
"droboticflow/internal/domain"
"encoding/json"
"net/http"
"testing"
)
func seedSliceDictionary(t *testing.T, f combinedDraftFixture, names ...string) {
t.Helper()
ids := []string{}
for _, name := range names {
id := domain.NewID("test-slice-label")
if err := f.store.UpsertAnnotationLabel(f.ctx, domain.AnnotationLabelRecord{ID: id, OrganizationID: f.task.OrganizationID, Kind: "slice", Code: id, Name: name, Status: domain.AnnotationLabelActive}); err != nil {
t.Fatal(err)
}
ids = append(ids, id)
}
if err := f.store.UpsertProjectSliceLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ID: "labels-" + f.task.ProjectID, ProjectID: f.task.ProjectID, OrganizationID: f.task.OrganizationID, SliceLabelIDs: ids}); err != nil {
t.Fatal(err)
}
}
func TestProjectSliceNamesRequireProjectDictionaryButAllowRepeatedNames(t *testing.T) {
f := newCombinedDraftFixture(t)
seedSliceDictionary(t, f, "测试切片")
s := &server{store: f.store}
valid := []domain.SliceSegment{{ID: "a", Name: "测试切片", Start: 0, End: 5}, {ID: "b", Name: "测试切片", Start: 5, End: 10}}
if issues := s.validateProjectSliceNames(f.ctx, &f.item, valid, "confirm"); len(issues) > 0 {
t.Fatal(issues)
}
for _, name := range []string{"随便填写", "测试切片 A", ""} {
bad := append([]domain.SliceSegment(nil), valid...)
bad[0].Name = name
if len(s.validateProjectSliceNames(f.ctx, &f.item, bad, "confirm")) == 0 {
t.Fatalf("accepted custom name %q", name)
}
}
old := f.item
old.Subject.Payload = map[string]any{"annotationWorkspace": map[string]any{"sliceSegments": []domain.SliceSegment{{ID: "old", Name: "历史自定义名", Start: 0, End: 10}}}}
historical := []domain.SliceSegment{{ID: "old", Name: "历史自定义名", Start: 0, End: 10}}
if len(s.validateProjectSliceNames(f.ctx, &old, historical, "submit")) != 0 {
t.Fatal("unchanged history rejected")
}
historical[0].Name = "伪造改名"
if len(s.validateProjectSliceNames(f.ctx, &old, historical, "draft")) == 0 {
t.Fatal("draft forged a historical name")
}
}
func TestClaimCountMatchesPrivateClaimPool(t *testing.T) {
f := randomClaimFixture(t, 3)
count := func() int {
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/claim-count?projectId="+f.task.ProjectID, f.annotator, nil)
if r.Code != 200 {
t.Fatal(r.Code, r.Body.String())
}
var p struct {
Count int `json:"count"`
}
json.Unmarshal(r.Body.Bytes(), &p)
if bytes.Contains(r.Body.Bytes(), []byte("random-item")) || bytes.Contains(r.Body.Bytes(), []byte("items")) {
t.Fatal("count leaks candidates")
}
return p.Count
}
if count() != 3 {
t.Fatal("wrong initial count")
}
claimRandom(t, f, "count-test-request-0001")
if count() != 2 {
t.Fatal("claim not reflected in count")
}
hidden := f.item
hidden.ID = "other-supplier-count"
hidden.AssignedUserID = ""
hidden.Status = domain.HumanItemReady
hidden.SupplierID = "other"
f.store.UpsertHumanItem(f.ctx, hidden)
if count() != 2 {
t.Fatal("foreign supplier counted")
}
for _, actor := range []domain.UserRecord{f.reviewer, f.admin} {
if r := f.request(http.MethodGet, "/api/plugins/annotation-qa/claim-count?projectId="+f.task.ProjectID, actor, nil); r.Code != 403 {
t.Fatal("wrong role can count")
}
}
if r := f.request(http.MethodGet, "/api/plugins/annotation-qa/claim-count?projectId=foreign", f.annotator, nil); r.Code != 403 {
t.Fatal("unknown project counted")
}
}
func TestThreeHumanRolesCannotReleaseButKeepOwnership(t *testing.T) {
for _, role := range []domain.UserRole{domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer} {
t.Run(string(role), func(t *testing.T) {
f := newCombinedDraftFixture(t)
actor := f.annotator
actor.Role = role
f.store.UpsertUser(f.ctx, actor)
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
batch.Stages = []domain.HumanStageDefinition{{ID: "test-stage", Kind: domain.HumanStageAnnotation, RequiredRole: string(role)}}
f.store.UpsertHumanBatch(f.ctx, *batch)
f.store.UpsertProjectAnnotationMembership(f.ctx, domain.ProjectAnnotationMembershipRecord{ID: "role-test", OrganizationID: f.task.OrganizationID, ProjectID: f.task.ProjectID, UserID: actor.ID, AssignmentRole: domain.AnnotationAssignmentRole(role)})
item := f.item
item.CurrentStageID = "test-stage"
f.store.UpsertHumanItem(f.ctx, item)
r := f.request(http.MethodPost, "/api/human-items/"+item.ID+"/release", actor, map[string]any{"version": item.Version})
if r.Code != 403 || !bytes.Contains(r.Body.Bytes(), []byte("release_forbidden")) {
t.Fatal(r.Code, r.Body.String())
}
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
if saved.AssignedUserID != actor.ID || saved.Version != item.Version {
t.Fatal("blocked release mutated ownership")
}
})
}
}
func TestDictionaryNamesAreUniquePerKindAtAPI(t *testing.T) {
f := newCombinedDraftFixture(t)
create := func(kind, code, name string) int {
return f.request(http.MethodPost, "/api/annotation-labels", f.admin, map[string]any{"kind": kind, "code": code, "name": name}).Code
}
if create("slice", "slice-one", " Same Name ") != 201 || create("action", "action-one", "Same Name") != 201 {
t.Fatal("separate dictionaries conflict")
}
if create("slice", "slice-two", "same name") != 409 {
t.Fatal("normalized duplicate name accepted")
}
r := f.request(http.MethodPost, "/api/annotation-labels", f.admin, map[string]any{"kind": "slice", "code": "three", "name": "Unique"})
var label domain.AnnotationLabelRecord
json.Unmarshal(r.Body.Bytes(), &label)
r = f.request(http.MethodPatch, "/api/annotation-labels/"+label.ID, f.admin, map[string]any{"name": "Same Name"})
if r.Code != 409 {
t.Fatal("rename created duplicate", r.Code, r.Body.String())
}
}