forked from longtao.wu/droboticflow
372 lines
23 KiB
Go
372 lines
23 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestFanyuCaptureClaimBindsAnnotatorSupplierAtomically(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
organization := adminCreateOrganization(t, handler, rootCookie, "Capture supplier", "capture-supplier")
|
|
if err := dataStore.UpsertSupplier(ctx, domain.SupplierRecord{ID: "supplier-capture-a", OrganizationID: organization.ID, Name: "Supplier A", Status: "active"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
annotator := adminCreateUser(t, handler, rootCookie, organization.ID, "Annotator", "capture-annotator@example.com", domain.UserRoleAnnotator, "annotator-password", "supplier-capture-a")
|
|
annotatorCookie := loginSession(t, handler, annotator.Phone, "annotator-password")
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: organization.ID, Name: "Capture supplier project"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
supplier := domain.SupplierRecord{ID: "supplier-capture-a", OrganizationID: organization.ID, Name: "Supplier A", Status: "active"}
|
|
if err := dataStore.UpsertSupplier(ctx, supplier); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ID: "member-capture-a", OrganizationID: organization.ID, ProjectID: project.ID, UserID: annotator.ID, SupplierID: supplier.ID, Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
batch := domain.HumanBatchRecord{ID: "batch-capture-claim", OrganizationID: organization.ID, PluginID: "annotation-qa", ProjectID: project.ID, WorkflowID: "workflow-capture-claim", RunID: "run-capture-claim", Stages: domain.DefaultFanyuV58HumanStages()}
|
|
if err := dataStore.UpsertHumanBatch(ctx, batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
task := domain.AnnotationTaskRecord{ID: "task-capture-claim", OrganizationID: organization.ID, ProjectID: project.ID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, TemplateID: "template", TemplateVersion: 6, Status: domain.AnnotationTaskStatusPendingAnnotation}
|
|
if err := dataStore.UpsertAnnotationTask(ctx, task); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
completeFixtureSourceValidation(t, combinedDraftFixture{ctx: ctx, store: dataStore}, task)
|
|
item := domain.HumanItemRecord{ID: "item-capture-claim", OrganizationID: organization.ID, PluginID: "annotation-qa", BatchID: batch.ID, ProjectID: project.ID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, LegacyTaskID: task.ID, CurrentStageID: domain.FanyuV58StageAnnotationCombined, Status: domain.HumanItemReady, Version: 1}
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
claimBody, _ := json.Marshal(map[string]any{"projectId": project.ID, "requestId": "supplier-claim-request-01"})
|
|
claimed := serveJSONWithCookie(handler, http.MethodPost, "/api/plugins/annotation-qa/random-claims", claimBody, annotatorCookie)
|
|
if claimed.Code != http.StatusOK {
|
|
t.Fatalf("claim=%d body=%s", claimed.Code, claimed.Body.String())
|
|
}
|
|
storedItem, _ := dataStore.GetHumanItem(ctx, item.ID)
|
|
storedTask, _ := dataStore.GetAnnotationTask(ctx, task.ID)
|
|
storedBatch, _ := dataStore.GetHumanBatch(ctx, batch.ID)
|
|
if storedItem == nil || storedTask == nil || storedBatch == nil || storedItem.SupplierID != supplier.ID || storedTask.SupplierID != supplier.ID || storedBatch.SupplierID != supplier.ID {
|
|
t.Fatalf("claim did not bind supplier item=%#v task=%#v batch=%#v", storedItem, storedTask, storedBatch)
|
|
}
|
|
}
|
|
|
|
func TestFanyuQualityRejectionRequiresProblemSlice(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
organization := adminCreateOrganization(t, handler, rootCookie, "Quality slices", "quality-slices")
|
|
annotator := adminCreateUser(t, handler, rootCookie, organization.ID, "Annotator", "quality-annotator@example.com", domain.UserRoleAnnotator, "annotator-password")
|
|
reviewer := adminCreateUser(t, handler, rootCookie, organization.ID, "Reviewer", "quality-reviewer@example.com", domain.UserRoleQualityReviewer, "reviewer-password")
|
|
reviewerCookie := loginSession(t, handler, reviewer.Phone, "reviewer-password")
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: organization.ID, Name: "Quality project"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
supplier := domain.SupplierRecord{ID: "supplier-quality", OrganizationID: organization.ID, Name: "Quality supplier", Status: "active"}
|
|
_ = dataStore.UpsertSupplier(ctx, supplier)
|
|
for _, member := range []domain.ProjectMemberRecord{
|
|
{ID: "member-annotator", OrganizationID: organization.ID, ProjectID: project.ID, UserID: annotator.ID, SupplierID: supplier.ID, Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator}},
|
|
{ID: "member-reviewer", OrganizationID: organization.ID, ProjectID: project.ID, UserID: reviewer.ID, SupplierID: supplier.ID, Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleQualityReviewer}},
|
|
} {
|
|
if err := dataStore.UpsertProjectMember(ctx, member); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
batch := domain.HumanBatchRecord{ID: "batch-quality-slices", OrganizationID: organization.ID, PluginID: "annotation-qa", ProjectID: project.ID, SupplierID: supplier.ID, WorkflowID: "workflow-quality-slices", RunID: "run-quality-slices", Stages: domain.DefaultFanyuV58HumanStages(), RequirePrePackageAcceptance: true, MaxReworkRounds: 5}
|
|
_ = dataStore.UpsertHumanBatch(ctx, batch)
|
|
task := domain.AnnotationTaskRecord{ID: "task-quality-slices", OrganizationID: organization.ID, ProjectID: project.ID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, TemplateID: "template", TemplateVersion: 6, SupplierID: supplier.ID, Status: domain.AnnotationTaskStatusPendingQuality, CurrentRevision: 1}
|
|
_ = dataStore.UpsertAnnotationTask(ctx, task)
|
|
_ = dataStore.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: "revision-quality-slices", OrganizationID: organization.ID, TaskID: task.ID, Revision: 1, TemplateVersion: 6, AuthorUserID: annotator.ID})
|
|
item := domain.HumanItemRecord{ID: "item-quality-slices", OrganizationID: organization.ID, PluginID: "annotation-qa", BatchID: batch.ID, ProjectID: project.ID, SupplierID: supplier.ID, WorkflowID: batch.WorkflowID, RunID: batch.RunID, LegacyTaskID: task.ID, CurrentStageID: domain.FanyuV58StageQualityReview, Status: domain.HumanItemReady, CurrentRevision: 1, Version: 1, MaxReworkRounds: 5, LatestAuthorUserID: annotator.ID}
|
|
_ = dataStore.UpsertHumanItem(ctx, item)
|
|
claimed := serveJSONWithCookie(handler, http.MethodPost, "/api/human-items/"+item.ID+"/claim", []byte(`{"version":1}`), reviewerCookie)
|
|
if claimed.Code != http.StatusOK {
|
|
t.Fatalf("claim=%d body=%s", claimed.Code, claimed.Body.String())
|
|
}
|
|
rejectWithoutSlice := serveJSONWithCookie(handler, http.MethodPost, "/api/plugins/annotation-qa/tasks/"+task.ID+"/reviews", []byte(`{"stage":"quality","decision":"rejected","comment":"边界错误","reasonCodes":["boundary"]}`), reviewerCookie)
|
|
if rejectWithoutSlice.Code != http.StatusBadRequest {
|
|
t.Fatalf("missing slice=%d body=%s", rejectWithoutSlice.Code, rejectWithoutSlice.Body.String())
|
|
}
|
|
reject := serveJSONWithCookie(handler, http.MethodPost, "/api/plugins/annotation-qa/tasks/"+task.ID+"/reviews", []byte(`{"stage":"quality","decision":"rejected","comment":"边界错误","reasonCodes":["boundary"],"problemSliceIds":["slice-000002"]}`), reviewerCookie)
|
|
if reject.Code != http.StatusCreated {
|
|
t.Fatalf("reject=%d body=%s", reject.Code, reject.Body.String())
|
|
}
|
|
rejects, _ := dataStore.ListHumanRejects(ctx, organization.ID, domain.HumanRejectQuery{ItemID: item.ID})
|
|
if len(rejects) != 1 || len(rejects[0].ProblemSliceIDs) != 1 || rejects[0].ProblemSliceIDs[0] != "slice-000002" {
|
|
t.Fatalf("problem slice audit=%#v", rejects)
|
|
}
|
|
}
|
|
|
|
func TestWorkflowRuntimePolicyDefaultsUpdatesAndKeepsHistory(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
organization := adminCreateOrganization(t, handler, rootCookie, "Runtime policy", "runtime-policy")
|
|
admin := adminCreateUser(t, handler, rootCookie, organization.ID, "Runtime Admin", "runtime-admin@example.com", domain.UserRoleAdmin, "runtime-password")
|
|
adminCookie := loginSession(t, handler, admin.Phone, "runtime-password")
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: organization.ID, Name: "runtime policy"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{OrganizationID: organization.ID, ProjectID: project.ID, Name: "capture flow"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
getRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/workflows/"+workflow.ID+"/runtime-policy", nil, adminCookie)
|
|
if getRecorder.Code != http.StatusOK || !jsonContains(getRecorder.Body.String(), `"maxConcurrentCaptureRuns":2`) || !jsonContains(getRecorder.Body.String(), `"acceptanceSampleProbability":0.5`) {
|
|
t.Fatalf("default policy=%d body=%s", getRecorder.Code, getRecorder.Body.String())
|
|
}
|
|
body, _ := json.Marshal(map[string]any{"maxConcurrentCaptureRuns": 3, "acceptanceSampleProbability": 0.25})
|
|
patchRecorder := serveJSONWithCookie(handler, http.MethodPatch, "/api/workflows/"+workflow.ID+"/runtime-policy", body, adminCookie)
|
|
if patchRecorder.Code != http.StatusOK || !jsonContains(patchRecorder.Body.String(), `"revision":1`) {
|
|
t.Fatalf("patch policy=%d body=%s", patchRecorder.Code, patchRecorder.Body.String())
|
|
}
|
|
historyRecorder := serveJSONWithCookie(handler, http.MethodGet, "/api/workflows/"+workflow.ID+"/runtime-policy/history", nil, adminCookie)
|
|
if historyRecorder.Code != http.StatusOK || !jsonContains(historyRecorder.Body.String(), `"maxConcurrentCaptureRuns":3`) || !jsonContains(historyRecorder.Body.String(), `"acceptanceSampleProbability":0.25`) {
|
|
t.Fatalf("policy history=%d body=%s", historyRecorder.Code, historyRecorder.Body.String())
|
|
}
|
|
stale := serveJSONWithCookie(handler, http.MethodPatch, "/api/workflows/"+workflow.ID+"/runtime-policy", []byte(`{"revision":0,"acceptanceSampleProbability":1}`), adminCookie)
|
|
if stale.Code != http.StatusConflict {
|
|
t.Fatalf("stale save=%d", stale.Code)
|
|
}
|
|
_ = dataStore.UpsertSupplier(ctx, domain.SupplierRecord{ID: "supplier-a", OrganizationID: organization.ID, Status: "active"})
|
|
manager := adminCreateUser(t, handler, rootCookie, organization.ID, "Supplier Manager", "runtime-supplier@example.com", domain.UserRoleAnnotator, "supplier-password", "supplier-a")
|
|
storedManager, _ := dataStore.GetUser(ctx, manager.ID)
|
|
storedManager.Role = domain.UserRoleSupplierManager
|
|
storedManager.SupplierID = "supplier-a"
|
|
if err := dataStore.UpsertUser(ctx, *storedManager); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ID: "member-a", OrganizationID: organization.ID, ProjectID: project.ID, UserID: "production-a", SupplierID: "supplier-a", Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleAnnotator}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
managerCookie := loginSession(t, handler, manager.Phone, "supplier-password")
|
|
readOnly := serveJSONWithCookie(handler, http.MethodGet, "/api/workflows/"+workflow.ID+"/runtime-policy", nil, managerCookie)
|
|
if readOnly.Code != http.StatusOK {
|
|
t.Fatalf("supplier read=%d %s", readOnly.Code, readOnly.Body.String())
|
|
}
|
|
writeDenied := serveJSONWithCookie(handler, http.MethodPatch, "/api/workflows/"+workflow.ID+"/runtime-policy", body, managerCookie)
|
|
if writeDenied.Code != http.StatusForbidden {
|
|
t.Fatalf("supplier write=%d", writeDenied.Code)
|
|
}
|
|
_ = dataStore.UpsertSupplier(ctx, domain.SupplierRecord{ID: "supplier-b", OrganizationID: organization.ID, Status: "active"})
|
|
otherManager := adminCreateUser(t, handler, rootCookie, organization.ID, "Other manager", "runtime-other@example.com", domain.UserRoleSupplierManager, "supplier-password", "supplier-b")
|
|
otherCookie := loginSession(t, handler, otherManager.Phone, "supplier-password")
|
|
unrelated := serveJSONWithCookie(handler, http.MethodGet, "/api/workflows/"+workflow.ID+"/runtime-policy", nil, otherCookie)
|
|
if unrelated.Code != http.StatusForbidden {
|
|
t.Fatalf("unrelated supplier read=%d", unrelated.Code)
|
|
}
|
|
}
|
|
|
|
func TestOrganizationLabelDictionaryAndProjectSelection(t *testing.T) {
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
handler := NewHandler(dataStore, nil)
|
|
rootCookie := bootstrapSuperAdminSession(t, handler)
|
|
organization := adminCreateOrganization(t, handler, rootCookie, "Labels", "labels")
|
|
admin := adminCreateUser(t, handler, rootCookie, organization.ID, "Label Admin", "label-admin@example.com", domain.UserRoleAdmin, "label-password")
|
|
adminCookie := loginSession(t, handler, admin.Phone, "label-password")
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: organization.ID, Name: "Label project"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
created := serveJSONWithCookie(handler, http.MethodPost, "/api/annotation-labels", []byte(`{"code":"pick","name":"拿取","category":"动作","status":"active"}`), adminCookie)
|
|
if created.Code != http.StatusCreated {
|
|
t.Fatalf("create label=%d body=%s", created.Code, created.Body.String())
|
|
}
|
|
var label domain.AnnotationLabelRecord
|
|
if err := json.Unmarshal(created.Body.Bytes(), &label); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if label.Version != 1 || len(label.History) != 1 {
|
|
t.Fatalf("created label has no first immutable revision: %#v", label)
|
|
}
|
|
patched := serveJSONWithCookie(handler, http.MethodPatch, "/api/annotation-labels/"+label.ID, []byte(`{"code":"pick","name":"拿取物品","category":"动作","description":"拿起目标物","example":"右手拿起水瓶","status":"active"}`), adminCookie)
|
|
if patched.Code != http.StatusOK {
|
|
t.Fatalf("patch label=%d body=%s", patched.Code, patched.Body.String())
|
|
}
|
|
if err := json.Unmarshal(patched.Body.Bytes(), &label); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if label.Version != 2 || len(label.History) != 2 || label.History[1].Example != "右手拿起水瓶" {
|
|
t.Fatalf("label revision history=%#v", label)
|
|
}
|
|
selectedBody, _ := json.Marshal(map[string]any{"labelIds": []string{label.ID}})
|
|
selected := serveJSONWithCookie(handler, http.MethodPut, "/api/projects/"+project.ID+"/annotation-labels", selectedBody, adminCookie)
|
|
if selected.Code != http.StatusOK || !jsonContains(selected.Body.String(), label.ID) {
|
|
t.Fatalf("select label=%d body=%s", selected.Code, selected.Body.String())
|
|
}
|
|
templateBody, _ := json.Marshal(map[string]any{
|
|
"projectId": project.ID, "name": "Global labels only", "version": 1,
|
|
"labels": []map[string]any{{"id": "hijack", "name": "项目自定义含义"}},
|
|
"prelabel": map[string]any{"enabled": false},
|
|
})
|
|
template := serveJSONWithCookie(handler, http.MethodPost, "/api/plugins/annotation-qa/templates", templateBody, adminCookie)
|
|
if template.Code != http.StatusCreated || !jsonContains(template.Body.String(), `"id":"pick"`) || jsonContains(template.Body.String(), "hijack") {
|
|
t.Fatalf("template labels=%d body=%s", template.Code, template.Body.String())
|
|
}
|
|
duplicate := serveJSONWithCookie(handler, http.MethodPost, "/api/annotation-labels", []byte(`{"code":"pick","name":"重复","status":"active"}`), adminCookie)
|
|
if duplicate.Code != http.StatusConflict {
|
|
t.Fatalf("duplicate label=%d body=%s", duplicate.Code, duplicate.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCaptureSamplingDecisionIsPersistedOnceAndUsesLatestPolicy(t *testing.T) {
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
server := &server{store: dataStore}
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: domain.DefaultOrganizationID, Name: "sampling"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, Name: "capture sampling"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
policy := domain.WorkflowRuntimePolicyRecord{
|
|
ID: "workflow-runtime-policy-" + workflow.ID, OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: project.ID, WorkflowID: workflow.ID, Revision: 1, MaxConcurrentCaptureRuns: 2,
|
|
AcceptanceSampleProbability: 0,
|
|
}
|
|
if updated, err := dataStore.UpdateWorkflowRuntimePolicyVersioned(ctx, policy, 0); err != nil || !updated {
|
|
t.Fatalf("seed policy updated=%v err=%v", updated, err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerScheduled, "capture-record-1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
capture := domain.CaptureRecord{ID: "capture-record-1", OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, WorkflowID: workflow.ID, RunID: run.ID, Status: domain.CaptureRecordWaiting}
|
|
if err := dataStore.UpsertCaptureRecord(ctx, capture); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
batch := domain.HumanBatchRecord{ID: "source-batch", OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, WorkflowID: workflow.ID, RunID: run.ID, Stages: domain.DefaultFanyuV58HumanStages()}
|
|
decision, err := server.ensureCaptureSamplingDecision(ctx, &batch)
|
|
if err != nil || decision == nil || decision.Decision != domain.CaptureSamplingNotSelected || decision.Probability != 0 {
|
|
t.Fatalf("first decision=%#v err=%v", decision, err)
|
|
}
|
|
policy.Revision = 2
|
|
policy.AcceptanceSampleProbability = 1
|
|
if updated, err := dataStore.UpdateWorkflowRuntimePolicyVersioned(ctx, policy, 1); err != nil || !updated {
|
|
t.Fatalf("update policy updated=%v err=%v", updated, err)
|
|
}
|
|
again, err := server.ensureCaptureSamplingDecision(ctx, &batch)
|
|
if err != nil || again == nil || again.Decision != domain.CaptureSamplingNotSelected || again.PolicyRevision != 1 {
|
|
t.Fatalf("idempotent decision=%#v err=%v", again, err)
|
|
}
|
|
}
|
|
|
|
func TestSubmissionFreezesAcceptancePolicyBeforeLaterSettingsChange(t *testing.T) {
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
s := &server{store: dataStore}
|
|
policy := domain.WorkflowRuntimePolicyRecord{ID: "p", WorkflowID: "wf", OrganizationID: domain.DefaultOrganizationID, Revision: 1, AcceptanceSampleProbability: 0, UpdatedAt: "2026-09-06T01:00:00Z", History: []domain.WorkflowRuntimePolicyRevision{{Revision: 1, AcceptanceSampleProbability: 0, UpdatedAt: "2026-09-06T01:00:00Z"}}}
|
|
if ok, err := dataStore.UpdateWorkflowRuntimePolicyVersioned(ctx, policy, 0); err != nil || !ok {
|
|
t.Fatal(err)
|
|
}
|
|
item := domain.HumanItemRecord{ID: "i", WorkflowID: "wf", BatchID: "b", OrganizationID: domain.DefaultOrganizationID, SubmittedAt: "2026-09-06T01:30:00Z"}
|
|
if err := s.freezeSubmissionAcceptancePolicy(ctx, &item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
policy.Revision = 2
|
|
policy.AcceptanceSampleProbability = 1
|
|
policy.UpdatedAt = "2026-09-06T02:00:00Z"
|
|
policy.History = append(policy.History, domain.WorkflowRuntimePolicyRevision{Revision: 2, AcceptanceSampleProbability: 1, UpdatedAt: policy.UpdatedAt})
|
|
if ok, err := dataStore.UpdateWorkflowRuntimePolicyVersioned(ctx, policy, 1); err != nil || !ok {
|
|
t.Fatal(err)
|
|
}
|
|
if err := dataStore.UpsertCaptureRecord(ctx, domain.CaptureRecord{ID: "capture", RunID: "run", OrganizationID: domain.DefaultOrganizationID}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
batch := domain.HumanBatchRecord{ID: "b", WorkflowID: "wf", RunID: "run", OrganizationID: domain.DefaultOrganizationID, Stages: domain.DefaultFanyuV58HumanStages()}
|
|
decision, err := s.ensureCaptureSamplingDecision(ctx, &batch)
|
|
if err != nil || decision.Probability != 0 || decision.PolicyRevision != 1 {
|
|
t.Fatalf("old submission changed: %#v %v", decision, err)
|
|
}
|
|
newItem := domain.HumanItemRecord{WorkflowID: "wf", SubmittedAt: "2026-09-06T02:30:00Z"}
|
|
if err := s.freezeSubmissionAcceptancePolicy(ctx, &newItem); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if *newItem.AcceptanceSampleProbability != 1 || newItem.AcceptancePolicyRevision != 2 {
|
|
t.Fatal("new submission did not use new policy")
|
|
}
|
|
probability, revision := acceptancePolicyAt(&policy, item.SubmittedAt)
|
|
if probability != 0 || revision != 1 {
|
|
t.Fatal("legacy submitted item did not use policy history")
|
|
}
|
|
if err := s.freezeSubmissionAcceptancePolicy(ctx, &item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if *item.AcceptanceSampleProbability != 0 || item.AcceptancePolicyRevision != 1 {
|
|
t.Fatal("resubmission changed an existing draw")
|
|
}
|
|
}
|
|
|
|
func TestManualSamplingUsesFrozenPerCaptureResultAndKeepsHistoricalItems(t *testing.T) {
|
|
zero, one, half, value := 0.0, 1.0, 0.5, 0.7
|
|
items := []domain.HumanItemRecord{
|
|
{ID: "excluded", AcceptanceSampleProbability: &zero, AcceptanceSampleValue: &one},
|
|
{ID: "included", AcceptanceSampleProbability: &one, AcceptanceSampleValue: &zero},
|
|
{ID: "not-selected", AcceptanceSampleProbability: &half, AcceptanceSampleValue: &value},
|
|
{ID: "historical"},
|
|
}
|
|
selected := sampledManualAcceptanceItems(items)
|
|
if len(selected) != 2 || selected[0].ID != "included" || selected[1].ID != "historical" {
|
|
t.Fatalf("selected=%#v", selected)
|
|
}
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
s := &server{store: dataStore}
|
|
batch := domain.HumanBatchRecord{ID: "manual-batch", OrganizationID: domain.DefaultOrganizationID, RunID: "manual-run", Stages: domain.DefaultFanyuV58HumanStages(), RequirePrePackageAcceptance: true}
|
|
item := items[0]
|
|
item.OrganizationID = domain.DefaultOrganizationID
|
|
item.BatchID = batch.ID
|
|
item.Status = domain.HumanItemAccepted
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ready, err := s.ensureFanyuV5PrePackageAcceptance(ctx, &batch)
|
|
if err != nil || !ready {
|
|
t.Fatalf("unsampled manual Capture did not proceed after QC: %v %v", ready, err)
|
|
}
|
|
acceptance, _ := dataStore.GetHumanBatch(ctx, fanyuV5PrePackageAcceptanceBatchID(batch.ID))
|
|
if acceptance != nil {
|
|
t.Fatal("0% manual Capture created an acceptance task")
|
|
}
|
|
}
|
|
|
|
func TestGenericCaptureSubmissionAlsoFreezesSampling(t *testing.T) {
|
|
f := newFanyuV5FourStageFixture(t, domain.FanyuV58StageAnnotationCombined, domain.HumanItemClaimed, "annotator-v5", 0)
|
|
f.batch.Stages = domain.DefaultFanyuV58HumanStages()
|
|
if err := f.store.UpsertHumanBatch(f.ctx, f.batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
policy := domain.WorkflowRuntimePolicyRecord{ID: "p", OrganizationID: domain.DefaultOrganizationID, WorkflowID: f.item.WorkflowID, Revision: 1, AcceptanceSampleProbability: 0, UpdatedAt: nowISO()}
|
|
if ok, err := f.store.UpdateWorkflowRuntimePolicyVersioned(f.ctx, policy, 0); err != nil || !ok {
|
|
t.Fatal(err)
|
|
}
|
|
response := f.request(http.MethodPost, "/api/human-items/"+f.item.ID+"/submissions", f.annotator, map[string]any{"version": f.item.Version, "revision": 1, "submissionRef": "ref-1"})
|
|
if response.Code != http.StatusOK {
|
|
t.Fatalf("submit=%d %s", response.Code, response.Body.String())
|
|
}
|
|
item, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if item.AcceptanceSampleProbability == nil || *item.AcceptanceSampleProbability != 0 || item.AcceptanceSampleValue == nil || *item.AcceptanceSampleValue != 1 {
|
|
t.Fatalf("snapshot=%#v", item)
|
|
}
|
|
}
|