forked from longtao.wu/droboticflow
295 lines
11 KiB
Go
295 lines
11 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/event"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func newQuerySyncSource(t *testing.T, monitors ...*event.CommandMonitor) *MongoStore {
|
|
t.Helper()
|
|
if os.Getenv("QUERY_REPLICA_LOCAL_TEST") != "1" {
|
|
t.Skip("isolated source/query Mongo required")
|
|
}
|
|
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
|
|
defer cancel()
|
|
opts := options.Client().ApplyURI("mongodb://127.0.0.1:27119/?directConnection=true")
|
|
if len(monitors) > 0 {
|
|
opts.SetMonitor(monitors[0])
|
|
}
|
|
c, e := mongo.Connect(ctx, opts)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var hello bson.M
|
|
if e = c.Database("admin").RunCommand(ctx, bson.M{"hello": 1}).Decode(&hello); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if hello["setName"] != nil {
|
|
t.Fatal("source must reproduce production's non-CDC standalone entry point")
|
|
}
|
|
db := c.Database(domain.NewID("query_sync_test"))
|
|
t.Cleanup(func() {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
_ = db.Drop(ctx)
|
|
_ = c.Disconnect(ctx)
|
|
})
|
|
s := NewMongoStore(db)
|
|
s.querySyncEnabled.Store(true)
|
|
return s
|
|
}
|
|
func readQuerySync(t *testing.T, c mongoCollection, id any) (bson.M, bson.M) {
|
|
t.Helper()
|
|
var d bson.M
|
|
if e := c.FindOne(t.Context(), bson.M{"_id": id}).Decode(&d); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return d, deletionDoc(d[querySyncField])
|
|
}
|
|
func querySyncRev(d bson.M) int64 { return int64(supplierNumber(d["revision"])) }
|
|
func TestQuerySyncAtomicVersionAndDraftSuppressionLocal(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
ctx := t.Context()
|
|
id := primitive.NewObjectID()
|
|
c := s.queryCollection("humanItems")
|
|
doc := bson.M{"_id": id, "id": "item", "organizationId": "org", "projectId": "p", "version": 1, "status": "claimed", "currentStageId": "annotation_combined", "subject": bson.M{"itemKey": "capture/part0001", "payload": bson.M{"annotationDraft": bson.M{"description": "original annotation"}}}}
|
|
if _, e := c.InsertOne(ctx, doc); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
_, meta := readQuerySync(t, c, id)
|
|
if querySyncRev(meta) != 1 || meta["dirty"] != true {
|
|
t.Fatal(meta)
|
|
}
|
|
_, e := c.Collection.UpdateOne(ctx, bson.M{"_id": id}, bson.M{"$set": bson.M{querySyncField + ".dirty": false}})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
delete(doc, "_id")
|
|
doc["version"] = 2
|
|
doc["subject"] = bson.M{"itemKey": "capture/part0001", "payload": bson.M{"annotationDraft": bson.M{"description": "latest annotation"}}}
|
|
r, e := c.ReplaceOne(ctx, bson.M{"id": "item", "version": 1}, doc)
|
|
if e != nil || r.MatchedCount != 1 {
|
|
t.Fatal(r, e)
|
|
}
|
|
current, meta := readQuerySync(t, c, id)
|
|
if querySyncRev(meta) != 1 || meta["dirty"] != false || deletionString(deletionDoc(deletionDoc(deletionDoc(current["subject"])["payload"])["annotationDraft"]), "description") != "latest annotation" {
|
|
t.Fatal("draft edit or source identity changed incorrectly", meta)
|
|
}
|
|
r, e = c.ReplaceOne(ctx, bson.M{"id": "item", "version": 1}, doc)
|
|
if e != nil || r.MatchedCount != 0 {
|
|
t.Fatal("business version CAS bypassed", r, e)
|
|
}
|
|
doc["status"] = "submitted"
|
|
doc["version"] = 3
|
|
if _, e = c.ReplaceOne(ctx, bson.M{"id": "item", "version": 2}, doc); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
_, meta = readQuerySync(t, c, id)
|
|
if querySyncRev(meta) != 2 || meta["dirty"] != true {
|
|
t.Fatal("semantic change lost", meta)
|
|
}
|
|
}
|
|
func TestQuerySyncIrrelevantLogWritesAndUpsertIdentityLocal(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
ctx := t.Context()
|
|
c := s.queryCollection("runs")
|
|
doc := bson.M{"id": "r", "organizationId": "org", "status": "running", "nodeExecutions": bson.A{bson.M{"nodeId": "n", "logs": bson.A{}}}}
|
|
if _, e := c.UpdateOne(ctx, bson.M{"id": "r"}, bson.M{"$set": doc}, options.Update().SetUpsert(true)); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var inserted bson.M
|
|
if e := c.FindOne(ctx, bson.M{"id": "r"}).Decode(&inserted); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if inserted["_id"] == nil {
|
|
t.Fatal("upsert lost generated identity")
|
|
}
|
|
id := inserted["_id"]
|
|
_, meta := readQuerySync(t, c, id)
|
|
before := querySyncRev(meta)
|
|
if _, e := c.UpdateOne(ctx, bson.M{"id": "r"}, bson.M{"$push": bson.M{"nodeExecutions.0.logs": bson.M{"message": "log must not be replicated"}}}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
_, meta = readQuerySync(t, c, id)
|
|
if querySyncRev(meta) != before {
|
|
t.Fatal("logs caused projection cycle", meta)
|
|
}
|
|
}
|
|
func TestQuerySyncDeleteRecoveryFencesLateDeleteAndPreservesEditsLocal(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
ctx := t.Context()
|
|
c := s.queryCollection("humanItems")
|
|
id := primitive.NewObjectID()
|
|
if _, e := c.InsertOne(ctx, bson.M{"_id": id, "id": "item", "version": 1, "subject": bson.M{"payload": bson.M{"annotationDraft": "must remain"}}}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
i, filter, finish, e := c.prepareDelete(ctx, bson.M{"id": "item", "version": 1}, false, nil)
|
|
defer finish()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
expired := time.Now().Add(-time.Second)
|
|
_, e = c.deleteIntents().UpdateOne(ctx, bson.M{"_id": i.ID}, bson.M{"$set": bson.M{"leaseUntil": expired}})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
i.Phase = "deleting"
|
|
i.LeaseUntil = expired
|
|
if ok, e := c.recoverDelete(ctx, i, time.Now()); e != nil || !ok {
|
|
t.Fatal(ok, e)
|
|
}
|
|
if r, e := c.Collection.DeleteOne(ctx, filter); e != nil || r.DeletedCount != 0 {
|
|
t.Fatal("late delete passed cancelled claim", r, e)
|
|
}
|
|
doc, _ := readQuerySync(t, c, id)
|
|
if deletionString(deletionDoc(deletionDoc(doc["subject"])["payload"]), "annotationDraft") != "must remain" {
|
|
t.Fatal("recovery changed annotation")
|
|
}
|
|
if _, e = c.DeleteOne(ctx, bson.M{"id": "item", "version": 1}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if n, e := c.CountDocuments(ctx, bson.M{"id": "item"}); e != nil || n != 0 {
|
|
t.Fatal("actual deletion changed", n, e)
|
|
}
|
|
}
|
|
|
|
func TestQuerySyncBulkPipelineAndDeleteSortLocal(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
ctx := t.Context()
|
|
c := s.queryCollection("humanItems")
|
|
if _, e := c.BulkWrite(ctx, []mongo.WriteModel{mongo.NewInsertOneModel().SetDocument(bson.M{"id": "a", "status": "ready", "version": 1}), mongo.NewInsertOneModel().SetDocument(bson.M{"id": "b", "status": "ready", "version": 1})}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, update := range []any{bson.D{{Key: "$set", Value: bson.M{"status": "claimed"}}}, bson.A{bson.M{"$set": bson.M{"status": "submitted"}}}, mongo.Pipeline{{{Key: "$set", Value: bson.M{"status": "ready"}}}}} {
|
|
if _, e := c.UpdateOne(ctx, bson.M{"id": "a"}, update); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
var deleted bson.M
|
|
if e := c.FindOneAndDelete(ctx, bson.M{}, options.FindOneAndDelete().SetSort(bson.D{{Key: "id", Value: -1}})).Decode(&deleted); e != nil || deleted["id"] != "b" {
|
|
t.Fatal("delete sort changed", deleted, e)
|
|
}
|
|
var nilModel *mongo.UpdateOneModel
|
|
if _, e := c.BulkWrite(ctx, []mongo.WriteModel{nilModel}); e == nil {
|
|
t.Fatal("nil model not rejected")
|
|
}
|
|
if _, e := c.UpdateOne(ctx, bson.M{"id": "a"}, mongo.Pipeline{}); e == nil {
|
|
t.Fatal("empty pipeline unexpectedly updates marker")
|
|
}
|
|
}
|
|
func TestQuerySyncPendingSubmissionVersionIsQueryRelevantLocal(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
ctx := t.Context()
|
|
c := s.queryCollection("humanItems")
|
|
d := bson.M{"id": "i", "version": 2, "status": "claimed", "pendingCombinedSubmissionId": "fanyu-v5-combined-i-v1"}
|
|
r, e := c.InsertOne(ctx, d)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
_, before := readQuerySync(t, c, r.InsertedID)
|
|
d["version"] = 3
|
|
if _, e = c.ReplaceOne(ctx, bson.M{"id": "i", "version": 2}, d); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
_, after := readQuerySync(t, c, r.InsertedID)
|
|
if querySyncRev(after) != querySyncRev(before)+1 {
|
|
t.Fatal("submission conflict classification version missed")
|
|
}
|
|
}
|
|
|
|
func TestQuerySyncImmutableReceiptRetryDoesNotWriteLocal(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
ctx := t.Context()
|
|
c := s.queryCollection("annotationWorklogEvents")
|
|
update := bson.M{"$setOnInsert": bson.M{"id": "receipt", "taskId": "t", "userId": "u", "eventType": "combined_submitted"}}
|
|
if _, e := c.UpdateOne(ctx, bson.M{"id": "receipt"}, update, options.Update().SetUpsert(true)); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var first bson.M
|
|
if e := c.FindOne(ctx, bson.M{"id": "receipt"}).Decode(&first); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
result, e := c.UpdateOne(ctx, bson.M{"id": "receipt"}, update, options.Update().SetUpsert(true))
|
|
if e != nil || result.ModifiedCount != 0 || result.UpsertedCount != 0 {
|
|
t.Fatal("immutable retry produced a write", result, e)
|
|
}
|
|
var after bson.M
|
|
if e = c.FindOne(ctx, bson.M{"id": "receipt"}).Decode(&after); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if !reflect.DeepEqual(first, after) {
|
|
t.Fatal("receipt retry changed data or sync marker")
|
|
}
|
|
}
|
|
func TestQuerySyncSubmissionRecoveryEventsRemainIdempotentLocal(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
if e := s.EnsureIndexes(t.Context()); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
checkSubmissionRecoveryEventOnce(t, s)
|
|
}
|
|
|
|
func TestQuerySyncAuthorityContractsLocal(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
run func(*testing.T, Store)
|
|
}{
|
|
{"supplier-management", supplierManagementContract},
|
|
{"supplier-deletion", supplierCascadeContract},
|
|
{"acceptance-authorization", testSharedAcceptanceStore},
|
|
{"acceptance-revocation", testAcceptanceRevocationRaceAndQueue},
|
|
{"progress-reads", testLatestProgressReads},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
if e := s.EnsureIndexes(t.Context()); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
tc.run(t, queryContractStore{s})
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestQuerySyncReplacementOptionsPreserveSDKSemanticsLocal(t *testing.T) {
|
|
s := newQuerySyncSource(t)
|
|
ctx := t.Context()
|
|
r, e := s.humanItems.ReplaceOne(ctx, bson.M{"id": "new"}, bson.M{"id": "new", "version": 1}, options.Replace().SetUpsert(true), options.Replace().SetComment("second option"))
|
|
if e != nil || r.UpsertedCount != 1 {
|
|
t.Fatal("multiple option upsert lost", r, e)
|
|
}
|
|
var after bson.M
|
|
e = s.humanItems.FindOneAndReplace(ctx, bson.M{"id": "new"}, bson.M{"id": "new", "version": 2}, options.FindOneAndReplace().SetReturnDocument(options.After), options.FindOneAndReplace().SetComment("second option")).Decode(&after)
|
|
if e != nil || supplierNumber(after["version"]) != 2 {
|
|
t.Fatal("multiple option return document lost", after, e)
|
|
}
|
|
}
|
|
|
|
// Older store contracts leave the source key empty. Supply a distinct synthetic
|
|
// identity so those fixtures also satisfy the production unique index; retain
|
|
// every real authorization, CAS, deletion and annotation assertion.
|
|
type queryContractStore struct{ *MongoStore }
|
|
|
|
func (s queryContractStore) UpsertHumanItem(ctx context.Context, item domain.HumanItemRecord) error {
|
|
if item.Subject.ItemKey == "" {
|
|
item.Subject.ItemKey = item.ID
|
|
}
|
|
return s.MongoStore.UpsertHumanItem(ctx, item)
|
|
}
|
|
|
|
func (s queryContractStore) ClaimHumanItemVersioned(ctx context.Context, item domain.HumanItemRecord, version int, role domain.UserRole) (bool, error) {
|
|
if item.Subject.ItemKey == "" {
|
|
item.Subject.ItemKey = item.ID
|
|
}
|
|
return s.MongoStore.ClaimHumanItemVersioned(ctx, item, version, role)
|
|
}
|