aboutsummaryrefslogtreecommitdiff
path: root/internal/api/api_test.go
diff options
context:
space:
mode:
authorRanXom <saiyedshizain@gmail.com>2026-07-13 19:35:37 +0530
committerRanXom <saiyedshizain@gmail.com>2026-07-13 19:35:37 +0530
commitb0857d4bf6f899426c5e1a115a60799332636afc (patch)
tree95088d63a2ded826d104c060f72d5e46a8cd2a5a /internal/api/api_test.go
parent887d212288db292dbbc557191ef1507b70ca8507 (diff)
downloadgalleryd-b0857d4bf6f899426c5e1a115a60799332636afc.tar.xz
galleryd-b0857d4bf6f899426c5e1a115a60799332636afc.zip
test(api): add photos endpoint tests
Diffstat (limited to 'internal/api/api_test.go')
-rw-r--r--internal/api/api_test.go107
1 files changed, 102 insertions, 5 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go
index 3940f6e..c54514d 100644
--- a/internal/api/api_test.go
+++ b/internal/api/api_test.go
@@ -1,27 +1,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"
)
-func TestHealth(t *testing.T) {
- server := New(Config{
+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,
- "/health",
+ "/api/photos",
nil,
)
rec := httptest.NewRecorder()
- server.mux.ServeHTTP(rec, req)
+ srv.mux.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
- t.Fatalf("expected 200, got %d", rec.Code)
+ 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,
+ )
}
}