forked from longtao.wu/droboticflow
190 lines
6.7 KiB
Go
190 lines
6.7 KiB
Go
package store
|
|
|
|
import (
|
|
"encoding/json"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"strings"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func TestCompactRunForStorageDropsTopLevelExecutionCopies(t *testing.T) {
|
|
run := domain.RunRecord{
|
|
ID: "run-1",
|
|
NodeExecutions: []domain.NodeExecutionRecord{
|
|
{
|
|
NodeID: "node-1",
|
|
NodeLabel: "目录扫描",
|
|
Status: domain.NodeExecutionStatusCompleted,
|
|
Logs: []domain.LogEntryRecord{
|
|
{Timestamp: "2026-04-21T00:00:00Z", Level: "INFO", Message: "scan started"},
|
|
},
|
|
ResourceSeries: []domain.ResourceSample{
|
|
{Timestamp: "2026-04-21T00:00:01Z", CPU: 10, Memory: 128},
|
|
},
|
|
Artifacts: []domain.ExecutionArtifact{
|
|
{ID: "artifact-1", RunID: "run-1", NodeID: "node-1", Name: "manifest.json", Type: domain.PortTypeJSON, Size: "1 KB", Path: "/tmp/manifest.json"},
|
|
},
|
|
},
|
|
},
|
|
Logs: []domain.LogEntryRecord{
|
|
{Timestamp: "2026-04-21T00:00:00Z", Level: "INFO", Message: "duplicated"},
|
|
},
|
|
ResourceSeries: []domain.ResourceSample{
|
|
{Timestamp: "2026-04-21T00:00:01Z", CPU: 99, Memory: 2048},
|
|
},
|
|
Artifacts: []domain.ExecutionArtifact{
|
|
{ID: "artifact-duplicate", RunID: "run-1", NodeID: "node-1", Name: "duplicate.json", Type: domain.PortTypeJSON, Size: "1 KB", Path: "/tmp/duplicate.json"},
|
|
},
|
|
}
|
|
|
|
compacted := compactRunForStorage(run)
|
|
if compacted.ID != run.ID {
|
|
t.Fatalf("compacted run id = %q, want %q", compacted.ID, run.ID)
|
|
}
|
|
if len(compacted.NodeExecutions) != 1 {
|
|
t.Fatalf("compacted node executions = %d, want 1", len(compacted.NodeExecutions))
|
|
}
|
|
if compacted.Logs != nil {
|
|
t.Fatalf("compacted logs should be nil, got %#v", compacted.Logs)
|
|
}
|
|
if compacted.ResourceSeries != nil {
|
|
t.Fatalf("compacted resource series should be nil, got %#v", compacted.ResourceSeries)
|
|
}
|
|
if compacted.Artifacts != nil {
|
|
t.Fatalf("compacted artifacts should be nil, got %#v", compacted.Artifacts)
|
|
}
|
|
if len(compacted.CaptureDirectories) != 0 {
|
|
t.Fatalf("unrelated artifacts must not invent Capture directories, got %#v", compacted.CaptureDirectories)
|
|
}
|
|
|
|
hydrateRunDerivedFields(&compacted)
|
|
if len(compacted.Logs) != 1 || compacted.Logs[0].Message != "scan started" {
|
|
t.Fatalf("hydrated logs = %#v", compacted.Logs)
|
|
}
|
|
if len(compacted.ResourceSeries) != 1 || compacted.ResourceSeries[0].CPU != 10 {
|
|
t.Fatalf("hydrated resource series = %#v", compacted.ResourceSeries)
|
|
}
|
|
if len(compacted.Artifacts) != 1 || compacted.Artifacts[0].ID != "artifact-1" {
|
|
t.Fatalf("hydrated artifacts = %#v", compacted.Artifacts)
|
|
}
|
|
}
|
|
|
|
func TestCompactRunForStoragePersistsCaptureDirectoryBeforeDroppingArtifacts(t *testing.T) {
|
|
run := domain.RunRecord{
|
|
ID: "run-capture",
|
|
NodeExecutions: []domain.NodeExecutionRecord{{Artifacts: []domain.ExecutionArtifact{{
|
|
Name: "blurred_capture-000042_camera_left_part0001.mp4",
|
|
}}}},
|
|
}
|
|
compacted := compactRunForStorage(run)
|
|
if len(compacted.CaptureDirectories) != 1 || compacted.CaptureDirectories[0] != "capture-000042" {
|
|
t.Fatalf("capture directories = %#v", compacted.CaptureDirectories)
|
|
}
|
|
}
|
|
|
|
func TestRunStorageFieldsToUnsetClearsStaleLifecycleState(t *testing.T) {
|
|
run := domain.RunRecord{}
|
|
actual := runStorageFieldsToUnset(run)
|
|
expected := []string{
|
|
"startedAt",
|
|
"currentNodeId",
|
|
"error",
|
|
"failure",
|
|
"progress",
|
|
"endedAt",
|
|
"durationMs",
|
|
"stopMode",
|
|
"stopRequestedAt",
|
|
}
|
|
if len(actual) != len(expected) {
|
|
t.Fatalf("unset fields = %#v, want %#v", actual, expected)
|
|
}
|
|
for index := range expected {
|
|
if actual[index] != expected[index] {
|
|
t.Fatalf("unset fields = %#v, want %#v", actual, expected)
|
|
}
|
|
}
|
|
|
|
run.StartedAt = "2026-08-17T00:00:00Z"
|
|
run.CurrentNodeID = "node-1"
|
|
run.Error = "failed"
|
|
run.Failure = &domain.RunFailure{Code: "failed"}
|
|
run.Progress = &domain.RunProgress{Stage: "train"}
|
|
run.EndedAt = "2026-08-18T00:00:00Z"
|
|
run.DurationMS = 10
|
|
run.StopMode = domain.RunStopModeGraceful
|
|
run.StopRequestedAt = "2026-08-18T00:00:00Z"
|
|
if fields := runStorageFieldsToUnset(run); len(fields) != 0 {
|
|
t.Fatalf("populated run should not unset lifecycle fields, got %#v", fields)
|
|
}
|
|
}
|
|
|
|
func TestDuplicateOperatorDocumentIDsToDeleteKeepsLatestRecord(t *testing.T) {
|
|
deleteIDs := duplicateOperatorDocumentIDsToDelete([]duplicateOperatorRecordRef{
|
|
{DocumentID: "older", UpdatedAt: "2026-04-01T00:00:00Z", CreatedAt: "2026-04-01T00:00:00Z"},
|
|
{DocumentID: "newer", UpdatedAt: "2026-06-01T00:00:00Z", CreatedAt: "2026-04-01T00:00:00Z"},
|
|
{DocumentID: "middle", UpdatedAt: "2026-05-01T00:00:00Z", CreatedAt: "2026-04-01T00:00:00Z"},
|
|
})
|
|
|
|
assertStringIDs(t, deleteIDs, []string{"middle", "older"})
|
|
}
|
|
|
|
func TestDuplicateOperatorDocumentIDsToDeleteUsesCreatedAtTieBreak(t *testing.T) {
|
|
deleteIDs := duplicateOperatorDocumentIDsToDelete([]duplicateOperatorRecordRef{
|
|
{DocumentID: "old-created", UpdatedAt: "2026-06-01T00:00:00Z", CreatedAt: "2026-04-01T00:00:00Z"},
|
|
{DocumentID: "new-created", UpdatedAt: "2026-06-01T00:00:00Z", CreatedAt: "2026-05-01T00:00:00Z"},
|
|
})
|
|
|
|
assertStringIDs(t, deleteIDs, []string{"old-created"})
|
|
}
|
|
|
|
func TestDuplicateOperatorDocumentIDsToDeleteUsesDocumentIDTieBreak(t *testing.T) {
|
|
deleteIDs := duplicateOperatorDocumentIDsToDelete([]duplicateOperatorRecordRef{
|
|
{DocumentID: "a", UpdatedAt: "2026-06-01T00:00:00Z", CreatedAt: "2026-06-01T00:00:00Z"},
|
|
{DocumentID: "z", UpdatedAt: "2026-06-01T00:00:00Z", CreatedAt: "2026-06-01T00:00:00Z"},
|
|
})
|
|
|
|
assertStringIDs(t, deleteIDs, []string{"a"})
|
|
}
|
|
|
|
func assertStringIDs(t *testing.T, actual []any, expected []string) {
|
|
t.Helper()
|
|
if len(actual) != len(expected) {
|
|
t.Fatalf("delete ids length = %d, want %d: %#v", len(actual), len(expected), actual)
|
|
}
|
|
for index, expectedID := range expected {
|
|
if actual[index] != expectedID {
|
|
t.Fatalf("delete id %d = %#v, want %q", index, actual[index], expectedID)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRetryHistorySurvivesStorageWithoutPublicJSON(t *testing.T) {
|
|
run := domain.RunRecord{ID: "run", RetryHistory: []domain.RunRetryAttempt{{Attempt: 1, NodeExecutions: []domain.NodeExecutionRecord{{NodeID: "package", Error: "failed", Artifacts: []domain.ExecutionArtifact{{Path: "/private/evidence"}}}}}}}
|
|
compacted := compactRunForStorage(run)
|
|
data, err := bson.Marshal(compacted)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var restored domain.RunRecord
|
|
if err := bson.Unmarshal(data, &restored); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(restored.RetryHistory) != 1 || restored.RetryHistory[0].NodeExecutions[0].Artifacts[0].Path != "/private/evidence" {
|
|
t.Fatal("lost BSON retry history")
|
|
}
|
|
restored.RetryHistory[0].NodeExecutions[0].Error = "changed"
|
|
if run.RetryHistory[0].NodeExecutions[0].Error != "failed" {
|
|
t.Fatal("aliased history")
|
|
}
|
|
public, err := json.Marshal(restored)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(public), "/private/evidence") || strings.Contains(string(public), "retryHistory") {
|
|
t.Fatal("private retry evidence exposed")
|
|
}
|
|
}
|