forked from longtao.wu/droboticflow
196 lines
8.0 KiB
Go
196 lines
8.0 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// Opt-in on the local replica only; the uniquely named synthetic database is
|
|
// separate from the snapshot and is removed after verification.
|
|
func TestReworkMongoHTTPRepeatedReturnAcrossRestart(t *testing.T) {
|
|
if os.Getenv("REWORK_MONGO_HTTP") != "1" {
|
|
t.Skip("opt-in local Mongo HTTP verification")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*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("rework_http_test_%d", time.Now().UnixNano()))
|
|
defer func() {
|
|
cleanup, done := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer done()
|
|
if e := db.Drop(cleanup); e != nil {
|
|
t.Error(e)
|
|
}
|
|
}()
|
|
ds := store.NewMongoStore(db)
|
|
must := func(e error) {
|
|
t.Helper()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
seed := func(name string, value any) {
|
|
t.Helper()
|
|
if _, e := db.Collection(name).InsertOne(ctx, value); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
f := reworkContractFixture(t)
|
|
f.item.ReviewRound = 0
|
|
for _, name := range []string{"projects", "users", "suppliers", "projectMembers", "annotationTasks", "humanBatches", "humanItems", "humanEvents", "humanStageAttempts", "annotationReviews", "annotationRevisions", "sliceRevisions"} {
|
|
_, e := db.Collection(name).Indexes().CreateOne(ctx, mongo.IndexModel{Keys: bson.D{{Key: "id", Value: 1}}, Options: options.Index().SetUnique(true)})
|
|
must(e)
|
|
}
|
|
org, e := f.store.GetOrganization(ctx, f.item.OrganizationID)
|
|
must(e)
|
|
if org != nil {
|
|
seed("organizations", org)
|
|
}
|
|
project, e := f.store.GetProject(ctx, f.task.ProjectID)
|
|
must(e)
|
|
seed("projects", project)
|
|
workflow, e := f.store.GetWorkflow(ctx, f.task.WorkflowID)
|
|
must(e)
|
|
seed("workflows", workflow)
|
|
template, e := f.store.GetAnnotationTemplate(ctx, f.task.TemplateID)
|
|
must(e)
|
|
seed("annotationTemplates", template)
|
|
supplier, e := f.store.GetSupplier(ctx, f.annotator.SupplierID)
|
|
must(e)
|
|
seed("suppliers", supplier)
|
|
for _, user := range []domain.UserRecord{f.admin, f.annotator, f.reviewer} {
|
|
seed("users", user)
|
|
}
|
|
members, e := f.store.ListProjectMembers(ctx, f.item.OrganizationID, f.task.ProjectID)
|
|
must(e)
|
|
for _, m := range members {
|
|
seed("projectMembers", m)
|
|
}
|
|
membership, e := f.store.ListProjectAnnotationMemberships(ctx, f.item.OrganizationID, f.task.ProjectID)
|
|
must(e)
|
|
for _, m := range membership {
|
|
seed("projectAnnotationMemberships", m)
|
|
}
|
|
batch, e := f.store.GetHumanBatch(ctx, f.item.BatchID)
|
|
must(e)
|
|
seed("humanBatches", batch)
|
|
f.item.Subject.Payload[dictionarySelectionHistoryKey] = []domain.DictionarySelection{{ID: "historical-base", Kind: "slice", Version: 2, Code: "sort", Name: "物品操作", Tags: []string{"商品"}}}
|
|
seed("humanItems", f.item)
|
|
seed("annotationTasks", f.task)
|
|
slices, e := f.store.ListSliceRevisions(ctx, f.item.OrganizationID, f.task.ID)
|
|
must(e)
|
|
baseClass := &domain.DictionarySelection{ID: "historical-base", Kind: "slice", Version: 2, Code: "sort", Name: "物品操作", Tags: []string{"商品"}}
|
|
actionClass := &domain.DictionarySelection{ID: "historical-action", Kind: "action", Version: 3, Code: "pick", Name: "拿取", Tags: []string{"抓取"}}
|
|
for _, r := range slices {
|
|
for i := range r.Segments {
|
|
r.Segments[i].Dictionary = baseClass
|
|
}
|
|
seed("sliceRevisions", r)
|
|
}
|
|
actions, e := f.store.ListAnnotationRevisions(ctx, f.item.OrganizationID, f.task.ID)
|
|
must(e)
|
|
for _, r := range actions {
|
|
for i := range r.Segments {
|
|
r.Segments[i].Dictionary = actionClass
|
|
}
|
|
seed("annotationRevisions", r)
|
|
}
|
|
f.handler = NewHandler(ds, nil)
|
|
reworkRejectRound(t, f, "mongo-first", []map[string]any{{"id": "old", "scope": "slice", "sliceId": "left", "text": "原问题"}}, nil)
|
|
for _, operation := range []string{"mongo-second", "mongo-third"} {
|
|
item, e := ds.GetHumanItem(ctx, f.item.ID)
|
|
must(e)
|
|
item.CurrentStageID = domain.FanyuV58StageQualityReview
|
|
item.Status = domain.HumanItemClaimed
|
|
item.AssignedUserID = f.reviewer.ID
|
|
item.CurrentRevision++
|
|
item.Version++
|
|
must(ds.UpsertHumanItem(ctx, *item))
|
|
task, e := ds.GetAnnotationTask(ctx, f.task.ID)
|
|
must(e)
|
|
task.CurrentRevision = item.CurrentRevision
|
|
task.Status = domain.AnnotationTaskStatusPendingQuality
|
|
must(ds.UpsertAnnotationTask(ctx, *task))
|
|
// A fresh API/store instance must recover the baseline and accept old-only return.
|
|
f.handler = NewHandler(store.NewMongoStore(db), nil)
|
|
reworkRejectRound(t, f, operation, []map[string]any{}, []map[string]any{{"opinionId": "old", "resolved": false, "note": "仍需修正"}})
|
|
w := f.request(http.MethodGet, "/api/human-items?pool=mine&annotationView=active&projectId="+f.item.ProjectID, f.annotator, nil)
|
|
var out struct {
|
|
Items []domain.HumanItemRecord `json:"items"`
|
|
}
|
|
must(json.Unmarshal(w.Body.Bytes(), &out))
|
|
if w.Code != 200 || len(out.Items) != 1 || out.Items[0].AssignedUserID != f.annotator.ID {
|
|
t.Fatalf("persisted return missing from author pool: %d %s", w.Code, w.Body.String())
|
|
}
|
|
if snap := savedDictionarySelection(&out.Items[0], &domain.DictionarySelection{ID: "historical-base", Version: 2}, "slice"); snap == nil || snap.Tags[0] != "商品" {
|
|
t.Fatal("classification recovery ledger lost across Mongo restart")
|
|
}
|
|
baseline, e := ds.GetReworkBaseline(ctx, store.ReworkBaselineID(item.ID, out.Items[0].ReviewRound))
|
|
must(e)
|
|
if baseline == nil || len(baseline.Slices) == 0 || baseline.Slices[0].Dictionary == nil || baseline.Slices[0].Dictionary.Version != 2 || baseline.Slices[0].Dictionary.Tags[0] != "商品" {
|
|
t.Fatal("base classification lost across Mongo review/restart")
|
|
}
|
|
t.Logf("%s: round %d persisted and visible after restart", operation, out.Items[0].ReviewRound)
|
|
}
|
|
item, e := ds.GetHumanItem(ctx, f.item.ID)
|
|
must(e)
|
|
historyID := store.ReworkHandlingID(item.ID, 1)
|
|
saved, e := ds.CompareAndSwapReworkHandling(ctx, domain.ReworkHandlingRecord{
|
|
ID: historyID, OrganizationID: item.OrganizationID, ItemID: item.ID, Round: 1, SkippedOpinionIDs: []string{},
|
|
}, 0)
|
|
must(e)
|
|
if !saved {
|
|
t.Fatal("history seed failed")
|
|
}
|
|
endpoint := "/api/human-items/" + item.ID + "/rework-handling"
|
|
for _, actor := range []domain.UserRecord{f.annotator, f.admin} {
|
|
f.handler = NewHandler(store.NewMongoStore(db), nil)
|
|
w := f.request(http.MethodPut, endpoint+"?round=1", actor,
|
|
map[string]any{"round": 1, "version": 1, "skippedOpinionIds": []string{"old"}})
|
|
if w.Code != http.StatusConflict {
|
|
t.Fatalf("historical write accepted: %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|
|
history, e := ds.GetReworkHandling(ctx, historyID)
|
|
must(e)
|
|
if history.Version != 1 || len(history.SkippedOpinionIDs) != 0 {
|
|
t.Fatalf("history changed: %+v", history)
|
|
}
|
|
item.AssignedUserID = f.reviewer.ID
|
|
must(ds.UpsertHumanItem(ctx, *item))
|
|
f.handler = NewHandler(store.NewMongoStore(db), nil)
|
|
w := f.request(http.MethodPut, endpoint, f.reviewer,
|
|
map[string]any{"round": item.ReviewRound, "version": 0, "skippedOpinionIds": []string{"old"}})
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("quality assignee write accepted: %d %s", w.Code, w.Body.String())
|
|
}
|
|
item.AssignedUserID = f.annotator.ID
|
|
must(ds.UpsertHumanItem(ctx, *item))
|
|
f.handler = NewHandler(store.NewMongoStore(db), nil)
|
|
w = f.request(http.MethodPut, endpoint, f.annotator,
|
|
map[string]any{"round": item.ReviewRound, "version": 0, "skippedOpinionIds": []string{"old"}})
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("current owner write failed: %d %s", w.Code, w.Body.String())
|
|
}
|
|
f.handler = NewHandler(store.NewMongoStore(db), nil)
|
|
w = f.request(http.MethodGet, endpoint, f.reviewer, nil)
|
|
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `"skippedOpinionIds":["old"]`) {
|
|
t.Fatalf("handling lost across restart: %d %s", w.Code, w.Body.String())
|
|
}
|
|
}
|