forked from longtao.wu/droboticflow
219 lines
8.9 KiB
Go
219 lines
8.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"errors"
|
|
"net/http"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestDeletedAnnotatorResetsOnlyUnsubmittedWork(t *testing.T) {
|
|
t.Setenv("DELETED_ANNOTATOR_HANDOFF", "true")
|
|
for _, submitted := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "before-quality", true: "quality-history"}[submitted], func(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"userId": f.annotator.ID, "sliceSegments": []domain.SliceSegment{{ID: "old", Name: "旧切片", Start: 0, End: 10}}, "segments": []domain.AnnotationSegment{{ID: "old-action", Start: 0, End: 10, Description: "旧描述"}}, "savedAt": nowISO()}
|
|
f.item.Subject.Payload["annotationDraft"] = f.item.Subject.Payload["annotationWorkspace"]
|
|
if submitted {
|
|
f.item.SubmittedAt = nowISO()
|
|
f.item.ReviewRound = 1
|
|
f.item.LatestAuthorUserID = f.annotator.ID
|
|
f.item.Status = domain.HumanItemRework
|
|
}
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := f.request(http.MethodDelete, "/api/users/"+f.annotator.ID, f.admin, nil)
|
|
if r.Code != 204 {
|
|
t.Fatalf("delete: %d %s", r.Code, r.Body.String())
|
|
}
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if submitted {
|
|
if !item.InheritedRework || len(mapValue(item.Subject.Payload["annotationWorkspace"])) == 0 {
|
|
t.Fatal("reviewed contents were erased or not routed as inherited rework")
|
|
}
|
|
} else {
|
|
if item.AnnotationEpoch != 1 || item.ProcessingJob != nil || item.Subject.Payload["annotationDraft"] != nil || item.Subject.Payload["annotationWorkspace"] != nil {
|
|
t.Fatalf("unsubmitted workspace survived reset: epoch=%d", item.AnnotationEpoch)
|
|
}
|
|
if item.AssignedUserID != "" || item.Status != domain.HumanItemReady {
|
|
t.Fatal("reset task not returned to pool")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type handoffFailureStore struct {
|
|
store.Store
|
|
failReset bool
|
|
beforeReset func(domain.HumanItemRecord)
|
|
}
|
|
|
|
func (s *handoffFailureStore) ResetDeletedAnnotatorTask(ctx context.Context, task domain.AnnotationTaskRecord) error {
|
|
if s.failReset {
|
|
s.failReset = false
|
|
return errors.New("synthetic restart boundary")
|
|
}
|
|
return s.Store.ResetDeletedAnnotatorTask(ctx, task)
|
|
}
|
|
func (s *handoffFailureStore) UpdateHumanItemVersioned(ctx context.Context, item domain.HumanItemRecord, v int) (bool, error) {
|
|
if item.AccountHandoff != nil && item.AccountHandoff.Pending && s.beforeReset != nil {
|
|
f := s.beforeReset
|
|
s.beforeReset = nil
|
|
f(item)
|
|
}
|
|
return s.Store.UpdateHumanItemVersioned(ctx, item, v)
|
|
}
|
|
|
|
func TestDeletedAnnotatorRestartFinishesResetOnce(t *testing.T) {
|
|
t.Setenv("DELETED_ANNOTATOR_HANDOFF", "true")
|
|
f := newCombinedDraftFixture(t)
|
|
f.item.Subject.Payload["annotationDraft"] = map[string]any{"segments": []domain.AnnotationSegment{{ID: "old", Description: "旧草稿"}}}
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
probe := &handoffFailureStore{Store: f.store, failReset: true}
|
|
f.handler = NewHandler(probe, nil)
|
|
if r := f.request("DELETE", "/api/users/"+f.annotator.ID, f.admin, nil); r.Code != 500 {
|
|
t.Fatal(r.Code, r.Body.String())
|
|
}
|
|
user, _ := f.store.GetUser(f.ctx, f.annotator.ID)
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if user.DeletionRequestedAt == "" || user.DeletedAt != "" || item.AccountHandoff == nil || !item.AccountHandoff.Pending {
|
|
t.Fatal("restart intent not durable")
|
|
}
|
|
if r := f.request("PUT", "/api/users/"+f.annotator.ID, f.admin, map[string]any{"status": "active"}); r.Code == 200 {
|
|
t.Fatal("deletion-in-progress account was reactivated")
|
|
}
|
|
// A new server has no memory of the request; only durable records are shared.
|
|
restarted := &server{store: f.store}
|
|
if err := restarted.recoverAccountHandoffs(f.ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item, _ = f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
user, _ = f.store.GetUser(f.ctx, f.annotator.ID)
|
|
if item.AnnotationEpoch != 1 || item.AccountHandoff.Pending || item.AssignedUserID != "" || user.DeletedAt == "" {
|
|
t.Fatal("reset did not resume")
|
|
}
|
|
// A repeated delete/recovery must never clear the next person's edits.
|
|
item.AssignedUserID = "next-author"
|
|
item.Status = domain.HumanItemClaimed
|
|
item.Subject.Payload["annotationDraft"] = map[string]any{"marker": "new work"}
|
|
item.Version++
|
|
if err := f.store.UpsertHumanItem(f.ctx, *item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if err := restarted.recoverAccountHandoffs(f.ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r := f.request("DELETE", "/api/users/"+f.annotator.ID, f.admin, nil); r.Code != 204 {
|
|
t.Fatal(r.Code)
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if !reflect.DeepEqual(before, after) {
|
|
t.Fatal("repeated deletion erased next author work")
|
|
}
|
|
}
|
|
|
|
func TestDeletedAnnotatorSubmissionWinsCASRace(t *testing.T) {
|
|
t.Setenv("DELETED_ANNOTATOR_HANDOFF", "true")
|
|
f := newCombinedDraftFixture(t)
|
|
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"marker": "submitted work"}
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
probe := &handoffFailureStore{Store: f.store}
|
|
probe.beforeReset = func(_ domain.HumanItemRecord) {
|
|
published := f.item
|
|
published.CurrentStageID = domain.FanyuV58StageQualityReview
|
|
published.Status = domain.HumanItemReady
|
|
published.AssignedUserID = ""
|
|
published.SubmittedAt = nowISO()
|
|
published.LatestAuthorUserID = f.annotator.ID
|
|
published.CurrentRevision = 1
|
|
published.Version++
|
|
ok, err := f.store.UpdateHumanItemVersioned(f.ctx, published, f.item.Version)
|
|
if err != nil || !ok {
|
|
t.Fatal("submission fixture lost", err)
|
|
}
|
|
}
|
|
f.handler = NewHandler(probe, nil)
|
|
if r := f.request("DELETE", "/api/users/"+f.annotator.ID, f.admin, nil); r.Code != 204 {
|
|
t.Fatal(r.Code, r.Body.String())
|
|
}
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if item.CurrentStageID != domain.FanyuV58StageQualityReview || item.AnnotationEpoch != 0 || item.Subject.Payload["annotationWorkspace"] == nil {
|
|
t.Fatal("deletion rewound a winning quality handoff")
|
|
}
|
|
}
|
|
|
|
func TestDeletedAnnotatorOldWritesCannotReviveReset(t *testing.T) {
|
|
t.Setenv("DELETED_ANNOTATOR_HANDOFF", "true")
|
|
f := newCombinedDraftFixture(t)
|
|
f.item.ProcessingJob = &domain.AnnotationProcessingJob{ID: "old-job", TaskID: f.task.ID, Kind: "slice", Status: "running", Owner: "old-worker", ActorID: f.annotator.ID, Revision: 3}
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
oldItem, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
oldTask, _ := f.store.GetAnnotationTask(f.ctx, f.task.ID)
|
|
if r := f.request("DELETE", "/api/users/"+f.annotator.ID, f.admin, nil); r.Code != 204 {
|
|
t.Fatal(r.Code, r.Body.String())
|
|
}
|
|
if ok, err := f.store.UpdateHumanItemVersioned(f.ctx, *oldItem, oldItem.Version); err != nil || ok {
|
|
t.Fatal("old edit won after reset", err)
|
|
}
|
|
if ok, _ := f.store.TouchAnnotationProcessing(f.ctx, f.item.ID, "old-job", "old-worker", nowISO(), "publishing", 1, 1); ok {
|
|
t.Fatal("old heartbeat revived")
|
|
}
|
|
if ok, _ := f.store.SaveAnnotationProcessing(f.ctx, f.item.ID, *oldItem.ProcessingJob); ok {
|
|
t.Fatal("old job revived")
|
|
}
|
|
if err := f.store.UpsertAnnotationTask(f.ctx, *oldTask); !errors.Is(err, store.ErrHumanClaimConflict) {
|
|
t.Fatal("old task publication admitted", err)
|
|
}
|
|
if err := f.store.UpsertSliceRevision(f.ctx, domain.SliceRevisionRecord{ID: "late", TaskID: f.task.ID, OrganizationID: f.task.OrganizationID, Revision: 3}); !errors.Is(err, store.ErrHumanClaimConflict) {
|
|
t.Fatal("late generation admitted", err)
|
|
}
|
|
processor := &server{store: f.store}
|
|
if err := processor.processAnnotationJob(f.ctx, *oldItem); err == nil {
|
|
t.Fatal("deleted actor job resumed")
|
|
}
|
|
processor.finishSliceConfirmation(f.ctx, f.task.ID, f.item.ID, "old-job", sliceConfirmationInput{}, 3, f.annotator.ID)
|
|
current, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if current.AnnotationEpoch != 1 || current.ProcessingJob != nil || current.CurrentRevision != 0 {
|
|
t.Fatal("old writes changed reset")
|
|
}
|
|
}
|
|
|
|
func TestDeletedAnnotatorRejectsMismatchedTaskScope(t *testing.T) {
|
|
t.Setenv("DELETED_ANNOTATOR_HANDOFF", "true")
|
|
f := newCombinedDraftFixture(t)
|
|
foreign := f.task
|
|
foreign.ID = "foreign-task"
|
|
foreign.OrganizationID = "another-organization"
|
|
foreign.ProjectID = "another-project"
|
|
if err := f.store.UpsertAnnotationTask(f.ctx, foreign); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.item.LegacyTaskID = foreign.ID
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r := f.request("DELETE", "/api/users/"+f.annotator.ID, f.admin, nil); r.Code != 500 {
|
|
t.Fatal("mismatched scope was reset", r.Code)
|
|
}
|
|
after, _ := f.store.GetAnnotationTask(f.ctx, foreign.ID)
|
|
if after.AnnotationEpoch != 0 || after.CurrentRevision != foreign.CurrentRevision {
|
|
t.Fatal("foreign task changed")
|
|
}
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if item.AnnotationEpoch != 0 || item.AccountHandoff != nil {
|
|
t.Fatal("ambiguous source was reset")
|
|
}
|
|
}
|