forked from longtao.wu/droboticflow
114 lines
4.1 KiB
Go
114 lines
4.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"os"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func acceptancePriorityStoreContract(t *testing.T, s Store) {
|
|
t.Helper()
|
|
ctx := t.Context()
|
|
v := domain.AcceptancePriority{OrganizationID: domain.DefaultOrganizationID, SupplierID: "producer", Enabled: true, ProjectIDs: []string{"p"}, ExpiresAt: time.Now().Add(time.Hour).UTC().Format(time.RFC3339), UpdatedBy: "admin"}
|
|
rows, e := s.ListAcceptancePriorities(ctx, v.OrganizationID)
|
|
if e != nil || len(rows) != 0 {
|
|
t.Fatalf("default %v %v", rows, e)
|
|
}
|
|
saved, e := s.SaveAcceptancePriority(ctx, v, 0)
|
|
if e != nil || saved.Version != 1 || len(saved.History) != 1 {
|
|
t.Fatalf("save %+v %v", saved, e)
|
|
}
|
|
var wins atomic.Int32
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 8; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
copy := v
|
|
copy.Enabled = false
|
|
if _, err := s.SaveAcceptancePriority(ctx, copy, 1); err == nil {
|
|
wins.Add(1)
|
|
} else if err != ErrAcceptancePriorityConflict {
|
|
t.Errorf("CAS %v", err)
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if wins.Load() != 1 {
|
|
t.Fatalf("CAS wins %d", wins.Load())
|
|
}
|
|
rows, e = s.ListAcceptancePriorities(ctx, v.OrganizationID)
|
|
if e != nil || len(rows) != 1 || rows[0].Enabled || rows[0].Version != 2 || len(rows[0].History) != 2 {
|
|
t.Fatalf("recovery %+v %v", rows, e)
|
|
}
|
|
rows[0].History[0].ProjectIDs[0] = "tampered"
|
|
rows, e = s.ListAcceptancePriorities(ctx, v.OrganizationID)
|
|
if e != nil || rows[0].History[0].ProjectIDs[0] != "p" {
|
|
t.Fatal("history mutated")
|
|
}
|
|
// Ownership remains resolvable after employee deletion; no employee record is consulted.
|
|
org := v.OrganizationID
|
|
source := domain.HumanBatchRecord{ID: "source", OrganizationID: org, ProjectID: "p"}
|
|
acceptance := domain.HumanBatchRecord{ID: "acceptance", OrganizationID: org, ProjectID: "p", SourceBatchID: source.ID}
|
|
for _, b := range []domain.HumanBatchRecord{source, acceptance} {
|
|
if err := s.UpsertHumanBatch(ctx, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
task := domain.AnnotationTaskRecord{ID: "task", OrganizationID: org, ProjectID: "p", SupplierID: "producer"}
|
|
if err := s.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
sourceItem := domain.HumanItemRecord{ID: "source-item", OrganizationID: org, ProjectID: "p", BatchID: source.ID, LegacyTaskID: task.ID, LatestAuthorUserID: "deleted-author", Version: 1}
|
|
if err := s.UpsertHumanItem(ctx, sourceItem); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
owners, err := s.ResolveAcceptanceSourceSuppliers(ctx, org, []string{acceptance.ID, "missing"})
|
|
if err != nil || owners[acceptance.ID] != "producer" || owners["missing"] != "" {
|
|
t.Fatalf("ownership %+v %v", owners, err)
|
|
}
|
|
sourceItem.SupplierID = "conflicting-producer"
|
|
sourceItem.Version++
|
|
if err := s.UpsertHumanItem(ctx, sourceItem); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
owners, err = s.ResolveAcceptanceSourceSuppliers(ctx, org, []string{acceptance.ID})
|
|
if err != nil || owners[acceptance.ID] != "" {
|
|
t.Fatalf("conflicting source %+v %v", owners, err)
|
|
}
|
|
rows, e = s.ListAcceptancePriorities(ctx, "foreign-org")
|
|
if e != nil || len(rows) != 0 {
|
|
t.Fatal("cross organization policy leak")
|
|
}
|
|
}
|
|
func TestAcceptancePriorityStoreMemory(t *testing.T) {
|
|
acceptancePriorityStoreContract(t, NewMemoryStore())
|
|
}
|
|
func TestAcceptancePriorityStoreMongo(t *testing.T) {
|
|
if os.Getenv("ACCEPTANCE_PRIORITY_LOCAL_MONGO") != "1" {
|
|
t.Skip("isolated local Mongo opt-in")
|
|
}
|
|
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
|
|
defer cancel()
|
|
client, e := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018"))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("acceptance_priority_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
acceptancePriorityStoreContract(t, NewMongoStore(db))
|
|
// New store instance uses persisted cancellation/history, without initialization writes.
|
|
rows, e := NewMongoStore(db).ListAcceptancePriorities(ctx, domain.DefaultOrganizationID)
|
|
if e != nil || len(rows) != 1 || rows[0].Enabled || rows[0].Version != 2 {
|
|
t.Fatal("restart lost policy")
|
|
}
|
|
}
|