droboticflow/internal/api/discard_reason_dictionary_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

181 lines
11 KiB
Go

package api
import (
"context"
"droboticflow/internal/domain"
"droboticflow/internal/store"
"fmt"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
)
// Opt-in isolated dictionary QA. References only previously generated synthetic
// media. No intake listener, real capture or customer account is changed.
func TestDiscardReasonDictionaryLiveFixture(t *testing.T) {
if os.Getenv("DISCARD_DICTIONARY_FIXTURE") != "1" {
t.Skip("explicit fixture only")
}
nonce := os.Getenv("DISCARD_DICTIONARY_NONCE")
if !regexp.MustCompile(`^2026[0-9a-z]{8,24}$`).MatchString(nonce) {
t.Fatal("invalid nonce")
}
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
client, e := mongo.Connect(ctx, options.Client().ApplyURI(os.Getenv("MONGO_URI")))
if e != nil {
t.Fatal("mongo connection")
}
defer client.Disconnect(context.Background())
s := store.NewMongoStore(client.Database(os.Getenv("MONGO_DB")))
org, project, workflow, run, task, batch, item := "org-reasons-"+nonce, "project-reasons-"+nonce, "workflow-reasons-"+nonce, "run-reasons-"+nonce, "task-reasons-"+nonce, "batch-reasons-"+nonce, "item-reasons-"+nonce
must := func(e error) {
t.Helper()
if e != nil {
t.Fatal(e)
}
}
roles := []domain.UserRole{domain.UserRoleAdmin, domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer}
media := os.Getenv("DISCARD_DICTIONARY_MEDIA") == "1"
if media {
if !strings.HasSuffix(nonce, "media") {
t.Fatal("media fixture suffix required")
}
connectionID := os.Getenv("ANNOTATION_WORKSPACE_CONNECTION_ID")
if connectionID == "" {
connectionID = os.Getenv("PROJECT_DELETION_WORKSPACE_CONNECTION_ID")
}
connection, e := s.GetStorageConnection(ctx, connectionID)
must(e)
if connection == nil {
t.Fatal("workspace connection unavailable")
}
org = connection.OrganizationID
roles = roles[1:] // No administrator is added to the customer organization.
}
if os.Getenv("DISCARD_DICTIONARY_MODE") == "configure-slice" {
if !media {
t.Fatal("media scope required")
}
p, e := s.GetProject(ctx, project)
must(e)
if p == nil || p.OrganizationID != org {
t.Fatal("fixture project scope")
}
id := "slice-reasons-" + nonce
must(s.UpsertAnnotationLabel(ctx, domain.AnnotationLabelRecord{ID: id, OrganizationID: org, Kind: "slice", Code: id, Name: "字典验收切片-" + nonce, Version: 1, Status: domain.AnnotationLabelActive}))
must(s.UpsertProjectSliceLabels(ctx, domain.ProjectAnnotationLabelsRecord{ID: "project-annotation-labels-" + project, ProjectID: project, OrganizationID: org, SliceLabelIDs: []string{id}}))
fmt.Println("MEDIA_SLICE_DICTIONARY_CONFIGURED")
return
}
if os.Getenv("DISCARD_DICTIONARY_MODE") == "rename-media" {
if !media {
t.Fatal("media scope required")
}
label, e := s.GetAnnotationLabel(ctx, "reason-adjust-"+nonce)
must(e)
if label == nil || label.OrganizationID != org {
t.Fatal("fixture reason scope")
}
label.Name = "调整头环(新版)"
label.Version++
label.Status = domain.AnnotationLabelInactive
must(s.UpsertAnnotationLabel(ctx, *label))
fmt.Println("MEDIA_REASON_RENAMED_DISABLED")
return
}
if os.Getenv("DISCARD_DICTIONARY_MODE") == "cleanup" {
for _, role := range roles {
u, e := s.GetUser(ctx, "user-reasons-"+nonce+"-"+string(role))
must(e)
if u != nil {
if u.OrganizationID != org {
t.Fatal("scope")
}
u.Status = domain.UserStatusDisabled
must(s.UpsertUser(ctx, *u))
}
}
if media {
for _, id := range []string{"reason-adjust-" + nonce, "reason-other-" + nonce, "slice-reasons-" + nonce} {
label, e := s.GetAnnotationLabel(ctx, id)
must(e)
if label != nil {
if label.OrganizationID != org {
t.Fatal("fixture label scope")
}
label.Status = domain.AnnotationLabelInactive
must(s.UpsertAnnotationLabel(ctx, *label))
}
}
}
fmt.Println("DICTIONARY_QA_DISABLED")
return
}
existing, e := s.GetOrganization(ctx, org)
must(e)
if existing != nil && !media {
t.Fatal("fixture exists")
}
if p, e := s.GetProject(ctx, project); e != nil || p != nil {
t.Fatal("fixture project exists")
}
password := os.Getenv("DISCARD_DICTIONARY_PASSWORD")
if len(password) < 20 {
t.Fatal("ephemeral password required")
}
hash, e := hashPassword(password)
must(e)
root := "/workspace/system/slice-discard-verification/20260917rules1/fixtures"
files := []struct {
name, port string
typ domain.PortType
}{{"capture_left.mp4", "fullLeftVideos", domain.PortTypeVideo}, {"capture_right.mp4", "fullRightVideos", domain.PortTypeVideo}, {"capture_left_pts.csv", "leftTimestamps", domain.PortTypeFile}, {"capture_right_pts.csv", "rightTimestamps", domain.PortTypeFile}, {"capture_imu.csv", "imu", domain.PortTypeFile}}
artifacts := []domain.ExecutionArtifact{}
bindings := []domain.NodeInputBinding{}
for _, f := range files {
path := filepath.Join(root, f.name)
info, e := os.Stat(path)
must(e)
a := domain.ExecutionArtifact{ID: run + "-" + f.name, RunID: run, NodeID: "source", Name: f.name, Type: f.typ, PortID: f.port, Path: path, SizeBytes: info.Size()}
artifacts = append(artifacts, a)
bindings = append(bindings, domain.NodeInputBinding{TargetPortID: f.port, SourceNodeID: "source", SourcePortID: f.port, Artifacts: []domain.ExecutionArtifact{a}})
}
if !media {
must(s.UpsertOrganization(ctx, domain.OrganizationRecord{ID: org, Name: "舍弃原因字典隔离验收", Slug: "reasons-" + nonce, Status: domain.OrganizationStatusActive, CreatedAt: nowISO()}))
}
must(s.UpsertProject(ctx, domain.ProjectRecord{ID: project, OrganizationID: org, Name: "舍弃原因字典隔离验收", Status: domain.ProjectStatusActive, CreatedAt: nowISO()}))
supplier := "supplier-reasons-" + nonce
must(s.UpsertSupplier(ctx, domain.SupplierRecord{ID: supplier, OrganizationID: org, Name: "隔离验收供应商", Status: "active"}))
for _, role := range roles {
id := "user-reasons-" + nonce + "-" + string(role)
must(s.UpsertUser(ctx, domain.UserRecord{ID: id, OrganizationID: org, Name: "原因字典验收-" + string(role), Phone: testPhoneFromSeed("reasons-" + nonce + "-" + string(role)), Role: role, Status: domain.UserStatusActive, SupplierID: supplier, PasswordHash: hash, CreatedAt: nowISO()}))
if role != domain.UserRoleAdmin {
must(s.UpsertProjectMember(ctx, domain.ProjectMemberRecord{ID: "member-reasons-" + nonce + "-" + string(role), OrganizationID: org, ProjectID: project, UserID: id, SupplierID: supplier, Roles: []domain.AnnotationAssignmentRole{domain.AnnotationAssignmentRole(role)}, CreatedAt: nowISO()}))
}
}
must(s.UpsertWorkflow(ctx, domain.WorkflowRecord{ID: workflow, OrganizationID: org, ProjectID: project, Name: "字典验收(无自动接入)", Status: domain.WorkflowStatus("idle"), Type: domain.WorkflowTypeBatch, Nodes: []domain.WorkflowNode{}, Edges: []domain.WorkflowEdge{}, CreatedAt: nowISO()}))
must(s.UpsertRun(ctx, domain.RunRecord{ID: run, OrganizationID: org, ProjectID: project, WorkflowID: workflow, Status: domain.RunStatusWaitingForHuman, ProcessingProfile: domain.ProcessingProfileRawHuman, CurrentNodeID: "human", Artifacts: artifacts, NodeExecutions: []domain.NodeExecutionRecord{{NodeID: "human", Status: domain.NodeExecutionStatusWaitingForHuman, InputBindings: bindings, InputArtifacts: artifacts}}, StartedAt: nowISO()}))
must(s.UpsertAnnotationTemplate(ctx, domain.AnnotationTemplateRecord{ID: "template-reasons-" + nonce, OrganizationID: org, ProjectID: project, Name: "隔离验收模板", Version: 6, Status: domain.AnnotationTemplatePublished, Labels: domain.ConvenienceStore22SemanticLabels(), HumanStages: domain.DefaultFanyuV58HumanStages()}))
must(s.UpsertHumanBatch(ctx, domain.HumanBatchRecord{ID: batch, OrganizationID: org, ProjectID: project, PluginID: "annotation-qa", WorkflowID: workflow, RunID: run, GateNodeID: "human", SupplierID: supplier, ProcessingProfile: domain.ProcessingProfileRawHuman, Stages: domain.DefaultFanyuV58HumanStages(), Status: domain.HumanBatchActive, ItemCount: 1, CreatedAt: nowISO()}))
slices := []domain.SliceSegment{{ID: "a", Name: "历史自由名称", Start: 0, End: 20}, {ID: "b", Name: "历史自由名称", Start: 20, End: 40}, {ID: "c", Name: "历史自由名称", Start: 40, End: 60}}
must(s.UpsertAnnotationTask(ctx, domain.AnnotationTaskRecord{ID: task, OrganizationID: org, ProjectID: project, WorkflowID: workflow, RunID: run, NodeID: "human", ArtifactID: artifacts[0].ID, SourceVideo: "/api/runs/" + run + "/artifacts/" + artifacts[0].ID + "/content", RightArtifactID: artifacts[1].ID, RightSourceVideo: "/api/runs/" + run + "/artifacts/" + artifacts[1].ID + "/content", SourceIsFullVideo: true, SourceDurationSeconds: 60, ClipStart: 0, ClipEnd: 60, CoverageStartSecond: 0, CoverageEndSecond: 60, CoverageMode: "full_common_range", TemplateID: "template-reasons-" + nonce, TemplateVersion: 6, AssignedUserID: "user-reasons-" + nonce + "-annotator", SupplierID: supplier, ProcessingProfile: domain.ProcessingProfileRawHuman, Status: domain.AnnotationTaskStatusPendingAnnotation, CreatedAt: nowISO()}))
must(s.UpsertHumanItem(ctx, domain.HumanItemRecord{ID: item, OrganizationID: org, ProjectID: project, PluginID: "annotation-qa", BatchID: batch, WorkflowID: workflow, RunID: run, GateNodeID: "human", LegacyTaskID: task, SupplierID: supplier, ProcessingProfile: domain.ProcessingProfileRawHuman, CurrentStageID: domain.FanyuV58StageAnnotationCombined, Status: domain.HumanItemClaimed, AssignedUserID: "user-reasons-" + nonce + "-annotator", Version: 1, CreatedAt: nowISO(), Subject: domain.HumanSubjectRef{ArtifactID: artifacts[0].ID, ItemKey: "dictionary-qa", Payload: map[string]any{"rightArtifactId": artifacts[1].ID, "captureDirectory": "dictionary-qa", "sliceWorkflow": map[string]any{"status": "editing", "phase": "slice"}, "annotationWorkspace": map[string]any{"sliceSegments": slices, "segments": []domain.AnnotationSegment{}, "operations": []domain.SliceOperation{}, "statuses": map[string]any{}}}}}))
if media {
for _, spec := range []struct {
id, name string
note bool
}{{"adjust", "调整头环", false}, {"other", "其他", true}} {
must(s.UpsertAnnotationLabel(ctx, domain.AnnotationLabelRecord{ID: "reason-" + spec.id + "-" + nonce, OrganizationID: org, Kind: "discard_reason", Code: "reason-" + spec.id + "-" + nonce, Name: spec.name + "-" + nonce, RequiresNote: spec.note, Version: 1, Status: domain.AnnotationLabelActive, CreatedAt: nowISO()}))
}
must(s.UpsertProjectDiscardReasonLabels(ctx, domain.ProjectAnnotationLabelsRecord{ID: "project-annotation-labels-" + project, ProjectID: project, OrganizationID: org, DiscardReasonLabelIDs: []string{"reason-adjust-" + nonce, "reason-other-" + nonce}}))
}
fmt.Println("DISCARD_DICTIONARY_READY " + project + " " + task + " " + item)
}