forked from longtao.wu/droboticflow
97 lines
4.2 KiB
Go
97 lines
4.2 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
)
|
|
|
|
func TestRetainCurrentAnnotationData(t *testing.T) {
|
|
for _, state := range []string{"completed", "failed", "processing", "pending"} {
|
|
t.Run(state, func(t *testing.T) {
|
|
ctx := context.Background()
|
|
db := store.NewMemoryStore()
|
|
root := t.TempDir()
|
|
// macOS test environments can symlink the temp parent; use its real path.
|
|
root, _ = filepath.EvalSymlinks(root)
|
|
s := &server{store: db, workspaceMountPath: root}
|
|
task := domain.AnnotationTaskRecord{ID: "task-a", OrganizationID: domain.DefaultOrganizationID, RunID: "run-a", NodeID: "human"}
|
|
item := domain.HumanItemRecord{ID: "item-a", OrganizationID: task.OrganizationID, LegacyTaskID: task.ID, RunID: task.RunID, CurrentStageID: domain.FanyuV58StageQualityReview, CurrentRevision: 2, Subject: domain.HumanSubjectRef{Payload: map[string]any{"materialization": map[string]any{"revision": 2, "status": "completed", "artifactId": "run-a-human-task-a-materialized-revision-000002"}}}}
|
|
if state == "failed" {
|
|
item.Subject.Payload["materialization"] = map[string]any{"revision": 1, "status": "completed"}
|
|
}
|
|
if state == "processing" {
|
|
item.Subject.Payload["sliceWorkflow"] = map[string]any{"status": "processing"}
|
|
}
|
|
if state == "pending" {
|
|
item.PendingCombinedSubmissionID = "pending"
|
|
}
|
|
if err := db.UpsertHumanItem(ctx, item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run := domain.RunRecord{ID: task.RunID, OrganizationID: task.OrganizationID}
|
|
for _, r := range []int{1, 2, 3} {
|
|
dir := filepath.Join(root, "system", "runs", task.RunID, "human-materializations", item.ID, task.ID, fmt.Sprintf("revision-%06d", r))
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "left.mp4"), []byte("test-video"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run.Artifacts = append(run.Artifacts, domain.ExecutionArtifact{ID: fmt.Sprintf("run-a-human-task-a-materialized-revision-%06d", r), Path: dir, PortID: "materializedSliceDirectory"})
|
|
if err := db.UpsertSliceRevision(ctx, domain.SliceRevisionRecord{ID: fmt.Sprint("slice", r), OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: r}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.UpsertAnnotationRevision(ctx, domain.AnnotationRevisionRecord{ID: fmt.Sprint("annotation", r), OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: r}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
run.Artifacts = append(run.Artifacts, domain.ExecutionArtifact{ID: "another-capture", Path: root})
|
|
run.NodeExecutions = []domain.NodeExecutionRecord{{NodeID: "human", Artifacts: append([]domain.ExecutionArtifact{}, run.Artifacts...)}}
|
|
if err := db.UpsertRun(ctx, run); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.UpsertAnnotationReview(ctx, domain.AnnotationReviewRecord{ID: "audit", OrganizationID: task.OrganizationID, TaskID: task.ID, Revision: 1, Comment: "keep opinion"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.retainCurrentAnnotationData(ctx, task, item.ID, 2); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
slices, _ := db.ListSliceRevisions(ctx, task.OrganizationID, task.ID)
|
|
annotations, _ := db.ListAnnotationRevisions(ctx, task.OrganizationID, task.ID)
|
|
want := 3
|
|
if state == "completed" {
|
|
want = 2
|
|
}
|
|
if len(slices) != want || len(annotations) != want {
|
|
t.Fatalf("retained data %d/%d want %d", len(slices), len(annotations), want)
|
|
}
|
|
_, oldErr := os.Stat(run.Artifacts[0].Path)
|
|
if (state == "completed") != os.IsNotExist(oldErr) {
|
|
t.Fatalf("old file state: %v", oldErr)
|
|
}
|
|
for _, a := range run.Artifacts[1:] {
|
|
if _, err := os.Stat(a.Path); err != nil {
|
|
t.Fatalf("current/newer/foreign path removed: %s", a.Path)
|
|
}
|
|
}
|
|
reviews, _ := db.ListAnnotationReviews(ctx, task.OrganizationID, task.ID)
|
|
if len(reviews) != 1 {
|
|
t.Fatal("audit removed")
|
|
}
|
|
persisted, _ := db.GetRun(ctx, run.ID)
|
|
if len(persisted.Artifacts) != want+1 || len(persisted.NodeExecutions[0].Artifacts) != want+1 {
|
|
t.Fatal("artifact references not pruned consistently")
|
|
}
|
|
if err := s.retainCurrentAnnotationData(ctx, task, item.ID, 2); err != nil {
|
|
t.Fatalf("cleanup retry: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|