droboticflow/internal/api/slice_recovery_test.go
d-robotics 8950746fca ✨ 06: restore first-pass reslice behind a submission-history gate
- server: handleSliceResetCreate splits on submission history using the
  established isRework signals (task revision fence, rework status, human
  annotation revision via fanyuV5CombinedSubmissionIsRework); first pass
  runs the original reset write path with sliceResetHistory audit, history
  keeps 409 slice_reset_disabled with in-place guidance (spec D1 revised)
- table tests pin both classes incl. the system-suggestion edge; the four
  retired-contract test files flip to split-flow assertions
- frontend: restore the reslice button, warning dialog with both reset
  modes, and authoritative reconciliation from f980696f~1; entry is gated
  client-side by the same signals (resliceEligible), normal and focused
  layouts share the restored footer entry; E-mode changes untouched
- dev preview regains the entry; two retirement-era nail assertions flip
  back with the gate asserted in their place
2026-10-14 21:55:29 +08:00

125 lines
5.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package api
import (
"context"
"errors"
"net/http"
"strings"
"testing"
"time"
"droboticflow/internal/domain"
)
func TestSliceProcessCancellationRetainsCause(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
err := writeFrameAccurateSlice(ctx, "source.mp4", "unused.mp4", 0, 1)
if !errors.Is(err, context.Canceled) {
t.Fatalf("lost process termination cause: %v", err)
}
}
func TestSliceRecoveryPreservesWorkspaceAndClaim(t *testing.T) {
for _, legacy := range []bool{false, true} {
f := newCombinedDraftFixture(t)
w := map[string]any{"status": "processing", "requestId": "dead", "revision": 4, "startedAt": time.Now().Add(-time.Hour).Format(time.RFC3339Nano)}
if !legacy {
w["leaseExpiresAt"] = time.Now().Add(-time.Minute).Format(time.RFC3339Nano)
}
f.item.Subject.Payload["sliceWorkflow"] = w
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": []domain.SliceSegment{{ID: "saved", Start: 0, End: 10}}}
f.store.UpsertHumanItem(f.ctx, f.item)
response := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID, f.annotator, nil)
if response.Code != 200 {
t.Fatalf("get: %d %s", response.Code, response.Body.String())
}
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if stringValue(mapValue(item.Subject.Payload["sliceWorkflow"])["status"]) != "failed" || item.AssignedUserID != f.annotator.ID || item.Subject.Payload["annotationWorkspace"] == nil {
t.Fatalf("lost recoverable state: %#v", item)
}
}
}
func TestSliceLiveLeaseIsNotRecoveredOrDuplicated(t *testing.T) {
f := newCombinedDraftFixture(t)
f.item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "processing", "requestId": "live", "revision": 1, "startedAt": time.Now().Add(-time.Hour).Format(time.RFC3339Nano), "leaseExpiresAt": time.Now().Add(time.Minute).Format(time.RFC3339Nano)}
f.store.UpsertHumanItem(f.ctx, f.item)
s := &server{store: f.store}
if err := s.recoverInterruptedSlice(f.ctx, &f.item, time.Now()); err != nil {
t.Fatal(err)
}
if stringValue(mapValue(f.item.Subject.Payload["sliceWorkflow"])["status"]) != "processing" {
t.Fatal("live work expired")
}
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-confirmations", f.annotator, map[string]any{"version": f.item.Version, "sliceSegments": []domain.SliceSegment{{ID: "s", Start: 0, End: 10}}})
if r.Code != http.StatusConflict {
t.Fatalf("duplicate: %d %s", r.Code, r.Body.String())
}
}
func TestSliceStaleWorkerCannotFinalizeNewRequest(t *testing.T) {
f := newCombinedDraftFixture(t)
f.item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "editing", "requestId": "new", "revision": 5}
f.store.UpsertHumanItem(f.ctx, f.item)
s := &server{store: f.store}
s.finishSliceConfirmation(f.ctx, f.task.ID, f.item.ID, "old", sliceConfirmationInput{}, 4, f.annotator.ID)
i, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if i.Version != f.item.Version || stringValue(mapValue(i.Subject.Payload["sliceWorkflow"])["requestId"]) != "new" {
t.Fatal("stale completion overwrote retry")
}
}
func TestSliceResetSplitsOnSubmissionHistoryForRevisionFence(t *testing.T) {
// Ticket 06:首标任务放行重切并保留 revision fence;有送检历史才 409。
f := newCombinedDraftFixture(t)
f.item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "failed", "revision": 7}
f.store.UpsertHumanItem(f.ctx, f.item)
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-reset", f.annotator, map[string]any{"version": f.item.Version})
if r.Code != http.StatusOK {
t.Fatalf("first-pass reset: %d %s", r.Code, r.Body.String())
}
i, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
if intValue(mapValue(i.Subject.Payload["sliceWorkflow"])["revision"]) != 7 {
t.Fatal("reset lost revision fence")
}
reworkTask := f.task
reworkTask.Status = domain.AnnotationTaskStatusRework
if err := f.store.UpsertAnnotationTask(f.ctx, reworkTask); err != nil {
t.Fatal(err)
}
i, _ = f.store.GetHumanItem(f.ctx, f.item.ID)
r = f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-reset", f.annotator, map[string]any{"version": i.Version})
if r.Code != http.StatusConflict || !strings.Contains(r.Body.String(), "slice_reset_disabled") {
t.Fatalf("reset must be disabled after submission history: %d %s", r.Code, r.Body.String())
}
i, _ = f.store.GetHumanItem(f.ctx, f.item.ID)
if intValue(mapValue(i.Subject.Payload["sliceWorkflow"])["revision"]) != 7 || i.Status != domain.HumanItemClaimed {
t.Fatal("disabled reset changed the revision fence or item state")
}
}
func TestSliceRecoveryLeavesDurableQueueInCharge(t *testing.T) {
for _, state := range []string{"queued", "running"} {
t.Run(state, func(t *testing.T) {
f := newCombinedDraftFixture(t)
f.item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "processing", "requestId": "durable", "leaseExpiresAt": time.Now().Add(-time.Hour).Format(time.RFC3339Nano)}
f.item.ProcessingJob = &domain.AnnotationProcessingJob{ID: "durable", Kind: "slice", Status: state, LeaseUntil: time.Now().Add(-time.Minute).Format(time.RFC3339Nano)}
if e := f.store.UpsertHumanItem(f.ctx, f.item); e != nil {
t.Fatal(e)
}
s := &server{store: f.store}
if e := s.recoverInterruptedSlice(f.ctx, &f.item, time.Now()); e != nil {
t.Fatal(e)
}
got, e := f.store.GetHumanItem(f.ctx, f.item.ID)
if e != nil {
t.Fatal(e)
}
if got.Version != f.item.Version || stringValue(mapValue(got.Subject.Payload["sliceWorkflow"])["status"]) != "processing" {
t.Fatal("API expired a durable job instead of leaving it queued for processor recovery")
}
})
}
}