droboticflow/internal/api/review_draft_anchor_test.go
d-robotics 74ea7f8274 🛡️ 03: deprecate task-scope review comments (ADR 0009)
Server: reject NEW task-scope problems on draft PUT (409
review_task_scope_deprecated, ID-matched against stored entries;
existing entries round-trip unchanged, full-replace semantics kept);
formal-rejection flattening no longer emits 整体任务 lines or
expands ProblemSliceIDs; rework-baseline reads hide task problems
(read-only narrowing); workforce stats stop counting task scope.

Frontend: remove task from composer scope lists and card cycle;
hide stored task opinions in rework banner/soft reminder/rail,
QC panel groups and history, acceptance source projection, stats
panel rows, frozen-comment displays (stripTaskOpinionLines);
submit/approve gates count visible problems only; stats panel
documents the 2026-10-14 scope switch.

Data untouched: no migration, no deletion, audit trail intact.
2026-10-14 19:02:29 +08:00

202 lines
10 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 (
"droboticflow/internal/domain"
"math"
"net/http"
"strings"
"testing"
)
func anchoredTaskProblem(id, text string, anchor map[string]any) map[string]any {
return map[string]any{"id": id, "scope": "task", "text": text, "viewAnchor": anchor}
}
func plainTaskProblem(id, text string) map[string]any {
return map[string]any{"id": id, "scope": "task", "text": text}
}
// anchorFromTestMap 容缺地把测试输入里的 viewAnchor 映射转成域结构(缺省字段为零值)。
func anchorFromTestMap(raw map[string]any) *domain.ReviewViewAnchor {
source, _ := raw["sourceItemId"].(string)
slice, _ := raw["sliceId"].(string)
basis, _ := raw["timeBasis"].(string)
at, _ := raw["atSeconds"].(float64)
return &domain.ReviewViewAnchor{SourceItemID: source, SliceID: slice, AtSeconds: at, TimeBasis: basis}
}
func assertGlobalTaskTarget(t *testing.T, p domain.ReviewProblem) {
t.Helper()
if p.Scope != "task" || p.SourceItemID != "" || p.SliceID != "" || p.ActionID != "" || p.Start != 0 || p.End != 0 || p.TimeBasis != "task" {
t.Fatalf("task problem lost global target semantics: %+v", p)
}
}
// Task-scope problems keep global target semantics while an optional view
// anchor persists the recorded picture, is canonicalized to the current
// evidence basis, clamps out-of-window time like problem records, and stays
// absent for legacy saves.
func TestQualityReviewDraftTaskViewAnchorPersistsClampsAndKeepsLegacy(t *testing.T) {
problems := []map[string]any{
plainTaskProblem("task-legacy", "整体历史问题"),
anchoredTaskProblem("task-anchor-clamped", "整体问题带锚点", map[string]any{"sliceId": "right", "atSeconds": 12, "timeBasis": "capture-seconds"}),
anchoredTaskProblem("task-anchor-converted", "整体问题锚点换算", map[string]any{"sliceId": "right", "atSeconds": 2, "timeBasis": "slice-seconds"}),
{"id": "action-bookmark", "scope": "action", "sliceId": "left", "actionId": "left-action", "text": "动作问题带画面书签", "atSeconds": 1, "viewAnchor": map[string]any{"sliceId": "right", "atSeconds": 6, "timeBasis": "capture-seconds"}},
}
expected := []float64{-1, 5, 7, 6}
for i, problem := range problems {
t.Run(problem["id"].(string), func(t *testing.T) {
f := qualityReviewDraftFixture(t)
rv, headers := openReviewDraftSession(t, f)
endpoint := "/api/human-items/" + f.item.ID + "/review-draft"
input := []map[string]any{problem}
baseVersion := 0
if problem["scope"] == "task" {
// ADR 0009:task 作用域仅存量回传——先按输入形状落库,再原样 PUT。
seed := domain.ReviewProblem{ID: problem["id"].(string), Scope: "task", Text: problem["text"].(string), TimeBasis: "task"}
if anchor, ok := problem["viewAnchor"].(map[string]any); ok {
seed.ViewAnchor = anchorFromTestMap(anchor)
}
seedLegacyOpinionDraft(t, f, endpoint, []domain.ReviewProblem{seed})
baseVersion = readReviewDraft(t, f, endpoint).Version
}
w := putReviewDraft(t, f, endpoint, rv, "anchor-save", baseVersion, input, headers)
if w.Code != 200 {
t.Fatalf("save: %d %s", w.Code, w.Body.String())
}
draft := readReviewDraft(t, f, endpoint)
p := checkProblemList(t, draft, problem["id"].(string))[0]
if i < 3 {
assertGlobalTaskTarget(t, p)
}
if i == 0 {
if p.ViewAnchor != nil {
t.Fatal("invented legacy anchor")
}
} else if p.ViewAnchor == nil || p.ViewAnchor.SliceID != "right" || p.ViewAnchor.AtSeconds != expected[i] || p.ViewAnchor.TimeBasis != "capture-seconds" {
t.Fatalf("anchor: %+v", p.ViewAnchor)
}
if i == 3 && (p.Start != 0 || p.End != 5 || p.ActionID != "left-action") {
t.Fatal("anchor changed target")
}
if w := putReviewDraft(t, f, endpoint, rv, "anchor-save", baseVersion, input, headers); w.Code != 200 {
t.Fatal(w.Body.String())
}
if readReviewDraft(t, f, endpoint).Version != baseVersion+1 {
t.Fatal("replay mutated draft")
}
})
}
}
func TestQualityReviewDraftRejectsInvalidViewAnchors(t *testing.T) {
f := qualityReviewDraftFixture(t)
resultVersion, headers := openReviewDraftSession(t, f)
endpoint := "/api/human-items/" + f.item.ID + "/review-draft"
invalid := [][]map[string]any{
{{"id": "bad-anchor", "scope": "task", "text": "外来来源", "viewAnchor": map[string]any{"sourceItemId": "source-a", "sliceId": "right", "atSeconds": 6, "timeBasis": "capture-seconds"}}},
{{"id": "bad-anchor", "scope": "task", "text": "缺失切片", "viewAnchor": map[string]any{"sliceId": "missing", "atSeconds": 6, "timeBasis": "capture-seconds"}}},
{{"id": "bad-anchor", "scope": "task", "text": "非法基准", "viewAnchor": map[string]any{"sliceId": "right", "atSeconds": 6, "timeBasis": "task"}}},
{{"id": "bad-anchor", "scope": "task", "text": "缺少切片", "viewAnchor": map[string]any{"atSeconds": 6, "timeBasis": "capture-seconds"}}},
}
for i, problems := range invalid {
w := putReviewDraft(t, f, endpoint, resultVersion, "bad-anchor-"+strings.Repeat("x", i+1), 0, problems, headers)
if w.Code != 400 {
t.Fatalf("invalid anchor %d accepted %d %s", i, w.Code, w.Body.String())
}
}
valid := []map[string]any{{"id": "ok-after-rejection", "scope": "slice", "sliceId": "left", "text": "拒绝后仍可保存", "atSeconds": 1}}
if w := putReviewDraft(t, f, endpoint, resultVersion, "anchor-recover", 0, valid, headers); w.Code != 200 {
t.Fatalf("failed anchor write stranded session %d %s", w.Code, w.Body.String())
}
}
// Aggregate acceptance keeps per-source anchor identity: two task problems
// anchored to different same-named members stay isolated, conversion to
// slice-seconds is canonical, and foreign members or slices are rejected.
func TestAcceptanceReviewDraftTaskViewAnchorIsolatesAggregateSources(t *testing.T) {
f := acceptanceReviewDraftFixture(t)
resultVersion, headers := openReviewDraftSession(t, f)
endpoint := "/api/human-items/" + f.item.ID + "/review-draft"
problems := []map[string]any{
anchoredTaskProblem("task-a", "来源A整体问题", map[string]any{"sourceItemId": "source-a", "sliceId": "same", "atSeconds": 2, "timeBasis": "slice-seconds"}),
anchoredTaskProblem("task-b", "来源B整体问题", map[string]any{"sourceItemId": "source-b", "sliceId": "same", "atSeconds": 3, "timeBasis": "slice-seconds"}),
plainTaskProblem("task-none", "历史整体问题"),
anchoredTaskProblem("task-converted", "锚点换算", map[string]any{"sourceItemId": "source-a", "sliceId": "same", "atSeconds": 12, "timeBasis": "capture-seconds"}),
}
expected := []float64{2, 3, -1, 2}
for i, problem := range problems {
f := acceptanceReviewDraftFixture(t)
rv, h := openReviewDraftSession(t, f)
ep := "/api/human-items/" + f.item.ID + "/review-draft"
// ADR 0009:task 作用域仅存量回传——先按输入形状落库,再原样 PUT。
seed := domain.ReviewProblem{ID: problem["id"].(string), Scope: "task", Text: problem["text"].(string), TimeBasis: "task"}
if anchor, ok := problem["viewAnchor"].(map[string]any); ok {
seed.ViewAnchor = anchorFromTestMap(anchor)
}
seedLegacyOpinionDraft(t, f, ep, []domain.ReviewProblem{seed})
if w := putReviewDraft(t, f, ep, rv, "anchor", readReviewDraft(t, f, ep).Version, []map[string]any{problem}, h); w.Code != 200 {
t.Fatal(w.Body.String())
}
p := readReviewDraft(t, f, ep).Problems[0]
assertGlobalTaskTarget(t, p)
if i == 2 {
if p.ViewAnchor != nil {
t.Fatal("invented anchor")
}
continue
}
wantSource := "source-a"
if i == 1 {
wantSource = "source-b"
}
if p.ViewAnchor == nil || p.ViewAnchor.SourceItemID != wantSource || p.ViewAnchor.AtSeconds != expected[i] || p.ViewAnchor.TimeBasis != "slice-seconds" {
t.Fatalf("anchor: %+v", p.ViewAnchor)
}
}
bad := [][]map[string]any{
{anchoredTaskProblem("bad-outside", "外来来源", map[string]any{"sourceItemId": "outsider", "sliceId": "same", "atSeconds": 1, "timeBasis": "slice-seconds"})},
{anchoredTaskProblem("bad-slice", "外来切片", map[string]any{"sourceItemId": "source-b", "sliceId": "left", "atSeconds": 1, "timeBasis": "slice-seconds"})},
}
for i, problems := range bad {
if w := putReviewDraft(t, f, endpoint, resultVersion, "aggregate-bad-"+strings.Repeat("x", i+1), 0, problems, headers); w.Code != 400 {
t.Fatalf("invalid aggregate anchor %d accepted %d %s", i, w.Code, w.Body.String())
}
}
}
func readBody(t *testing.T, f combinedDraftFixture, endpoint string) string {
t.Helper()
w := f.request(http.MethodGet, endpoint, f.reviewer, nil)
if w.Code != 200 {
t.Fatalf("read draft %d %s", w.Code, w.Body.String())
}
return w.Body.String()
}
// JSON cannot carry NaN or infinities, so nonfinite anchor time is checked at
// the canonicalization seam directly, alongside the discarded-slice rule:
// a bookmark may recall a discarded slice even though an action target may not.
func TestCanonicalReviewProblemsRejectsNonFiniteAndAllowsDiscardedAnchor(t *testing.T) {
evidence := []reviewTargetEvidence{{slices: []domain.SliceSegment{{ID: "kept", Start: 0, End: 5}, {ID: "dropped", Start: 5, End: 10, DeliveryDisposition: "discard"}}, actions: []domain.AnnotationSegment{{ID: "kept-action", Start: 1, End: 4}}}}
for _, at := range []float64{math.NaN(), math.Inf(1), math.Inf(-1)} {
problems := []domain.ReviewProblem{{ID: "p", Scope: "task", Text: "整体问题", ViewAnchor: &domain.ReviewViewAnchor{SliceID: "kept", AtSeconds: at, TimeBasis: "capture-seconds"}}}
if _, err := canonicalReviewProblems(problems, evidence); err == nil {
t.Fatalf("nonfinite anchor time %v accepted", at)
}
}
problems := []domain.ReviewProblem{
{ID: "task-anchor", Scope: "task", Text: "整体问题", ViewAnchor: &domain.ReviewViewAnchor{SliceID: "dropped", AtSeconds: 7, TimeBasis: "capture-seconds"}},
{ID: "action-anchor", Scope: "action", SliceID: "kept", ActionID: "kept-action", Text: "动作问题", ViewAnchor: &domain.ReviewViewAnchor{SliceID: "dropped", AtSeconds: 6, TimeBasis: "capture-seconds"}},
}
canonical, err := canonicalReviewProblems(problems, evidence)
if err != nil {
t.Fatalf("discarded anchor rejected: %v", err)
}
if len(canonical) != 2 || canonical[0].ViewAnchor.SliceID != "dropped" || canonical[0].ViewAnchor.AtSeconds != 7 || canonical[1].ViewAnchor.AtSeconds != 6 {
t.Fatalf("discarded anchor not canonicalized: %+v", canonical)
}
}