forked from longtao.wu/droboticflow
313 lines
13 KiB
Go
313 lines
13 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
// slow-page report S4: GET /api/human-batches must stay a read-only path —
|
|
// no flushHumanReleasePartitions writes — while the background sweeper still
|
|
// releases wait-expired batches the write paths no longer cover.
|
|
func newHumanBatchesFlushServer(t *testing.T) (*server, *store.MemoryStore, domain.HumanBatchRecord) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
dataStore := store.NewMemoryStore()
|
|
organization := domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Name: "Default Organization", Slug: "default", Status: domain.OrganizationStatusActive}
|
|
if err := dataStore.UpsertOrganization(ctx, organization); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
admin := domain.UserRecord{ID: "admin-1", Name: "admin", Role: domain.UserRoleAdmin, Status: domain.UserStatusActive, OrganizationID: organization.ID}
|
|
if err := dataStore.UpsertUser(ctx, admin); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
batch := domain.HumanBatchRecord{
|
|
ID: "batch-sweep", OrganizationID: organization.ID, PluginID: "annotation-qa", ProjectID: "project-1", WorkflowID: "workflow-1", RunID: "run-1", GateNodeID: "gate-1",
|
|
ReleasePolicy: domain.HumanReleaseAcceptedPartition, PartitionMaxItems: 10, PartitionMaxWaitSeconds: 1, Status: domain.HumanBatchActive,
|
|
}
|
|
if err := dataStore.UpsertHumanBatch(ctx, batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Accepted long enough ago that the PartitionMaxWaitSeconds window has
|
|
// expired — the exact case the old GET-path flush used to release.
|
|
item := domain.HumanItemRecord{
|
|
ID: "item-sweep-1", OrganizationID: organization.ID, PluginID: "annotation-qa", BatchID: batch.ID,
|
|
ProjectID: batch.ProjectID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, GateNodeID: batch.GateNodeID,
|
|
Status: domain.HumanItemAccepted, AcceptedAt: nowISO(), Version: 1,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "clip-1"},
|
|
}
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return &server{store: dataStore}, dataStore, batch
|
|
}
|
|
|
|
func TestHumanBatchesListDoesNotFlushReleasePartitions(t *testing.T) {
|
|
ctx := context.Background()
|
|
server, dataStore, batch := newHumanBatchesFlushServer(t)
|
|
organization, err := dataStore.GetOrganization(ctx, domain.DefaultOrganizationID)
|
|
if err != nil || organization == nil {
|
|
t.Fatalf("organization=%#v err=%v", organization, err)
|
|
}
|
|
admin, err := dataStore.GetUser(ctx, "admin-1")
|
|
if err != nil || admin == nil {
|
|
t.Fatalf("admin=%#v err=%v", admin, err)
|
|
}
|
|
principal := &authenticatedPrincipal{User: *admin, Organization: *organization}
|
|
request := httptest.NewRequest(http.MethodGet, "/api/human-batches", nil).WithContext(
|
|
context.WithValue(ctx, authContextKey{}, principal),
|
|
)
|
|
response := httptest.NewRecorder()
|
|
server.handleHumanBatchesList(response, request)
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("status=%d body=%s", response.Code, response.Body.String())
|
|
}
|
|
partitions, err := dataStore.ListReleasePartitions(ctx, batch.OrganizationID, batch.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(partitions) != 0 {
|
|
t.Fatalf("GET must not create release partitions, got %#v", partitions)
|
|
}
|
|
stored, err := dataStore.GetHumanItem(ctx, "item-sweep-1")
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("item=%#v err=%v", stored, err)
|
|
}
|
|
if stored.ReleasePartitionID != "" || stored.Version != 1 {
|
|
t.Fatalf("GET must not write items: partition=%q version=%d", stored.ReleasePartitionID, stored.Version)
|
|
}
|
|
}
|
|
|
|
func TestHumanReleaseSweeperFlushesWaitExpiredBatches(t *testing.T) {
|
|
ctx := context.Background()
|
|
server, dataStore, batch := newHumanBatchesFlushServer(t)
|
|
ids, err := dataStore.ListHumanBatchIDsAwaitingRelease(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(ids) != 1 || ids[0] != batch.ID {
|
|
t.Fatalf("awaiting ids=%#v", ids)
|
|
}
|
|
server.sweepHumanReleasePartitions()
|
|
partitions, err := dataStore.ListReleasePartitions(ctx, batch.OrganizationID, batch.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(partitions) != 1 || len(partitions[0].ItemIDs) != 1 {
|
|
t.Fatalf("sweeper must release the wait-expired batch: %#v", partitions)
|
|
}
|
|
stored, err := dataStore.GetHumanItem(ctx, "item-sweep-1")
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("item=%#v err=%v", stored, err)
|
|
}
|
|
if stored.ReleasePartitionID == "" {
|
|
t.Fatal("sweeper must assign the item's release partition")
|
|
}
|
|
// Nothing left awaiting: the sweeper's next pass is a no-op.
|
|
ids, err = dataStore.ListHumanBatchIDsAwaitingRelease(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(ids) != 0 {
|
|
t.Fatalf("no batch should remain awaiting, got %#v", ids)
|
|
}
|
|
}
|
|
|
|
func TestHumanReleaseSweeperSkipsAggregateAndUnconfirmedPrePackageBatches(t *testing.T) {
|
|
ctx := context.Background()
|
|
server, dataStore, waitBatch := newHumanBatchesFlushServer(t)
|
|
|
|
// 1. Aggregate pre-package acceptance batch (has SourceBatchID and single prepackage_acceptance stage).
|
|
aggregateBatch := domain.HumanBatchRecord{
|
|
ID: fanyuV5PrePackageAcceptanceBatchID("batch-source-1"), OrganizationID: waitBatch.OrganizationID,
|
|
PluginID: "annotation-qa", ProjectID: waitBatch.ProjectID, WorkflowID: waitBatch.WorkflowID, RunID: waitBatch.RunID,
|
|
GateNodeID: "gate-1-prepackage-acceptance", SourceBatchID: "batch-source-1",
|
|
Status: domain.HumanBatchActive, Stages: []domain.HumanStageDefinition{{ID: fanyuV5PrePackageAcceptanceStageID}},
|
|
}
|
|
if err := dataStore.UpsertHumanBatch(ctx, aggregateBatch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertHumanItem(ctx, domain.HumanItemRecord{
|
|
ID: fanyuV5PrePackageAcceptanceItemID("batch-source-1"), OrganizationID: waitBatch.OrganizationID,
|
|
PluginID: "annotation-qa", BatchID: aggregateBatch.ID, ProjectID: waitBatch.ProjectID,
|
|
WorkflowID: waitBatch.WorkflowID, RunID: waitBatch.RunID, GateNodeID: aggregateBatch.GateNodeID,
|
|
Status: domain.HumanItemAccepted, AcceptedAt: "2026-01-01T00:00:00Z", Version: 1,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// 2. Source batch requiring pre-package acceptance that still has unconfirmed acceptance item.
|
|
unconfirmedBatch := domain.HumanBatchRecord{
|
|
ID: "batch-unconfirmed", OrganizationID: waitBatch.OrganizationID, PluginID: "annotation-qa",
|
|
ProjectID: waitBatch.ProjectID, WorkflowID: waitBatch.WorkflowID, RunID: waitBatch.RunID, GateNodeID: "gate-unconfirmed",
|
|
ReleasePolicy: domain.HumanReleaseAllAccepted, RequirePrePackageAcceptance: true, Status: domain.HumanBatchActive,
|
|
}
|
|
if err := dataStore.UpsertHumanBatch(ctx, unconfirmedBatch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertHumanItem(ctx, domain.HumanItemRecord{
|
|
ID: "item-unconfirmed-1", OrganizationID: waitBatch.OrganizationID, PluginID: "annotation-qa",
|
|
BatchID: unconfirmedBatch.ID, ProjectID: waitBatch.ProjectID, WorkflowID: waitBatch.WorkflowID, RunID: waitBatch.RunID,
|
|
GateNodeID: unconfirmedBatch.GateNodeID, Status: domain.HumanItemAccepted, AcceptedAt: "2026-01-01T00:00:00Z", Version: 1,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertHumanItem(ctx, domain.HumanItemRecord{
|
|
ID: fanyuV5PrePackageAcceptanceItemID(unconfirmedBatch.ID), OrganizationID: waitBatch.OrganizationID, PluginID: "annotation-qa",
|
|
BatchID: fanyuV5PrePackageAcceptanceBatchID(unconfirmedBatch.ID), ProjectID: waitBatch.ProjectID,
|
|
WorkflowID: waitBatch.WorkflowID, RunID: waitBatch.RunID, GateNodeID: unconfirmedBatch.GateNodeID + "-prepackage-acceptance",
|
|
Subject: domain.HumanSubjectRef{
|
|
ItemKey: unconfirmedBatch.ID,
|
|
Payload: map[string]any{
|
|
"items": []map[string]any{
|
|
{
|
|
"finalSlices": []map[string]any{{"id": "slice-1", "start": 0, "end": 10}},
|
|
"materialization": map[string]any{"artifactId": "artifact-materialized-1"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
CurrentStageID: fanyuV5PrePackageAcceptanceStageID, Status: domain.HumanItemReady, Version: 1,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
ids, err := dataStore.ListHumanBatchIDsAwaitingRelease(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, id := range ids {
|
|
if id == aggregateBatch.ID {
|
|
t.Fatalf("aggregate pre-package acceptance batch must not be listed as awaiting release: %#v", ids)
|
|
}
|
|
}
|
|
|
|
server.sweepHumanReleasePartitions()
|
|
|
|
aggPartitions, err := dataStore.ListReleasePartitions(ctx, waitBatch.OrganizationID, aggregateBatch.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(aggPartitions) != 0 {
|
|
t.Fatalf("aggregate batch must not produce release partitions, got %#v", aggPartitions)
|
|
}
|
|
unconfirmedPartitions, err := dataStore.ListReleasePartitions(ctx, waitBatch.OrganizationID, unconfirmedBatch.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(unconfirmedPartitions) != 0 {
|
|
t.Fatalf("unconfirmed pre-package batch must not produce release partitions, got %#v", unconfirmedPartitions)
|
|
}
|
|
waitPartitions, err := dataStore.ListReleasePartitions(ctx, waitBatch.OrganizationID, waitBatch.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(waitPartitions) != 1 {
|
|
t.Fatalf("wait-expired batch must still release, got %#v", waitPartitions)
|
|
}
|
|
}
|
|
|
|
func TestHumanReleaseSweeperRecoversMissingPrePackageAcceptanceItem(t *testing.T) {
|
|
ctx := context.Background()
|
|
server, dataStore, waitBatch := newHumanBatchesFlushServer(t)
|
|
now := nowISO()
|
|
|
|
sourceBatch := domain.HumanBatchRecord{
|
|
ID: "batch-missing-acceptance", OrganizationID: waitBatch.OrganizationID, PluginID: "annotation-qa",
|
|
ProjectID: waitBatch.ProjectID, WorkflowID: waitBatch.WorkflowID, RunID: waitBatch.RunID, GateNodeID: "gate-missing-acceptance",
|
|
Stages: domain.DefaultFanyuV58HumanStages(), ReleasePolicy: domain.HumanReleaseAllAccepted,
|
|
RequirePrePackageAcceptance: true, Status: domain.HumanBatchActive, CreatedAt: now, UpdatedAt: now,
|
|
}
|
|
if err := dataStore.UpsertHumanBatch(ctx, sourceBatch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task := domain.AnnotationTaskRecord{
|
|
ID: "task-missing-acceptance", OrganizationID: waitBatch.OrganizationID, ProjectID: sourceBatch.ProjectID,
|
|
WorkflowID: sourceBatch.WorkflowID, RunID: sourceBatch.RunID, NodeID: sourceBatch.GateNodeID,
|
|
ArtifactID: "artifact-missing-acceptance", SourceVideo: "bos://fixture/video.mp4",
|
|
ClipStart: 0, ClipEnd: 20, TemplateID: "template-1", TemplateVersion: 6,
|
|
Status: domain.AnnotationTaskStatusAccepted, CurrentRevision: 1, CreatedAt: now, UpdatedAt: now,
|
|
}
|
|
if err := dataStore.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{
|
|
ID: "slice-missing-acceptance", OrganizationID: waitBatch.OrganizationID, TaskID: task.ID,
|
|
Revision: 1, Segments: []domain.SliceSegment{{ID: "slice-1", Start: 0, End: 20}}, AuthorUserID: "annotator-1", CreatedAt: now,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{
|
|
ID: "revision-missing-acceptance", OrganizationID: waitBatch.OrganizationID, TaskID: task.ID,
|
|
Revision: 1, TemplateVersion: 6, Segments: []domain.AnnotationSegment{{ID: "seg-1", LabelID: "01", Label: "测试", Start: 0, End: 20}},
|
|
AuthorUserID: "annotator-1", CreatedAt: now,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertHumanItem(ctx, domain.HumanItemRecord{
|
|
ID: "item-missing-acceptance-1", OrganizationID: waitBatch.OrganizationID, PluginID: "annotation-qa",
|
|
BatchID: sourceBatch.ID, ProjectID: sourceBatch.ProjectID, WorkflowID: sourceBatch.WorkflowID,
|
|
RunID: sourceBatch.RunID, GateNodeID: sourceBatch.GateNodeID, LegacyTaskID: task.ID,
|
|
Subject: domain.HumanSubjectRef{
|
|
ArtifactID: task.ArtifactID,
|
|
ItemKey: "clip-1",
|
|
Payload: map[string]any{
|
|
"materialization": map[string]any{"artifactId": "artifact-materialized-1"},
|
|
},
|
|
},
|
|
CurrentStageID: domain.FanyuV58StageQualityReview, Status: domain.HumanItemAccepted,
|
|
CurrentRevision: 1, ReviewRound: 1, Version: 2, AcceptedAt: now, CreatedAt: now, UpdatedAt: now,
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// Simulate crash/timeout after source item was accepted but before the
|
|
// aggregate pre-package acceptance batch/item was created.
|
|
server.sweepHumanReleasePartitions()
|
|
|
|
acceptanceBatch, err := dataStore.GetHumanBatch(ctx, fanyuV5PrePackageAcceptanceBatchID(sourceBatch.ID))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if acceptanceBatch == nil {
|
|
t.Fatal("sweeper must recover and create the missing pre-package acceptance batch")
|
|
}
|
|
acceptanceItem, err := dataStore.GetHumanItem(ctx, fanyuV5PrePackageAcceptanceItemID(sourceBatch.ID))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if acceptanceItem == nil || acceptanceItem.Status != domain.HumanItemReady {
|
|
t.Fatalf("sweeper must recover and create the missing pre-package acceptance item in ready state, got %#v", acceptanceItem)
|
|
}
|
|
partitions, err := dataStore.ListReleasePartitions(ctx, waitBatch.OrganizationID, sourceBatch.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(partitions) != 0 {
|
|
t.Fatalf("source batch must still wait for pre-package acceptance approval before releasing, got %#v", partitions)
|
|
}
|
|
|
|
// Once the recovered pre-package acceptance item is accepted, the next sweep
|
|
// must flush the source batch's release partition.
|
|
acceptanceItem.Status = domain.HumanItemAccepted
|
|
acceptanceItem.AcceptedAt = now
|
|
acceptanceItem.Version++
|
|
if err := dataStore.UpsertHumanItem(ctx, *acceptanceItem); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
server.sweepHumanReleasePartitions()
|
|
|
|
partitions, err = dataStore.ListReleasePartitions(ctx, waitBatch.OrganizationID, sourceBatch.ID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(partitions) != 1 {
|
|
t.Fatalf("source batch must release after recovered pre-package acceptance item is accepted, got %#v", partitions)
|
|
}
|
|
}
|