forked from longtao.wu/droboticflow
193 lines
8.5 KiB
Go
193 lines
8.5 KiB
Go
package store
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
type sourceProofCase struct {
|
|
name, timelineState, timelineFP, sourceState, sourceFP, superseded, currentFP, want string
|
|
indexed bool
|
|
}
|
|
|
|
func sourceProofCases() []sourceProofCase {
|
|
return []sourceProofCase{
|
|
{name: "physical-changed-missing-proof", timelineState: "completed", timelineFP: "A", currentFP: "B", want: "unverified"},
|
|
{name: "physical-current-task-proof", timelineState: "completed", timelineFP: "A", currentFP: "A"},
|
|
{name: "physical-current-certificate", timelineState: "completed", timelineFP: "A", currentFP: "B", sourceState: "completed", sourceFP: "B"},
|
|
{name: "physical-wrong-certificate", timelineState: "completed", timelineFP: "A", currentFP: "B", sourceState: "completed", sourceFP: "A", want: "unverified"},
|
|
{name: "physical-current-pending", timelineState: "completed", timelineFP: "A", currentFP: "B", sourceState: "queued", want: "pending"},
|
|
{name: "physical-current-failed", timelineState: "completed", timelineFP: "A", currentFP: "B", sourceState: "failed", sourceFP: "B", want: "failed"},
|
|
{name: "indexed-changed-missing-proof", indexed: true, timelineState: "completed", timelineFP: "A", currentFP: "B", want: "unverified"},
|
|
{name: "indexed-current-task-proof", indexed: true, timelineState: "completed", timelineFP: "A", currentFP: "A"},
|
|
{name: "indexed-normal-pending-task-proof", indexed: true, timelineState: "queued", currentFP: "A", want: "pending"},
|
|
{name: "indexed-pending-task-proof", indexed: true, timelineState: "queued", currentFP: "A", superseded: "A", want: "pending"},
|
|
{name: "legacy-timeline-only", timelineState: "completed", currentFP: "A", want: "unverified"},
|
|
{name: "legacy-no-record", currentFP: "A", want: "unverified"},
|
|
{name: "controlled-repair", indexed: true, timelineState: "completed", timelineFP: "B", currentFP: "A", superseded: "A"},
|
|
{name: "repair-not-for-later-source", timelineState: "completed", timelineFP: "B", currentFP: "C", superseded: "A", want: "unverified"},
|
|
{name: "repair-later-proof-pending", timelineState: "completed", timelineFP: "B", currentFP: "C", superseded: "A", sourceState: "running", want: "pending"},
|
|
{name: "repair-later-proof-failed", timelineState: "completed", timelineFP: "B", currentFP: "C", superseded: "A", sourceState: "failed", want: "failed"},
|
|
}
|
|
}
|
|
|
|
func (c sourceProofCase) records(id string) (domain.AnnotationTaskRecord, domain.HumanItemRecord, *domain.AnnotationIndexJob, *domain.AnnotationIndexJob) {
|
|
// Real fingerprints include the Run identity. Keep fixture jobs unique
|
|
// across cases too, so a B proof for another Run cannot alias this one.
|
|
fp := func(v string) string {
|
|
if v == "" {
|
|
return ""
|
|
}
|
|
return id + "-" + v
|
|
}
|
|
task := domain.AnnotationTaskRecord{ID: id, OrganizationID: "proof-org", ProjectID: "proof-project", RunID: id, TemplateVersion: 4}
|
|
item := domain.HumanItemRecord{ID: id, OrganizationID: task.OrganizationID, ProjectID: task.ProjectID, RunID: task.RunID, LegacyTaskID: id, Version: 7, ProcessingJob: &domain.AnnotationProcessingJob{Kind: "slice", Status: "failed", SourceIndex: c.indexed, SourceFingerprint: fp(c.currentFP)}, Subject: domain.HumanSubjectRef{Payload: map[string]any{"annotationWorkspace": map[string]any{"preserve": "reviewed contents"}, "annotationDraft": map[string]any{"preserve": "latest edits"}}}}
|
|
var timeline, source *domain.AnnotationIndexJob
|
|
job := domain.AnnotationIndexJob{OrganizationID: task.OrganizationID, ProjectID: task.ProjectID, RunID: task.RunID}
|
|
if c.timelineState != "" {
|
|
v := job
|
|
v.ID = id
|
|
v.Status = c.timelineState
|
|
v.SourceFingerprint = fp(c.timelineFP)
|
|
v.RevalidateSource = true
|
|
v.SupersededSourceFingerprint = fp(c.superseded)
|
|
timeline = &v
|
|
}
|
|
if c.sourceState != "" {
|
|
v := job
|
|
v.ID = AnnotationSourceJobID(task.OrganizationID, fp(c.currentFP))
|
|
v.Status = c.sourceState
|
|
v.SourceFingerprint = fp(c.sourceFP)
|
|
source = &v
|
|
}
|
|
return task, item, timeline, source
|
|
}
|
|
|
|
func TestSourceAdmissionCurrentOutboxProof(t *testing.T) {
|
|
for i, c := range sourceProofCases() {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
task, item, timeline, source := c.records(fmt.Sprint("proof-", i))
|
|
before := clone(item)
|
|
if got := AnnotationItemSourceJobsReady(task, item, timeline, source); got != (c.want == "") {
|
|
t.Errorf("ready=%t want status=%q", got, c.want)
|
|
}
|
|
if got := AnnotationSourceAdmissionStatus(task, item, timeline, source); got != c.want {
|
|
t.Errorf("status=%q want=%q", got, c.want)
|
|
}
|
|
if !reflect.DeepEqual(item, before) {
|
|
t.Fatal("admission changed saved annotation")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceAdmissionCurrentOutboxProofMongo(t *testing.T) {
|
|
ctx, s := poolLocalMongo(t)
|
|
for i, c := range sourceProofCases() {
|
|
task, item, timeline, source := c.records(fmt.Sprint("proof-", i))
|
|
for name, value := range map[string]any{"annotationTasks": task, "humanItems": item} {
|
|
if _, err := s.db.Collection(name).InsertOne(ctx, value); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
for _, job := range []*domain.AnnotationIndexJob{timeline, source} {
|
|
if job != nil {
|
|
if _, err := s.db.Collection("annotationIndexJobs").InsertOne(ctx, job); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|
|
var projected struct {
|
|
Ready bool `bson:"_sourceReady"`
|
|
Status string `bson:"_sourceAdmissionStatus"`
|
|
}
|
|
pipeline := append([]bson.D{{{Key: "$match", Value: bson.M{"id": item.ID}}}}, annotationSourceAdmissionStatusPipeline()...)
|
|
cur, err := s.humanItems.Aggregate(ctx, pipeline)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !cur.Next(ctx) {
|
|
t.Fatal("missing projection", cur.Err())
|
|
}
|
|
err = cur.Decode(&projected)
|
|
cur.Close(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if projected.Ready != (c.want == "") || projected.Status != c.want {
|
|
t.Errorf("%s Mongo ready=%t status=%q want=%q", c.name, projected.Ready, projected.Status, c.want)
|
|
}
|
|
rows := []domain.HumanItemRecord{item}
|
|
if err := s.EnrichAnnotationSourceStatuses(ctx, rows); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rows[0].SourceValidationStatus != c.want {
|
|
t.Errorf("%s batch status=%q want=%q", c.name, rows[0].SourceValidationStatus, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSourceAdmissionExistingTaskUpdateDoesNotEnqueue(t *testing.T) {
|
|
ctx := t.Context()
|
|
s := NewMemoryStore()
|
|
task := domain.AnnotationTaskRecord{ID: "legacy-no-job", OrganizationID: "proof-org", ProjectID: "proof-project", RunID: "run", TemplateVersion: 4}
|
|
s.annotationTasks[task.ID] = task
|
|
task.Status = domain.AnnotationTaskStatusPendingQuality
|
|
if err := s.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if job, _ := s.GetAnnotationIndexJob(ctx, task.ID); job != nil {
|
|
t.Fatal("existing projection update started full validation")
|
|
}
|
|
}
|
|
|
|
func TestSourceAdmissionExistingTaskUpdateDoesNotEnqueueMongo(t *testing.T) {
|
|
ctx, s := poolLocalMongo(t)
|
|
task := domain.AnnotationTaskRecord{ID: "legacy-no-job", OrganizationID: "proof-org", ProjectID: "proof-project", RunID: "run", TemplateVersion: 4}
|
|
if _, err := s.annotationTasks.InsertOne(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task.Status = domain.AnnotationTaskStatusPendingQuality
|
|
if err := s.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if job, _ := s.GetAnnotationIndexJob(ctx, task.ID); job != nil {
|
|
t.Fatal("existing projection update started full validation")
|
|
}
|
|
// Explicit repair remains restart-safe and idempotent; ordinary updates
|
|
// must not overwrite its queued/running/completed/failed evidence.
|
|
if err := s.RetryAnnotationSourceValidation(ctx, task, true, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
job, err := s.GetAnnotationIndexJob(ctx, task.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, state := range []string{"queued", "running", "completed", "failed"} {
|
|
job.Status = state
|
|
job.SourceFingerprint = "A"
|
|
if err := mongoUpsert(ctx, s.db.Collection("annotationIndexJobs"), job.ID, job); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after, err := s.GetAnnotationIndexJob(ctx, task.ID)
|
|
if err != nil || !reflect.DeepEqual(job, after) {
|
|
t.Fatal("projection update replaced repair evidence", state, err)
|
|
}
|
|
}
|
|
// A genuinely new legacy-format task still enters full validation.
|
|
task.ID = "new-legacy-task"
|
|
if err := s.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if job, _ := s.GetAnnotationIndexJob(ctx, task.ID); job == nil || !job.RevalidateSource || job.Status != "queued" {
|
|
t.Fatal("new task lost automatic validation")
|
|
}
|
|
}
|