forked from longtao.wu/droboticflow
172 lines
9.3 KiB
Go
172 lines
9.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/config"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
// Opt-in real browser/API/Mongo contract. Only a random local database and
|
|
// generated five-second videos are used; credentials live for this process.
|
|
func TestSupplierAcceptanceBrowser(t *testing.T) {
|
|
if os.Getenv("ACCEPTANCE_BROWSER_TEST") != "1" {
|
|
t.Skip("local browser opt-in")
|
|
}
|
|
ctx, cancel := context.WithTimeout(t.Context(), 8*time.Minute)
|
|
defer cancel()
|
|
client, err := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("acceptance_browser_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
ds := store.NewMongoStore(db)
|
|
must := func(e error) {
|
|
t.Helper()
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
root := t.TempDir()
|
|
mediaDir := filepath.Join(root, "system", "runs", "acceptance-run", "slices", "slice-000001")
|
|
must(os.MkdirAll(mediaDir, 0700))
|
|
video := filepath.Join(mediaDir, "left.mp4")
|
|
output, e := exec.CommandContext(ctx, "ffmpeg", "-hide_banner", "-loglevel", "error", "-f", "lavfi", "-i", "testsrc2=size=320x240:rate=30", "-t", "5", "-c:v", "libx264", "-pix_fmt", "yuv420p", "-movflags", "+faststart", video).CombinedOutput()
|
|
if e != nil {
|
|
t.Fatalf("video: %v %s", e, output)
|
|
}
|
|
data, e := os.ReadFile(video)
|
|
must(e)
|
|
must(os.WriteFile(filepath.Join(mediaDir, "right.mp4"), data, 0600))
|
|
pts := "clip_time_ms\n"
|
|
bounds := []float64{}
|
|
for i := 0; i <= 150; i++ {
|
|
bounds = append(bounds, float64(i)/30)
|
|
if i < 150 {
|
|
pts += fmt.Sprintf("%.6f\n", float64(i)*1000/30)
|
|
}
|
|
}
|
|
for _, name := range []string{"left_pts.csv", "right_pts.csv"} {
|
|
must(os.WriteFile(filepath.Join(mediaDir, name), []byte(pts), 0600))
|
|
}
|
|
must(os.WriteFile(filepath.Join(mediaDir, "imu.csv"), []byte("timestamp_us,gx,gy,gz\n0,0,0,0\n5000000,0,0,0\n"), 0600))
|
|
syncJSON, _ := json.Marshal(map[string]any{"window": map[string]any{"startTimestampUs": 0, "endTimestampUs": 5000000}, "boundaryContract": "hardware-frame-v1", "boundarySeconds": bounds, "left": map[string]any{"pts": map[string]any{"rowCount": 150}}})
|
|
must(os.WriteFile(filepath.Join(mediaDir, "synchronized-slice.json"), syncJSON, 0600))
|
|
must(os.WriteFile(filepath.Join(mediaDir, "annotation.json"), []byte(`{"actions":[{"id":"action-1","clipStartMs":0,"clipEndMs":5000,"description":"合成验收动作"}]}`), 0600))
|
|
org := domain.DefaultOrganizationID
|
|
must(ds.UpsertOrganizationPlugin(ctx, domain.OrganizationPluginRecord{OrganizationID: org, PluginID: "annotation-qa", Enabled: true, Config: defaultAnnotationPluginConfig()}))
|
|
must(ds.UpsertOrganization(ctx, domain.OrganizationRecord{ID: org, Name: "本地共享验收验证", Slug: "acceptance-local", Status: domain.OrganizationStatusActive}))
|
|
for _, sp := range []domain.SupplierRecord{{ID: "local-producer", Name: "生产供应商", OrganizationID: org, Status: "active"}, {ID: "local-reviewer", Name: "验收供应商", OrganizationID: org, Status: "active"}} {
|
|
must(ds.UpsertSupplier(ctx, sp))
|
|
}
|
|
secret := make([]byte, 24)
|
|
_, e = rand.Read(secret)
|
|
must(e)
|
|
password := hex.EncodeToString(secret)
|
|
hash, e := hashPassword(password)
|
|
must(e)
|
|
users := []domain.UserRecord{{ID: "local-admin", Role: domain.UserRoleAdmin, Phone: "13900009001", Name: "隔离管理员"}, {ID: "local-manager", Role: domain.UserRoleSupplierManager, SupplierID: "local-reviewer", Phone: "13900009002", Name: "隔离供应商管理员"}, {ID: "local-review-user", Role: domain.UserRoleAcceptanceReviewer, SupplierID: "local-reviewer", Phone: "13900009003", Name: "隔离验收员", AcceptanceAccessMode: "managed"}}
|
|
for _, u := range users {
|
|
u.OrganizationID = org
|
|
u.Status = domain.UserStatusActive
|
|
u.PasswordHash = hash
|
|
must(ds.UpsertUser(ctx, u))
|
|
}
|
|
for projectIndex, project := range []string{"acceptance-project-a", "acceptance-project-b"} {
|
|
must(ds.UpsertProject(ctx, domain.ProjectRecord{ID: project, OrganizationID: org, Name: project}))
|
|
runID := "run-" + project
|
|
projectMediaDir := filepath.Join(root, "system", "runs", runID, "slices", "slice-000001")
|
|
must(os.MkdirAll(projectMediaDir, 0700))
|
|
for _, name := range []string{"left.mp4", "right.mp4", "left_pts.csv", "right_pts.csv", "imu.csv", "synchronized-slice.json", "annotation.json"} {
|
|
data, e := os.ReadFile(filepath.Join(mediaDir, name))
|
|
must(e)
|
|
must(os.WriteFile(filepath.Join(projectMediaDir, name), data, 0600))
|
|
}
|
|
artifact := domain.ExecutionArtifact{ID: "local-media", RunID: runID, Name: "revision.directory.index", Type: domain.PortTypeFile, Path: filepath.Dir(filepath.Dir(projectMediaDir)), Data: map[string]any{"schemaVersion": "droboticflow.directory-index/v1", "datasetType": "directory", "presentation": "collection", "rootPath": filepath.Dir(filepath.Dir(projectMediaDir))}}
|
|
must(ds.UpsertRun(ctx, domain.RunRecord{ID: runID, OrganizationID: org, ProjectID: project, Status: domain.RunStatusCompleted, NodeExecutions: []domain.NodeExecutionRecord{{NodeID: "human", Artifacts: []domain.ExecutionArtifact{artifact}}}}))
|
|
for n := 0; n < 3; n++ {
|
|
sourceID := fmt.Sprintf("source-%d-%d", projectIndex, n)
|
|
sourceBatch := domain.HumanBatchRecord{ID: "batch-" + sourceID, GateNodeID: "gate-" + sourceID, OrganizationID: org, ProjectID: project, RunID: runID, PluginID: "annotation-qa", Stages: domain.DefaultFanyuV58HumanStages(), RequirePrePackageAcceptance: true, ProcessingProfile: domain.ProcessingProfileRawHuman, Status: domain.HumanBatchActive}
|
|
batch := sourceBatch
|
|
batch.ID = fanyuV5PrePackageAcceptanceBatchID(sourceBatch.ID)
|
|
batch.SourceBatchID = sourceBatch.ID
|
|
batch.GateNodeID += "-acceptance"
|
|
batch.RequirePrePackageAcceptance = false
|
|
batch.Stages = []domain.HumanStageDefinition{{ID: "prepackage_acceptance", Kind: domain.HumanStageAcceptance, RequiredRole: "acceptance_reviewer"}}
|
|
must(ds.UpsertHumanBatch(ctx, sourceBatch))
|
|
must(ds.UpsertHumanBatch(ctx, batch))
|
|
item := domain.HumanItemRecord{ID: fanyuV5PrePackageAcceptanceItemID(sourceBatch.ID), OrganizationID: org, ProjectID: project, RunID: runID, BatchID: batch.ID, PluginID: "annotation-qa", ProcessingProfile: domain.ProcessingProfileRawHuman, CurrentStageID: "prepackage_acceptance", Status: domain.HumanItemReady, Version: 1, ReviewRound: 1, CreatedAt: fmt.Sprintf("2020-01-0%dT00:00:00Z", n*2+projectIndex+1), Subject: domain.HumanSubjectRef{ItemKey: sourceID, Payload: map[string]any{"items": []any{map[string]any{"sourceHumanItemId": sourceID, "runId": runID, "captureDirectory": sourceID, "materialization": map[string]any{"runId": runID, "artifactId": artifact.ID, "revision": 1, "sliceCount": 1, "coverage": map[string]any{"contiguous": true, "gapMicroseconds": 0, "overlapMicroseconds": 0, "durationSeconds": 5}}, "finalSlices": []domain.SliceSegment{{ID: "slice-1", Name: "合成测试切片", Start: 0, End: 5}}, "annotation": []domain.AnnotationSegment{{ID: "action-1", Start: 0, End: 5, Description: "合成验收动作"}}}}}}}
|
|
must(ds.UpsertHumanItem(ctx, item))
|
|
source := item
|
|
source.ID = sourceID
|
|
source.SupplierID = "local-producer"
|
|
source.BatchID = sourceBatch.ID
|
|
source.CurrentStageID = domain.FanyuV58StageQualityReview
|
|
source.Status = domain.HumanItemAccepted
|
|
source.AcceptedAt = nowISO()
|
|
must(ds.UpsertHumanItem(ctx, source))
|
|
}
|
|
}
|
|
must(ds.EnsureIndexes(ctx))
|
|
// Verify the shipping backup tool against these synthetic identities. It
|
|
// must not initialize compatibility records or overwrite an existing backup.
|
|
backupPath := filepath.Join(t.TempDir(), "acceptance-before.json")
|
|
backup := func() ([]byte, error) {
|
|
cmd := exec.CommandContext(ctx, "go", "run", "./cmd/acceptance-access-audit", "--org", org, "--deployment-version", "local-synthetic-fixture", "--output", backupPath)
|
|
cmd.Dir = "../.."
|
|
cmd.Env = append(os.Environ(), "MONGO_URI=mongodb://127.0.0.1:27018", "MONGO_DB="+db.Name())
|
|
return cmd.CombinedOutput()
|
|
}
|
|
backupOutput, err := backup()
|
|
if err != nil {
|
|
t.Fatalf("backup: %v %s", err, backupOutput)
|
|
}
|
|
var backupRecord struct {
|
|
AcceptanceAccounts []map[string]any `json:"acceptanceAccounts"`
|
|
CompatibilitySnapshots []map[string]any `json:"compatibilitySnapshots"`
|
|
}
|
|
backupData, err := os.ReadFile(backupPath)
|
|
must(err)
|
|
must(json.Unmarshal(backupData, &backupRecord))
|
|
if len(backupRecord.AcceptanceAccounts) != 1 || len(backupRecord.CompatibilitySnapshots) != 0 {
|
|
t.Fatalf("unexpected read-only backup: %s", backupData)
|
|
}
|
|
if info, err := os.Stat(backupPath); err != nil || info.Mode().Perm() != 0600 {
|
|
t.Fatal("backup permissions")
|
|
}
|
|
if _, err := backup(); err == nil {
|
|
t.Fatal("backup overwrote existing file")
|
|
}
|
|
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
|
h := NewHandlerWithConfig(ds, nil, config.AppConfig{WorkspaceMountPath: root})
|
|
listener, e := net.Listen("tcp", "127.0.0.1:3001")
|
|
must(e)
|
|
server := &http.Server{Handler: h}
|
|
go server.Serve(listener)
|
|
defer server.Close()
|
|
cmd := exec.CommandContext(ctx, "node", "tests/browser/supplier-acceptance.mjs")
|
|
cmd.Dir = "../.."
|
|
cmd.Env = append(os.Environ(), "ACCEPTANCE_QA_PASSWORD="+password)
|
|
output, e = cmd.CombinedOutput()
|
|
t.Log(string(output))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|