File size: 2,147 Bytes
84ffb7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package models

import (
	"strings"
	"testing"
	"time"
)

func TestValidStatus(t *testing.T) {
	for _, s := range []string{StatusToRead, StatusReading, StatusFinished} {
		if !ValidStatus(s) {
			t.Errorf("expected %q to be valid", s)
		}
	}
	for _, s := range []string{"", "read", "DONE", "finished "} {
		if ValidStatus(s) {
			t.Errorf("expected %q to be invalid", s)
		}
	}
}

func TestStatsProgress(t *testing.T) {
	cases := []struct {
		goal, finished int
		want           int
	}{
		{10, 0, 0},
		{10, 5, 50},
		{10, 10, 100},
		{10, 15, 100}, // over-achievement caps at 100
		{0, 5, 0},     // no goal β†’ no progress
	}
	for _, c := range cases {
		s := Stats{Goal: c.goal, FinishedYear: c.finished}
		if got := s.Progress(); got != c.want {
			t.Errorf("Progress(goal=%d, finished=%d) = %d, want %d", c.goal, c.finished, got, c.want)
		}
	}
}

func TestBookStars(t *testing.T) {
	r := func(n int) *int { return &n }

	cases := []struct {
		rating *int
		want   string
	}{
		{nil, ""},
		{r(5), "β˜…β˜…β˜…β˜…β˜…"},
		{r(3), "β˜…β˜…β˜…β˜†β˜†"},
		{r(1), "β˜…β˜†β˜†β˜†β˜†"},
		{r(0), "β˜…β˜†β˜†β˜†β˜†"}, // clamped up
		{r(9), "β˜…β˜…β˜…β˜…β˜…"}, // clamped down
	}
	for _, c := range cases {
		b := Book{Rating: c.rating}
		if got := b.Stars(); got != c.want {
			t.Errorf("Stars(%v) = %q, want %q", c.rating, got, c.want)
		}
	}
}

func TestBookRatingValueAndText(t *testing.T) {
	r := 4
	b := Book{Rating: &r}
	if b.RatingValue() != 4 {
		t.Errorf("RatingValue() = %d, want 4", b.RatingValue())
	}
	if (Book{}).RatingValue() != 0 {
		t.Error("unrated book must report 0")
	}

	review := "loved it"
	b2 := Book{Review: &review}
	if b2.ReviewText() != "loved it" {
		t.Errorf("ReviewText() = %q", b2.ReviewText())
	}
	if (Book{}).ReviewText() != "" {
		t.Error("nil review must be empty string")
	}
}

func TestBookFinishedDate(t *testing.T) {
	if (Book{}).FinishedDate() != "" {
		t.Error("unfinished book must have no date")
	}
	when := time.Date(2026, 3, 15, 0, 0, 0, 0, time.UTC)
	b := Book{FinishedAt: &when}
	if !strings.Contains(b.FinishedDate(), "2026") {
		t.Errorf("FinishedDate() = %q, want a 2026 date", b.FinishedDate())
	}
}