aboutsummaryrefslogtreecommitdiff
path: root/internal/thumbnail/generator_test.go
diff options
context:
space:
mode:
authorSaiyed Shizain <saiyedshizain@gmail.com>2026-07-13 14:44:12 +0530
committerGitHub <noreply@github.com>2026-07-13 14:44:12 +0530
commitcc639337ed5ccb4634b2a338f764cf9c73b02e4f (patch)
treeeae608923098a07f1413988cf0c79fc8ce4211be /internal/thumbnail/generator_test.go
parented70370d9f58d39c3b31f5506e449aeb5d562a77 (diff)
parentc2e38ad8c3cff2d3c6ea627855b2b4f02dc3e9ba (diff)
downloadgalleryd-cc639337ed5ccb4634b2a338f764cf9c73b02e4f.tar.xz
galleryd-cc639337ed5ccb4634b2a338f764cf9c73b02e4f.zip
Merge pull request #5 from RanXom/feat/thumbnail
Feat/thumbnail: implement thumbnail generation and caching
Diffstat (limited to 'internal/thumbnail/generator_test.go')
-rw-r--r--internal/thumbnail/generator_test.go91
1 files changed, 91 insertions, 0 deletions
diff --git a/internal/thumbnail/generator_test.go b/internal/thumbnail/generator_test.go
new file mode 100644
index 0000000..a5fbf67
--- /dev/null
+++ b/internal/thumbnail/generator_test.go
@@ -0,0 +1,91 @@
+package thumbnail
+
+import (
+ "context"
+ "os"
+ "testing"
+ "time"
+
+ "github.com/RanXom/galleryd/internal/gallery"
+ "github.com/RanXom/galleryd/internal/scanner"
+ "github.com/RanXom/galleryd/internal/testfixtures"
+)
+
+func TestGenerator(t *testing.T) {
+ cacheDir := t.TempDir()
+
+ generator, err := New(cacheDir)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ photo := gallery.Photo{
+ ID: "canon40d",
+
+ File: scanner.File{
+ Path: testfixtures.Canon40D(),
+ Root: "photos",
+ RelativePath: "Canon_40D.jpg",
+ ModTime: time.Now(),
+ },
+ }
+
+ t.Run("generates thumbnail", func(t *testing.T) {
+ thumb, err := generator.Generate(
+ context.Background(),
+ photo,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if _, err := os.Stat(thumb.Path); err != nil {
+ t.Fatalf("thumbnail was not created: %v", err)
+ }
+ })
+
+ t.Run("reuses cached thumbnail", func(t *testing.T) {
+ first, err := generator.Generate(
+ context.Background(),
+ photo,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ info1, err := os.Stat(first.Path)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ time.Sleep(10 * time.Millisecond)
+
+ second, err := generator.Generate(
+ context.Background(),
+ photo,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ info2, err := os.Stat(second.Path)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !info1.ModTime().Equal(info2.ModTime()) {
+ t.Fatal("thumbnail was regenerated instead of reused")
+ }
+ })
+
+ t.Run("respects cancelled context", func(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ _, err := generator.Generate(ctx, photo)
+
+ if err == nil {
+ t.Fatal("expected context cancellation")
+ }
+ })
+}