forked from longtao.wu/droboticflow
186 lines
7.9 KiB
Go
186 lines
7.9 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
// seam ② (spec §6, ticket 07): the submit structure-change summary is a
|
|
// server-side diff between the rework return baseline and the submitted
|
|
// workspace (Q7:b / R1-Q7:b). Counts are net structure differences — the
|
|
// same reading as ADR 0006's "复审以保存后差异为准" — not a client event
|
|
// log. Table-driven cases: merge / split / discard / boundary move one
|
|
// each, plus the no-change zero default.
|
|
func TestStructureChangeSummaryCounts(t *testing.T) {
|
|
keep := func(id string, start, end float64) domain.SliceSegment {
|
|
return domain.SliceSegment{ID: id, Start: start, End: end, DeliveryDisposition: "keep"}
|
|
}
|
|
discard := func(id string, start, end float64) domain.SliceSegment {
|
|
return domain.SliceSegment{ID: id, Start: start, End: end, DeliveryDisposition: "discard"}
|
|
}
|
|
for _, scenario := range []struct {
|
|
name string
|
|
baseline []domain.SliceSegment
|
|
submitted []domain.SliceSegment
|
|
want domain.StructureChangeSummary
|
|
}{
|
|
{
|
|
name: "no structure change keeps every count zero",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 5), keep("b", 5, 10)},
|
|
submitted: []domain.SliceSegment{keep("a", 0, 5), keep("b", 5, 10)},
|
|
want: domain.StructureChangeSummary{Merged: 0, Split: 0, Discarded: 0, BoundaryMoves: 0},
|
|
},
|
|
{
|
|
name: "merge of two adjacent slices counts one disappeared boundary",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 5), keep("b", 5, 10)},
|
|
submitted: []domain.SliceSegment{keep("a", 0, 10)},
|
|
want: domain.StructureChangeSummary{Merged: 1},
|
|
},
|
|
{
|
|
name: "three-way merge counts two disappeared boundaries",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 3), keep("b", 3, 6), keep("c", 6, 10)},
|
|
submitted: []domain.SliceSegment{keep("a", 0, 10)},
|
|
want: domain.StructureChangeSummary{Merged: 2},
|
|
},
|
|
{
|
|
name: "split adds one new boundary",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 10)},
|
|
submitted: []domain.SliceSegment{keep("a1", 0, 4), keep("a2", 4, 10)},
|
|
want: domain.StructureChangeSummary{Split: 1},
|
|
},
|
|
{
|
|
name: "boundary move keeps the cut and counts the displacement",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 5), keep("b", 5, 10)},
|
|
submitted: []domain.SliceSegment{keep("a", 0, 6), keep("b", 6, 10)},
|
|
want: domain.StructureChangeSummary{BoundaryMoves: 1},
|
|
},
|
|
{
|
|
name: "discard flip counts one newly discarded slice without boundary edits",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 5), keep("b", 5, 10)},
|
|
submitted: []domain.SliceSegment{discard("a", 0, 5), keep("b", 5, 10)},
|
|
want: domain.StructureChangeSummary{Discarded: 1},
|
|
},
|
|
{
|
|
name: "slice already discarded in the baseline is not counted again",
|
|
baseline: []domain.SliceSegment{discard("a", 0, 5), keep("b", 5, 10)},
|
|
submitted: []domain.SliceSegment{discard("a", 0, 5), keep("b", 5, 10)},
|
|
want: domain.StructureChangeSummary{},
|
|
},
|
|
{
|
|
name: "merge plus discard reports both",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 5), keep("b", 5, 10)},
|
|
submitted: []domain.SliceSegment{discard("a", 0, 10)},
|
|
want: domain.StructureChangeSummary{Merged: 1, Discarded: 1},
|
|
},
|
|
{
|
|
name: "merge plus moved surviving boundary reports one merge and one move",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 4), keep("b", 4, 7), keep("c", 7, 10)},
|
|
submitted: []domain.SliceSegment{keep("a", 0, 6), keep("c", 6, 10)},
|
|
want: domain.StructureChangeSummary{Merged: 1, BoundaryMoves: 1},
|
|
},
|
|
{
|
|
name: "split of an already discarded slice counts the split only",
|
|
baseline: []domain.SliceSegment{discard("a", 0, 10)},
|
|
submitted: []domain.SliceSegment{discard("a1", 0, 4), discard("a2", 4, 10)},
|
|
want: domain.StructureChangeSummary{Split: 1},
|
|
},
|
|
{
|
|
name: "two independent boundary moves count separately",
|
|
baseline: []domain.SliceSegment{keep("a", 0, 3), keep("b", 3, 7), keep("c", 7, 10)},
|
|
submitted: []domain.SliceSegment{keep("a", 0, 2), keep("b", 2, 6), keep("c", 6, 10)},
|
|
want: domain.StructureChangeSummary{BoundaryMoves: 2},
|
|
},
|
|
{
|
|
name: "empty structures degrade to zeros",
|
|
baseline: nil,
|
|
submitted: nil,
|
|
want: domain.StructureChangeSummary{},
|
|
},
|
|
} {
|
|
t.Run(scenario.name, func(t *testing.T) {
|
|
got := structureChangeSummary(scenario.baseline, scenario.submitted)
|
|
if got != scenario.want {
|
|
t.Fatalf("structureChangeSummary(%+v, %+v) = %+v, want %+v", scenario.baseline, scenario.submitted, got, scenario.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type structureSummarySubmissionResponse struct {
|
|
SliceRevision struct {
|
|
Revision int `json:"revision"`
|
|
StructureChangeSummary *domain.StructureChangeSummary `json:"structureChangeSummary"`
|
|
} `json:"sliceRevision"`
|
|
}
|
|
|
|
func submitContinuousInvalidPartition(t *testing.T, f combinedDraftFixture, sliceSegments []map[string]any) structureSummarySubmissionResponse {
|
|
t.Helper()
|
|
r := f.request(http.MethodPost, "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/combined-submissions", f.annotator, map[string]any{
|
|
"version": f.item.Version,
|
|
"sliceSegments": sliceSegments,
|
|
"segments": []map[string]any{{"id": "a", "start": 0, "end": 10, "status": "invalid", "isInvalid": true}},
|
|
})
|
|
if r.Code != http.StatusCreated {
|
|
t.Fatalf("submission: HTTP %d %s", r.Code, r.Body.String())
|
|
}
|
|
var response structureSummarySubmissionResponse
|
|
if err := json.Unmarshal(r.Body.Bytes(), &response); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return response
|
|
}
|
|
|
|
func TestCombinedSubmissionAttachesStructureChangeSummaryOnRework(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
seedCompletedMaterialization(t, f, 1)
|
|
item := f.item
|
|
item.ReviewRound = 1
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertReworkBaseline(f.ctx, domain.ReworkBaselineRecord{
|
|
ID: store.ReworkBaselineID(item.ID, 1), OrganizationID: item.OrganizationID, ItemID: item.ID, Round: 1,
|
|
Slices: []domain.SliceSegment{
|
|
{ID: "base-a", Start: 0, End: 5, DeliveryDisposition: "keep"},
|
|
{ID: "base-b", Start: 5, End: 10, DeliveryDisposition: "keep"},
|
|
},
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// The annotator merged the two baseline slices before resubmitting.
|
|
response := submitContinuousInvalidPartition(t, f, []map[string]any{{"id": "merged", "name": "测试切片", "start": 0, "end": 10}})
|
|
if response.SliceRevision.StructureChangeSummary == nil {
|
|
t.Fatal("rework submission must attach a structure change summary")
|
|
}
|
|
want := domain.StructureChangeSummary{Merged: 1}
|
|
if *response.SliceRevision.StructureChangeSummary != want {
|
|
t.Fatalf("summary = %+v, want %+v", *response.SliceRevision.StructureChangeSummary, want)
|
|
}
|
|
stored, err := f.store.ListSliceRevisions(f.ctx, f.task.OrganizationID, f.task.ID)
|
|
if err != nil || len(stored) != 1 {
|
|
t.Fatalf("stored revisions=%d err=%v", len(stored), err)
|
|
}
|
|
if stored[0].StructureChangeSummary == nil || *stored[0].StructureChangeSummary != want {
|
|
t.Fatalf("summary must persist with the submission record: %+v", stored[0].StructureChangeSummary)
|
|
}
|
|
}
|
|
|
|
func TestCombinedSubmissionOmitsStructureChangeSummaryWithoutBaseline(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
seedCompletedMaterialization(t, f, 1)
|
|
// First-pass submission: no review round, no return baseline (ticket 07
|
|
// contract — 首标送检无退回基线时不附).
|
|
response := submitContinuousInvalidPartition(t, f, []map[string]any{{"id": "s", "name": "测试切片", "start": 0, "end": 10}})
|
|
if response.SliceRevision.StructureChangeSummary != nil {
|
|
t.Fatalf("first submission must not attach a summary: %+v", response.SliceRevision.StructureChangeSummary)
|
|
}
|
|
stored, err := f.store.ListSliceRevisions(f.ctx, f.task.OrganizationID, f.task.ID)
|
|
if err != nil || len(stored) != 1 || stored[0].StructureChangeSummary != nil {
|
|
t.Fatalf("stored summary must stay nil: %+v err=%v", stored, err)
|
|
}
|
|
}
|