-
Notifications
You must be signed in to change notification settings - Fork 0
/
yofukashi_test.go
118 lines (105 loc) · 3.06 KB
/
yofukashi_test.go
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
115
116
117
118
package yofukashi_test
import (
"blekksprut.net/yofukashi"
"blekksprut.net/yofukashi/nex"
"io"
"os"
"strings"
"testing"
"time"
)
type request struct {
io.Writer
io.Reader
}
func (request) Close() error {
return nil
}
func midnight() time.Time {
t := time.Now()
return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
}
func midday() time.Time {
t := time.Now()
return time.Date(t.Year(), t.Month(), t.Day(), 12, 1, 0, 0, t.Location())
}
func TestServe(t *testing.T) {
station := nex.Station{FS: os.DirFS(".")}
req := request{Reader: strings.NewReader("/README.gmi"), Writer: io.Discard}
station.Serve(req)
}
func TestStation(t *testing.T) {
station := nex.Station{FS: os.DirFS(".")}
req := request{Reader: strings.NewReader("/README.gmi"), Writer: io.Discard}
err := station.ServeAt(midnight(), req)
if err != nil {
t.Errorf("should succeed")
}
}
func TestIndex(t *testing.T) {
station := nex.Station{FS: os.DirFS("station")}
req := request{Reader: strings.NewReader("/"), Writer: io.Discard}
err := station.ServeAt(midnight(), req)
if err != nil {
t.Errorf("should serve up the index")
}
}
func TestMissingIndex(t *testing.T) {
station := nex.Station{FS: os.DirFS(".")}
req := request{Reader: strings.NewReader("/"), Writer: io.Discard}
err := station.ServeAt(midnight(), req)
if err == nil {
t.Errorf("no index.nex, should fail")
}
}
func TestHours(t *testing.T) {
station := nex.Station{os.DirFS("."), true, 35.6764}
req := request{Reader: strings.NewReader("/"), Writer: io.Discard}
err := station.ServeAt(midday(), req)
if err == nil {
t.Errorf("outside opening hours, should fail")
}
}
func TestClosingTemplate(t *testing.T) {
station := nex.Station{os.DirFS("station"), true, 35.6764}
req := request{Reader: strings.NewReader("/"), Writer: io.Discard}
err := station.ServeAt(midday(), req)
if err == nil {
t.Errorf("outside opening hours, should fail")
}
}
func TestOpeningEstimates(t *testing.T) {
station := nex.Station{os.DirFS("."), true, 35.6764}
var res strings.Builder
req := request{Reader: strings.NewReader("/"), Writer: &res}
now := time.Now()
_, dusk := yofukashi.DawnDusk(now, 35.6764)
t.Run("Hours", func(t *testing.T) {
d, _ := time.ParseDuration("-5h")
station.ServeAt(dusk.Add(d), req)
if !strings.Contains(res.String(), "5 hours") {
t.Errorf("failed to estimate number of hours until opening")
}
})
t.Run("AFewHours", func(t *testing.T) {
d, _ := time.ParseDuration("-90m")
station.ServeAt(dusk.Add(d), req)
if !strings.Contains(res.String(), "an hour or two") {
t.Errorf("failed to estimate number of hours until opening")
}
})
t.Run("Minutes", func(t *testing.T) {
d, _ := time.ParseDuration("-11m")
station.ServeAt(dusk.Add(d), req)
if !strings.Contains(res.String(), "10 minutes") {
t.Errorf("failed to estimate number of minutes until opening")
}
})
t.Run("Soon", func(t *testing.T) {
d, _ := time.ParseDuration("-3s")
station.ServeAt(dusk.Add(d), req)
if !strings.Contains(res.String(), "soon") {
t.Errorf("failed to estimate number of seconds until opening")
}
})
}