mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-18 03:36:35 -07:00
chore: initial public commit
This commit is contained in:
commit
fc9054b78c
250 changed files with 32809 additions and 0 deletions
74
internal/catalog/images_test.go
Normal file
74
internal/catalog/images_test.go
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
package catalog_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/gabehf/koito/internal/catalog"
|
||||
"github.com/gabehf/koito/internal/cfg"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestImageLifecycle(t *testing.T) {
|
||||
|
||||
// serve yuu.jpg as test image
|
||||
imageBytes, err := os.ReadFile(filepath.Join("static", "yuu.jpg"))
|
||||
require.NoError(t, err)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "image/jpeg")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(imageBytes)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
imgID := uuid.New()
|
||||
|
||||
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
|
||||
require.NoError(t, err)
|
||||
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeMedium)
|
||||
require.NoError(t, err)
|
||||
|
||||
// ensure download is correct
|
||||
|
||||
imagePath := filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "full", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.NoError(t, err)
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NoError(t, catalog.DeleteImage(imgID))
|
||||
|
||||
// ensure delete works
|
||||
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "full", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.Error(t, err)
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.Error(t, err)
|
||||
|
||||
// re-download for prune
|
||||
|
||||
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeFull)
|
||||
require.NoError(t, err)
|
||||
err = catalog.DownloadAndCacheImage(context.Background(), imgID, server.URL, catalog.ImageSizeMedium)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.NoError(t, catalog.PruneOrphanedImages(context.Background(), store))
|
||||
|
||||
// ensure prune works
|
||||
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "full", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.Error(t, err)
|
||||
imagePath = filepath.Join(cfg.ConfigDir(), catalog.ImageCacheDir, "medium", imgID.String())
|
||||
_, err = os.Stat(imagePath)
|
||||
assert.Error(t, err)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue