Preserve legacy system suggestions and unreadable saved content across workspace writers. Compare explicit dictionary snapshots and normalize empty optional evidence during save readback; keep transient autosave failures retryable. Cover recovery entry points, source shapes, failed saves and stale-snapshot rejection with isolated HTTP and browser regressions.
150 lines
7.7 KiB
Go
150 lines
7.7 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"errors"
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestInitialSliceConfirmationPreservesAutosavedActions(t *testing.T) {
|
|
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
|
|
for _, editing := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "draft", true: "editing-workspace"}[editing], func(t *testing.T) {
|
|
f, root := annotationProcessorFixture(t)
|
|
h := f.item
|
|
slices := []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 4}}
|
|
actions := []domain.AnnotationSegment{{ID: "pending-action", Start: 0, End: 1, Status: "pending"}, {ID: "described-action", Start: 1, End: 3, Status: "described", Description: "已保存的人工描述"}, {ID: "invalid-action", Start: 3, End: 4, Status: "invalid"}}
|
|
workspace := map[string]any{"sliceSegments": slices, "segments": actions, "statuses": map[string]string{"s": "pending"}, "userId": f.annotator.ID, "savedAt": nowISO(), "savedVersion": h.Version}
|
|
if editing {
|
|
h.Subject.Payload["annotationWorkspace"] = workspace
|
|
h.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "editing", "phase": "slice"}
|
|
} else {
|
|
h.Subject.Payload["annotationDraft"] = workspace
|
|
}
|
|
if e := f.store.UpsertHumanItem(f.ctx, h); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-confirmations", f.annotator, map[string]any{"version": h.Version, "sliceSegments": slices})
|
|
if r.Code != http.StatusAccepted {
|
|
t.Fatalf("first confirmation blocked by its own autosaved placeholders: %d %s", r.Code, r.Body.String())
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, h.ID)
|
|
if len(listValue(mapValue(after.ProcessingJob.Input["retryWorkspace"])["segments"])) != len(actions) {
|
|
t.Fatal("accepted first confirmation lost saved actions")
|
|
}
|
|
// A durable job must retain the same input through a claim/restart boundary.
|
|
now := time.Now().UTC()
|
|
claimed, e := f.store.ClaimAnnotationProcessing(f.ctx, "worker", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
if e != nil || claimed == nil {
|
|
t.Fatal("accepted work not durable", e)
|
|
}
|
|
processor := &server{store: f.store, workspaceMountPath: root, externalAnnotationProcessor: true}
|
|
processor.executeAnnotationJob(context.Background(), *claimed)
|
|
after, _ = f.store.GetHumanItem(f.ctx, h.ID)
|
|
if after.ProcessingJob.Status != "completed" {
|
|
t.Fatalf("generation failed: %+v", after.ProcessingJob)
|
|
}
|
|
if got := decodeMaterializationValue[domain.AnnotationSegment](annotationWorkspacePayload(after)["segments"]); !reflect.DeepEqual(got, actions) {
|
|
t.Fatalf("generation erased saved actions: %+v", got)
|
|
}
|
|
// Explicit first-pass reset keeps old slice revisions/materialization.
|
|
// Autosaving new pending actions must not strand this editing epoch.
|
|
reset := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-reset", f.annotator, map[string]any{"version": after.Version, "resetToUnifiedRange": false})
|
|
if reset.Code != 200 {
|
|
t.Fatalf("reset: %d %s", reset.Code, reset.Body)
|
|
}
|
|
after, _ = f.store.GetHumanItem(f.ctx, h.ID)
|
|
saved := f.request(http.MethodPut, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/drafts", f.annotator, map[string]any{"version": after.Version, "sliceSegments": slices, "segments": actions, "statuses": map[string]string{"s": "pending"}})
|
|
if saved.Code != 200 {
|
|
t.Fatalf("save reset: %d %s", saved.Code, saved.Body)
|
|
}
|
|
after, _ = f.store.GetHumanItem(f.ctx, h.ID)
|
|
confirmed := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-confirmations", f.annotator, map[string]any{"version": after.Version, "sliceSegments": slices})
|
|
if confirmed.Code != 202 {
|
|
t.Fatalf("reset confirmation stranded by retained history: %d %s", confirmed.Code, confirmed.Body)
|
|
}
|
|
claimed, e = f.store.ClaimAnnotationProcessing(f.ctx, "reset-worker", now.Format(time.RFC3339Nano), now.Add(time.Minute).Format(time.RFC3339Nano))
|
|
if e != nil || claimed == nil {
|
|
t.Fatal("reset job unavailable", e)
|
|
}
|
|
processor.executeAnnotationJob(context.Background(), *claimed)
|
|
after, _ = f.store.GetHumanItem(f.ctx, h.ID)
|
|
if after.ProcessingJob.Status != "completed" || !reflect.DeepEqual(decodeMaterializationValue[domain.AnnotationSegment](annotationWorkspacePayload(after)["segments"]), actions) {
|
|
t.Fatalf("reset completion lost saved input: %+v", after.ProcessingJob)
|
|
}
|
|
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestInitialSliceConfirmationRejectsUnsavedOrForeignSnapshot(t *testing.T) {
|
|
t.Setenv("ANNOTATION_PROCESSOR_MODE", "external")
|
|
for _, mode := range []string{"unsaved-slices", "other-owner", "formal-history", "human-zero", "slice-human-zero", "failed-submit"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
f, _ := annotationProcessorFixture(t)
|
|
h := f.item
|
|
slices := []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 4}}
|
|
workspace := map[string]any{"sliceSegments": slices, "segments": []domain.AnnotationSegment{{ID: "saved", Start: 0, End: 4, Status: "pending"}}, "userId": f.annotator.ID, "savedAt": nowISO(), "savedVersion": h.Version}
|
|
h.Subject.Payload["annotationDraft"] = workspace
|
|
if mode == "other-owner" {
|
|
workspace["userId"] = "someone-else"
|
|
}
|
|
if mode == "formal-history" {
|
|
if err := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "formal", OrganizationID: h.OrganizationID, TaskID: f.task.ID, Revision: 1}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if mode == "human-zero" {
|
|
if err := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "human-zero", OrganizationID: h.OrganizationID, TaskID: f.task.ID, Revision: 0, AuthorUserID: f.annotator.ID}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if mode == "slice-human-zero" {
|
|
if err := f.store.UpsertSliceRevision(f.ctx, domain.SliceRevisionRecord{ID: "human-zero", OrganizationID: h.OrganizationID, TaskID: f.task.ID, Revision: 0, AuthorUserID: f.annotator.ID}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if mode == "failed-submit" {
|
|
h.ProcessingJob = &domain.AnnotationProcessingJob{ID: "failed-submit", Kind: "submit", Status: "failed", Input: map[string]any{"retained": true}}
|
|
}
|
|
if err := f.store.UpsertHumanItem(f.ctx, h); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, _ := f.store.GetHumanItem(f.ctx, h.ID)
|
|
if mode == "unsaved-slices" {
|
|
slices = []domain.SliceSegment{{ID: "changed", Name: "测试切片", Start: 0, End: 4}}
|
|
}
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-confirmations", f.annotator, map[string]any{"version": h.Version, "sliceSegments": slices})
|
|
after, _ := f.store.GetHumanItem(f.ctx, h.ID)
|
|
if r.Code != 409 || !reflect.DeepEqual(before, after) {
|
|
t.Fatalf("unsafe initial confirmation: %d %s", r.Code, r.Body)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type initialSliceReadFailure struct{ store.Store }
|
|
|
|
func (s initialSliceReadFailure) ListSliceRevisions(context.Context, string, string) ([]domain.SliceRevisionRecord, error) {
|
|
return nil, errors.New("synthetic read unavailable")
|
|
}
|
|
func TestInitialSliceConfirmationDoesNotMislabelReadFailure(t *testing.T) {
|
|
f, _ := annotationProcessorFixture(t)
|
|
h := f.item
|
|
h.Subject.Payload["annotationWorkspace"] = map[string]any{"segments": []domain.AnnotationSegment{{ID: "saved", Start: 0, End: 4, Status: "pending"}}}
|
|
if e := f.store.UpsertHumanItem(f.ctx, h); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
f.handler = NewHandler(initialSliceReadFailure{f.store}, nil)
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-confirmations", f.annotator, map[string]any{"version": h.Version})
|
|
if r.Code != 500 || strings.Contains(r.Body.String(), "snapshot_conflict") {
|
|
t.Fatalf("read failure mislabeled: %d %s", r.Code, r.Body)
|
|
}
|
|
}
|