package cli import ( "bytes" "io" "os" "path/filepath" "strings" "testing" "time" "github.com/thepeterstone/claudomator/internal/config" "github.com/thepeterstone/claudomator/internal/storage" "github.com/thepeterstone/claudomator/internal/task" ) func makeProjectTask(t *testing.T, dir string) *task.Task { t.Helper() db, err := storage.Open(filepath.Join(dir, "test.db")) if err != nil { t.Fatalf("storage.Open: %v", err) } defer db.Close() now := time.Now().UTC() tk := &task.Task{ ID: "proj-task-id", Name: "Project Task", Project: "test-project", Agent: task.AgentConfig{Type: "claude", Instructions: "do it", Model: "sonnet"}, Priority: task.PriorityNormal, Tags: []string{}, DependsOn: []string{}, Retry: task.RetryConfig{MaxAttempts: 1, Backoff: "exponential"}, State: task.StatePending, CreatedAt: now, UpdatedAt: now, } if err := db.CreateTask(tk); err != nil { t.Fatalf("CreateTask: %v", err) } return tk } func captureStdout(fn func()) string { old := os.Stdout r, w, _ := os.Pipe() os.Stdout = w fn() w.Close() os.Stdout = old var buf bytes.Buffer io.Copy(&buf, r) return buf.String() } func withDB(t *testing.T, dbPath string, fn func()) { t.Helper() origCfg := cfg if cfg == nil { cfg = &config.Config{} } cfg.DBPath = dbPath defer func() { cfg = origCfg }() fn() } func TestListTasks_ShowsProject(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") makeProjectTask(t, dir) withDB(t, dbPath, func() { out := captureStdout(func() { if err := listTasks(""); err != nil { t.Fatalf("listTasks: %v", err) } }) if !strings.Contains(out, "test-project") { t.Errorf("list output missing project 'test-project':\n%s", out) } }) } func TestStatusCmd_ShowsProject(t *testing.T) { dir := t.TempDir() dbPath := filepath.Join(dir, "test.db") tk := makeProjectTask(t, dir) withDB(t, dbPath, func() { out := captureStdout(func() { if err := showStatus(tk.ID); err != nil { t.Fatalf("showStatus: %v", err) } }) if !strings.Contains(out, "test-project") { t.Errorf("status output missing project 'test-project':\n%s", out) } }) }