forked from longtao.wu/droboticflow
120 lines
5.7 KiB
Go
120 lines
5.7 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func TestDeletedAnnotatorMongoRestartAndHistory(t *testing.T) {
|
|
if os.Getenv("DELETED_ANNOTATOR_LOCAL_MONGO") != "1" {
|
|
t.Skip("isolated local Mongo opt-in")
|
|
}
|
|
t.Setenv("DELETED_ANNOTATOR_HANDOFF", "true")
|
|
ctx, cancel := context.WithTimeout(t.Context(), 90*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018/?directConnection=true"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("deleted_account_api_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
for _, name := range []string{"organizations", "projects", "workflows", "users", "suppliers", "projectMembers", "annotationTasks", "annotationRevisions", "sliceRevisions", "humanItems", "humanBatches", "humanEvents", "humanStageAttempts", "humanClaimRequests"} {
|
|
if _, err := db.Collection(name).Indexes().CreateOne(ctx, mongo.IndexModel{Keys: bson.D{{Key: "id", Value: 1}}, Options: options.Index().SetUnique(true)}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, index := range []mongo.IndexModel{
|
|
{Keys: bson.D{{Key: "organizationId", Value: 1}, {Key: "currentStageId", Value: 1}, {Key: "reworkRoutingPending", Value: 1}, {Key: "status", Value: 1}}, Options: options.Index().SetName("deleted_author_routing_v1")},
|
|
{Keys: bson.D{{Key: "organizationId", Value: 1}, {Key: "currentStageId", Value: 1}, {Key: "inheritedRework", Value: 1}, {Key: "assignedUserId", Value: 1}, {Key: "status", Value: 1}}, Options: options.Index().SetName("deleted_author_unassigned_v1")},
|
|
} {
|
|
if _, err := db.Collection("humanItems").Indexes().CreateOne(ctx, index); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
f := newCombinedDraftFixture(t)
|
|
ds := store.NewMongoStore(db)
|
|
must := func(err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
must(ds.UpsertOrganization(ctx, domain.OrganizationRecord{ID: f.item.OrganizationID, Status: domain.OrganizationStatusActive}))
|
|
supplier, _ := f.store.GetSupplier(ctx, f.annotator.SupplierID)
|
|
must(ds.UpsertSupplier(ctx, *supplier))
|
|
project, _ := f.store.GetProject(ctx, f.item.ProjectID)
|
|
must(ds.UpsertProject(ctx, *project))
|
|
for _, user := range []domain.UserRecord{f.admin, f.annotator, f.reviewer} {
|
|
must(ds.UpsertUser(ctx, user))
|
|
}
|
|
members, _ := f.store.ListProjectMembers(ctx, f.item.OrganizationID, f.item.ProjectID)
|
|
for _, member := range members {
|
|
must(ds.UpsertProjectMember(ctx, member))
|
|
}
|
|
batch, _ := f.store.GetHumanBatch(ctx, f.item.BatchID)
|
|
must(ds.UpsertHumanBatch(ctx, *batch))
|
|
must(ds.UpsertAnnotationTask(ctx, f.task))
|
|
f.item.ProcessingJob = &domain.AnnotationProcessingJob{ID: "old-job", TaskID: f.task.ID, ActorID: f.annotator.ID, Kind: "slice", Revision: 2, Status: "running", Owner: "old-process"}
|
|
f.item.Subject.Payload["annotationDraft"] = map[string]any{"marker": "private work"}
|
|
must(ds.UpsertHumanItem(ctx, f.item))
|
|
// Already-reviewed material remains unrelated to the pre-quality reset.
|
|
protected := f.item
|
|
protected.ID = "reviewed-source"
|
|
protected.AssignedUserID = ""
|
|
protected.CurrentStageID = domain.FanyuV58StageQualityReview
|
|
protected.SubmittedAt = nowISO()
|
|
protected.LatestAuthorUserID = f.annotator.ID
|
|
protected.ProcessingJob = nil
|
|
protected.Subject.Payload = map[string]any{"annotationWorkspace": map[string]any{"marker": "formal work"}}
|
|
must(ds.UpsertHumanItem(ctx, protected))
|
|
must(ds.UpsertHumanStageAttempt(ctx, domain.HumanStageAttemptRecord{ID: "formal-review", OrganizationID: protected.OrganizationID, ItemID: protected.ID, StageID: domain.FanyuV58StageQualityReview, Status: domain.HumanItemAccepted, Comment: "正式质检记录"}))
|
|
before, _ := ds.GetHumanItem(ctx, protected.ID)
|
|
reviewsBefore, _ := ds.ListHumanStageAttempts(ctx, protected.OrganizationID, protected.ID)
|
|
probe := &handoffFailureStore{Store: ds, 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())
|
|
}
|
|
pending, _ := ds.GetHumanItem(ctx, f.item.ID)
|
|
user, _ := ds.GetUser(ctx, f.annotator.ID)
|
|
if pending.AccountHandoff == nil || !pending.AccountHandoff.Pending || pending.AnnotationEpoch != 1 || user.DeletionRequestedAt == "" {
|
|
t.Fatal("Mongo lost durable reset intent")
|
|
}
|
|
// Reconstruct both handler state and Store; no process-local recovery data.
|
|
restartedStore := store.NewMongoStore(db)
|
|
restarted := &server{store: restartedStore}
|
|
if err := restarted.recoverAccountHandoffs(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item, _ := restartedStore.GetHumanItem(ctx, f.item.ID)
|
|
user, _ = restartedStore.GetUser(ctx, f.annotator.ID)
|
|
if item.AnnotationEpoch != 1 || item.AccountHandoff.Pending || item.AssignedUserID != "" || item.ProcessingJob != nil || user.DeletedAt == "" {
|
|
t.Fatal("Mongo recovery incomplete")
|
|
}
|
|
if ok, err := restartedStore.UpdateHumanItemVersioned(ctx, f.item, f.item.Version); err != nil || ok {
|
|
t.Fatal("old submit/save won after persisted reset", err)
|
|
}
|
|
after, _ := restartedStore.GetHumanItem(ctx, protected.ID)
|
|
reviewsAfter, _ := restartedStore.ListHumanStageAttempts(ctx, protected.OrganizationID, protected.ID)
|
|
if !reflect.DeepEqual(before, after) || !reflect.DeepEqual(reviewsBefore, reviewsAfter) {
|
|
t.Fatal("quality history changed during account deletion")
|
|
}
|
|
if err := restarted.recoverAccountHandoffs(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rows, err := ds.ListHumanEvents(ctx, f.item.OrganizationID, f.item.ID); err != nil || len(rows) != 1 {
|
|
t.Fatal("restart duplicated reset audit", err)
|
|
}
|
|
}
|