# frozen_string_literal: true require "rails_helper" RSpec.describe "Analytics ingest", type: :request do it "accetta eventi validi, normalizza il path e aggrega" do post "/analytics/events", params: { events: [ { type: "pageview", path: "/clubs/461d369a-104c-4d3c-8860-1518b4e95d35", device: "desktop", ts: Time.current.to_i * 1000 }, { type: "click", path: "/clubs/461d369a-104c-4d3c-8860-1518b4e95d35", device: "desktop", x: 25, y: 50, ts: Time.current.to_i * 1000 }, { type: "move", path: "/clubs/461d369a-104c-4d3c-8860-1518b4e95d35", device: "desktop", x: 40, y: 30, n: 12, ts: Time.current.to_i * 1000 }, { type: "scroll", path: "/clubs/461d369a-104c-4d3c-8860-1518b4e95d35", device: "desktop", scroll: 80, ts: Time.current.to_i * 1000 } ] }, as: :json expect(response).to have_http_status(:accepted) body = JSON.parse(response.body) expect(body["accepted"]).to eq(4) expect(AnalyticsEvent.count).to eq(0) expect(AnalyticsPageStat.find_by(page_path: "/clubs/:id").pageview_count).to eq(1) expect(AnalyticsPageStat.find_by(page_path: "/clubs/:id").max_scroll_pct).to eq(80) expect(AnalyticsPageCell.where(page_path: "/clubs/:id").sum(:click_count)).to eq(1) expect(AnalyticsPageCell.where(page_path: "/clubs/:id").sum(:move_count)).to eq(12) end it "rifiuta path esclusi" do post "/analytics/events", params: { events: [ { type: "pageview", path: "/admin/ops", device: "desktop", ts: Time.current.to_i * 1000 }, { type: "pageview", path: "/live/461d369a-104c-4d3c-8860-1518b4e95d35", device: "mobile", ts: Time.current.to_i * 1000 } ] }, as: :json expect(response).to have_http_status(:accepted) expect(JSON.parse(response.body)["accepted"]).to eq(0) expect(AnalyticsEvent.count).to eq(0) end end