Files
redux-scraper/spec/models/domain/post_file_thumbnail_spec.rb
2025-03-03 23:28:05 +00:00

172 lines
4.5 KiB
Ruby

# typed: false
require "rails_helper"
RSpec.describe Domain::PostFileThumbnail do
describe ".find_or_create_from_post_file" do
let(:post) { create(:domain_post_fa_post) }
let(:blob_file) do
fixture_path =
Rails.root.join(
"test/fixtures/files/images/thumb-036aaab6-content-container.jpeg",
)
create(
:blob_file,
contents: File.binread(fixture_path),
content_type: "image/jpeg",
)
end
let(:log_entry) do
create(:http_log_entry, content_type: "image/jpeg", response: blob_file)
end
let(:post_file) do
create(:domain_post_file, post: post, state: "ok", log_entry: log_entry)
end
before do
# Ensure directories exist
FileUtils.mkdir_p(Domain::PostFileThumbnail::TMP_DIR)
end
it "creates a thumbnail for a valid post file" do
thumbnail =
described_class.find_or_create_from_post_file(
post_file,
Domain::ThumbnailType::Small,
)
expect(thumbnail).to be_a(Domain::PostFileThumbnail)
expect(thumbnail.post_file).to eq(post_file)
expect(thumbnail.thumbnail_type).to eq("small")
# Verify the thumbnail file was created on disk
thumbnail_path = thumbnail.absolute_file_path
expect(thumbnail_path).not_to be_nil
expect(File.exist?(thumbnail_path)).to be true
end
it "returns existing thumbnail if one already exists" do
# Create a thumbnail first
existing =
described_class.find_or_create_from_post_file(
post_file,
Domain::ThumbnailType::Small,
)
expect(existing).not_to be_nil
# Try to find or create again
thumbnail =
described_class.find_or_create_from_post_file(
post_file,
Domain::ThumbnailType::Small,
)
# Should return the existing one without creating a new one
expect(thumbnail).to eq(existing)
end
it "returns nil for a post file that's not in 'ok' state" do
post_file.update!(state: "pending")
thumbnail =
described_class.find_or_create_from_post_file(
post_file,
Domain::ThumbnailType::Small,
)
expect(thumbnail).to be_nil
end
it "returns nil for a post file without a log entry" do
post_file.update!(log_entry: nil)
thumbnail =
described_class.find_or_create_from_post_file(
post_file,
Domain::ThumbnailType::Small,
)
expect(thumbnail).to be_nil
end
it "returns nil for a post file with an unsupported content type" do
# Create a new log entry with PDF content type since HttpLogEntry is immutable
pdf_blob =
create(
:blob_file,
content_type: "application/pdf",
contents: "fake pdf data",
)
pdf_log_entry =
create(
:http_log_entry,
content_type: "application/pdf",
response: pdf_blob,
)
pdf_post_file =
create(
:domain_post_file,
post: post,
state: "ok",
log_entry: pdf_log_entry,
)
thumbnail =
described_class.find_or_create_from_post_file(
pdf_post_file,
Domain::ThumbnailType::Small,
)
expect(thumbnail).to be_nil
end
it "returns nil if the post file has no blob" do
allow(post_file).to receive(:blob).and_return(nil)
thumbnail =
described_class.find_or_create_from_post_file(
post_file,
Domain::ThumbnailType::Small,
)
expect(thumbnail).to be_nil
end
end
describe "#absolute_file_path" do
let(:post) { create(:domain_post_fa_post) }
let(:blob_file) do
create(
:blob_file,
content_type: "image/jpeg",
contents: "fake image data",
)
end
let(:post_file) do
create(:domain_post_file, post: post, state: "ok", blob: blob_file)
end
let(:thumbnail) do
create(
:domain_post_file_thumbnail,
post_file: post_file,
thumbnail_type: "small",
)
end
it "constructs the correct file path" do
expect(thumbnail.absolute_file_path).to start_with(
Domain::PostFileThumbnail::THUMBNAIL_ROOT_DIR,
)
end
it "returns nil if thumbnail_type is nil" do
thumbnail.thumbnail_type = nil
expect(thumbnail.absolute_file_path).to be_nil
end
it "returns nil if post_file is nil" do
thumbnail.post_file = nil
expect(thumbnail.absolute_file_path).to be_nil
end
end
end