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
119
120
121
122
123
124
|
package api
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/RanXom/galleryd/internal/gallery"
"github.com/RanXom/galleryd/internal/metadata"
"github.com/RanXom/galleryd/internal/service"
)
type fakeGalleryService struct {
photos []gallery.Photo
err error
}
func (f fakeGalleryService) Load(ctx context.Context) error {
return nil
}
func (f fakeGalleryService) Gallery(ctx context.Context) ([]gallery.Photo, error) {
return f.photos, f.err
}
func (f fakeGalleryService) Photo(
ctx context.Context,
id string,
) (gallery.Photo, error) {
for _, photo := range f.photos {
if photo.ID == id {
return photo, nil
}
}
return gallery.Photo{}, service.ErrPhotoNotFound
}
func TestPhotos(t *testing.T) {
srv := New(Config{
Address: ":0",
Gallery: fakeGalleryService{
photos: []gallery.Photo{
{
ID: "abc123",
Metadata: metadata.Metadata{
Width: 100,
Height: 50,
DateTaken: time.Date(
2025,
1,
1,
12,
0,
0,
0,
time.UTC,
),
},
},
},
},
})
req := httptest.NewRequest(
http.MethodGet,
"/api/photos",
nil,
)
rec := httptest.NewRecorder()
srv.mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf(
"expected %d, got %d",
http.StatusOK,
rec.Code,
)
}
var response []photoResponse
if err := json.NewDecoder(rec.Body).Decode(&response); err != nil {
t.Fatalf("decode response: %v", err)
}
if len(response) != 1 {
t.Fatalf(
"expected 1 photo, got %d",
len(response),
)
}
photo := response[0]
if photo.ID != "abc123" {
t.Fatalf(
"expected id %q, got %q",
"abc123",
photo.ID,
)
}
if photo.ThumbnailURL != "/thumb/abc123" {
t.Fatalf(
"unexpected thumbnail url: %q",
photo.ThumbnailURL,
)
}
if photo.PhotoURL != "/photo/abc123" {
t.Fatalf(
"unexpected photo url: %q",
photo.PhotoURL,
)
}
}
|