forked from longtao.wu/droboticflow
469 lines
26 KiB
Go
469 lines
26 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
type combinedDraftFixture struct {
|
|
ctx context.Context
|
|
store *store.MemoryStore
|
|
handler http.Handler
|
|
admin domain.UserRecord
|
|
annotator domain.UserRecord
|
|
reviewer domain.UserRecord
|
|
task domain.AnnotationTaskRecord
|
|
item domain.HumanItemRecord
|
|
}
|
|
|
|
func newCombinedDraftFixture(t *testing.T, supplierIDs ...string) combinedDraftFixture {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
supplierID := domain.DefaultSupplierID(domain.DefaultOrganizationID)
|
|
if len(supplierIDs) > 0 {
|
|
supplierID = supplierIDs[0]
|
|
}
|
|
if err := dataStore.UpsertSupplier(ctx, domain.SupplierRecord{ID: supplierID, OrganizationID: domain.DefaultOrganizationID, Name: "Fixture supplier", Status: "active"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := domain.UserRecord{ID: "draft-admin", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAdmin, Status: domain.UserStatusActive}
|
|
annotator := domain.UserRecord{ID: "draft-annotator", SupplierID: supplierID, OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAnnotator, Status: domain.UserStatusActive}
|
|
reviewer := domain.UserRecord{ID: "draft-reviewer", SupplierID: supplierID, OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleQualityReviewer, Status: domain.UserStatusActive}
|
|
for _, user := range []domain.UserRecord{admin, annotator, reviewer} {
|
|
if err := dataStore.UpsertUser(ctx, user); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
projectID, workflowID := "draft-project", "draft-workflow"
|
|
if err := dataStore.UpsertProject(ctx, domain.ProjectRecord{ID: projectID, OrganizationID: domain.DefaultOrganizationID, Name: "泛宇草稿项目"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertWorkflow(ctx, domain.WorkflowRecord{ID: workflowID, OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, Name: "持续标注工作流"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertAnnotationTemplate(ctx, domain.AnnotationTemplateRecord{ID: "draft-template", OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, Name: "22 类语义", Version: 6, Status: domain.AnnotationTemplatePublished, Labels: domain.ConvenienceStore22SemanticLabels()}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, membership := range []domain.ProjectAnnotationMembershipRecord{
|
|
{ID: "draft-annotator-membership", OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, UserID: annotator.ID, AssignmentRole: domain.AnnotationRoleAnnotator},
|
|
{ID: "draft-reviewer-membership", OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, UserID: reviewer.ID, AssignmentRole: domain.AnnotationRoleQualityReviewer},
|
|
} {
|
|
if err := dataStore.UpsertProjectAnnotationMembership(ctx, membership); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, user := range []domain.UserRecord{annotator, reviewer} {
|
|
if err := dataStore.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ID: "member-" + user.ID, OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, UserID: user.ID, SupplierID: supplierID, Roles: domain.AccountProductionRoles(user)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
batch := domain.HumanBatchRecord{ID: "draft-batch", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", ProjectID: projectID, WorkflowID: workflowID, RunID: "draft-run", Stages: domain.DefaultFanyuV58HumanStages(), Status: domain.HumanBatchActive}
|
|
if err := dataStore.UpsertHumanBatch(ctx, batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task := domain.AnnotationTaskRecord{ID: "draft-task", OrganizationID: domain.DefaultOrganizationID, ProjectID: projectID, WorkflowID: workflowID, RunID: batch.RunID, SourceVideo: "capture-custom/camera_left.mp4", ClipStart: 0, ClipEnd: 10, TemplateID: "draft-template", TemplateVersion: 6, Status: domain.AnnotationTaskStatusPendingAnnotation}
|
|
if err := dataStore.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item := domain.HumanItemRecord{ID: "draft-item", OrganizationID: domain.DefaultOrganizationID, PluginID: "annotation-qa", BatchID: batch.ID, ProjectID: projectID, WorkflowID: workflowID, RunID: batch.RunID, LegacyTaskID: task.ID, CurrentStageID: domain.FanyuV58StageAnnotationCombined, Status: domain.HumanItemClaimed, AssignedUserID: annotator.ID, Version: 4, Subject: domain.HumanSubjectRef{ItemKey: "capture-custom/camera_left.mp4", Payload: map[string]any{"preserved": true, "captureDirectory": "capture-custom"}}}
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return combinedDraftFixture{ctx: ctx, store: dataStore, handler: NewHandler(dataStore, nil), admin: admin, annotator: annotator, reviewer: reviewer, task: task, item: item}
|
|
}
|
|
|
|
func (fixture combinedDraftFixture) request(method string, target string, user domain.UserRecord, body any) *httptest.ResponseRecorder {
|
|
encoded, _ := json.Marshal(body)
|
|
request := httptest.NewRequest(method, target, bytes.NewReader(encoded))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
csrf := "combined-draft-csrf"
|
|
request.AddCookie(&http.Cookie{Name: csrfCookieName, Value: csrf})
|
|
request.Header.Set(csrfHeaderName, csrf)
|
|
request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}}))
|
|
recorder := httptest.NewRecorder()
|
|
fixture.handler.ServeHTTP(recorder, request)
|
|
return recorder
|
|
}
|
|
|
|
func TestCombinedAnnotationDraftUsesHumanItemCASWithoutAdvancingStage(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, fixture, "拿取前")
|
|
payload := map[string]any{
|
|
"version": fixture.item.Version,
|
|
"sliceSegments": []map[string]any{
|
|
{"id": "slice-001", "name": "拿取前", "start": 0.0, "end": 5.0, "annotationStatus": "labeled"},
|
|
{"id": "slice-002", "start": 5.0, "end": 10.0, "annotationStatus": "empty_confirmed"},
|
|
},
|
|
"operations": []map[string]any{{"type": "resize", "segmentIds": []string{"slice-001"}}},
|
|
"segments": []map[string]any{{"id": "event-001", "labelId": "01", "description": "拿取商品", "start": 1.0, "end": 2.0, "confidence": 1.0}},
|
|
"statuses": map[string]string{"slice-001": "labeled", "slice-002": "empty_confirmed"},
|
|
}
|
|
recorder := fixture.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/drafts", fixture.annotator, payload)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("save draft status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
stored, err := fixture.store.GetHumanItem(fixture.ctx, fixture.item.ID)
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("stored item=%#v err=%v", stored, err)
|
|
}
|
|
if stored.Version != fixture.item.Version+1 || stored.CurrentStageID != domain.FanyuV58StageAnnotationCombined || stored.Status != domain.HumanItemClaimed || stored.AssignedUserID != fixture.annotator.ID {
|
|
t.Fatalf("draft must only advance CAS version: %#v", stored)
|
|
}
|
|
draft := mapValue(stored.Subject.Payload["annotationDraft"])
|
|
if draft["userId"] != fixture.annotator.ID || stringValue(draft["savedAt"]) == "" || mapValue(stored.Subject.Payload)["preserved"] != true {
|
|
t.Fatalf("draft metadata or existing payload missing: %#v", stored.Subject.Payload)
|
|
}
|
|
if len(normalizeStructuredValue(draft["sliceSegments"]).([]any)) != 2 || len(normalizeStructuredValue(draft["operations"]).([]any)) != 1 {
|
|
t.Fatalf("draft content=%#v", draft)
|
|
}
|
|
statuses := mapValue(draft["statuses"])
|
|
if stringValue(statuses["slice-001"]) != "labeled" || stringValue(statuses["slice-002"]) != "empty_confirmed" {
|
|
t.Fatalf("draft statuses=%#v", statuses)
|
|
}
|
|
|
|
stale := fixture.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/drafts", fixture.annotator, payload)
|
|
if stale.Code != http.StatusConflict {
|
|
t.Fatalf("stale draft status=%d body=%s", stale.Code, stale.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestManagerCannotClaimProductionHumanStageWithoutProjectRole(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
item := fixture.item
|
|
item.Status = domain.HumanItemReady
|
|
item.AssignedUserID = ""
|
|
if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recorder := fixture.request(http.MethodPost, "/api/human-items/"+item.ID+"/claim", fixture.admin, map[string]any{"version": item.Version})
|
|
if recorder.Code != http.StatusForbidden {
|
|
t.Fatalf("manager without annotator project role must not claim production work: status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCombinedAnnotationDraftRejectsNonAnnotatorEvenWhenTaskIsVisible(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
recorder := fixture.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/drafts", fixture.reviewer, map[string]any{
|
|
"version": fixture.item.Version, "sliceSegments": []map[string]any{{"id": "slice", "start": 0.0, "end": 10.0}}, "segments": []any{},
|
|
})
|
|
if recorder.Code != http.StatusForbidden {
|
|
t.Fatalf("reviewer draft status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCombinedAnnotationDraftRejectsAnnotatorWhoDidNotClaimTheItem(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
other := domain.UserRecord{ID: "other-draft-annotator", SupplierID: fixture.annotator.SupplierID, OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAnnotator, Status: domain.UserStatusActive}
|
|
if err := fixture.store.UpsertUser(fixture.ctx, other); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := fixture.store.UpsertProjectAnnotationMembership(fixture.ctx, domain.ProjectAnnotationMembershipRecord{ID: "other-draft-membership", OrganizationID: domain.DefaultOrganizationID, ProjectID: fixture.task.ProjectID, UserID: other.ID, AssignmentRole: domain.AnnotationRoleAnnotator}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := fixture.store.UpsertProjectMember(fixture.ctx, domain.ProjectMemberRecord{
|
|
ID: "other-draft-member", OrganizationID: other.OrganizationID, ProjectID: fixture.task.ProjectID,
|
|
UserID: other.ID, SupplierID: other.SupplierID, Roles: domain.AccountProductionRoles(other),
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recorder := fixture.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/drafts", other, map[string]any{
|
|
"version": fixture.item.Version, "sliceSegments": []map[string]any{{"id": "slice", "start": 0.0, "end": 10.0}}, "segments": []any{},
|
|
})
|
|
if recorder.Code != http.StatusConflict || !bytes.Contains(recorder.Body.Bytes(), []byte("human_item_claim_required")) {
|
|
t.Fatalf("unassigned annotator draft status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCombinedAnnotationDraftRejectsCrossSliceSemanticEvent(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
recorder := fixture.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/drafts", fixture.annotator, map[string]any{
|
|
"version": fixture.item.Version,
|
|
"sliceSegments": []map[string]any{
|
|
{"id": "slice-a", "start": 0.0, "end": 5.0},
|
|
{"id": "slice-b", "start": 5.0, "end": 10.0},
|
|
},
|
|
"segments": []map[string]any{{"id": "crossing", "labelId": "01", "description": "跨片描述", "start": 4.5, "end": 5.5, "confidence": 1.0}},
|
|
})
|
|
if recorder.Code != http.StatusBadRequest || !bytes.Contains(recorder.Body.Bytes(), []byte("fully contained in exactly one submitted final slice")) {
|
|
t.Fatalf("cross-slice draft status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSliceResetClearsDescriptionsAndReturnsToEditing(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
item := fixture.item
|
|
item.Subject.Payload["annotationDraft"] = map[string]any{
|
|
"sliceSegments": []map[string]any{{"id": "slice-a", "name": "原切片", "start": 0.0, "end": 10.0}},
|
|
"segments": []map[string]any{{"id": "description-a", "description": "会被清空", "start": 0.0, "end": 1.0}},
|
|
"statuses": map[string]string{"slice-a": "labeled"},
|
|
}
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recorder := fixture.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/slice-reset", fixture.annotator, map[string]any{"version": item.Version})
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("slice reset status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
stored, err := fixture.store.GetHumanItem(fixture.ctx, item.ID)
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("stored item=%#v err=%v", stored, err)
|
|
}
|
|
workspace := mapValue(stored.Subject.Payload["annotationWorkspace"])
|
|
if len(decodeMaterializationValue[domain.AnnotationSegment](workspace["segments"])) != 0 || stringValue(mapValue(stored.Subject.Payload["sliceWorkflow"])["phase"]) != "slice" {
|
|
t.Fatalf("reset did not clear descriptions: %#v", stored.Subject.Payload)
|
|
}
|
|
if _, exists := stored.Subject.Payload["annotationDraft"]; exists {
|
|
t.Fatalf("reset must not recreate a draft payload: %#v", stored.Subject.Payload)
|
|
}
|
|
}
|
|
|
|
func TestAnnotationConfirmationRequiresMaterializedSlicesAndAcceptsSparseDescriptions(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, fixture, "前段", "后段")
|
|
payload := map[string]any{
|
|
"version": fixture.item.Version,
|
|
"sliceSegments": []map[string]any{
|
|
{"id": "slice-a", "name": "前段", "start": 0.0, "end": 5.0},
|
|
{"id": "slice-b", "name": "后段", "start": 5.0, "end": 10.0},
|
|
},
|
|
"segments": []map[string]any{
|
|
{"id": "description-a", "description": "伸手接近商品", "start": 0.5, "end": 1.5, "confidence": 1.0},
|
|
{"id": "description-b", "description": "拿起商品", "start": 2.5, "end": 3.5, "confidence": 1.0},
|
|
},
|
|
"statuses": map[string]string{"slice-a": "labeled", "slice-b": "pending"},
|
|
}
|
|
blocked := fixture.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/annotation-confirmations", fixture.annotator, payload)
|
|
if blocked.Code != http.StatusConflict || !bytes.Contains(blocked.Body.Bytes(), []byte("slice_materialization_required")) {
|
|
t.Fatalf("confirmation before materialization status=%d body=%s", blocked.Code, blocked.Body.String())
|
|
}
|
|
|
|
item := fixture.item
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
confirmed := fixture.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/annotation-confirmations", fixture.annotator, payload)
|
|
if confirmed.Code != http.StatusOK {
|
|
t.Fatalf("sparse confirmation status=%d body=%s", confirmed.Code, confirmed.Body.String())
|
|
}
|
|
stored, err := fixture.store.GetHumanItem(fixture.ctx, fixture.item.ID)
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("stored item=%#v err=%v", stored, err)
|
|
}
|
|
workspace := mapValue(stored.Subject.Payload["annotationWorkspace"])
|
|
if len(decodeMaterializationValue[domain.AnnotationSegment](workspace["segments"])) != 2 {
|
|
t.Fatalf("confirmed workspace=%#v", workspace)
|
|
}
|
|
if _, exists := stored.Subject.Payload["annotationDraft"]; exists {
|
|
t.Fatalf("new confirmation must not write annotationDraft: %#v", stored.Subject.Payload)
|
|
}
|
|
|
|
// Confirming with a draft segment that has an empty description should succeed
|
|
payloadWithEmptySegment := map[string]any{
|
|
"version": stored.Version,
|
|
"sliceSegments": []map[string]any{
|
|
{"id": "slice-a", "name": "前段", "start": 0.0, "end": 5.0},
|
|
{"id": "slice-b", "name": "后段", "start": 5.0, "end": 10.0},
|
|
},
|
|
"segments": []map[string]any{
|
|
{"id": "description-a", "description": "伸手接近商品", "start": 0.5, "end": 1.5, "confidence": 1.0},
|
|
{"id": "description-empty", "description": "", "start": 2.5, "end": 3.5, "confidence": 1.0},
|
|
},
|
|
"statuses": map[string]string{"slice-a": "labeled", "slice-b": "pending"},
|
|
}
|
|
partiallyFilled := fixture.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/annotation-confirmations", fixture.annotator, payloadWithEmptySegment)
|
|
if partiallyFilled.Code != http.StatusOK {
|
|
t.Fatalf("partially filled confirmation status=%d body=%s", partiallyFilled.Code, partiallyFilled.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestReleaseCombinedTaskClearsUnconfirmedWorkspace(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
item := fixture.item
|
|
item.Subject.Payload["annotationDraft"] = map[string]any{"segments": []any{map[string]any{"id": "legacy"}}}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"segments": []any{map[string]any{"id": "current"}}}
|
|
if err := fixture.store.UpsertHumanItem(fixture.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
recorder := fixture.request(http.MethodPost, "/api/human-items/"+item.ID+"/release", fixture.admin, map[string]any{"version": item.Version})
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("release status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
stored, err := fixture.store.GetHumanItem(fixture.ctx, item.ID)
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("stored item=%#v err=%v", stored, err)
|
|
}
|
|
if stored.AssignedUserID != "" || stored.Status != domain.HumanItemReady {
|
|
t.Fatalf("released item=%#v", stored)
|
|
}
|
|
if _, exists := stored.Subject.Payload["annotationDraft"]; exists {
|
|
t.Fatalf("release retained legacy draft: %#v", stored.Subject.Payload)
|
|
}
|
|
if _, exists := stored.Subject.Payload["annotationWorkspace"]; exists {
|
|
t.Fatalf("release retained unconfirmed workspace: %#v", stored.Subject.Payload)
|
|
}
|
|
}
|
|
|
|
func TestReleaseReworkPreservesSubmittedRevisions(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
item := f.item
|
|
item.CurrentRevision, item.ReviewRound = 2, 1
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"segments": []any{"draft"}}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "submitted", OrganizationID: item.OrganizationID, TaskID: f.task.ID, Revision: 2}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertSliceRevision(f.ctx, domain.SliceRevisionRecord{ID: "submitted-slices", OrganizationID: item.OrganizationID, TaskID: f.task.ID, Revision: 2}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := f.request(http.MethodPost, "/api/human-items/"+item.ID+"/release", f.admin, map[string]any{"version": item.Version})
|
|
if r.Code != http.StatusOK {
|
|
t.Fatalf("release: %d %s", r.Code, r.Body.String())
|
|
}
|
|
stored, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
annotations, _ := f.store.ListAnnotationRevisions(f.ctx, item.OrganizationID, f.task.ID)
|
|
slices, _ := f.store.ListSliceRevisions(f.ctx, item.OrganizationID, f.task.ID)
|
|
if stored.CurrentRevision != 2 || len(annotations) != 1 || len(slices) != 1 || stored.Status != domain.HumanItemRework {
|
|
t.Fatalf("audit history lost: %#v annotations=%v slices=%v", stored, annotations, slices)
|
|
}
|
|
if mapValue(stored.Subject.Payload["sliceWorkflow"])["discardedWorkspace"] != true {
|
|
t.Fatal("released workspace must start fresh without deleting audit history")
|
|
}
|
|
}
|
|
|
|
func TestBlockedQualityReleasePreservesMaterialization(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
item := f.item
|
|
item.CurrentStageID, item.AssignedUserID = "quality_review", f.reviewer.ID
|
|
item.Subject.Payload["materialization"] = map[string]any{"verified": true}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := f.request(http.MethodPost, "/api/human-items/"+item.ID+"/release", f.reviewer, map[string]any{"version": item.Version})
|
|
if r.Code != http.StatusForbidden {
|
|
t.Fatalf("release: %d %s", r.Code, r.Body.String())
|
|
}
|
|
stored, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if mapValue(stored.Subject.Payload["materialization"])["verified"] != true {
|
|
t.Fatal("reviewer release deleted submitted materialization")
|
|
}
|
|
}
|
|
|
|
func TestSliceConfirmationRejectsMissingFrameSourcesWithoutAdvancingHumanStage(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, fixture, "前半段", "后半段")
|
|
recorder := fixture.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/slice-confirmations", fixture.annotator, map[string]any{
|
|
"version": fixture.item.Version,
|
|
"sliceSegments": []map[string]any{
|
|
{"id": "slice-a", "name": "前半段", "start": 0.0, "end": 5.0},
|
|
{"id": "slice-b", "name": "后半段", "start": 5.0, "end": 10.0},
|
|
},
|
|
})
|
|
if recorder.Code != http.StatusUnprocessableEntity || !bytes.Contains(recorder.Body.Bytes(), []byte("frame_boundaries_unavailable")) {
|
|
t.Fatalf("slice confirmation status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
stored, err := fixture.store.GetHumanItem(fixture.ctx, fixture.item.ID)
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("stored item=%#v err=%v", stored, err)
|
|
}
|
|
if stored.CurrentStageID != domain.FanyuV58StageAnnotationCombined || stored.Version != fixture.item.Version {
|
|
t.Fatalf("slice confirmation advanced stage or lost status: %#v", stored)
|
|
}
|
|
}
|
|
|
|
func TestCombinedSubmissionStillRequiresCompleteSliceCoverage(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
recorder := fixture.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/combined-submissions", fixture.annotator, map[string]any{
|
|
"version": fixture.item.Version, "requireSliceCompletion": true,
|
|
"sliceSegments": []map[string]any{{"id": "slice", "start": 1.0, "end": 10.0, "annotationStatus": "pending"}},
|
|
"segments": []any{},
|
|
})
|
|
if recorder.Code != http.StatusBadRequest || !bytes.Contains(recorder.Body.Bytes(), []byte("full_coverage_required")) {
|
|
t.Fatalf("incomplete combined submission status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
stored, _ := fixture.store.GetHumanItem(fixture.ctx, fixture.item.ID)
|
|
if stored == nil || stored.Version != fixture.item.Version || stored.CurrentStageID != domain.FanyuV58StageAnnotationCombined {
|
|
t.Fatalf("completion validation must not mutate item: %#v", stored)
|
|
}
|
|
}
|
|
|
|
func TestCombinedSubmissionSupportsImmediateAsynchronousResponse(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, fixture, "测试切片")
|
|
payload := map[string]any{
|
|
"version": fixture.item.Version, "requireSliceCompletion": true,
|
|
"sliceSegments": []map[string]any{{"id": "slice", "name": "测试切片", "start": 0.0, "end": 10.0, "annotationStatus": "labeled"}},
|
|
"segments": []map[string]any{{"id": "invalid", "start": 0, "end": 10, "description": "无效片段"}},
|
|
}
|
|
encoded, _ := json.Marshal(payload)
|
|
request := httptest.NewRequest(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/combined-submissions", bytes.NewReader(encoded))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
request.Header.Set("Prefer", "respond-async")
|
|
csrf := "combined-async-csrf"
|
|
request.AddCookie(&http.Cookie{Name: csrfCookieName, Value: csrf})
|
|
request.Header.Set(csrfHeaderName, csrf)
|
|
request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, &authenticatedPrincipal{User: fixture.annotator, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}}))
|
|
recorder := httptest.NewRecorder()
|
|
fixture.handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusAccepted {
|
|
t.Fatalf("async combined submission status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var response map[string]any
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response["status"] != "processing" || response["submissionId"] == "" {
|
|
t.Fatalf("async combined submission response=%#v", response)
|
|
}
|
|
}
|
|
|
|
func TestCombinedSubmissionCannotBypassDescriptionOverlapChecks(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
payload := map[string]any{
|
|
"version": fixture.item.Version, "requireSliceCompletion": false,
|
|
"sliceSegments": []map[string]any{{"id": "slice", "start": 0.0, "end": 10.0, "annotationStatus": "pending"}},
|
|
"segments": []map[string]any{{"id": "a", "start": 1, "end": 5, "description": "拿起"}, {"id": "b", "start": 4, "end": 6, "description": "放下"}},
|
|
}
|
|
encoded, _ := json.Marshal(payload)
|
|
request := httptest.NewRequest(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID+"/combined-submissions", bytes.NewReader(encoded))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
request.Header.Set("Prefer", "respond-async")
|
|
csrf := "combined-optional-description-csrf"
|
|
request.AddCookie(&http.Cookie{Name: csrfCookieName, Value: csrf})
|
|
request.Header.Set(csrfHeaderName, csrf)
|
|
request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, &authenticatedPrincipal{User: fixture.annotator, Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive}}))
|
|
recorder := httptest.NewRecorder()
|
|
fixture.handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusBadRequest || !bytes.Contains(recorder.Body.Bytes(), []byte("events must not overlap")) {
|
|
t.Fatalf("coverage bypass status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestAnnotationTaskGetEnrichesLinkedHumanItemDisplay(t *testing.T) {
|
|
fixture := newCombinedDraftFixture(t)
|
|
recorder := fixture.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+fixture.task.ID, fixture.annotator, nil)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("task get status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
var response struct {
|
|
HumanItem domain.HumanItemRecord `json:"humanItem"`
|
|
}
|
|
if err := json.Unmarshal(recorder.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.HumanItem.Display == nil || response.HumanItem.Display.ProjectName != "泛宇草稿项目" || response.HumanItem.Display.WorkflowName != "持续标注工作流" || len(response.HumanItem.Display.CaptureDirectories) != 1 || response.HumanItem.Display.CaptureDirectories[0] != "capture-custom" {
|
|
t.Fatalf("linked human display=%#v", response.HumanItem.Display)
|
|
}
|
|
}
|