From 887d212288db292dbbc557191ef1507b70ca8507 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:30:02 +0530 Subject: refactor(api): abstract thumbnail generator --- internal/api/api_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 internal/api/api_test.go (limited to 'internal/api/api_test.go') diff --git a/internal/api/api_test.go b/internal/api/api_test.go new file mode 100644 index 0000000..3940f6e --- /dev/null +++ b/internal/api/api_test.go @@ -0,0 +1,27 @@ +package api + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestHealth(t *testing.T) { + server := New(Config{ + Address: ":0", + }) + + req := httptest.NewRequest( + http.MethodGet, + "/health", + nil, + ) + + rec := httptest.NewRecorder() + + server.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d", rec.Code) + } +} -- cgit v1.2.3 From b0857d4bf6f899426c5e1a115a60799332636afc Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:35:37 +0530 Subject: test(api): add photos endpoint tests --- internal/api/api_test.go | 107 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 102 insertions(+), 5 deletions(-) (limited to 'internal/api/api_test.go') 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, + ) } } -- cgit v1.2.3 From 5f9a07afe0fd42cd9870c864eb30941343bf51e9 Mon Sep 17 00:00:00 2001 From: RanXom Date: Mon, 13 Jul 2026 19:39:06 +0530 Subject: test(api): add thumbnail endpoint tests --- internal/api/api_test.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'internal/api/api_test.go') diff --git a/internal/api/api_test.go b/internal/api/api_test.go index c54514d..c1980fd 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -11,6 +11,7 @@ import ( "github.com/RanXom/galleryd/internal/gallery" "github.com/RanXom/galleryd/internal/metadata" "github.com/RanXom/galleryd/internal/service" + "github.com/RanXom/galleryd/internal/thumbnail" ) type fakeGalleryService struct { @@ -18,6 +19,24 @@ type fakeGalleryService struct { err error } +type fakeThumbnailGenerator struct { + path string + err error +} + +func (f fakeThumbnailGenerator) Generate( + ctx context.Context, + photo gallery.Photo, +) (thumbnail.Thumbnail, error) { + if f.err != nil { + return thumbnail.Thumbnail{}, f.err + } + + return thumbnail.Thumbnail{ + Path: f.path, + }, nil +} + func (f fakeGalleryService) Load(ctx context.Context) error { return nil } @@ -122,3 +141,46 @@ func TestPhotos(t *testing.T) { ) } } + +func TestThumbnail(t *testing.T) { + srv := New(Config{ + Address: ":0", + + Gallery: fakeGalleryService{ + photos: []gallery.Photo{ + { + ID: "abc123", + }, + }, + }, + + Thumbnails: fakeThumbnailGenerator{ + path: "../scanner/testdata/gallery/vac/image.webp", + }, + }) + + req := httptest.NewRequest( + http.MethodGet, + "/thumb/abc123", + nil, + ) + + rec := httptest.NewRecorder() + + srv.mux.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf( + "expected %d, got %d", + http.StatusOK, + rec.Code, + ) + } + + if got := rec.Header().Get("Content-Type"); got != "image/webp" { + t.Fatalf( + "expected image/webp, got %q", + got, + ) + } +} -- cgit v1.2.3