diff options
Diffstat (limited to 'internal/storage/db_test.go')
| -rw-r--r-- | internal/storage/db_test.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go index 09bbdfc..b53234a 100644 --- a/internal/storage/db_test.go +++ b/internal/storage/db_test.go @@ -1256,4 +1256,80 @@ func TestUpdateProject(t *testing.T) { } } +func makeDepTask(id string, dependsOn []string) *task.Task { + now := time.Now().UTC() + if dependsOn == nil { + dependsOn = []string{} + } + return &task.Task{ + ID: id, Name: "Task " + id, + Agent: task.AgentConfig{Type: "claude", Instructions: "x"}, + Priority: task.PriorityNormal, + Retry: task.RetryConfig{MaxAttempts: 1, Backoff: "linear"}, + Tags: []string{}, + DependsOn: dependsOn, + State: task.StatePending, + CreatedAt: now, UpdatedAt: now, + } +} + +// TestListDependents verifies ListDependents returns exactly the tasks that +// directly depend on the given ID, not transitive dependents and not +// unrelated tasks. +func TestListDependents(t *testing.T) { + db := testDB(t) + + // A has no deps. + // B and C both depend directly on A. + // D depends on B (transitive on A, not direct). + // E has no relation to A at all. + a := makeDepTask("ld-a", nil) + b := makeDepTask("ld-b", []string{"ld-a"}) + c := makeDepTask("ld-c", []string{"ld-a"}) + d := makeDepTask("ld-d", []string{"ld-b"}) + e := makeDepTask("ld-e", nil) + + for _, tk := range []*task.Task{a, b, c, d, e} { + if err := db.CreateTask(tk); err != nil { + t.Fatalf("CreateTask(%s): %v", tk.ID, err) + } + } + + deps, err := db.ListDependents("ld-a") + if err != nil { + t.Fatalf("ListDependents: %v", err) + } + if len(deps) != 2 { + t.Fatalf("want 2 direct dependents of ld-a, got %d: %+v", len(deps), deps) + } + got := map[string]bool{} + for _, dep := range deps { + got[dep.ID] = true + } + if !got["ld-b"] || !got["ld-c"] { + t.Errorf("expected dependents ld-b and ld-c, got %v", got) + } + if got["ld-d"] || got["ld-e"] { + t.Errorf("ListDependents must not include transitive (ld-d) or unrelated (ld-e) tasks: %v", got) + } + + // ld-d depends only on ld-b. + depsOfB, err := db.ListDependents("ld-b") + if err != nil { + t.Fatalf("ListDependents(ld-b): %v", err) + } + if len(depsOfB) != 1 || depsOfB[0].ID != "ld-d" { + t.Errorf("want [ld-d] as dependents of ld-b, got %+v", depsOfB) + } + + // A task with no dependents returns an empty slice, not an error. + depsOfE, err := db.ListDependents("ld-e") + if err != nil { + t.Fatalf("ListDependents(ld-e): %v", err) + } + if len(depsOfE) != 0 { + t.Errorf("want no dependents of ld-e, got %+v", depsOfE) + } +} + |
