- Add comprehensive file information display (content type, size, dimensions) - Move complex image dimension logic from views to TelegramBotLogsHelper - Add percentage calculations to performance metrics section - Use Rails associations instead of manual BlobFile lookups - Update slow requests filtering to use total_request_time column - Enhance search result thumbnails and improve post linking - Add comprehensive test coverage for helper methods - Improve error handling and type safety throughout
127 lines
4.0 KiB
Ruby
127 lines
4.0 KiB
Ruby
# typed: false
|
||
|
||
require "rails_helper"
|
||
|
||
RSpec.describe TelegramBotLogsHelper, type: :helper do
|
||
describe "#status_color_class" do
|
||
it "returns blue classes for processing status" do
|
||
log = build(:telegram_bot_log, :processing)
|
||
expect(helper.status_color_class(log)).to eq("bg-blue-100 text-blue-800")
|
||
end
|
||
|
||
it "returns green classes for success status" do
|
||
log = build(:telegram_bot_log, :successful)
|
||
expect(helper.status_color_class(log)).to eq(
|
||
"bg-green-100 text-green-800",
|
||
)
|
||
end
|
||
|
||
it "returns red classes for error status" do
|
||
log = build(:telegram_bot_log, :with_error)
|
||
expect(helper.status_color_class(log)).to eq("bg-red-100 text-red-800")
|
||
end
|
||
|
||
it "returns orange classes for invalid_image status" do
|
||
log = build(:telegram_bot_log, :invalid_image)
|
||
expect(helper.status_color_class(log)).to eq(
|
||
"bg-orange-100 text-orange-800",
|
||
)
|
||
end
|
||
|
||
it "returns slate classes for unknown status" do
|
||
log = build(:telegram_bot_log)
|
||
allow(log).to receive(:status).and_return("unknown_status")
|
||
expect(helper.status_color_class(log)).to eq(
|
||
"bg-slate-100 text-slate-800",
|
||
)
|
||
end
|
||
|
||
it "handles success status with no results" do
|
||
log = build(:telegram_bot_log, :with_no_results)
|
||
expect(helper.status_color_class(log)).to eq(
|
||
"bg-green-100 text-green-800",
|
||
)
|
||
end
|
||
end
|
||
|
||
describe "#image_dimensions_for_blob_file" do
|
||
it "returns N/A for nil blob_file" do
|
||
expect(helper.image_dimensions_for_blob_file(nil)).to eq("N/A")
|
||
end
|
||
|
||
it "returns N/A for non-image content type" do
|
||
blob_file = build(:blob_file, content_type: "text/plain")
|
||
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq("N/A")
|
||
end
|
||
|
||
it "returns dimensions for static image" do
|
||
blob_file = build(:blob_file, content_type: "image/jpeg")
|
||
mock_vips_image = double("Vips::Image", width: 1920, height: 1080)
|
||
mock_media = instance_double(LoadedMedia::StaticImage)
|
||
|
||
allow(LoadedMedia).to receive(:from_file).with(
|
||
blob_file.content_type,
|
||
blob_file.absolute_file_path,
|
||
).and_return(mock_media)
|
||
allow(mock_media).to receive(:is_a?).with(
|
||
LoadedMedia::StaticImage,
|
||
).and_return(true)
|
||
allow(mock_media).to receive(:instance_variable_get).with(
|
||
:@vips_image,
|
||
).and_return(mock_vips_image)
|
||
|
||
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq(
|
||
"1920×1080",
|
||
)
|
||
end
|
||
|
||
it "returns dimensions for GIF" do
|
||
blob_file = build(:blob_file, content_type: "image/gif")
|
||
mock_media = instance_double(LoadedMedia::Gif)
|
||
|
||
allow(LoadedMedia).to receive(:from_file).with(
|
||
blob_file.content_type,
|
||
blob_file.absolute_file_path,
|
||
).and_return(mock_media)
|
||
allow(mock_media).to receive(:is_a?).with(
|
||
LoadedMedia::StaticImage,
|
||
).and_return(false)
|
||
allow(mock_media).to receive(:is_a?).with(LoadedMedia::Gif).and_return(
|
||
true,
|
||
)
|
||
allow(mock_media).to receive(:instance_variable_get).with(
|
||
:@width,
|
||
).and_return(800)
|
||
allow(mock_media).to receive(:instance_variable_get).with(
|
||
:@height,
|
||
).and_return(600)
|
||
|
||
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq("800×600")
|
||
end
|
||
|
||
it "returns N/A for unsupported media type" do
|
||
blob_file = build(:blob_file, content_type: "image/webp")
|
||
mock_media = double("UnsupportedMedia")
|
||
|
||
allow(LoadedMedia).to receive(:from_file).with(
|
||
blob_file.content_type,
|
||
blob_file.absolute_file_path,
|
||
).and_return(mock_media)
|
||
|
||
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq("N/A")
|
||
end
|
||
|
||
it "returns 'Unable to determine' when exception occurs" do
|
||
blob_file = build(:blob_file, content_type: "image/jpeg")
|
||
|
||
allow(LoadedMedia).to receive(:from_file).and_raise(
|
||
StandardError.new("File not found"),
|
||
)
|
||
|
||
expect(helper.image_dimensions_for_blob_file(blob_file)).to eq(
|
||
"Unable to determine",
|
||
)
|
||
end
|
||
end
|
||
end
|