forked from longtao.wu/droboticflow
426 lines
21 KiB
Go
426 lines
21 KiB
Go
package api
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/config"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func captureTestInput(connectionID, bucket, prefix, fingerprint string) map[string]domain.ArtifactPayload {
|
|
return map[string]domain.ArtifactPayload{
|
|
"sourceDirectory": {
|
|
Mode: domain.ArtifactTransferModeRef,
|
|
Type: domain.PortTypeFile,
|
|
Ref: &domain.ArtifactReference{
|
|
Provider: domain.StorageProviderBOS, ConnectionID: connectionID, Bucket: bucket,
|
|
Prefix: prefix, VersionID: fingerprint, Kind: domain.ArtifactRefKindDirectory,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func TestCaptureAutomationCanBindReleasedWorkflowWithoutEditingDefinition(t *testing.T) {
|
|
original := storageConnectionBrowserResolver
|
|
t.Cleanup(func() { storageConnectionBrowserResolver = original })
|
|
storageConnectionBrowserResolver = func(context.Context, *domain.StorageConnectionRecord) (objectStorageBrowser, func(), error) {
|
|
return deliveryProbeBrowser{}, nil, nil
|
|
}
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: domain.DefaultOrganizationID, Name: "Fanyu"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
connection, err := dataStore.CreateStorageConnection(ctx, domain.CreateStorageConnectionInput{
|
|
OrganizationID: domain.DefaultOrganizationID, Name: "Fanyu BOS", Provider: domain.StorageProviderBOS,
|
|
Endpoint: "https://bj.bcebos.com", Bucket: "drobodata-fanyu", AccessKeyID: "ak", SecretAccessKey: "sk",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create storage connection: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, Name: "Fanyu released workflow",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
WorkflowInputs: []domain.WorkflowInputDefinition{{
|
|
ID: "sourceDirectory", Name: "sourceDirectory", Type: domain.PortTypeFile,
|
|
Container: domain.PortContainerDirectory, PayloadMode: domain.ArtifactTransferModeRef, Required: true,
|
|
}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
workflow.DefinitionState = domain.WorkflowDefinitionStateReleased
|
|
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
t.Fatalf("release workflow: %v", err)
|
|
}
|
|
body, _ := json.Marshal(domain.CaptureAutomationConfig{
|
|
DeliveryPrefix: "deliveries",
|
|
Enabled: true, ConnectionID: connection.ID, Bucket: "drobodata-fanyu", Prefix: "incoming",
|
|
Schema: "orbbec_ego_stereo_v1", CompletionMode: "required_files", ManifestName: "source-manifest.json",
|
|
WorkflowInputID: "sourceDirectory", PollIntervalSeconds: 30, MaxRetryAttempts: 3, RetryBackoffSeconds: 45,
|
|
})
|
|
handler := NewHandler(dataStore, nil)
|
|
request := httptest.NewRequest(http.MethodPut, "/api/workflows/"+workflow.ID+"/capture-automation", bytes.NewReader(body))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("PUT capture automation status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
stored, err := dataStore.GetWorkflow(ctx, workflow.ID)
|
|
if err != nil || stored == nil || stored.CaptureAutomation == nil || !stored.CaptureAutomation.Enabled {
|
|
t.Fatalf("capture automation not stored: %#v err=%v", stored, err)
|
|
}
|
|
if stored.DefinitionState != domain.WorkflowDefinitionStateReleased {
|
|
t.Fatalf("operational binding must not unlock workflow definition: %s", stored.DefinitionState)
|
|
}
|
|
if stored.CaptureAutomation.CompletionMode != "required_files" {
|
|
t.Fatalf("unexpected completion mode: %#v", stored.CaptureAutomation)
|
|
}
|
|
if stored.MaxRetryAttempts != 3 || stored.RetryBackoffSeconds != 45 {
|
|
t.Fatalf("capture retry policy not applied to workflow: %#v", stored)
|
|
}
|
|
}
|
|
|
|
func TestCaptureAutomationCountsSeparateFailuresAndCancellations(t *testing.T) {
|
|
counts := captureAutomationCounts([]domain.CaptureRecord{
|
|
{Status: domain.CaptureRecordFailed},
|
|
{Status: domain.CaptureRecordCancelled},
|
|
{Status: domain.CaptureRecordDiscarded},
|
|
})
|
|
if counts.Failed != 1 || counts.Cancelled != 1 || counts.Discarded != 1 {
|
|
t.Fatalf("terminal capture counts were combined: %#v", counts)
|
|
}
|
|
}
|
|
|
|
func TestCaptureAutomationReleasedWorkflowRejectsManualProductionRootRun(t *testing.T) {
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: domain.DefaultOrganizationID, Name: "Fanyu"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
OrganizationID: domain.DefaultOrganizationID,
|
|
ProjectID: project.ID,
|
|
Name: "Capture listener",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
WorkflowInputs: []domain.WorkflowInputDefinition{{
|
|
ID: "sourceDirectory", Name: "sourceDirectory", Type: domain.PortTypeFile,
|
|
Container: domain.PortContainerDirectory, PayloadMode: domain.ArtifactTransferModeRef, Required: true,
|
|
}},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
workflow.DefinitionState = domain.WorkflowDefinitionStateReleased
|
|
workflow.CaptureAutomation = &domain.CaptureAutomationConfig{Enabled: true, WorkflowInputID: "sourceDirectory"}
|
|
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
handler := NewHandler(dataStore, nil)
|
|
body := bytes.NewBufferString(`{"trigger":"manual","mode":"production","inputs":{"sourceDirectory":{"mode":"ref","type":"file","ref":{"provider":"bos","bucket":"drobodata-fanyu","prefix":"test","kind":"directory"}}}}`)
|
|
request := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/run", body)
|
|
request.Header.Set("Content-Type", "application/json")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusNotFound {
|
|
t.Fatalf("manual Capture root run status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
runs, err := dataStore.ListRuns(ctx, "")
|
|
if err != nil || len(runs) != 0 {
|
|
t.Fatalf("manual root request created runs: %#v err=%v", runs, err)
|
|
}
|
|
}
|
|
|
|
func TestCaptureRecordCancelStopsRunCancelsHumanWorkAndKeepsRetryMetadata(t *testing.T) {
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: domain.DefaultOrganizationID, Name: "Fanyu cancel"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, Name: "Capture cancel",
|
|
Type: domain.WorkflowTypeStreaming, MaxRetryAttempts: 2, RetryBackoffSeconds: 60,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
workflow.DefinitionState = domain.WorkflowDefinitionStateReleased
|
|
workflow.CaptureAutomation = &domain.CaptureAutomationConfig{Enabled: true, MaxRetryAttempts: 2, RetryBackoffSeconds: 60}
|
|
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run, err := dataStore.CreateRun(ctx, workflow.ID, domain.RunTriggerScheduled)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run.Status = domain.RunStatusRunning
|
|
run.Attempt = 2
|
|
run.ParentRunID = "run-parent"
|
|
run.AvailableAt = "2026-08-31T00:01:00Z"
|
|
if err := dataStore.UpsertRun(ctx, *run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
record := domain.CaptureRecord{
|
|
ID: "capture-cancel", OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID,
|
|
WorkflowID: workflow.ID, Prefix: "acceptance/cancel", CaptureID: "cancel", Status: domain.CaptureRecordRunning,
|
|
RunID: run.ID, DiscoveredAt: nowISO(), UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertCaptureRecord(ctx, record); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
batch := domain.HumanBatchRecord{
|
|
ID: "batch-cancel", OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, WorkflowID: workflow.ID,
|
|
RunID: run.ID, Status: domain.HumanBatchActive, Stages: domain.DefaultFanyuV58HumanStages(), CreatedAt: nowISO(), UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertHumanBatch(ctx, batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item := domain.HumanItemRecord{
|
|
ID: "item-cancel", OrganizationID: domain.DefaultOrganizationID, BatchID: batch.ID, ProjectID: project.ID,
|
|
WorkflowID: workflow.ID, RunID: run.ID, CurrentStageID: domain.FanyuV58StageAnnotationCombined,
|
|
Status: domain.HumanItemClaimed, AssignedUserID: "annotator", Version: 1, CreatedAt: nowISO(), UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
request := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/capture-automation/records/"+record.ID+"/cancel", bytes.NewBufferString(`{"reason":"用户停止验收任务","mode":"graceful"}`))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("cancel status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
stored, _ := dataStore.GetCaptureRecord(ctx, record.ID)
|
|
if stored == nil || stored.Status != domain.CaptureRecordCancelled || len(stored.Actions) != 1 || stored.Actions[0].Action != domain.CaptureActionCancelled {
|
|
t.Fatalf("capture cancellation not persisted: %#v", stored)
|
|
}
|
|
stopped, _ := dataStore.GetRun(ctx, run.ID)
|
|
if stopped == nil || stopped.Status != domain.RunStatusStopping || stopped.StopMode != domain.RunStopModeGraceful {
|
|
t.Fatalf("run was not gracefully stopped: %#v", stopped)
|
|
}
|
|
storedBatch, _ := dataStore.GetHumanBatch(ctx, batch.ID)
|
|
storedItem, _ := dataStore.GetHumanItem(ctx, item.ID)
|
|
if storedBatch == nil || storedBatch.Status != domain.HumanBatchBlocked || storedItem == nil || storedItem.Status != domain.HumanItemCancelled || storedItem.AssignedUserID != "" {
|
|
t.Fatalf("human work was not cancelled: batch=%#v item=%#v", storedBatch, storedItem)
|
|
}
|
|
|
|
getRequest := httptest.NewRequest(http.MethodGet, "/api/workflows/"+workflow.ID+"/capture-automation", nil)
|
|
getRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(getRecorder, getRequest)
|
|
if getRecorder.Code != http.StatusOK || !strings.Contains(getRecorder.Body.String(), `"attempt":2`) || !strings.Contains(getRecorder.Body.String(), `"maxAttempts":3`) || !strings.Contains(getRecorder.Body.String(), `"parentRunId":"run-parent"`) {
|
|
t.Fatalf("capture retry metadata missing: %d %s", getRecorder.Code, getRecorder.Body.String())
|
|
}
|
|
if strings.Contains(getRecorder.Body.String(), `"nextRetryAt"`) {
|
|
t.Fatalf("a running non-retry Run must not expose nextRetryAt: %s", getRecorder.Body.String())
|
|
}
|
|
|
|
duplicate := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/capture-automation/records/"+record.ID+"/cancel", bytes.NewBufferString(`{"reason":"重复取消","mode":"graceful"}`))
|
|
duplicate.Header.Set("Content-Type", "application/json")
|
|
duplicateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(duplicateRecorder, duplicate)
|
|
if duplicateRecorder.Code != http.StatusConflict {
|
|
t.Fatalf("duplicate cancel status=%d body=%s", duplicateRecorder.Code, duplicateRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCaptureRecordReprocessCreatesAuditedChildRunOnce(t *testing.T) {
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: domain.DefaultOrganizationID, Name: "Fanyu"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, Name: "Capture lifecycle",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
WorkflowInputs: []domain.WorkflowInputDefinition{{
|
|
ID: "sourceDirectory", Name: "sourceDirectory", Type: domain.PortTypeFile,
|
|
Container: domain.PortContainerDirectory, PayloadMode: domain.ArtifactTransferModeRef, Required: true,
|
|
}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
workflow.DefinitionState = domain.WorkflowDefinitionStateReleased
|
|
workflow.CaptureAutomation = &domain.CaptureAutomationConfig{
|
|
Enabled: true, ConnectionID: "storage-fanyu", Bucket: "drobodata-fanyu", Prefix: "incoming",
|
|
CompletionMode: "required_files", WorkflowInputID: "sourceDirectory", PollIntervalSeconds: 30,
|
|
}
|
|
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
t.Fatalf("store workflow: %v", err)
|
|
}
|
|
prefix := "incoming/arbitrary-record"
|
|
fingerprint := "fingerprint-1"
|
|
initial, err := dataStore.CreateRunWithInputs(ctx, workflow.ID, domain.RunTriggerScheduled, captureTestInput("storage-fanyu", "drobodata-fanyu", prefix, fingerprint), "capture-record-1")
|
|
if err != nil {
|
|
t.Fatalf("create initial run: %v", err)
|
|
}
|
|
initial.Status = domain.RunStatusCancelled
|
|
initial.EndedAt = nowISO()
|
|
if err := dataStore.UpsertRun(ctx, *initial); err != nil {
|
|
t.Fatalf("store initial run: %v", err)
|
|
}
|
|
record := domain.CaptureRecord{
|
|
ID: "capture-record-1", OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID,
|
|
WorkflowID: workflow.ID, ConnectionID: "storage-fanyu", Bucket: "drobodata-fanyu",
|
|
Prefix: prefix, CaptureID: "arbitrary-record", Fingerprint: fingerprint,
|
|
Status: domain.CaptureRecordCancelled, RunID: initial.ID, DiscoveredAt: nowISO(), UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertCaptureRecord(ctx, record); err != nil {
|
|
t.Fatalf("store capture: %v", err)
|
|
}
|
|
batch := domain.HumanBatchRecord{
|
|
ID: "batch-cancelled-run", OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID,
|
|
WorkflowID: workflow.ID, RunID: initial.ID, Status: domain.HumanBatchActive,
|
|
Stages: domain.DefaultFanyuV58HumanStages(), CreatedAt: nowISO(), UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertHumanBatch(ctx, batch); err != nil {
|
|
t.Fatalf("store human batch: %v", err)
|
|
}
|
|
humanItem := domain.HumanItemRecord{
|
|
ID: "human-item-cancelled-run", OrganizationID: domain.DefaultOrganizationID, BatchID: batch.ID,
|
|
ProjectID: project.ID, WorkflowID: workflow.ID, RunID: initial.ID, CurrentStageID: domain.FanyuV58StageAnnotationCombined,
|
|
Status: domain.HumanItemClaimed, AssignedUserID: "annotator", Version: 1,
|
|
Subject: domain.HumanSubjectRef{ItemKey: "capture"}, CreatedAt: nowISO(), UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertHumanItem(ctx, humanItem); err != nil {
|
|
t.Fatalf("store human item: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
body := bytes.NewBufferString(`{"reason":"修复配置后重新处理"}`)
|
|
request := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/capture-automation/records/"+record.ID+"/reprocess", body)
|
|
request.Header.Set("Content-Type", "application/json")
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("reprocess status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
stored, err := dataStore.GetCaptureRecord(ctx, record.ID)
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("get capture: %#v err=%v", stored, err)
|
|
}
|
|
if stored.Status != domain.CaptureRecordQueued || stored.RunID == initial.ID || len(stored.Actions) != 2 {
|
|
t.Fatalf("capture was not requeued with audit history: %#v", stored)
|
|
}
|
|
retryRun, err := dataStore.GetRun(ctx, stored.RunID)
|
|
if err != nil || retryRun == nil {
|
|
t.Fatalf("get retry run: %#v err=%v", retryRun, err)
|
|
}
|
|
if retryRun.ParentRunID != initial.ID || retryRun.Attempt != 2 || retryRun.TaskID != record.ID || retryRun.Trigger != domain.RunTriggerRetry {
|
|
t.Fatalf("unexpected retry lineage: %#v", retryRun)
|
|
}
|
|
storedBatch, _ := dataStore.GetHumanBatch(ctx, batch.ID)
|
|
storedHumanItem, _ := dataStore.GetHumanItem(ctx, humanItem.ID)
|
|
if storedBatch == nil || storedBatch.Status != domain.HumanBatchBlocked {
|
|
t.Fatalf("old human batch was not blocked: %#v", storedBatch)
|
|
}
|
|
if storedHumanItem == nil || storedHumanItem.Status != domain.HumanItemCancelled || storedHumanItem.AssignedUserID != "" || storedHumanItem.CancellationReason == "" {
|
|
t.Fatalf("old human item was not cancelled and released: %#v", storedHumanItem)
|
|
}
|
|
duplicateRequest := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/capture-automation/records/"+record.ID+"/reprocess", bytes.NewBufferString(`{"reason":"重复点击"}`))
|
|
duplicateRequest.Header.Set("Content-Type", "application/json")
|
|
duplicateRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(duplicateRecorder, duplicateRequest)
|
|
if duplicateRecorder.Code != http.StatusConflict {
|
|
t.Fatalf("duplicate reprocess status=%d body=%s", duplicateRecorder.Code, duplicateRecorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestCaptureRecordDiscardAndRestorePreserveAuditAndPreviousStatus(t *testing.T) {
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
project, err := dataStore.CreateProject(ctx, domain.CreateProjectInput{OrganizationID: domain.DefaultOrganizationID, Name: "Fanyu"})
|
|
if err != nil {
|
|
t.Fatalf("create project: %v", err)
|
|
}
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID, Name: "Capture lifecycle",
|
|
Type: domain.WorkflowTypeStreaming,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
workflow.DefinitionState = domain.WorkflowDefinitionStateReleased
|
|
workflow.CaptureAutomation = &domain.CaptureAutomationConfig{Enabled: true}
|
|
if err := dataStore.UpsertWorkflow(ctx, *workflow); err != nil {
|
|
t.Fatalf("store workflow: %v", err)
|
|
}
|
|
record := domain.CaptureRecord{
|
|
ID: "capture-record-discard", OrganizationID: domain.DefaultOrganizationID, ProjectID: project.ID,
|
|
WorkflowID: workflow.ID, Prefix: "incoming/bad-record", CaptureID: "bad-record",
|
|
Status: domain.CaptureRecordIncomplete, DiscoveredAt: nowISO(), UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertCaptureRecord(ctx, record); err != nil {
|
|
t.Fatalf("store capture: %v", err)
|
|
}
|
|
handler := NewHandler(dataStore, nil)
|
|
discardRequest := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/capture-automation/records/"+record.ID+"/discard", bytes.NewBufferString(`{"reason":"源数据确认不可用"}`))
|
|
discardRequest.Header.Set("Content-Type", "application/json")
|
|
discardRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(discardRecorder, discardRequest)
|
|
if discardRecorder.Code != http.StatusOK {
|
|
t.Fatalf("discard status=%d body=%s", discardRecorder.Code, discardRecorder.Body.String())
|
|
}
|
|
stored, _ := dataStore.GetCaptureRecord(ctx, record.ID)
|
|
if stored == nil || stored.Status != domain.CaptureRecordDiscarded || stored.StatusBeforeDiscard != domain.CaptureRecordIncomplete || len(stored.Actions) != 1 {
|
|
t.Fatalf("capture was not discarded with audit: %#v", stored)
|
|
}
|
|
restoreRequest := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/capture-automation/records/"+record.ID+"/restore", bytes.NewBufferString(`{"reason":"复核后允许继续上传"}`))
|
|
restoreRequest.Header.Set("Content-Type", "application/json")
|
|
restoreRecorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(restoreRecorder, restoreRequest)
|
|
if restoreRecorder.Code != http.StatusOK {
|
|
t.Fatalf("restore status=%d body=%s", restoreRecorder.Code, restoreRecorder.Body.String())
|
|
}
|
|
stored, _ = dataStore.GetCaptureRecord(ctx, record.ID)
|
|
if stored == nil || stored.Status != domain.CaptureRecordIncomplete || stored.StatusBeforeDiscard != "" || len(stored.Actions) != 2 {
|
|
t.Fatalf("capture was not restored: %#v", stored)
|
|
}
|
|
}
|
|
|
|
func TestCaptureLifecycleMutationRejectsAnnotationRole(t *testing.T) {
|
|
ctx := t.Context()
|
|
dataStore := store.NewMemoryStore()
|
|
workflow, err := dataStore.CreateWorkflow(ctx, domain.CreateWorkflowInput{
|
|
OrganizationID: domain.DefaultOrganizationID, Name: "Capture lifecycle", Type: domain.WorkflowTypeStreaming,
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create workflow: %v", err)
|
|
}
|
|
record := domain.CaptureRecord{
|
|
ID: "capture-private", OrganizationID: domain.DefaultOrganizationID, WorkflowID: workflow.ID,
|
|
Prefix: "incoming/private", CaptureID: "private", Status: domain.CaptureRecordIncomplete,
|
|
DiscoveredAt: nowISO(), UpdatedAt: nowISO(),
|
|
}
|
|
if err := dataStore.UpsertCaptureRecord(ctx, record); err != nil {
|
|
t.Fatalf("store capture: %v", err)
|
|
}
|
|
handler := NewHandlerWithConfig(dataStore, nil, config.AppConfig{RequireSessionAuth: true})
|
|
request := httptest.NewRequest(http.MethodPost, "/api/workflows/"+workflow.ID+"/capture-automation/records/"+record.ID+"/discard", bytes.NewBufferString(`{"reason":"unauthorized"}`))
|
|
request.AddCookie(&http.Cookie{Name: csrfCookieName, Value: "csrf"})
|
|
request.Header.Set(csrfHeaderName, "csrf")
|
|
request.Header.Set("Content-Type", "application/json")
|
|
request = request.WithContext(context.WithValue(request.Context(), authContextKey{}, &authenticatedPrincipal{
|
|
User: domain.UserRecord{ID: "annotator", OrganizationID: domain.DefaultOrganizationID, Role: domain.UserRoleAnnotator},
|
|
Organization: domain.OrganizationRecord{ID: domain.DefaultOrganizationID, Status: domain.OrganizationStatusActive},
|
|
}))
|
|
recorder := httptest.NewRecorder()
|
|
handler.ServeHTTP(recorder, request)
|
|
if recorder.Code != http.StatusForbidden {
|
|
t.Fatalf("annotator lifecycle mutation status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|