0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 23:04:22 +08:00
discourse/spec/lib/letter_avatar_spec.rb
Alan Guo Xiang Tan 9e2a10b427
DEV: Add image-processing regression coverage (#42117)
Previously, important image-processing behavior lacked precise
regression coverage, making image-library upgrades difficult to assess
safely.

This change adds coverage without changing production behavior:

- Exercises public upload and image-model outcomes for SVG sanitization,
format detection and conversion, transparency, animation metadata, EXIF
orientation normalization, malformed input rejection, crop positioning,
tiny-image resizing, dominant-color caching, and image-quality
selection.
- Keeps normal LetterAvatar RSpec coverage portable by generating one
representative PNG and checking its format and requested dimensions.
- Adds a separate Linux-only `script/letter_avatar_pixel_diff` workflow
that compares A–Z renders with committed baselines and generates an HTML
expected/actual/highlighted-diff report. Use `--update-baselines` when
intentionally accepting rendering changes.
- Uses deterministic fixtures with regeneration guidance beside the
relevant specs.
2026-07-30 15:28:07 +08:00

35 lines
977 B
Ruby
Vendored

# frozen_string_literal: true
require "letter_avatar"
RSpec.describe LetterAvatar do
describe ".cleanup_old" do
it "removes stale cache directories" do
path = LetterAvatar.cache_path
FileUtils.mkdir_p(path + "junk")
LetterAvatar.generate("test", 100)
LetterAvatar.cleanup_old
expect(Dir.entries(File.dirname(path)).length).to eq(3)
end
end
describe ".generate" do
it "generates a PNG avatar with the requested dimensions" do
username = "A"
avatar_size = 45
generated_path = described_class.generate(username, avatar_size, cache: false)
expect(FastImage.type(generated_path)).to eq(:png)
expect(FastImage.size(generated_path)).to eq([avatar_size, avatar_size])
ensure
if generated_path
identity = LetterAvatar::Identity.from_username(username)
FileUtils.rm_f(generated_path)
FileUtils.rm_f(described_class.fullsize_path(identity))
end
end
end
end