summaryrefslogtreecommitdiff
path: root/internal/cli/project_test.go
blob: c62e181e72d9e54de03e5e08ada191bd4e7062c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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)
		}
	})
}