forked from longtao.wu/droboticflow
496 lines
22 KiB
Go
496 lines
22 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/config"
|
|
"droboticflow/internal/domain"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestSourceValidationFailureBlocksRandomClaimWithoutLosingDraft(t *testing.T) {
|
|
f := randomClaimFixture(t, 1, false)
|
|
task, err := f.store.GetAnnotationTask(f.ctx, "random-task-0")
|
|
if err != nil || task == nil {
|
|
t.Fatal(err)
|
|
}
|
|
task.SourceIsFullVideo, task.TemplateVersion, task.ArtifactID = true, 5, "source-left"
|
|
if err := f.store.UpsertAnnotationTask(f.ctx, *task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item, _ := f.store.GetHumanItem(f.ctx, "random-item-0")
|
|
item.Subject.Payload = map[string]any{"draft": "keep existing annotation"}
|
|
if err := f.store.UpsertHumanItem(f.ctx, *item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now().UTC()
|
|
job, err := f.store.ClaimAnnotationIndex(f.ctx, "test-validator", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
if err != nil || job == nil {
|
|
t.Fatal("missing validation job", err)
|
|
}
|
|
job.Status, job.Error, job.Attempt = "failed", "IMU row 418451 invalid type gyr", 5
|
|
if ok, err := f.store.FinishAnnotationIndex(f.ctx, *job); err != nil || !ok {
|
|
t.Fatal(err)
|
|
}
|
|
got := claimRandom(t, f, "invalid-source-000001")
|
|
if got.ID != "" {
|
|
t.Fatalf("failed source validation was still assigned: %s", got.ID)
|
|
}
|
|
current, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
if current.AssignedUserID != "" || current.Version != item.Version || current.Subject.Payload["draft"] != "keep existing annotation" {
|
|
t.Fatalf("quarantine altered existing content/ownership: %+v", current)
|
|
}
|
|
count := f.request(http.MethodGet, "/api/plugins/annotation-qa/claim-count?projectId="+task.ProjectID, f.annotator, nil)
|
|
var payload struct {
|
|
Count int `json:"count"`
|
|
}
|
|
if count.Code != 200 || json.Unmarshal(count.Body.Bytes(), &payload) != nil || payload.Count != 0 {
|
|
t.Fatalf("invalid source counted as available: %d %s", count.Code, count.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestPendingSourceValidationIsNotAssigned(t *testing.T) {
|
|
f := randomClaimFixture(t, 1, false)
|
|
task, _ := f.store.GetAnnotationTask(f.ctx, "random-task-0")
|
|
task.SourceIsFullVideo, task.TemplateVersion, task.ArtifactID = true, 5, "source-left"
|
|
if err := f.store.UpsertAnnotationTask(f.ctx, *task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if item := claimRandom(t, f, "pending-source-00001"); item.ID != "" {
|
|
t.Fatalf("pending source was assigned: %s", item.ID)
|
|
}
|
|
}
|
|
|
|
func TestSourceValidationRetryRequiresAdminAndResumesOnlyAfterPass(t *testing.T) {
|
|
f := randomClaimFixture(t, 1, false)
|
|
task, _ := f.store.GetAnnotationTask(f.ctx, "random-task-0")
|
|
task.SourceIsFullVideo, task.TemplateVersion, task.ArtifactID = true, 5, "source-left"
|
|
if e := f.store.UpsertAnnotationTask(f.ctx, *task); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
now := time.Now().UTC()
|
|
job, e := f.store.ClaimAnnotationIndex(f.ctx, "validation-owner", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
if e != nil || job == nil {
|
|
t.Fatal(e)
|
|
}
|
|
job.Status, job.Attempt, job.Error = "failed", 5, `IMU CSV imu.csv invalid type "gyr" at row 418451`
|
|
if ok, e := f.store.FinishAnnotationIndex(f.ctx, *job); !ok || e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
route := "/api/plugins/annotation-qa/tasks/" + task.ID + "/source-validation-retries"
|
|
for _, user := range []domain.UserRecord{f.annotator, f.reviewer} {
|
|
r := f.request(http.MethodPost, route, user, map[string]any{"sourceRepaired": true})
|
|
if r.Code != 403 {
|
|
t.Fatalf("non-admin revalidation %d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
denied := f.request(http.MethodPost, route, f.admin, map[string]any{})
|
|
if denied.Code != 422 {
|
|
t.Fatal(denied.Code, denied.Body.String())
|
|
}
|
|
response := f.request(http.MethodPost, route, f.admin, map[string]any{"sourceRepaired": true})
|
|
if response.Code != 202 {
|
|
t.Fatal(response.Code, response.Body.String())
|
|
}
|
|
if got := claimRandom(t, f, "revalidating-source-1"); got.ID != "" {
|
|
t.Fatal("revalidation admitted before success")
|
|
}
|
|
// An old validator completion must not supersede a reset job.
|
|
stale := *job
|
|
stale.Status = "completed"
|
|
if ok, e := f.store.FinishAnnotationIndex(f.ctx, stale); ok || e != nil {
|
|
t.Fatalf("old validator survived reset: %v %v", ok, e)
|
|
}
|
|
replacement, e := f.store.ClaimAnnotationIndex(f.ctx, "replacement-validator", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
if e != nil || replacement == nil {
|
|
t.Fatal(e)
|
|
}
|
|
replacement.Status, replacement.Error, replacement.SourceFingerprint = "completed", "", "validated-source"
|
|
if ok, e := f.store.FinishAnnotationIndex(f.ctx, *replacement); !ok || e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if got := claimRandom(t, f, "revalidated-source-01"); got.ID != "random-item-0" {
|
|
t.Fatalf("validated task not resumed: %s", got.ID)
|
|
}
|
|
}
|
|
|
|
func TestSourceValidationStatusIsSafeSpecificAndPreservesOwnedTask(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
task := f.task
|
|
task.SourceIsFullVideo, task.ArtifactID = true, "source-left"
|
|
f.store.UpsertAnnotationTask(f.ctx, task)
|
|
if err := f.store.RetryAnnotationSourceValidation(f.ctx, task, true, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now().UTC()
|
|
job, _ := f.store.ClaimAnnotationIndex(f.ctx, "validator", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
job.Status, job.Error = "failed", `IMU CSV /private/credential-root/imu.csv invalid type "gyr" at row 418451`
|
|
f.store.FinishAnnotationIndex(f.ctx, *job)
|
|
for _, path := range []string{"?view=status", "/frame-timeline"} {
|
|
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+task.ID+path, f.annotator, nil)
|
|
expected := 200
|
|
if path == "/frame-timeline" {
|
|
expected = 422
|
|
}
|
|
if r.Code != expected || !strings.Contains(r.Body.String(), "418451") || !strings.Contains(r.Body.String(), "gyr") || strings.Contains(r.Body.String(), "credential-root") {
|
|
t.Fatalf("status not safe/specific: %d %s", r.Code, r.Body.String())
|
|
}
|
|
if !strings.Contains(r.Body.String(), `"retryAllowed":false`) {
|
|
t.Fatal("worker received admin retry permission")
|
|
}
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if after.Version != f.item.Version || after.AssignedUserID != f.annotator.ID || after.Subject.Payload["preserved"] != true {
|
|
t.Fatal("diagnostic read changed owned work")
|
|
}
|
|
}
|
|
|
|
func TestCurrentPreparedSourceFailureBlocksClaimAfterTimelinePass(t *testing.T) {
|
|
f := randomClaimFixture(t, 1, false)
|
|
task, _ := f.store.GetAnnotationTask(f.ctx, "random-task-0")
|
|
f.store.EnqueueAnnotationIndex(f.ctx, *task)
|
|
now := time.Now().UTC()
|
|
job, _ := f.store.ClaimAnnotationIndex(f.ctx, "timeline", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
job.Status, job.SourceFingerprint = "completed", "validated-source"
|
|
f.store.FinishAnnotationIndex(f.ctx, *job)
|
|
f.store.EnqueueAnnotationSourceIndex(f.ctx, *task, "current-source", false)
|
|
job, _ = f.store.ClaimAnnotationIndex(f.ctx, "source", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
job.Status, job.Error = "failed", "IMU row 8 invalid type gyr"
|
|
f.store.FinishAnnotationIndex(f.ctx, *job)
|
|
item, _ := f.store.GetHumanItem(f.ctx, "random-item-0")
|
|
item.ProcessingJob = &domain.AnnotationProcessingJob{SourceFingerprint: "current-source", Status: "failed"}
|
|
f.store.UpsertHumanItem(f.ctx, *item)
|
|
if got := claimRandom(t, f, "prepared-failed-0001"); got.ID != "" {
|
|
t.Fatalf("source certificate failure ignored: %s", got.ID)
|
|
}
|
|
// An unrelated old fingerprint must not quarantine the current source.
|
|
item.ProcessingJob.SourceFingerprint = "repaired-source"
|
|
f.store.UpsertHumanItem(f.ctx, *item)
|
|
f.store.EnqueueAnnotationSourceIndex(f.ctx, *task, "repaired-source", false)
|
|
job, _ = f.store.ClaimAnnotationIndex(f.ctx, "repaired", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
job.Status, job.SourceFingerprint = "completed", "repaired-source"
|
|
f.store.FinishAnnotationIndex(f.ctx, *job)
|
|
if got := claimRandom(t, f, "prepared-passed-0001"); got.ID != "random-item-0" {
|
|
t.Fatal("stale source failure poisoned repaired task")
|
|
}
|
|
}
|
|
|
|
func TestSourceValidationLegacyWithoutJobCannotBeClaimed(t *testing.T) {
|
|
f := randomClaimFixture(t, 1, false)
|
|
if got := claimRandom(t, f, "legacy-source-000001"); got.ID != "" {
|
|
t.Fatal("legacy task without complete proof was admitted")
|
|
}
|
|
}
|
|
|
|
func TestSourceRepairRevalidatesChangedFilesWithoutReusingFailedCertificateID(t *testing.T) {
|
|
t.Setenv("ANNOTATION_SOURCE_INDEX", "true")
|
|
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
|
|
t.Setenv("ANNOTATION_PROCESSOR_ROLE", "validation")
|
|
f, root := annotationProcessorFixture(t)
|
|
path := filepath.Join(root, "imu.csv")
|
|
original, e := os.ReadFile(path)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
broken := strings.TrimSuffix(string(original), "gyro\n") + "gyr\n"
|
|
if e = os.WriteFile(path, []byte(broken), 0600); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
engine := &server{store: f.store, workspaceMountPath: root, annotationSourceIndex: true}
|
|
run, _ := f.store.GetRun(f.ctx, f.task.RunID)
|
|
failedFingerprint, e := engine.annotationSourceFingerprint(f.ctx, f.task, *run)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = engine.annotationFrameTimelineForTask(f.ctx, f.task, "left"); e == nil || !strings.Contains(e.Error(), "gyr") {
|
|
t.Fatalf("bad source was not actually rejected: %v", e)
|
|
}
|
|
now := time.Now().UTC()
|
|
stamp := now.Format(time.RFC3339Nano)
|
|
until := now.Add(time.Minute).Format(time.RFC3339Nano)
|
|
base, _ := f.store.ClaimAnnotationIndex(f.ctx, "broken-timeline", stamp, until)
|
|
base.Status, base.Error = "failed", e.Error()
|
|
f.store.FinishAnnotationIndex(f.ctx, *base)
|
|
f.store.EnqueueAnnotationSourceIndex(f.ctx, f.task, failedFingerprint, false)
|
|
source, _ := f.store.ClaimAnnotationIndex(f.ctx, "broken-source", stamp, until)
|
|
source.Status, source.Error = "failed", e.Error()
|
|
f.store.FinishAnnotationIndex(f.ctx, *source)
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
item.ProcessingJob = &domain.AnnotationProcessingJob{ID: "preserved-failed-slice", Kind: "slice", TaskID: f.task.ID, Status: "failed", SourceFingerprint: failedFingerprint, SourceIndex: true, Input: map[string]any{"draft": "saved annotation"}}
|
|
f.store.UpsertHumanItem(f.ctx, *item)
|
|
if e = os.WriteFile(path, original, 0600); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
repairedFingerprint, e := engine.annotationSourceFingerprint(f.ctx, f.task, *run)
|
|
if e != nil || repairedFingerprint == failedFingerprint {
|
|
t.Fatal("repair did not change source identity", e)
|
|
}
|
|
retry := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/source-validation-retries", f.admin, map[string]any{"sourceRepaired": true})
|
|
if retry.Code != 202 {
|
|
t.Fatal(retry.Code, retry.Body.String())
|
|
}
|
|
ctx, cancel := context.WithCancel(f.ctx)
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: root}, 1, false)
|
|
}()
|
|
defer func() {
|
|
cancel()
|
|
if done != nil {
|
|
<-done
|
|
}
|
|
}()
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
job, _ := f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
|
|
if job.Status == "completed" {
|
|
if !job.RevalidateSource || job.SourceFingerprint != repairedFingerprint {
|
|
t.Fatalf("completion lacks current source proof: %+v", job)
|
|
}
|
|
previous, _ := f.store.GetAnnotationIndexJob(f.ctx, source.ID)
|
|
if previous.Status != "failed" {
|
|
t.Fatal("old failed certificate job was relabeled successful")
|
|
}
|
|
current, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if current.Version != item.Version || current.ProcessingJob.Input["draft"] != "saved annotation" {
|
|
t.Fatal("repair overwrote saved work")
|
|
}
|
|
state, e := engine.annotationSourceValidationForTask(f.ctx, f.task, current, true)
|
|
if e != nil || state.Status != "valid" {
|
|
t.Fatalf("repaired admission state=%+v err=%v", state, e)
|
|
}
|
|
cert, _ := f.store.GetAnnotationTimelineIndex(f.ctx, sourceCertificateID(f.task.OrganizationID, repairedFingerprint))
|
|
if cert == nil {
|
|
t.Fatal("current source certificate missing")
|
|
}
|
|
cancel()
|
|
<-done
|
|
done = nil
|
|
// A later source version cannot inherit the earlier repair's exemption.
|
|
current.ProcessingJob.SourceFingerprint = "later-failed-source"
|
|
f.store.UpsertHumanItem(f.ctx, *current)
|
|
f.store.EnqueueAnnotationSourceIndex(f.ctx, f.task, "later-failed-source", false)
|
|
later, _ := f.store.ClaimAnnotationIndex(f.ctx, "later-validator", stamp, until)
|
|
later.Status, later.Error = "failed", "IMU row 9 invalid type gyr"
|
|
f.store.FinishAnnotationIndex(f.ctx, *later)
|
|
state, e = engine.annotationSourceValidationForTask(f.ctx, f.task, current, true)
|
|
if e != nil || state.Status != "failed" {
|
|
t.Fatalf("earlier repair bypassed later failure: %+v %v", state, e)
|
|
}
|
|
return
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
t.Fatal("repaired source did not complete real validation")
|
|
}
|
|
|
|
func TestRequiredSourceCertificateCannotBeMissing(t *testing.T) {
|
|
f := randomClaimFixture(t, 1, false)
|
|
task, _ := f.store.GetAnnotationTask(f.ctx, "random-task-0")
|
|
f.store.EnqueueAnnotationIndex(f.ctx, *task)
|
|
now := time.Now().UTC()
|
|
job, _ := f.store.ClaimAnnotationIndex(f.ctx, "timeline", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
job.Status, job.SourceFingerprint = "completed", "validated-source"
|
|
f.store.FinishAnnotationIndex(f.ctx, *job)
|
|
item, _ := f.store.GetHumanItem(f.ctx, "random-item-0")
|
|
item.ProcessingJob = &domain.AnnotationProcessingJob{SourceFingerprint: "not-enqueued-yet", SourceIndex: true}
|
|
f.store.UpsertHumanItem(f.ctx, *item)
|
|
if got := claimRandom(t, f, "missing-source-00001"); got.ID != "" {
|
|
t.Fatal("required missing source certificate admitted")
|
|
}
|
|
item.ProcessingJob.SourceIndex = false
|
|
f.store.UpsertHumanItem(f.ctx, *item)
|
|
if got := claimRandom(t, f, "legacy-physical-001"); got.ID != "" {
|
|
t.Fatal("legacy physical outbox borrowed another source version's proof")
|
|
}
|
|
item.ProcessingJob.SourceFingerprint = "validated-source"
|
|
if err := f.store.UpsertHumanItem(f.ctx, *item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := claimRandom(t, f, "legacy-physical-002"); got.ID != "random-item-0" {
|
|
t.Fatal("matching complete task proof did not admit physical outbox")
|
|
}
|
|
}
|
|
|
|
func TestSourceFailureDetectedAfterPriorSuccessRevokesAdmission(t *testing.T) {
|
|
f, root := annotationProcessorFixture(t)
|
|
now := time.Now().UTC()
|
|
job, _ := f.store.ClaimAnnotationIndex(f.ctx, "old-valid", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
job.Status, job.SourceFingerprint = "completed", "validated-source"
|
|
f.store.FinishAnnotationIndex(f.ctx, *job)
|
|
path := filepath.Join(root, "imu.csv")
|
|
b, _ := os.ReadFile(path)
|
|
if e := os.WriteFile(path, []byte(strings.TrimSuffix(string(b), "gyro\n")+"gyr\n"), 0600); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
response := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.admin, nil)
|
|
if response.Code != 422 || !strings.Contains(response.Body.String(), `"retryAllowed":true`) {
|
|
t.Fatal(response.Code, response.Body.String())
|
|
}
|
|
persisted, _ := f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
|
|
if persisted.Status != "failed" {
|
|
t.Fatal("new source failure did not revoke admission")
|
|
}
|
|
response = f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/source-validation-retries", f.admin, map[string]any{"sourceRepaired": true})
|
|
if response.Code != 202 {
|
|
t.Fatalf("advertised recovery was unavailable: %d %s", response.Code, response.Body.String())
|
|
}
|
|
if ok, e := f.store.FailAnnotationSourceValidation(f.ctx, f.task, job.CreatedAt, "stale HTTP validation failure"); ok || e != nil {
|
|
t.Fatal("old read error overrode active revalidation", ok, e)
|
|
}
|
|
}
|
|
|
|
func TestSourceValidationCanceledTimelineDoesNotQuarantineSource(t *testing.T) {
|
|
f, _ := annotationProcessorFixture(t)
|
|
now := time.Now().UTC()
|
|
job, err := f.store.ClaimAnnotationIndex(f.ctx, "valid-before-cancel", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
if err != nil || job == nil {
|
|
t.Fatal("validation job missing", err)
|
|
}
|
|
job.Status, job.SourceFingerprint = "completed", "validated-source"
|
|
if ok, err := f.store.FinishAnnotationIndex(f.ctx, *job); !ok || err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
ctx = context.WithValue(ctx, authContextKey{}, &authenticatedPrincipal{User: f.admin, Organization: domain.OrganizationRecord{ID: f.task.OrganizationID, Status: domain.OrganizationStatusActive}})
|
|
request := httptest.NewRequest(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", nil).WithContext(ctx)
|
|
response := httptest.NewRecorder()
|
|
f.handler.ServeHTTP(response, request)
|
|
current, err := f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
|
|
if err != nil || current.Status != "completed" {
|
|
t.Fatalf("canceled read quarantined source: status=%s err=%v response=%s", current.Status, err, response.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSourceValidationRejectsNonFiniteIMUSamples(t *testing.T) {
|
|
for _, value := range []string{"NaN", "+Inf", "not-a-number"} {
|
|
t.Run(value, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "imu.csv")
|
|
body := "timestamp_us,x,y,z,type\n1000000,0,0,0,accel\n1000000," + value + ",0,0,gyro\n"
|
|
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := scanHardwareIMU(context.Background(), path, nil)
|
|
if err == nil || safeSourceValidationMessage(err.Error()) != "IMU 第 3 行数值不合法" {
|
|
t.Fatalf("invalid numeric sample was not identified: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSourceValidationUnverifiedLegacySourceCanRecoverWithoutLosingWork(t *testing.T) {
|
|
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
|
|
t.Setenv("ANNOTATION_PROCESSOR_ROLE", "validation")
|
|
f, root := annotationProcessorFixture(t)
|
|
completeFixtureSourceValidation(t, f, f.task)
|
|
f.task.ID, f.task.TemplateVersion = "legacy-source-task", 4
|
|
f.item.ID, f.item.LegacyTaskID = "legacy-source-item", f.task.ID
|
|
f.item.Subject.ItemKey = "legacy-source"
|
|
f.item.Subject.Payload["draft"] = "legacy saved work"
|
|
if err := f.store.UpsertAnnotationTask(f.ctx, f.task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now().UTC()
|
|
weak, err := f.store.ClaimAnnotationIndex(f.ctx, "legacy-timeline", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
if err != nil || weak == nil || weak.ID != f.task.ID {
|
|
t.Fatal("missing legacy fixture", err)
|
|
}
|
|
weak.Status, weak.RevalidateSource, weak.SourceFingerprint = "completed", false, ""
|
|
if ok, err := f.store.FinishAnnotationIndex(f.ctx, *weak); !ok || err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
read := func(ctx context.Context, user domain.UserRecord, org string) *httptest.ResponseRecorder {
|
|
ctx = context.WithValue(ctx, authContextKey{}, &authenticatedPrincipal{User: user, Organization: domain.OrganizationRecord{ID: org, Status: domain.OrganizationStatusActive}})
|
|
request := httptest.NewRequest(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", nil).WithContext(ctx)
|
|
response := httptest.NewRecorder()
|
|
f.handler.ServeHTTP(response, request)
|
|
return response
|
|
}
|
|
canceled, cancelRead := context.WithCancel(f.ctx)
|
|
cancelRead()
|
|
if response := read(canceled, f.admin, f.task.OrganizationID); response.Code != 422 {
|
|
t.Fatalf("canceled legacy read: %d %s", response.Code, response.Body.String())
|
|
}
|
|
foreign := f.admin
|
|
foreign.OrganizationID = "other-source-org"
|
|
if response := read(f.ctx, foreign, foreign.OrganizationID); response.Code != 403 && response.Code != 404 {
|
|
t.Fatalf("cross-organization legacy read allowed: %d", response.Code)
|
|
}
|
|
if job, err := f.store.GetAnnotationIndexJob(f.ctx, f.task.ID); err != nil || job == nil || job.Status != "completed" || job.SourceFingerprint != "" {
|
|
t.Fatal("canceled/unauthorized read changed legacy source", err)
|
|
}
|
|
path := filepath.Join(root, "imu.csv")
|
|
original, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(strings.TrimSuffix(string(original), "gyro\n")+"gyr\n"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
response := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.admin, nil)
|
|
if response.Code != 422 || !strings.Contains(response.Body.String(), `"retryAllowed":true`) || !strings.Contains(response.Body.String(), "source_validation_unverified") {
|
|
t.Fatalf("known bad legacy source has no recovery: %d %s", response.Code, response.Body.String())
|
|
}
|
|
job, err := f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
|
|
if err != nil || job == nil || job.Status != "completed" || job.SourceFingerprint != "" {
|
|
t.Fatalf("unverified legacy read mutated validation state: %+v %v", job, err)
|
|
}
|
|
response = f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/source-validation-retries", f.annotator, map[string]any{"sourceRepaired": true})
|
|
if response.Code != 403 {
|
|
t.Fatalf("legacy retry bypassed admin role: %d", response.Code)
|
|
}
|
|
if err := os.WriteFile(path, original, 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
response = f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/source-validation-retries", f.admin, map[string]any{"sourceRepaired": true})
|
|
if response.Code != 202 {
|
|
t.Fatal(response.Code, response.Body.String())
|
|
}
|
|
ctx, cancel := context.WithCancel(f.ctx)
|
|
done := make(chan error, 1)
|
|
go func() {
|
|
done <- RunAnnotationProcessor(ctx, f.store, config.AppConfig{WorkspaceMountPath: root}, 1, false)
|
|
}()
|
|
defer func() { cancel(); <-done }()
|
|
deadline := time.Now().Add(10 * time.Second)
|
|
for {
|
|
job, err = f.store.GetAnnotationIndexJob(f.ctx, f.task.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if job.Status == "completed" {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("legacy recovery did not finish: %+v", job)
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
response = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.annotator, nil)
|
|
if response.Code != 200 {
|
|
t.Fatal(response.Code, response.Body.String())
|
|
}
|
|
item, err := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if err != nil || item.Version != f.item.Version || item.AssignedUserID != f.item.AssignedUserID || item.Subject.Payload["draft"] != "legacy saved work" {
|
|
t.Fatal("legacy recovery changed ownership/draft", err)
|
|
}
|
|
item.Subject.Payload["materialization"] = map[string]any{"sourceIndex": true}
|
|
if err := f.store.UpsertHumanItem(f.ctx, *item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
response = f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/frame-timeline", f.annotator, nil)
|
|
if response.Code != 200 {
|
|
t.Fatalf("complete current proof was not reusable: %d %s", response.Code, response.Body.String())
|
|
}
|
|
}
|