droboticflow/internal/api/slice_disposition_live_test.go
eust-w 106235bb02 ✨ Remove Email field, complete phone-only account identity (ticket 05)
Contract step of the account-phone-login expand-migrate-contract rollout.
Phone is now the sole account identifier end to end:

- domain.UserRecord/CreateUserInput: delete Email, drop Phone's omitempty
  (required going forward).
- store: delete email_unique Mongo index; phone_unique is now non-sparse
  (unique, not sparse) since every account creation path requires a phone.
  Delete FindUserByEmail, ensureUserEmailAvailableLocked, and
  ErrDuplicateUserEmail (confirmed dead via grep - all real call sites were
  already migrated to the *Phone equivalents by tickets 01-03). Mongo
  duplicate-key fallback and the account-mutation-guard's stale-write
  snapshot/filter now key off Phone instead of Email.
- api: drop the now-impossible ErrDuplicateUserEmail branch in
  renderUserMutationError. The account-hierarchy audit endpoint and the
  supplier-deletion receipt struct both switch their identifying field from
  Email to Phone, since both are populated fresh from current account state
  (not frozen historical snapshots) - continuing to read a "email" key would
  silently go blank for every account created after this change.
- frontend: src/types.ts User.phone is now required (email removed);
  AccountHierarchyIssue.email -> .phone in services/api/users.ts and
  pages/Users.tsx to match the backend audit endpoint's renamed field.
