split common visual search logic out

This commit is contained in:
Dylan Knutson
2025-08-14 19:11:13 +00:00
parent 90d2cce076
commit 2a8d631b29
4 changed files with 173 additions and 112 deletions

View File

@@ -130,33 +130,46 @@ RSpec.describe Domain::PostsController, type: :controller do
context "with an image URL" do
let(:mock_hash_value) { "1010101010101010" }
let(:mock_detail_hash_value) { "0101010101010101" }
let(:mock_fingerprints) { Domain::PostFile::BitFingerprint.none }
let(:temp_file_path) { "/tmp/test_image.jpg" }
# Mock the GenerateFingerprintsResult structure that generate_fingerprints returns
let(:mock_generate_fingerprints_results) do
[
Domain::VisualSearchHelper::GenerateFingerprintsResult.new(
thumb_path: "/tmp/thumb1.jpg",
fingerprint: mock_hash_value,
detail_fingerprint: mock_detail_hash_value,
),
]
end
# Mock the final similar fingerprints results
let(:mock_similar_fingerprints) { Domain::PostFile::BitFingerprint.none }
it "uses Phash::Fingerprint model methods for fingerprinting and finding similar images" do
# We need to mock the image downloading and processing since we can't do that in tests
allow(controller).to receive(:process_image_input).and_return(
[temp_file_path, "image/jpeg"],
)
allow(controller).to receive(:create_thumbnail).and_return(
"data:image/jpeg;base64,FAKE",
)
# Set up expectations for our model methods - this is what we're really testing
expect(Domain::PostFile::BitFingerprint).to receive(
:from_file_path,
).with(temp_file_path).and_return(mock_hash_value)
# Add expectation for detail fingerprint
expect(Domain::PostFile::BitFingerprint).to receive(
:detail_from_file_path,
).with(temp_file_path).and_return(mock_detail_hash_value)
expect(controller.helpers).to receive(:generate_fingerprints).with(
temp_file_path,
"image/jpeg",
anything,
).and_return(mock_generate_fingerprints_results)
# Mock the similar fingerprints search
expect(controller.helpers).to receive(:find_similar_fingerprints).with(
fingerprint_value: mock_hash_value,
fingerprint_detail_value: mock_detail_hash_value,
).and_return(mock_fingerprints)
mock_generate_fingerprints_results,
).and_return(mock_similar_fingerprints)
# Mock the thumbnail data URI creation
expect(controller.helpers).to receive(
:create_image_thumbnail_data_uri,
).with("/tmp/thumb1.jpg", "image/jpeg").and_return(
"data:image/jpeg;base64,FAKE",
)
post :visual_results,
params: {
@@ -170,11 +183,7 @@ RSpec.describe Domain::PostsController, type: :controller do
expect(assigns(:uploaded_image_data_uri)).to eq(
"data:image/jpeg;base64,FAKE",
)
expect(assigns(:uploaded_hash_value)).to eq(mock_hash_value)
expect(assigns(:uploaded_detail_hash_value)).to eq(
mock_detail_hash_value,
)
expect(assigns(:matches)).to eq(mock_fingerprints.to_a)
expect(assigns(:matches)).to eq(mock_similar_fingerprints.to_a)
end
end
end