consolidate image serving and clean up pipeline
Move all image URL logic into ProductImage.url/2 and thumbnail_url/1, remove dead on-demand generation code from Optimizer, strip controller routes down to SVG recolor only, fix mockup startup check to verify all variant formats, and isolate test image cache directory. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,13 +17,9 @@ defmodule SimpleshopTheme.Images.OptimizeWorkerTest do
|
||||
|
||||
assert :ok = perform_job(OptimizeWorker, %{image_id: image.id})
|
||||
|
||||
# Verify pre-generated variants were created (AVIF and WebP only, not JPEG)
|
||||
for w <- [400, 800, 1200], fmt <- [:avif, :webp] do
|
||||
for w <- [400, 800, 1200], fmt <- [:avif, :webp, :jpg] do
|
||||
assert File.exists?(cache_path(image.id, w, fmt))
|
||||
end
|
||||
|
||||
# JPEG is generated on-demand, not pre-generated
|
||||
refute File.exists?(cache_path(image.id, 400, :jpg))
|
||||
end
|
||||
|
||||
test "cancels for missing image" do
|
||||
|
||||
@@ -62,16 +62,14 @@ defmodule SimpleshopTheme.Images.OptimizerTest do
|
||||
|
||||
assert {:ok, [400, 800, 1200]} = Optimizer.process_for_image(image.id)
|
||||
|
||||
# Only AVIF and WebP are pre-generated (JPEG is on-demand)
|
||||
for w <- [400, 800, 1200], fmt <- [:avif, :webp] do
|
||||
# Source WebP for Plug.Static serving
|
||||
assert File.exists?(Path.join(Optimizer.cache_dir(), "#{image.id}.webp"))
|
||||
|
||||
for w <- [400, 800, 1200], fmt <- [:avif, :webp, :jpg] do
|
||||
assert File.exists?(cache_path(image.id, w, fmt)),
|
||||
"Missing #{w}.#{fmt}"
|
||||
end
|
||||
|
||||
# JPEG should NOT be pre-generated
|
||||
refute File.exists?(cache_path(image.id, 400, :jpg))
|
||||
|
||||
# Thumbnail is still pre-generated as JPEG
|
||||
assert File.exists?(cache_path(image.id, "thumb", :jpg))
|
||||
end
|
||||
|
||||
@@ -138,36 +136,4 @@ defmodule SimpleshopTheme.Images.OptimizerTest do
|
||||
refute Optimizer.disk_variants_exist?(image.id, 1200)
|
||||
end
|
||||
end
|
||||
|
||||
describe "generate_jpeg_on_demand/3" do
|
||||
test "generates JPEG variant and caches to disk" do
|
||||
image = image_fixture(%{source_width: 1200, source_height: 800})
|
||||
|
||||
# JPEG shouldn't exist yet
|
||||
refute File.exists?(cache_path(image.id, 400, :jpg))
|
||||
|
||||
# Generate on-demand
|
||||
{:ok, path} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 400)
|
||||
|
||||
assert File.exists?(path)
|
||||
assert path == cache_path(image.id, 400, :jpg)
|
||||
end
|
||||
|
||||
test "returns cached path if JPEG already exists" do
|
||||
image = image_fixture(%{source_width: 1200, source_height: 800})
|
||||
|
||||
# Generate first time
|
||||
{:ok, path1} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 800)
|
||||
{:ok, %{mtime: mtime1}} = File.stat(path1)
|
||||
|
||||
Process.sleep(1100)
|
||||
|
||||
# Generate second time - should return cached
|
||||
{:ok, path2} = Optimizer.generate_jpeg_on_demand(image.data, image.id, 800)
|
||||
{:ok, %{mtime: mtime2}} = File.stat(path2)
|
||||
|
||||
assert path1 == path2
|
||||
assert mtime1 == mtime2, "File was regenerated instead of cached"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -67,28 +67,49 @@ defmodule SimpleshopTheme.Products.ProductImageTest do
|
||||
# Display helpers
|
||||
# =============================================================================
|
||||
|
||||
describe "display_url/2" do
|
||||
describe "url/2" do
|
||||
test "prefers local image_id over src" do
|
||||
image = %{image_id: "abc-123", src: "https://cdn.example.com/img.jpg"}
|
||||
assert ProductImage.display_url(image) == "/images/abc-123/variant/800.webp"
|
||||
assert ProductImage.url(image) == "/image_cache/abc-123-800.webp"
|
||||
end
|
||||
|
||||
test "accepts custom size" do
|
||||
test "accepts custom width" do
|
||||
image = %{image_id: "abc-123", src: "https://cdn.example.com/img.jpg"}
|
||||
assert ProductImage.display_url(image, 400) == "/images/abc-123/variant/400.webp"
|
||||
assert ProductImage.url(image, 400) == "/image_cache/abc-123-400.webp"
|
||||
end
|
||||
|
||||
test "handles mockup URLs with size suffix" do
|
||||
image = %{image_id: nil, src: "/mockups/product-1"}
|
||||
assert ProductImage.url(image, 800) == "/mockups/product-1-800.webp"
|
||||
end
|
||||
|
||||
test "falls back to src when no image_id" do
|
||||
image = %{image_id: nil, src: "https://cdn.example.com/img.jpg"}
|
||||
assert ProductImage.display_url(image) == "https://cdn.example.com/img.jpg"
|
||||
assert ProductImage.url(image) == "https://cdn.example.com/img.jpg"
|
||||
end
|
||||
|
||||
test "returns nil when neither image_id nor src" do
|
||||
assert ProductImage.display_url(%{image_id: nil, src: nil}) == nil
|
||||
assert ProductImage.url(%{image_id: nil, src: nil}) == nil
|
||||
end
|
||||
|
||||
test "returns nil for nil input" do
|
||||
assert ProductImage.display_url(nil) == nil
|
||||
assert ProductImage.url(nil) == nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "thumbnail_url/1" do
|
||||
test "returns thumb path for local image" do
|
||||
image = %{image_id: "abc-123"}
|
||||
assert ProductImage.thumbnail_url(image) == "/image_cache/abc-123-thumb.jpg"
|
||||
end
|
||||
|
||||
test "falls back to src when no image_id" do
|
||||
image = %{image_id: nil, src: "https://cdn.example.com/img.jpg"}
|
||||
assert ProductImage.thumbnail_url(image) == "https://cdn.example.com/img.jpg"
|
||||
end
|
||||
|
||||
test "returns nil for nil input" do
|
||||
assert ProductImage.thumbnail_url(nil) == nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user