forked from longtao.wu/droboticflow
412 lines
21 KiB
Go
412 lines
21 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func TestDictionarySelectionsPersistThroughHTTPAndDictionaryMaintenance(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
labels := []domain.AnnotationLabelRecord{
|
|
{ID: "base", OrganizationID: f.item.OrganizationID, Kind: "slice", Code: "base", Name: "物品操作", Tags: []string{"商品"}, Version: 2, Status: domain.AnnotationLabelActive},
|
|
{ID: "action", OrganizationID: f.item.OrganizationID, Kind: "action", Code: "pick", Name: "拿取", Tags: []string{"抓取"}, Version: 3, Status: domain.AnnotationLabelActive},
|
|
}
|
|
for _, label := range labels {
|
|
if err := f.store.UpsertAnnotationLabel(f.ctx, label); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
selection := domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID, LabelIDs: []string{"action"}, SliceLabelIDs: []string{"base"}}
|
|
if err := f.store.UpsertProjectAnnotationLabels(f.ctx, selection); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertProjectSliceLabels(f.ctx, selection); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
forgedBase := *labels[0].Selection()
|
|
forgedBase.Tags = []string{"FORGED"}
|
|
forgedAction := *labels[1].Selection()
|
|
forgedAction.Tags = []string{"FORGED"}
|
|
payload := combinedAnnotationDraftInput{Version: f.item.Version, SliceSegments: []domain.SliceSegment{{ID: "s", Name: "物品操作", Start: 0, End: 10, Dictionary: &forgedBase}}, Segments: []domain.AnnotationSegment{{ID: "a", Start: 1, End: 2, Description: "拿起杯子", Dictionary: &forgedAction}}}
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/drafts"
|
|
save := func(actor domain.UserRecord) *domain.HumanItemRecord {
|
|
t.Helper()
|
|
r := f.request(http.MethodPut, endpoint, actor, payload)
|
|
if r.Code != 200 {
|
|
t.Fatalf("save: %d %s", r.Code, r.Body.String())
|
|
}
|
|
item, err := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return item
|
|
}
|
|
saved := save(f.annotator)
|
|
draft := annotationWorkspacePayload(saved)
|
|
slices := decodeMaterializationValue[domain.SliceSegment](draft["sliceSegments"])
|
|
actions := decodeMaterializationValue[domain.AnnotationSegment](draft["segments"])
|
|
if !reflect.DeepEqual(slices[0].Dictionary, labels[0].Selection()) || !reflect.DeepEqual(actions[0].Dictionary, labels[1].Selection()) {
|
|
t.Fatal("server trusted forged tags")
|
|
}
|
|
// Empty action text never clears the independent base classification.
|
|
payload.Version = saved.Version
|
|
payload.SliceSegments = slices
|
|
payload.Segments = nil
|
|
saved = save(f.annotator)
|
|
slices = decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
|
if slices[0].Dictionary == nil || slices[0].Dictionary.Tags[0] != "商品" {
|
|
t.Fatal("blank description lost base class")
|
|
}
|
|
for i := range labels {
|
|
labels[i].Version++
|
|
labels[i].Name += "新版"
|
|
labels[i].Tags = []string{"新标签"}
|
|
labels[i].Status = domain.AnnotationLabelInactive
|
|
f.store.UpsertAnnotationLabel(f.ctx, labels[i])
|
|
}
|
|
f.store.UpsertProjectSliceLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
|
payload.Version = saved.Version
|
|
payload.SliceSegments = slices
|
|
saved = save(f.annotator)
|
|
// A fresh handler recovers the frozen snapshot, independent of active dictionaries.
|
|
f.handler = NewHandler(f.store, nil)
|
|
payload.Version = saved.Version
|
|
saved = save(f.annotator)
|
|
slices = decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
|
if slices[0].Dictionary.Version != 2 || slices[0].Dictionary.Name != "物品操作" {
|
|
t.Fatal("historical classification rewritten")
|
|
}
|
|
payload.Version = saved.Version
|
|
payload.Segments = []domain.AnnotationSegment{{ID: "new-action", Start: 1, End: 2, Description: "拿取", Dictionary: &forgedAction}}
|
|
if r := f.request(http.MethodPut, endpoint, f.annotator, payload); r.Code != 400 {
|
|
t.Fatalf("stale/new action accepted %d %s", r.Code, r.Body.String())
|
|
}
|
|
if r := f.request(http.MethodPut, endpoint, f.reviewer, payload); r.Code != 403 {
|
|
t.Fatal("reviewer can change classification")
|
|
}
|
|
}
|
|
|
|
func TestDictionaryTagsHaveIndependentRevisionAndAdminAuthorization(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
body := map[string]any{"kind": "slice", "name": "分拣", "code": "sort", "category": "business-category", "aliases": []string{"别名"}, "tags": []string{" 商品 ", "商品", "分拣"}}
|
|
r := f.request("POST", "/api/annotation-labels", f.annotator, body)
|
|
if r.Code != 403 {
|
|
t.Fatal("annotator maintained dictionary")
|
|
}
|
|
r = f.request("POST", "/api/annotation-labels", f.admin, body)
|
|
if r.Code != 201 {
|
|
t.Fatalf("%d %s", r.Code, r.Body.String())
|
|
}
|
|
var label domain.AnnotationLabelRecord
|
|
if err := json.Unmarshal(r.Body.Bytes(), &label); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(label.Tags, []string{"商品", "分拣"}) || label.Category != "business-category" || len(label.Aliases) != 1 {
|
|
t.Fatal("tags confused with other fields")
|
|
}
|
|
body["tags"] = []string{}
|
|
r = f.request("PATCH", "/api/annotation-labels/"+label.ID, f.admin, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %s", r.Code, r.Body.String())
|
|
}
|
|
var updated domain.AnnotationLabelRecord
|
|
json.Unmarshal(r.Body.Bytes(), &updated)
|
|
if updated.Version != 2 || len(updated.Tags) != 0 || !reflect.DeepEqual(updated.History[0].Tags, label.Tags) {
|
|
t.Fatal("tag history lost")
|
|
}
|
|
}
|
|
|
|
func TestLegacyClassificationDoesNotGuessCurrentTagsAtConfirmation(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
legacy := domain.SliceSegment{ID: "legacy", Name: "物品操作", Start: 0, End: 10}
|
|
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": []domain.SliceSegment{legacy}}
|
|
f.store.UpsertHumanItem(f.ctx, f.item)
|
|
f.store.UpsertAnnotationLabel(f.ctx, domain.AnnotationLabelRecord{ID: "current", OrganizationID: f.item.OrganizationID, Kind: "slice", Code: "base", Name: legacy.Name, Tags: []string{"现在新增的标签"}, Version: 9, Status: domain.AnnotationLabelActive})
|
|
f.store.UpsertProjectSliceLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID, SliceLabelIDs: []string{"current"}})
|
|
server := &server{store: f.store}
|
|
slices := []domain.SliceSegment{legacy}
|
|
if issues := server.validateProjectSliceNames(f.ctx, &f.item, slices, "confirm"); len(issues) > 0 || slices[0].Dictionary != nil {
|
|
t.Fatal("legacy tags guessed at confirm")
|
|
}
|
|
slices[0].Dictionary = &domain.DictionarySelection{ID: "current", Version: 9}
|
|
if issues := server.validateProjectSliceNames(f.ctx, &f.item, slices, "confirm"); len(issues) > 0 || slices[0].Dictionary == nil || slices[0].Dictionary.Tags[0] != "现在新增的标签" {
|
|
t.Fatal("explicit new selection did not freeze current tags")
|
|
}
|
|
}
|
|
|
|
func TestSliceEditApplicationCanonicalizesAgainstStoredDictionaryState(t *testing.T) {
|
|
for _, foreign := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "forged-tags", true: "foreign-word"}[foreign], func(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
selection, _ := f.store.GetProjectAnnotationLabels(f.ctx, f.task.ProjectID)
|
|
label, _ := f.store.GetAnnotationLabel(f.ctx, selection.SliceLabelIDs[0])
|
|
label.Tags = []string{"服务端标签"}
|
|
label.Version = 2
|
|
f.store.UpsertAnnotationLabel(f.ctx, *label)
|
|
before := []domain.SliceSegment{{ID: "s", Name: label.Name, Start: 0, End: 10}}
|
|
item := f.item
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": before, "segments": []domain.AnnotationSegment{}}
|
|
f.store.UpsertHumanItem(f.ctx, item)
|
|
incoming := *label.Selection()
|
|
incoming.Tags = []string{"FORGED"}
|
|
if foreign {
|
|
incoming.ID = "foreign-label"
|
|
}
|
|
proposed := append([]domain.SliceSegment(nil), before...)
|
|
proposed[0].Dictionary = &incoming
|
|
body := sliceEditApplicationInput{ApplicationID: "classification-apply", combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: proposed}}
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-applications", f.annotator, body)
|
|
if foreign {
|
|
if r.Code != 400 {
|
|
t.Fatalf("foreign classification accepted %d %s", r.Code, r.Body.String())
|
|
}
|
|
return
|
|
}
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %s", r.Code, r.Body.String())
|
|
}
|
|
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
got := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
|
if got[0].Dictionary == nil || !reflect.DeepEqual(got[0].Dictionary.Tags, label.Tags) {
|
|
t.Fatal("client classification snapshot trusted")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestProjectActionDictionaryReturnsVersionedItemsForAnnotator(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
label := domain.AnnotationLabelRecord{ID: "action-word", OrganizationID: f.item.OrganizationID, Code: "pick", Name: "拿取", Kind: "action", Version: 3, Tags: []string{"抓取"}, Status: domain.AnnotationLabelActive}
|
|
f.store.UpsertAnnotationLabel(f.ctx, label)
|
|
f.store.UpsertProjectAnnotationLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ID: "selection", ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID, LabelIDs: []string{label.ID}})
|
|
r := f.request("GET", "/api/projects/"+f.task.ProjectID+"/annotation-labels", f.annotator, nil)
|
|
var response struct {
|
|
ID string `json:"id"`
|
|
LabelIDs []string `json:"labelIds"`
|
|
Items []domain.AnnotationLabelRecord `json:"items"`
|
|
}
|
|
if json.Unmarshal(r.Body.Bytes(), &response) != nil || r.Code != 200 || response.ID != "selection" || len(response.LabelIDs) != 1 || len(response.Items) != 1 || response.Items[0].Version != 3 || response.Items[0].Tags[0] != "抓取" {
|
|
t.Fatalf("%d %s", r.Code, r.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestUndoRestoresOnlyServerRememberedClassificationsAfterDictionaryMaintenance(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
srv := &server{store: f.store}
|
|
base := domain.AnnotationLabelRecord{ID: "base-old", OrganizationID: f.item.OrganizationID, Kind: "slice", Code: "base", Name: "旧基础分类", Version: 2, Tags: []string{"原标签"}, Status: domain.AnnotationLabelActive}
|
|
action := domain.AnnotationLabelRecord{ID: "action-old", OrganizationID: f.item.OrganizationID, Kind: "action", Code: "pick", Name: "旧动作分类", Version: 3, Tags: []string{"原动作标签"}, Status: domain.AnnotationLabelActive}
|
|
f.store.UpsertAnnotationLabel(f.ctx, base)
|
|
f.store.UpsertAnnotationLabel(f.ctx, action)
|
|
selection := domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID, LabelIDs: []string{action.ID}, SliceLabelIDs: []string{base.ID}}
|
|
f.store.UpsertProjectAnnotationLabels(f.ctx, selection)
|
|
f.store.UpsertProjectSliceLabels(f.ctx, selection)
|
|
oldSlices := []domain.SliceSegment{{ID: "s", Name: base.Name, Start: 0, End: 10, Dictionary: base.Selection()}}
|
|
oldActions := []domain.AnnotationSegment{{ID: "a", Description: "原描述", Start: 0, End: 10, Status: "described", Dictionary: action.Selection()}}
|
|
item := f.item
|
|
if issues := srv.validateProjectSliceNames(f.ctx, &item, oldSlices, "draft"); len(issues) > 0 {
|
|
t.Fatal(issues)
|
|
}
|
|
if issues := srv.canonicalActionDictionaries(f.ctx, &item, oldActions); len(issues) > 0 {
|
|
t.Fatal(issues)
|
|
}
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate", "status": "completed"}
|
|
// Later editing selected a different classification. The word is subsequently
|
|
// renamed, retagged, disabled and unlinked by an administrator.
|
|
item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": []domain.SliceSegment{{ID: "s", Name: "另一个分类", Start: 0, End: 10}}, "segments": []domain.AnnotationSegment{{ID: "a", Description: "修改后描述", Start: 0, End: 10, Status: "described"}}}
|
|
f.store.UpsertHumanItem(f.ctx, item)
|
|
base.Name = "新版"
|
|
base.Tags = []string{"新标签"}
|
|
base.Version++
|
|
base.Status = domain.AnnotationLabelInactive
|
|
f.store.UpsertAnnotationLabel(f.ctx, base)
|
|
action.Version++
|
|
action.Status = domain.AnnotationLabelInactive
|
|
f.store.UpsertAnnotationLabel(f.ctx, action)
|
|
f.store.UpsertProjectSliceLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
|
f.store.UpsertProjectAnnotationLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
|
// Simulate serialization and restart; do not use process-local snapshot state.
|
|
f.handler = NewHandler(f.store, nil)
|
|
oldSlices[0].Dictionary.Tags = []string{"FORGED"}
|
|
oldActions[0].Dictionary.Tags = []string{"FORGED"}
|
|
body := combinedAnnotationDraftInput{Version: item.Version, SliceSegments: oldSlices, Segments: oldActions}
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-edit-restorations", f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("undo %d %s", r.Code, r.Body.String())
|
|
}
|
|
saved, _ := f.store.GetHumanItem(f.ctx, item.ID)
|
|
slices := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
|
actions := decodeMaterializationValue[domain.AnnotationSegment](annotationWorkspacePayload(saved)["segments"])
|
|
if slices[0].Dictionary.Version != 2 || slices[0].Dictionary.Tags[0] != "原标签" || actions[0].Dictionary.Version != 3 || actions[0].Dictionary.Tags[0] != "原动作标签" {
|
|
t.Fatal("undo lost or forged frozen classification")
|
|
}
|
|
}
|
|
|
|
func TestDirectDispositionCannotPersistForeignOrForgedSliceDictionary(t *testing.T) {
|
|
for _, foreign := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "forged-tags", true: "foreign-word"}[foreign], func(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
body := dispositionFixturePayload(t, f)
|
|
selection, _ := f.store.GetProjectAnnotationLabels(f.ctx, f.task.ProjectID)
|
|
label, _ := f.store.GetAnnotationLabel(f.ctx, selection.SliceLabelIDs[0])
|
|
label.Tags = []string{"服务端标签"}
|
|
label.Version = 2
|
|
f.store.UpsertAnnotationLabel(f.ctx, *label)
|
|
body.SliceSegments[0].Name = label.Name
|
|
d := *label.Selection()
|
|
d.Tags = []string{"FORGED"}
|
|
if foreign {
|
|
d.ID = "foreign"
|
|
}
|
|
body.SliceSegments[0].Dictionary = &d
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if foreign {
|
|
if r.Code != 400 {
|
|
t.Fatalf("foreign dictionary saved %d %s", r.Code, r.Body.String())
|
|
}
|
|
return
|
|
}
|
|
if r.Code != 200 {
|
|
t.Fatalf("%d %s", r.Code, r.Body.String())
|
|
}
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
saved := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(item)["sliceSegments"])
|
|
if saved[0].Dictionary == nil || !reflect.DeepEqual(saved[0].Dictionary.Tags, label.Tags) {
|
|
t.Fatal("forged tags persisted through direct disposition")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDispositionUndoRedoPreservesClassificationsAfterDictionaryRemoval(t *testing.T) {
|
|
for _, application := range []bool{false, true} {
|
|
t.Run(map[bool]string{false: "direct", true: "panel-application"}[application], func(t *testing.T) {
|
|
f := continuousFixture(t)
|
|
body := dispositionFixturePayload(t, f)
|
|
selection, _ := f.store.GetProjectAnnotationLabels(f.ctx, f.task.ProjectID)
|
|
base, _ := f.store.GetAnnotationLabel(f.ctx, selection.SliceLabelIDs[0])
|
|
base.Tags = []string{"基础标签"}
|
|
base.Version = 2
|
|
f.store.UpsertAnnotationLabel(f.ctx, *base)
|
|
reason, _ := f.store.GetAnnotationLabel(f.ctx, body.ReasonLabelID)
|
|
reason.Tags = []string{"舍弃标签"}
|
|
f.store.UpsertAnnotationLabel(f.ctx, *reason)
|
|
for i := range body.SliceSegments {
|
|
body.SliceSegments[i].Name = base.Name
|
|
body.SliceSegments[i].Dictionary = base.Selection()
|
|
}
|
|
original := append([]domain.SliceSegment(nil), body.SliceSegments...)
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID
|
|
r := f.request("POST", endpoint+"/slice-dispositions", f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("discard %d %s", r.Code, r.Body.String())
|
|
}
|
|
var first struct {
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &first)
|
|
base.Name = "新名字"
|
|
base.Version++
|
|
base.Tags = []string{"新标签"}
|
|
base.Status = domain.AnnotationLabelInactive
|
|
f.store.UpsertAnnotationLabel(f.ctx, *base)
|
|
reason.Version++
|
|
reason.Status = domain.AnnotationLabelInactive
|
|
f.store.UpsertAnnotationLabel(f.ctx, *reason)
|
|
f.store.UpsertProjectSliceLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
|
f.store.UpsertProjectDiscardReasonLabels(f.ctx, domain.ProjectAnnotationLabelsRecord{ProjectID: f.task.ProjectID, OrganizationID: f.item.OrganizationID})
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"phase": "annotate", "status": "completed"}
|
|
f.store.UpsertHumanItem(f.ctx, *item)
|
|
f.handler = NewHandler(f.store, nil)
|
|
if application {
|
|
input := sliceEditApplicationInput{ApplicationID: "undo-discard", RestoreClassifications: true, combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: item.Version, SliceSegments: original}, Dispositions: []sliceDispositionInput{{SliceID: body.SliceID, Disposition: "keep"}}}
|
|
r = f.request("POST", endpoint+"/slice-edit-applications", f.annotator, input)
|
|
} else {
|
|
undo := body
|
|
undo.combinedAnnotationDraftInput = first.Workspace
|
|
undo.Version = item.Version
|
|
undo.Disposition = "keep"
|
|
undo.RestoreClassifications = true
|
|
undo.RestorePreviousName = true
|
|
undo.RestoreNameFromVersion = first.Item.Version
|
|
r = f.request("POST", endpoint+"/slice-dispositions", f.annotator, undo)
|
|
}
|
|
if r.Code != 200 {
|
|
t.Fatalf("undo %d %s", r.Code, r.Body.String())
|
|
}
|
|
var restored struct {
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &restored)
|
|
d := restored.Workspace.SliceSegments[1].Dictionary
|
|
if d == nil || d.Version != 2 || d.Tags[0] != "基础标签" {
|
|
t.Fatal("undo lost frozen base category")
|
|
}
|
|
if application {
|
|
input := sliceEditApplicationInput{ApplicationID: "redo-discard", RestoreClassifications: true, combinedAnnotationDraftInput: combinedAnnotationDraftInput{Version: restored.Item.Version, SliceSegments: first.Workspace.SliceSegments}, Dispositions: []sliceDispositionInput{{SliceID: body.SliceID, Disposition: "discard", ReasonLabelID: body.ReasonLabelID, ReasonVersion: body.ReasonVersion}}}
|
|
r = f.request("POST", endpoint+"/slice-edit-applications", f.annotator, input)
|
|
} else {
|
|
redo := body
|
|
redo.combinedAnnotationDraftInput = restored.Workspace
|
|
redo.Version = restored.Item.Version
|
|
redo.RestoreClassifications = true
|
|
r = f.request("POST", endpoint+"/slice-dispositions", f.annotator, redo)
|
|
}
|
|
if r.Code != 200 {
|
|
t.Fatalf("redo %d %s", r.Code, r.Body.String())
|
|
}
|
|
var redone struct {
|
|
Workspace combinedAnnotationDraftInput `json:"workspace"`
|
|
}
|
|
json.Unmarshal(r.Body.Bytes(), &redone)
|
|
d = redone.Workspace.SliceSegments[1].DiscardReasonDictionary
|
|
if d == nil || d.Version != 1 || d.Tags[0] != "舍弃标签" {
|
|
t.Fatal("redo lost frozen discard reason")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUnconfirmedRestoredNamesCannotBecomeFinalClassification(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
srv := &server{store: f.store}
|
|
slices := []domain.SliceSegment{{ID: "s", Name: "未选择字典的临时名称", Start: 0, End: 10}}
|
|
if issues := srv.validateProjectSliceNames(f.ctx, &f.item, slices, "restore"); len(issues) > 0 {
|
|
t.Fatal(issues)
|
|
}
|
|
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{"sliceSegments": slices}
|
|
for _, mode := range []string{"confirm", "submit"} {
|
|
if issues := srv.validateProjectSliceNames(f.ctx, &f.item, slices, mode); len(issues) == 0 {
|
|
t.Fatalf("unfinished name bypassed %s", mode)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestLegacyDispositionCannotSeedDictionaryRecoveryHistory(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
f.task.TemplateVersion = 5
|
|
f.store.UpsertAnnotationTask(f.ctx, f.task)
|
|
body := dispositionFixturePayload(t, f)
|
|
body.Segments[0].Handedness = "both"
|
|
body.SliceSegments[0].Name = "原有名称"
|
|
body.SliceSegments[0].TaskLabelID = "legacy-task-label"
|
|
body.SliceSegments[0].TaskLabel = "原有任务分类"
|
|
body.SliceSegments[0].Dictionary = &domain.DictionarySelection{ID: "FORGED", Kind: "action", Version: 9, Name: "伪造分类", Code: "forged", Tags: []string{"FORGED"}}
|
|
r := f.request("POST", "/api/plugins/annotation-qa/tasks/"+f.task.ID+"/slice-dispositions", f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("legacy save %d %s", r.Code, r.Body.String())
|
|
}
|
|
saved, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
slices := decodeMaterializationValue[domain.SliceSegment](annotationWorkspacePayload(saved)["sliceSegments"])
|
|
if slices[0].Dictionary != nil || slices[0].Name != "原有名称" || slices[0].TaskLabelID != "legacy-task-label" || savedDictionarySelection(saved, &domain.DictionarySelection{ID: "FORGED", Version: 9}, "action") != nil {
|
|
t.Fatal("legacy task seeded a dictionary snapshot or lost legacy labels")
|
|
}
|
|
}
|