droboticflow-new/internal/api/slice_reset_firstpass_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

124 lines
5.2 KiB
Go

package api
import (
"net/http"
"strings"
"testing"
"droboticflow/internal/domain"
)
// Ticket 06 (spec rework-ux-adjust, D1 revised 2026-10-14): the reslice write
// endpoint is first-pass only. A task without submission history resets
// through the original write path and appends sliceResetHistory audit
// evidence; anything with submission history (rework status, a human
// annotation revision, or an advanced task revision fence) keeps the 409
// slice_reset_disabled contract from R3-2:a and is never mutated.
func TestSliceResetSplitsFirstPassFromSubmissionHistory(t *testing.T) {
scenarios := []struct {
name string
seed func(t *testing.T, f combinedDraftFixture)
wantStatus int
}{
{name: "first pass keep structure", wantStatus: http.StatusOK},
{
name: "system suggestion is not submission history",
seed: func(t *testing.T, f combinedDraftFixture) {
if err := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "suggestion-only", OrganizationID: f.task.OrganizationID, TaskID: f.task.ID, Revision: 0, AuthorUserID: "system", Segments: []domain.AnnotationSegment{}}); err != nil {
t.Fatal(err)
}
},
wantStatus: http.StatusOK,
},
{
name: "rework task status",
seed: func(t *testing.T, f combinedDraftFixture) {
task := f.task
task.Status = domain.AnnotationTaskStatusRework
if err := f.store.UpsertAnnotationTask(f.ctx, task); err != nil {
t.Fatal(err)
}
},
wantStatus: http.StatusConflict,
},
{
name: "human annotation revision",
seed: func(t *testing.T, f combinedDraftFixture) {
if err := f.store.UpsertAnnotationRevision(f.ctx, domain.AnnotationRevisionRecord{ID: "human-history", OrganizationID: f.task.OrganizationID, TaskID: f.task.ID, Revision: 1, AuthorUserID: f.annotator.ID, Segments: []domain.AnnotationSegment{}}); err != nil {
t.Fatal(err)
}
},
wantStatus: http.StatusConflict,
},
{
name: "task revision fence",
seed: func(t *testing.T, f combinedDraftFixture) {
task := f.task
task.CurrentRevision = 1
if err := f.store.UpsertAnnotationTask(f.ctx, task); err != nil {
t.Fatal(err)
}
},
wantStatus: http.StatusConflict,
},
}
for _, scenario := range scenarios {
t.Run(scenario.name, func(t *testing.T) {
f := sliceRestorationFixture(t)
body := dispositionFixturePayload(t, f)
path := "/api/plugins/annotation-qa/tasks/" + f.task.ID
if r := f.request(http.MethodPost, path+"/slice-dispositions", f.annotator, body); r.Code != http.StatusOK {
t.Fatal(r.Body.String())
}
item, _ := readSliceRestoration(t, f)
item = enterAnnotatePhase(t, f, item)
if scenario.seed != nil {
scenario.seed(t, f)
}
r := f.request(http.MethodPost, path+"/slice-reset", f.annotator, map[string]any{"version": item.Version, "resetToUnifiedRange": false})
if r.Code != scenario.wantStatus {
t.Fatalf("%s: HTTP %d %s", scenario.name, r.Code, r.Body.String())
}
stored, workspace := readSliceRestoration(t, f)
if scenario.wantStatus == http.StatusConflict {
if !strings.Contains(r.Body.String(), "slice_reset_disabled") {
t.Fatalf("%s: refusal must keep the deprecation code: %s", scenario.name, r.Body.String())
}
if stored.Version != item.Version || stored.Subject.Payload["sliceResetHistory"] != nil || stringValue(mapValue(stored.Subject.Payload["sliceWorkflow"])["phase"]) != "annotate" {
t.Fatalf("%s: refused reset mutated the item: %#v", scenario.name, stored.Subject.Payload)
}
return
}
if stored.Version != item.Version+1 {
t.Fatalf("%s: reset must advance the CAS version: %d", scenario.name, stored.Version)
}
history, _ := normalizeStructuredValue(stored.Subject.Payload["sliceResetHistory"]).([]any)
if len(history) != 1 {
t.Fatalf("%s: sliceResetHistory audit evidence missing: %#v", scenario.name, stored.Subject.Payload["sliceResetHistory"])
}
entry := mapValue(history[0])
if entry["actorUserId"] != f.annotator.ID || intValue(entry["itemVersion"]) != stored.Version || entry["resetToUnifiedRange"] != false {
t.Fatalf("%s: reset history entry lacks identity: %#v", scenario.name, entry)
}
if len(workspace.SliceSegments) != 3 || len(workspace.Segments) != 0 || len(workspace.Operations) != 0 {
t.Fatalf("%s: keep-structure reset changed the partition: %+v", scenario.name, workspace.SliceSegments)
}
for _, slice := range workspace.SliceSegments {
if len(slice.DiscardedAnnotations) != 0 {
t.Fatalf("%s: reset left an action archive live: %+v", scenario.name, slice)
}
if slice.AnnotationStatus != domain.SliceAnnotationPending {
t.Fatalf("%s: reset statuses must return to pending: %+v", scenario.name, slice)
}
}
workflow := mapValue(stored.Subject.Payload["sliceWorkflow"])
if stringValue(workflow["phase"]) != "slice" || stringValue(workflow["status"]) != "editing" || intValue(workflow["resetFromVersion"]) != item.Version {
t.Fatalf("%s: reset must return the workflow to slice editing: %#v", scenario.name, workflow)
}
if _, exists := stored.Subject.Payload["annotationDraft"]; exists {
t.Fatalf("%s: reset must drop the annotation draft: %#v", scenario.name, stored.Subject.Payload)
}
})
}
}