Files
akkounts/spec/services/nostr_manager/process_profile_image_spec.rb
raucao ab3c2bffd6 Process and relay Nostr profile images server-side
Add a server-side API for avatar and header images: uploads are cropped with Vips (400x400 and 1500x500, GIFs passed through, EXIF stripped, transparency flattened to white) and stored as one pending attachment per field. Saving the profile flushes them to the configured Blossom server, authorized by a user-signed kind-24242 token whose x tags cover the stored hashes.
2026-11-19 21:58:08 +04:00

62 lines
2.1 KiB
Ruby

require "rails_helper"
RSpec.describe NostrManager::ProcessProfileImage, type: :model do
let(:fixture_path) { Rails.root.join("spec/fixtures/files/taipei.jpg") }
let(:bytes) { File.binread(fixture_path) }
let(:gif_bytes) { Base64.decode64("R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7") }
def process(field:, content_type: "image/jpeg", data: bytes)
described_class.call(field: field, io: StringIO.new(data), content_type: content_type)
end
def dimensions(image_bytes)
image = Vips::Image.new_from_buffer(image_bytes, "")
[image.width, image.height]
end
it "cover-crops the avatar to 400x400 without flattening a non-alpha image" do
result = process(field: "picture")
expect(dimensions(result)).to eq([400, 400])
expect(Vips::Image.new_from_buffer(result, "").bands).to eq(3)
end
it "cover-crops the header image to 1500x500" do
result = process(field: "banner")
expect(dimensions(result)).to eq([1500, 500])
end
it "passes GIFs through unchanged" do
expect(process(field: "picture", content_type: "image/gif", data: gif_bytes)).to eq(gif_bytes)
end
it "flattens transparency to white for JPEG output" do
transparent = Vips::Image.black(50, 50, bands: 4).write_to_buffer(".webp")
result = process(field: "picture", content_type: "image/webp", data: transparent)
image = Vips::Image.new_from_buffer(result, "")
expect(image.bands).to eq(3)
expect(image.getpoint(20, 20)).to eq([255.0, 255.0, 255.0])
end
it "preserves alpha for PNG output" do
transparent = Vips::Image.black(50, 50, bands: 4).write_to_buffer(".png")
result = process(field: "picture", content_type: "image/png", data: transparent)
image = Vips::Image.new_from_buffer(result, "")
expect(image.bands).to eq(4)
end
it "rejects unknown fields" do
expect(process(field: "something")).to be_nil
end
it "rejects data that is not an image" do
expect(process(field: "picture", data: "not an image")).to be_nil
end
it "rejects images over the pixel/size guard" do
stub_const("NostrManager::ProcessProfileImage::MAX_SIDE", 10)
expect(process(field: "picture")).to be_nil
end
end