forked from longtao.wu/droboticflow
205 lines
8.2 KiB
Go
205 lines
8.2 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
type sourceProofReadStore struct {
|
|
*store.MemoryStore
|
|
timeline, source *domain.AnnotationIndexJob
|
|
taskWrites int
|
|
}
|
|
|
|
func (s *sourceProofReadStore) GetAnnotationIndexJob(_ context.Context, id string) (*domain.AnnotationIndexJob, error) {
|
|
for _, j := range []*domain.AnnotationIndexJob{s.timeline, s.source} {
|
|
if j != nil && j.ID == id {
|
|
return j, nil
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
func (s *sourceProofReadStore) UpsertAnnotationTask(ctx context.Context, v domain.AnnotationTaskRecord) error {
|
|
s.taskWrites++
|
|
return s.MemoryStore.UpsertAnnotationTask(ctx, v)
|
|
}
|
|
|
|
func TestSourceValidationCurrentOutboxStatusAndTimeline(t *testing.T) {
|
|
for _, c := range []struct {
|
|
name, timelineFP, timelineState, sourceState, sourceFP, superseded, current, want string
|
|
indexed bool
|
|
}{
|
|
{name: "physical-changed", timelineFP: "A", current: "B", want: "unverified"},
|
|
{name: "physical-same", timelineFP: "A", current: "A", want: "valid"},
|
|
{name: "physical-source-completed", timelineFP: "A", current: "B", sourceState: "completed", sourceFP: "B", want: "valid"},
|
|
{name: "physical-source-pending", timelineFP: "A", current: "B", sourceState: "queued", want: "pending"},
|
|
{name: "physical-source-failed", timelineFP: "A", current: "B", sourceState: "failed", want: "failed"},
|
|
{name: "indexed-changed", indexed: true, timelineFP: "A", current: "B", want: "unverified"},
|
|
{name: "indexed-task-pending", indexed: true, timelineState: "queued", current: "B", want: "pending"},
|
|
{name: "explicit-repair", indexed: true, timelineFP: "B", current: "A", superseded: "A", want: "valid"},
|
|
{name: "repair-not-for-C", timelineFP: "B", current: "C", superseded: "A", want: "unverified"},
|
|
} {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
item := f.item
|
|
item.ProcessingJob = &domain.AnnotationProcessingJob{Kind: "slice", Status: "failed", SourceFingerprint: c.current, SourceIndex: c.indexed}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"saved": "keep reviewed contents"}
|
|
item.Subject.Payload["annotationDraft"] = map[string]any{"saved": "keep newer edits"}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
job := domain.AnnotationIndexJob{ID: f.task.ID, OrganizationID: f.task.OrganizationID, ProjectID: f.task.ProjectID, RunID: f.task.RunID, Status: "completed", RevalidateSource: true, SourceFingerprint: c.timelineFP, SupersededSourceFingerprint: c.superseded}
|
|
if c.timelineState != "" {
|
|
job.Status = c.timelineState
|
|
}
|
|
probe := &sourceProofReadStore{MemoryStore: f.store, timeline: &job}
|
|
if c.sourceState != "" {
|
|
source := job
|
|
source.ID = store.AnnotationSourceJobID(job.OrganizationID, c.current)
|
|
source.Status = c.sourceState
|
|
source.SourceFingerprint = c.sourceFP
|
|
source.SupersededSourceFingerprint = ""
|
|
probe.source = &source
|
|
}
|
|
f.handler = NewHandler(probe, nil)
|
|
check := func(status annotationSourceValidation) {
|
|
t.Helper()
|
|
if status.Status != c.want || !status.Required || status.RetryAllowed != (c.want == "unverified" || c.want == "failed") {
|
|
t.Fatalf("status=%+v want=%s", status, c.want)
|
|
}
|
|
}
|
|
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"?view=status", f.admin, nil)
|
|
var response struct {
|
|
SourceValidation annotationSourceValidation `json:"sourceValidation"`
|
|
}
|
|
if r.Code != 200 || json.Unmarshal(r.Body.Bytes(), &response) != nil {
|
|
t.Fatal(r.Code, r.Body.String())
|
|
}
|
|
check(response.SourceValidation)
|
|
if c.want != "valid" {
|
|
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.admin, nil)
|
|
var blocked struct {
|
|
Details struct {
|
|
SourceValidation annotationSourceValidation `json:"sourceValidation"`
|
|
} `json:"details"`
|
|
}
|
|
if r.Code != 422 || json.Unmarshal(r.Body.Bytes(), &blocked) != nil {
|
|
t.Fatal(r.Code, r.Body.String())
|
|
}
|
|
check(blocked.Details.SourceValidation)
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if probe.taskWrites != 0 || !reflect.DeepEqual(before, after) {
|
|
t.Fatal("source reads changed saved work or task projection")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceValidationLegacyDetailsGETDoesNotEnqueueMongo(t *testing.T) {
|
|
if os.Getenv("SOURCE_ADMISSION_LOCAL_MONGO") != "1" {
|
|
t.Skip("isolated local Mongo opt-in")
|
|
}
|
|
ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018/?directConnection=true").SetServerSelectionTimeout(3*time.Second))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
db := client.Database(fmt.Sprintf("source_read_boundary_%d", time.Now().UnixNano()))
|
|
t.Cleanup(func() {
|
|
c, stop := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer stop()
|
|
if err := db.Drop(c); err != nil {
|
|
t.Error(err)
|
|
}
|
|
_ = client.Disconnect(c)
|
|
})
|
|
ds := store.NewMongoStore(db)
|
|
f := newCombinedDraftFixture(t)
|
|
must := func(err error) {
|
|
t.Helper()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
project, err := f.store.GetProject(ctx, f.task.ProjectID)
|
|
must(err)
|
|
must(ds.UpsertProject(ctx, *project))
|
|
supplier, err := f.store.GetSupplier(ctx, f.annotator.SupplierID)
|
|
must(err)
|
|
must(ds.UpsertSupplier(ctx, *supplier))
|
|
template, err := f.store.GetAnnotationTemplate(ctx, f.task.TemplateID)
|
|
must(err)
|
|
must(ds.UpsertAnnotationTemplate(ctx, *template))
|
|
batch, err := f.store.GetHumanBatch(ctx, f.item.BatchID)
|
|
must(err)
|
|
must(ds.UpsertHumanBatch(ctx, *batch))
|
|
for _, user := range []domain.UserRecord{f.admin, f.annotator, f.reviewer} {
|
|
must(ds.UpsertUser(ctx, user))
|
|
}
|
|
task := f.task
|
|
task.TemplateVersion = 4
|
|
task.SourceIsFullVideo = false
|
|
task.ArtifactID = ""
|
|
_, err = db.Collection("annotationTasks").InsertOne(ctx, task)
|
|
must(err) // A persisted legacy record with no validation job.
|
|
item := f.item
|
|
item.PendingCombinedSubmissionID = "old-submit"
|
|
item.PendingCombinedPayloadSHA = "old-content"
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"saved": "reviewed contents"}
|
|
item.Subject.Payload["annotationDraft"] = map[string]any{"saved": "newer edits"}
|
|
must(ds.UpsertHumanItem(ctx, item))
|
|
before, err := ds.GetHumanItem(ctx, item.ID)
|
|
must(err)
|
|
must(ds.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: "annotation-old-submit", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1, Segments: []domain.AnnotationSegment{{ID: "saved-action", Start: 0, End: 5, Description: "preserve formal result"}}}))
|
|
f.handler = NewHandler(ds, nil)
|
|
for _, view := range []string{"?view=status", "?view=summary", "?view=history", ""} {
|
|
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+task.ID+view, f.admin, nil)
|
|
if r.Code != 200 {
|
|
t.Fatalf("view=%q code=%d body=%s", view, r.Code, r.Body.String())
|
|
}
|
|
job, err := ds.GetAnnotationIndexJob(ctx, task.ID)
|
|
must(err)
|
|
if job != nil {
|
|
t.Fatalf("GET %q queued full source validation without confirmation", view)
|
|
}
|
|
}
|
|
after, err := ds.GetHumanItem(ctx, item.ID)
|
|
must(err)
|
|
if after.CurrentStageID != "quality_review" || after.PendingCombinedSubmissionID != "" {
|
|
t.Fatal("GET did not exercise interrupted submission recovery")
|
|
}
|
|
if !reflect.DeepEqual(before.Subject, after.Subject) {
|
|
t.Fatal("recovery discarded saved workspace/draft")
|
|
}
|
|
revisions, err := ds.ListAnnotationRevisions(ctx, task.OrganizationID, task.ID)
|
|
must(err)
|
|
if len(revisions) != 1 || len(revisions[0].Segments) != 1 || revisions[0].Segments[0].Description != "preserve formal result" {
|
|
t.Fatal("formal annotation was altered")
|
|
}
|
|
// A new server instance reading the same database also must not enqueue.
|
|
f.handler = NewHandler(ds, nil)
|
|
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+task.ID, f.admin, nil)
|
|
if r.Code != 200 {
|
|
t.Fatal(r.Code, r.Body.String())
|
|
}
|
|
job, err := ds.GetAnnotationIndexJob(ctx, task.ID)
|
|
must(err)
|
|
if job != nil {
|
|
t.Fatal("restart/details read queued historical validation")
|
|
}
|
|
}
|