forked from longtao.wu/droboticflow
79 lines
3.3 KiB
Go
79 lines
3.3 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
// The browser can lose the first response or close after the server commits.
|
|
// Recovery observes only the public claim/read APIs, without inspecting receipts.
|
|
func TestContinuousClaimLostResponseReplaysWithoutConsumingNextTask(t *testing.T) {
|
|
f := randomClaimFixture(t, 2)
|
|
body := map[string]any{"projectId": f.task.ProjectID, "requestId": "closed-page-unknown-claim"}
|
|
if response := f.request(http.MethodPost, "/api/plugins/annotation-qa/random-claims", f.annotator, body); response.Code != 200 {
|
|
t.Fatalf("initial request: %d %s", response.Code, response.Body.String())
|
|
}
|
|
var claimedID string
|
|
for i := 0; i < 3; i++ {
|
|
response := f.request(http.MethodPost, "/api/plugins/annotation-qa/random-claims", f.annotator, body)
|
|
var recovered struct {
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
Replayed bool `json:"replayed"`
|
|
}
|
|
if err := json.Unmarshal(response.Body.Bytes(), &recovered); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if response.Code != 200 || !recovered.Replayed || recovered.Item.ID == "" || recovered.Item.AssignedUserID != f.annotator.ID {
|
|
t.Fatalf("recovery: %d %s", response.Code, response.Body.String())
|
|
}
|
|
if claimedID != "" && recovered.Item.ID != claimedID {
|
|
t.Fatal("same request allocated another task")
|
|
}
|
|
claimedID = recovered.Item.ID
|
|
count := f.request(http.MethodGet, "/api/plugins/annotation-qa/claim-count?projectId="+f.task.ProjectID, f.annotator, nil)
|
|
var pool struct {
|
|
Count int `json:"count"`
|
|
}
|
|
if err := json.Unmarshal(count.Body.Bytes(), &pool); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if count.Code != 200 || pool.Count != 1 {
|
|
t.Fatalf("replay consumed next task: %d %s", count.Code, count.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestContinuousPendingSubmissionReadbackRetainsCurrentTask(t *testing.T) {
|
|
f := newCombinedDraftFixture(t)
|
|
item := f.item
|
|
item.PendingCombinedSubmissionID = fanyuV5CombinedSubmissionID(item.ID, item.Version-1)
|
|
item.PendingCombinedPayloadSHA = "pending-current-task"
|
|
item.ProcessingJob = &domain.AnnotationProcessingJob{ID: item.PendingCombinedSubmissionID, TaskID: f.task.ID, Kind: "submit", Status: "queued"}
|
|
if err := f.store.UpsertHumanItem(f.ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, path := range []string{"/drafts", "/slice-reset", "/annotation-confirmations"} {
|
|
method := http.MethodPost
|
|
if path == "/drafts" {
|
|
method = http.MethodPut
|
|
}
|
|
response := f.request(method, "/api/plugins/annotation-qa/tasks/"+f.task.ID+path, f.annotator, map[string]any{"version": item.Version})
|
|
if response.Code != http.StatusConflict {
|
|
t.Fatalf("pending mutation %s: %d %s", path, response.Code, response.Body.String())
|
|
}
|
|
read := f.request(http.MethodGet, "/api/plugins/annotation-qa/tasks/"+f.task.ID, f.annotator, nil)
|
|
var detail struct {
|
|
HumanItem domain.HumanItemRecord `json:"humanItem"`
|
|
}
|
|
if err := json.Unmarshal(read.Body.Bytes(), &detail); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if read.Code != 200 || detail.HumanItem.PendingCombinedSubmissionID != item.PendingCombinedSubmissionID || detail.HumanItem.CurrentStageID != domain.FanyuV58StageAnnotationCombined || detail.HumanItem.AssignedUserID != f.annotator.ID || detail.HumanItem.Version != item.Version {
|
|
t.Fatalf("processing advanced/lost current task: %d %s", read.Code, read.Body.String())
|
|
}
|
|
}
|
|
}
|