forked from longtao.wu/droboticflow
152 lines
5.6 KiB
Go
152 lines
5.6 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"crypto/sha256"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func TestAnnotationBufferAndClaimExposeCaptureNameBeforeTaskIsOpened(t *testing.T) {
|
|
f := randomClaimFixture(t, 1)
|
|
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
|
|
if err := f.store.UpsertHumanBatch(f.ctx, *batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const name = "FYLX_S15_AZEC86400BP_19700103_161946/part0011"
|
|
capture := domain.CaptureRecord{ID: "display-capture", OrganizationID: f.item.OrganizationID, ProjectID: f.task.ProjectID, CaptureID: name}
|
|
if err := f.store.UpsertCaptureRecord(f.ctx, capture); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run := domain.RunRecord{ID: "display-run", OrganizationID: f.item.OrganizationID, ProjectID: f.task.ProjectID, TaskID: capture.ID}
|
|
if err := f.store.UpsertRun(f.ctx, run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range []string{f.item.ID, "random-item-0"} {
|
|
item, _ := f.store.GetHumanItem(f.ctx, id)
|
|
item.RunID = run.ID
|
|
item.Subject.ItemKey = "run-opaque-artifact-source-left.mp4"
|
|
if err := f.store.UpsertHumanItem(f.ctx, *item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
check := func(item domain.HumanItemRecord) {
|
|
t.Helper()
|
|
if item.Display == nil || len(item.Display.CaptureDirectories) != 1 || item.Display.CaptureDirectories[0] != name {
|
|
t.Errorf("capture name missing before detail hydration: item=%s display=%+v", item.ID, item.Display)
|
|
}
|
|
}
|
|
r := f.request(http.MethodGet, "/api/plugins/annotation-qa/work-buffer?projectId="+f.task.ProjectID, f.annotator, nil)
|
|
var buffer struct {
|
|
Items []domain.HumanItemRecord `json:"items"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &buffer); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Code != 200 || len(buffer.Items) != 1 {
|
|
t.Fatalf("buffer %d %s", r.Code, r.Body.String())
|
|
}
|
|
check(buffer.Items[0])
|
|
r = f.request(http.MethodPost, "/api/plugins/annotation-qa/random-claims", f.annotator, map[string]any{"projectId": f.task.ProjectID, "requestId": "capture-name-before-detail"})
|
|
var claim struct {
|
|
Item domain.HumanItemRecord `json:"item"`
|
|
}
|
|
if err := json.Unmarshal(r.Body.Bytes(), &claim); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Code != 200 {
|
|
t.Fatalf("claim %d %s", r.Code, r.Body.String())
|
|
}
|
|
check(claim.Item)
|
|
r = f.request(http.MethodGet, "/api/plugins/annotation-qa/work-buffer?projectId="+f.task.ProjectID, f.annotator, nil)
|
|
if err := json.Unmarshal(r.Body.Bytes(), &buffer); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Code != 200 || len(buffer.Items) != 2 {
|
|
t.Fatalf("refreshed buffer %d %s", r.Code, r.Body.String())
|
|
}
|
|
for _, item := range buffer.Items {
|
|
check(item)
|
|
}
|
|
}
|
|
|
|
func TestAnnotationBufferSummaryIsExplicitAndKeepsStatus(t *testing.T) {
|
|
f := randomClaimFixture(t, 1)
|
|
batch, _ := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
|
|
if err := f.store.UpsertHumanBatch(f.ctx, *batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if item.Subject.Payload == nil {
|
|
item.Subject.Payload = map[string]any{}
|
|
}
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "completed", "phase": "annotate"}
|
|
// Varied deterministic values avoid measuring only the compression of a
|
|
// repeated string. Keep the same task count in both response modes.
|
|
fixture := make([]map[string]any, 256)
|
|
for i := range fixture {
|
|
fixture[i] = map[string]any{"id": fmt.Sprintf("slice-%d", i), "digest": fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("fixture-%d", i)))), "startFrame": i * 30, "endFrame": (i + 1) * 30}
|
|
}
|
|
item.Subject.Payload["largeFixture"] = fixture
|
|
if err := f.store.UpsertHumanItem(f.ctx, *item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
base := "/api/plugins/annotation-qa/work-buffer?projectId=" + f.task.ProjectID
|
|
compact := f.request(http.MethodGet, base+"&view=summary", f.annotator, nil)
|
|
old := f.request(http.MethodGet, base, f.annotator, nil)
|
|
if old.Code != 200 || compact.Code != 200 {
|
|
t.Fatalf("responses %d %d", old.Code, compact.Code)
|
|
}
|
|
var legacy, summary map[string]any
|
|
if err := json.Unmarshal(old.Body.Bytes(), &legacy); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := json.Unmarshal(compact.Body.Bytes(), &summary); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ok := legacy["items"]; !ok {
|
|
t.Fatal("legacy items missing")
|
|
}
|
|
if _, ok := summary["items"]; ok {
|
|
t.Fatal("summary must not masquerade as items")
|
|
}
|
|
rows, ok := summary["summaries"].([]any)
|
|
if !ok || len(rows) != 1 {
|
|
t.Fatalf("unexpected summaries: %v", summary)
|
|
}
|
|
row := rows[0].(map[string]any)
|
|
if row["id"] != item.ID || row["sliceStatus"] != "completed" || row["slicePhase"] != "annotate" {
|
|
t.Fatalf("summary lost state: %v", row)
|
|
}
|
|
if len(legacy["items"].([]any)) != len(rows) || legacy["occupied"] != summary["occupied"] || legacy["capacity"] != summary["capacity"] {
|
|
t.Fatal("summary changed buffer membership or capacity")
|
|
}
|
|
compressedSize := func(body []byte) int {
|
|
t.Helper()
|
|
var out bytes.Buffer
|
|
writer := gzip.NewWriter(&out)
|
|
if _, err := writer.Write(body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := writer.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return out.Len()
|
|
}
|
|
oldGzip, compactGzip := compressedSize(old.Body.Bytes()), compressedSize(compact.Body.Bytes())
|
|
t.Logf("buffer bytes raw=%d/%d gzip=%d/%d (legacy/summary)", old.Body.Len(), compact.Body.Len(), oldGzip, compactGzip)
|
|
if compactGzip*2 > oldGzip {
|
|
t.Fatalf("summary gzip reduction below 50%%: %d vs %d", compactGzip, oldGzip)
|
|
}
|
|
if compact.Body.Len()*2 >= old.Body.Len() {
|
|
t.Fatalf("summary failed to halve fixture bytes: %d vs %d", compact.Body.Len(), old.Body.Len())
|
|
}
|
|
}
|