summaryrefslogtreecommitdiff
path: root/internal/reporter/reporter_test.go
blob: 1ddce2368153d4ef3e88bc229cd6dab93959c82e (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
103
104
105
106
107
108
109
110
111
112
113
114
package reporter

import (
	"bytes"
	"encoding/json"
	"strings"
	"testing"
	"time"

	"github.com/claudomator/claudomator/internal/storage"
)

func sampleExecutions() []*storage.Execution {
	now := time.Date(2026, 2, 8, 10, 0, 0, 0, time.UTC)
	return []*storage.Execution{
		{
			ID: "exec-1", TaskID: "task-1", Status: "COMPLETED",
			StartTime: now, EndTime: now.Add(2 * time.Minute),
			ExitCode: 0, CostUSD: 0.25,
		},
		{
			ID: "exec-2", TaskID: "task-2", Status: "FAILED",
			StartTime: now, EndTime: now.Add(30 * time.Second),
			ExitCode: 1, CostUSD: 0.10, ErrorMsg: "something broke",
		},
	}
}

func TestConsoleReporter_WithExecutions(t *testing.T) {
	r := &ConsoleReporter{}
	var buf bytes.Buffer
	err := r.Generate(&buf, sampleExecutions())
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	output := buf.String()
	if !strings.Contains(output, "COMPLETED") {
		t.Error("missing COMPLETED status")
	}
	if !strings.Contains(output, "FAILED") {
		t.Error("missing FAILED status")
	}
	if !strings.Contains(output, "1 completed, 1 failed") {
		t.Errorf("missing summary in output: %s", output)
	}
	if !strings.Contains(output, "$0.3500") {
		t.Errorf("missing total cost in output: %s", output)
	}
}

func TestConsoleReporter_Empty(t *testing.T) {
	r := &ConsoleReporter{}
	var buf bytes.Buffer
	r.Generate(&buf, []*storage.Execution{})
	if !strings.Contains(buf.String(), "No executions") {
		t.Error("expected 'No executions' message")
	}
}

func TestJSONReporter(t *testing.T) {
	r := &JSONReporter{Pretty: false}
	var buf bytes.Buffer
	err := r.Generate(&buf, sampleExecutions())
	if err != nil {
		t.Fatal(err)
	}

	var result []storage.Execution
	if err := json.Unmarshal(buf.Bytes(), &result); err != nil {
		t.Fatalf("invalid JSON: %v", err)
	}
	if len(result) != 2 {
		t.Errorf("want 2 results, got %d", len(result))
	}
	if result[0].Status != "COMPLETED" {
		t.Errorf("want COMPLETED, got %q", result[0].Status)
	}
}

func TestJSONReporter_Pretty(t *testing.T) {
	r := &JSONReporter{Pretty: true}
	var buf bytes.Buffer
	r.Generate(&buf, sampleExecutions())
	if !strings.Contains(buf.String(), "  ") {
		t.Error("expected indented JSON")
	}
}

func TestHTMLReporter(t *testing.T) {
	r := &HTMLReporter{}
	var buf bytes.Buffer
	err := r.Generate(&buf, sampleExecutions())
	if err != nil {
		t.Fatal(err)
	}

	html := buf.String()
	if !strings.Contains(html, "<!DOCTYPE html>") {
		t.Error("missing DOCTYPE")
	}
	if !strings.Contains(html, "Claudomator Report") {
		t.Error("missing title")
	}
	if !strings.Contains(html, "COMPLETED") {
		t.Error("missing COMPLETED status")
	}
	if !strings.Contains(html, "FAILED") {
		t.Error("missing FAILED status")
	}
	if !strings.Contains(html, "$0.3500") {
		t.Error("missing total cost")
	}
}