forked from longtao.wu/droboticflow
101 lines
3.9 KiB
Go
101 lines
3.9 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
type annotationDictionaryReadFailure struct {
|
|
store.Store
|
|
err error
|
|
}
|
|
|
|
func (s annotationDictionaryReadFailure) GetProjectAnnotationLabels(context.Context, string) (*domain.ProjectAnnotationLabelsRecord, error) {
|
|
return nil, s.err
|
|
}
|
|
|
|
func TestAnnotationDraftDictionaryReadFailureIsRetryable(t *testing.T) {
|
|
for _, failure := range []struct {
|
|
name string
|
|
err error
|
|
status int
|
|
}{{"unavailable", errors.New("synthetic database unavailable"), 503}, {"timeout", context.DeadlineExceeded, 504}} {
|
|
t.Run(failure.name, func(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, f, "测试切片")
|
|
// Historical first-pass state: saved cuts/actions but no sliceWorkflow.
|
|
f.item.Subject.Payload["annotationWorkspace"] = map[string]any{
|
|
"sliceSegments": []domain.SliceSegment{{ID: "s", Start: 0, End: 10}},
|
|
"segments": []domain.AnnotationSegment{{ID: "retained", Start: 0, End: 10, Status: "pending"}},
|
|
"userId": f.annotator.ID, "savedAt": nowISO(),
|
|
}
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
body := combinedAnnotationDraftInput{Version: before.Version,
|
|
SliceSegments: []domain.SliceSegment{{ID: "s", Name: "测试切片", Start: 0, End: 10}},
|
|
Segments: []domain.AnnotationSegment{{ID: "retained", Start: 0, End: 10, Status: "pending"}},
|
|
}
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/drafts"
|
|
f.handler = NewHandler(annotationDictionaryReadFailure{f.store, failure.err}, nil)
|
|
r := f.request(http.MethodPut, endpoint, f.annotator, body)
|
|
if r.Code != failure.status {
|
|
t.Fatalf("dependency read failure was cached as content rejection: %d %s", r.Code, r.Body.String())
|
|
}
|
|
if strings.Contains(r.Body.String(), failure.err.Error()) {
|
|
t.Fatal("response exposed the prerequisite database error")
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if !reflect.DeepEqual(before, after) {
|
|
t.Fatal("failed save changed existing annotation state")
|
|
}
|
|
f.handler = NewHandler(f.store, nil)
|
|
r = f.request(http.MethodPut, endpoint, f.annotator, body)
|
|
if r.Code != 200 {
|
|
t.Fatalf("same contents cannot be retried after recovery: %d %s", r.Code, r.Body.String())
|
|
}
|
|
after, _ = f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
draft := mapValue(after.Subject.Payload["annotationDraft"])
|
|
if got := decodeMaterializationValue[domain.AnnotationSegment](draft["segments"]); !reflect.DeepEqual(got, body.Segments) {
|
|
t.Fatal("retry lost retained actions")
|
|
}
|
|
if !reflect.DeepEqual(after.Subject.Payload["annotationWorkspace"], before.Subject.Payload["annotationWorkspace"]) {
|
|
t.Fatal("autosave overwrote confirmed work")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAnnotationDraftDependencyFixKeepsContentAndPermissionRejections(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
seedSliceDictionary(t, f, "测试切片")
|
|
body := combinedAnnotationDraftInput{Version: f.item.Version,
|
|
SliceSegments: []domain.SliceSegment{{ID: "s", Name: "不属于项目字典的名称", Start: 0, End: 10}},
|
|
Segments: []domain.AnnotationSegment{{ID: "retained", Start: 0, End: 10, Status: "pending"}},
|
|
}
|
|
endpoint := "/api/plugins/annotation-qa/tasks/" + f.task.ID + "/drafts"
|
|
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
for _, actor := range []domain.UserRecord{f.annotator, f.reviewer} {
|
|
want := http.StatusBadRequest
|
|
if actor.ID == f.reviewer.ID {
|
|
want = http.StatusForbidden
|
|
}
|
|
r := f.request(http.MethodPut, endpoint, actor, body)
|
|
if r.Code != want {
|
|
t.Fatalf("content/permission rejection changed: %d %s", r.Code, r.Body.String())
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if !reflect.DeepEqual(before, after) {
|
|
t.Fatal("rejected save changed existing task")
|
|
}
|
|
}
|
|
}
|