# frozen_string_literal: true require "rails_helper" RSpec.describe "Analytics snapshots", type: :request do def jpeg_upload path = Rails.root.join("tmp/analytics-snap-test.jpg") FileUtils.mkdir_p(path.dirname) # minimal valid-ish jpeg bytes for ActiveStorage File.binwrite(path, "\xFF\xD8\xFF\xE0#{'x' * 12_000}\xFF\xD9") Rack::Test::UploadedFile.new(path, "image/jpeg") end it "accetta uno screenshot e lo associa al path" do post "/analytics/snapshot", params: { path: "/prezzi", device: "desktop", width: 1440, height: 2200, image: jpeg_upload } expect(response).to have_http_status(:accepted) snap = AnalyticsPageSnapshot.find_by(page_path: "/prezzi", device: "desktop") expect(snap).to be_present expect(snap.image).to be_attached expect(snap.width).to eq(1440) end it "rifiuta path esclusi" do expect do post "/analytics/snapshot", params: { path: "/admin/analytics", device: "desktop", width: 1440, height: 900, image: jpeg_upload } end.not_to change(AnalyticsPageSnapshot, :count) expect(response).to have_http_status(:unprocessable_content) end it "salta snapshot con cookie anteprima staff" do expect do post "/analytics/snapshot", params: { path: "/prezzi-suppress", device: "desktop", width: 1440, height: 2200, image: jpeg_upload }, headers: { "Cookie" => "#{Analytics::Suppress::COOKIE_NAME}=1" } end.not_to change(AnalyticsPageSnapshot, :count) expect(response).to have_http_status(:accepted) body = JSON.parse(response.body) expect(body["skipped"]).to eq(true) expect(body["suppressed"]).to eq(true) end end