- Rewrote the wide blast radius of Go test fixtures that constructed users
  by email across internal/api/*_test.go and internal/store/*_test.go,
  including the two dedicated memory-store uniqueness tests (merged into
  phone-only equivalents) and several opt-in "live" acceptance-fixture
  tests gated behind env vars for real Fanyu deployments.

Repo-wide grep of Go and TS/TSX for Email/email/邮箱 turned up no remaining
account-identifier references outside of a few harmless test-only cosmetic
labels (e.g. "Email Tenant" as an org display name in a seed helper) and
assertions that explicitly check "邮箱" text is absent from API responses.

go build/vet clean. go test ./internal/api/... ./internal/store/...
./internal/domain/... : 75 failures, identical test names to the
pre-ticket-05 baseline (diffed against a throwaway worktree at the prior
tip). npm run lint:web (tsc --noEmit) clean. npm run test:frontend:
599/586/13, matching the stated baseline exactly.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 14:30:50 +08:00

242 lines
15 KiB
Go

package api
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// Explicit deployment acceptance fixture, never part of ordinary test runs.
// Seeds only synthetic input plus the human-stage starting point. The actual
// public API performs annotation/review/acceptance, and Worker executes packaging.
// No existing employee, workflow, capture or input data is overwritten.
func TestSeedSliceDiscardFanyu(t *testing.T) {
if os.Getenv("FANYU_DISCARD_SEED") != "1" {
t.Skip("explicit Fanyu acceptance seeding only")
}
nonce, mode := os.Getenv("FANYU_DISCARD_NONCE"), os.Getenv("FANYU_DISCARD_CASE")
if !regexp.MustCompile(`^2026[0-9a-z-]{6,32}$`).MatchString(nonce) {
t.Fatal("invalid scoped verification nonce")
}
if mode != "keep" && mode != "mixed" && mode != "discard" && mode != "disable-users" {
t.Fatal("invalid verification case")
}
org := os.Getenv("FANYU_DISCARD_ORG")
if org != "org-29cd7a684faba19f630f8ad5890e18bb" {
t.Fatal("explicit Fanyu organization required")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
client, err := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI")))
if err != nil {
t.Fatal("connect acceptance store")
}
defer client.Disconnect(ctx)
s := store.NewMongoStore(client.Database(os.Getenv("MONGO_DB")))
projectNonce := nonce
if shared := os.Getenv("FANYU_DISCARD_PROJECT_NONCE"); shared != "" {
if !regexp.MustCompile(`^2026[0-9a-z-]{6,32}$`).MatchString(shared) {
t.Fatal("invalid shared project nonce")
}
projectNonce = shared
}
projectID, supplierID := "project-discard-verify-"+projectNonce, "supplier-discard-verify-"+nonce
roles := []domain.UserRole{domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer}
users := map[string]domain.UserRecord{}
password := os.Getenv("FANYU_DISCARD_PASSWORD")
if len(password) < 20 {
t.Fatal("ephemeral test password required")
}
must := func(e error) {
t.Helper()
if e != nil {
t.Fatal(e)
}
}
project, err := s.GetProject(ctx, projectID)
must(err)
if project != nil && (project.OrganizationID != org || project.Description != "Isolated slice-discard acceptance fixture; no intake listener") {
t.Fatal("fixture project scope mismatch")
}
if mode == "disable-users" {
if project == nil {
t.Fatal("fixture project not found")
}
for _, role := range roles {
u, e := s.GetUser(ctx, "user-discard-verify-"+nonce+"-"+string(role))
must(e)
if u == nil || u.OrganizationID != org || u.SupplierID != supplierID {
t.Fatal("test account identity mismatch")
}
u.Status = domain.UserStatusDisabled
must(s.UpsertUser(ctx, *u))
}
if label, e := s.GetAnnotationLabel(ctx, "slice-label-discard-verify-"+nonce); e == nil && label != nil {
label.Status = domain.AnnotationLabelInactive
must(s.UpsertAnnotationLabel(ctx, *label))
}
fmt.Println("Disabled only the three scoped acceptance test accounts")
return
}
if project == nil {
must(s.UpsertProject(ctx, domain.ProjectRecord{ID: projectID, OrganizationID: org, Name: "舍弃片段验收-" + nonce, Description: "Isolated slice-discard acceptance fixture; no intake listener", Status: domain.ProjectStatusActive, CreatedAt: nowISO()}))
must(s.SyncDefaultSupplierProjectMembers(ctx, org, projectID, ""))
}
if supplier, e := s.GetSupplier(ctx, supplierID); e != nil {
must(e)
} else if supplier == nil {
must(s.UpsertSupplier(ctx, domain.SupplierRecord{ID: supplierID, OrganizationID: org, Name: "舍弃验收专用供应商-" + nonce, Status: "active", CreatedAt: nowISO()}))
} else if supplier.OrganizationID != org {
t.Fatal("supplier scope mismatch")
}
for _, role := range roles {
id := "user-discard-verify-" + nonce + "-" + string(role)
u, e := s.GetUser(ctx, id)
must(e)
if u == nil {
hash, e := hashPassword(password)
must(e)
value := domain.UserRecord{ID: id, OrganizationID: org, Name: "舍弃验收-" + string(role), Phone: testPhoneFromSeed("discard-" + nonce + "-" + string(role)), Role: role, SupplierID: supplierID, PasswordHash: hash, Status: domain.UserStatusActive, CreatedAt: nowISO()}
must(s.UpsertUser(ctx, value))
u = &value
} else if u.OrganizationID != org || u.SupplierID != supplierID || u.Role != role {
t.Fatal("test user scope mismatch")
}
users[string(role)] = *u
must(s.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ID: "member-discard-" + nonce + "-" + string(role), OrganizationID: org, ProjectID: projectID, UserID: id, SupplierID: supplierID, Roles: []domain.AnnotationAssignmentRole{domain.AnnotationAssignmentRole(role)}}))
}
root := filepath.Join("/workspace/system/slice-discard-verification", nonce)
for _, name := range []string{"capture_left.mp4", "capture_right.mp4", "capture_left_pts.csv", "capture_right_pts.csv", "capture_imu.csv"} {
if info, e := os.Stat(filepath.Join(root, "fixtures", name)); e != nil || info.Size() == 0 {
t.Fatal("upload the synthetic fixture before seeding")
}
}
for _, kind := range []string{"camera", "imu"} {
must(os.WriteFile(filepath.Join(root, "fixtures", "calibration_"+kind+".yaml"), []byte(kind+": synthetic-verification-fixture\n"), 0644))
}
base, e := s.GetTemplate(ctx, rawHumanTemplatePrefix+org)
must(e)
if base == nil {
t.Fatal("Fanyu raw-human template missing")
}
workflowID, runID, taskID, batchID, itemID := "workflow-discard-verify-"+nonce+"-"+mode, "run-discard-verify-"+nonce+"-"+mode, "task-discard-verify-"+nonce+"-"+mode, "batch-discard-verify-"+nonce+"-"+mode, "item-discard-verify-"+nonce+"-"+mode
attachMedia := func() {
h, e := s.GetHumanItem(ctx, itemID)
must(e)
task, e := s.GetAnnotationTask(ctx, taskID)
must(e)
if h == nil || task == nil || h.ProjectID != projectID || task.ProjectID != projectID || h.OrganizationID != org || task.OrganizationID != org {
t.Fatal("fixture media scope mismatch")
}
h.Subject.ArtifactID = runID + "-capture_left.mp4"
h.Subject.Payload["rightArtifactId"] = runID + "-capture_right.mp4"
v := h.Version
h.Version++
updated, e := s.UpdateHumanItemVersioned(ctx, *h, v)
must(e)
if !updated {
t.Fatal("fixture changed during media repair")
}
task.RightArtifactID = runID + "-capture_right.mp4"
task.RightSourceVideo = "/api/runs/" + runID + "/artifacts/" + task.RightArtifactID + "/content"
task.ProcessingProfile = domain.ProcessingProfileRawHuman
must(s.UpsertAnnotationTask(ctx, *task))
}
if existing, e := s.GetRun(ctx, runID); e != nil {
must(e)
} else if existing != nil {
if os.Getenv("FANYU_DISCARD_REPAIR_MEDIA") == "1" {
attachMedia()
fmt.Println("Repaired only synthetic fixture video references")
return
}
t.Fatal("verification case already exists; will not overwrite it")
}
nodes := []domain.WorkflowNode{}
nodeIDs := map[string]bool{}
for _, node := range base.Nodes {
if node.ID == "fanyu-v56-directory" {
continue
}
if node.ID == "fanyu-v56-normalize" {
node.Data.Label = "固定验收输入(预置,未运行前置算子)"
node.Data.Inputs = nil
}
nodes = append(nodes, node)
nodeIDs[node.ID] = true
}
edges := []domain.WorkflowEdge{}
for _, edge := range base.Edges {
if nodeIDs[edge.Source] && nodeIDs[edge.Target] {
edges = append(edges, edge)
}
}
if len(nodes) != 3 {
t.Fatal("unexpected raw-human node contract")
}
must(s.UpsertWorkflow(ctx, domain.WorkflowRecord{ID: workflowID, OrganizationID: org, ProjectID: projectID, Name: "舍弃闭环验收-" + mode, Description: "Synthetic 60s input, verify human loop and actual package job only", Type: domain.WorkflowTypeBatch, ExecutionScope: domain.WorkflowExecutionScopeProduction, DefinitionState: domain.WorkflowDefinitionStateReleased, DefinitionVersion: 1, DefinitionFamilyID: workflowID, MaxConcurrentRuns: 1, MaxRetryAttempts: 0, NodeTimeoutSeconds: 600, Nodes: nodes, Edges: edges, Status: domain.WorkflowStatusRunning, CreatedAt: nowISO(), UpdatedAt: nowISO()}))
_, err = s.UpdateWorkflowRuntimePolicyVersioned(ctx, domain.WorkflowRuntimePolicyRecord{ID: "workflow-runtime-policy-" + workflowID, OrganizationID: org, ProjectID: projectID, WorkflowID: workflowID, Revision: 1, MaxConcurrentCaptureRuns: 1, AcceptanceSampleProbability: 1}, 0)
must(err)
templateID := "annotation-template-discard-" + nonce
must(s.UpsertAnnotationTemplate(ctx, domain.AnnotationTemplateRecord{ID: templateID, OrganizationID: org, ProjectID: projectID, Name: "舍弃验收描述字典-" + nonce, Version: 6, Status: domain.AnnotationTemplatePublished, Labels: domain.ConvenienceStore22SemanticLabels(), HumanStages: domain.DefaultFanyuV58HumanStages()}))
dictionaryID := "slice-label-discard-verify-" + nonce
must(s.UpsertAnnotationLabel(ctx, domain.AnnotationLabelRecord{ID: dictionaryID, OrganizationID: org, Kind: "slice", Code: dictionaryID, Name: "规则验收-" + nonce, Status: domain.AnnotationLabelActive, Version: 1, CreatedByUserID: "acceptance-fixture", CreatedAt: nowISO()}))
must(s.UpsertProjectSliceLabels(ctx, domain.ProjectAnnotationLabelsRecord{ID: "project-annotation-labels-" + projectID, ProjectID: projectID, OrganizationID: org, SliceLabelIDs: []string{dictionaryID}, UpdatedAt: nowISO()}))
artifacts := []domain.ExecutionArtifact{}
bindings := []domain.NodeInputBinding{}
for _, spec := range []struct {
name, port, target string
typ domain.PortType
}{{"capture_left.mp4", "leftVideos", "fullLeftVideos", domain.PortTypeVideo}, {"capture_right.mp4", "rightVideos", "fullRightVideos", domain.PortTypeVideo}, {"capture_left_pts.csv", "leftTimestamps", "leftTimestamps", domain.PortTypeFile}, {"capture_right_pts.csv", "rightTimestamps", "rightTimestamps", domain.PortTypeFile}, {"capture_imu.csv", "imu", "imu", domain.PortTypeFile}, {"calibration_camera.yaml", "calibrations", "", domain.PortTypeFile}, {"calibration_imu.yaml", "calibrations", "", domain.PortTypeFile}} {
path := filepath.Join(root, "fixtures", spec.name)
info, e := os.Stat(path)
must(e)
a := domain.ExecutionArtifact{ID: runID + "-" + spec.name, RunID: runID, NodeID: "fanyu-v56-normalize", Name: spec.name, Type: spec.typ, PortID: spec.port, PortName: spec.port, Path: path, SizeBytes: info.Size(), Data: map[string]any{"collectionId": "capture", "fixture": true}}
artifacts = append(artifacts, a)
if spec.target != "" {
bindings = append(bindings, domain.NodeInputBinding{TargetPortID: spec.target, SourceNodeID: "fanyu-v56-normalize", SourcePortID: spec.port, Artifacts: []domain.ExecutionArtifact{a}})
}
}
leftID := runID + "-capture_left.mp4"
run := domain.RunRecord{ID: runID, OrganizationID: org, ProjectID: projectID, ProjectName: "舍弃片段验收-" + nonce, WorkflowID: workflowID, WorkflowName: "舍弃闭环验收-" + mode, ProcessingProfile: domain.ProcessingProfileRawHuman, TaskID: "capture-discard-verify-" + nonce + "-" + mode, Mode: domain.RunModeProduction, Trigger: domain.RunTriggerManual, Attempt: 1, Status: domain.RunStatusWaitingForHuman, CurrentNodeID: "fanyu-v56-human", StartedAt: nowISO(), UpdatedAt: nowISO(), Artifacts: artifacts, NodeExecutions: []domain.NodeExecutionRecord{{NodeID: "fanyu-v56-normalize", NodeLabel: "固定验收输入(预置)", Status: domain.NodeExecutionStatusCompleted, Artifacts: artifacts, EndedAt: nowISO()}, {NodeID: "fanyu-v56-human", NodeLabel: "人工切片、质检、验收", Status: domain.NodeExecutionStatusWaitingForHuman, InputArtifacts: artifacts[:5], InputBindings: bindings, StartedAt: nowISO()}, {NodeID: "fanyu-v56-package", NodeLabel: "真实打包验证", Status: domain.NodeExecutionStatusQueued}}}
must(s.UpsertRun(ctx, run))
must(s.UpsertHumanBatch(ctx, domain.HumanBatchRecord{ID: batchID, OrganizationID: org, PluginID: "annotation-qa", ProjectID: projectID, SupplierID: supplierID, WorkflowID: workflowID, RunID: runID, GateNodeID: "fanyu-v56-human", ProcessingProfile: domain.ProcessingProfileRawHuman, Status: domain.HumanBatchActive, Stages: domain.DefaultFanyuV58HumanStages(), RequirePrePackageAcceptance: true, ReleasePolicy: domain.HumanReleaseAllAccepted, ItemCount: 1, MaxReworkRounds: 5, CreatedAt: nowISO()}))
one, zero := 1.0, 0.0
must(s.UpsertHumanItem(ctx, domain.HumanItemRecord{ID: itemID, OrganizationID: org, PluginID: "annotation-qa", ProjectID: projectID, SupplierID: supplierID, BatchID: batchID, WorkflowID: workflowID, RunID: runID, GateNodeID: "fanyu-v56-human", LegacyTaskID: taskID, ProcessingProfile: domain.ProcessingProfileRawHuman, CurrentStageID: domain.FanyuV58StageAnnotationCombined, Status: domain.HumanItemReady, Version: 1, AcceptanceSampleProbability: &one, AcceptanceSampleValue: &zero, AcceptancePolicyRevision: 1, Subject: domain.HumanSubjectRef{ItemKey: "capture-" + mode, Payload: map[string]any{"captureDirectory": "capture-" + mode, "sliceWorkflow": map[string]any{"status": "editing", "phase": "slice"}}}}))
must(s.UpsertAnnotationTask(ctx, domain.AnnotationTaskRecord{ID: taskID, OrganizationID: org, ProjectID: projectID, WorkflowID: workflowID, RunID: runID, NodeID: "fanyu-v56-human", ArtifactID: leftID, SourceVideo: "/api/runs/" + runID + "/artifacts/" + leftID + "/content", SourceIsFullVideo: true, SourceDurationSeconds: 60, ClipStart: 0, ClipEnd: 60, CoverageStartSecond: 0, CoverageEndSecond: 60, CoverageMode: "full_common_range", TemplateID: templateID, TemplateVersion: 6, LabelSnapshot: domain.ConvenienceStore22SemanticLabels(), SupplierID: supplierID, Status: domain.AnnotationTaskStatusPendingAnnotation, CreatedAt: nowISO()}))
attachMedia()
connections, e := s.ListStorageConnectionsByOrganization(ctx, org)
must(e)
connectionID := ""
for _, c := range connections {
if c.Provider == domain.StorageProviderBOS && c.Bucket == "drobodata-fanyu" {
connectionID = c.ID
break
}
}
if connectionID == "" {
t.Fatal("Fanyu BOS connection unavailable")
}
deliveryPrefix := "acceptance-tests/slice-discard-" + nonce + "/Discard-QA__" + projectID + "/capture-" + mode
must(s.UpsertCaptureRecord(ctx, domain.CaptureRecord{ID: run.TaskID, OrganizationID: org, ProjectID: projectID, WorkflowID: workflowID, ConnectionID: connectionID, Bucket: "drobodata-fanyu", Prefix: "synthetic-slice-discard-" + nonce + "/capture-" + mode, CaptureID: "capture-" + mode, RunID: runID, Status: domain.CaptureRecordWaiting, BusinessStage: domain.CaptureStageWaitingAnnotation, Delivery: &domain.CaptureDeliveryTarget{ConnectionID: connectionID, Bucket: "drobodata-fanyu", Prefix: deliveryPrefix}, SamplingDecision: &domain.CaptureSamplingDecision{Decision: domain.CaptureSamplingSelected, Probability: 1, PolicyRevision: 1, RandomValue: 0, DecidedAt: nowISO(), SourceHumanBatchID: batchID}, DiscoveredAt: nowISO()}))
phones := map[string]string{}
for role, user := range users {
phones[role] = user.Phone
}
state := map[string]any{"projectId": projectID, "workflowId": workflowID, "runId": runID, "taskId": taskID, "humanItemId": itemID, "batchId": batchID, "case": mode, "phones": phones, "deliveryPrefix": deliveryPrefix, "connectionId": connectionID, "bucket": "drobodata-fanyu", "fixtureRoot": root}
body, _ := json.MarshalIndent(state, "", " ")
must(os.WriteFile(filepath.Join(root, "state-"+mode+".json"), body, 0600))
fmt.Println("FANYU_DISCARD_FIXTURE " + strings.TrimSpace(string(body)))
}