# frozen_string_literal: true require "rails_helper" RSpec.describe AppAnnouncement do def create_notice!(**attrs) described_class.create!({ kind: "info", severity: "info", status: "published", title: "Avviso", body: "Testo", show_on_android: true, show_on_ios: true, show_on_web_public: false, show_on_web_private: false }.merge(attrs)) end it "is active on the app when published without a time window" do item = create_notice!(kind: "maintenance", severity: "warning", title: "Manutenzione", body: "Domani dalle 2 alle 4.") expect(described_class.for_app).to include(item) expect(described_class.for_channel(:web_public)).not_to include(item) end it "hides drafts and expired notices" do create_notice!(status: "draft", title: "Bozza", body: "Non visibile") create_notice!(title: "Scaduto", body: "Fine", ends_at: 1.hour.ago) live = create_notice!(kind: "update", title: "Aggiornamento", body: "Nuova versione") expect(described_class.for_app.map(&:title)).to eq(["Aggiornamento"]) expect(live.as_api_json(platform: "android")[:action_url]).to eq(MatchLiveTv.play_store_url) expect(live.as_api_json(platform: "ios")[:action_url]).to eq(MatchLiveTv.app_store_url) end it "filters by selected channels" do public_item = create_notice!( title: "Pubblico", show_on_android: false, show_on_ios: false, show_on_web_public: true ) private_item = create_notice!( title: "Privato", show_on_android: false, show_on_ios: false, show_on_web_private: true ) android_item = create_notice!(title: "Android", show_on_ios: false) ios_item = create_notice!(title: "iOS", show_on_android: false, show_on_ios: true) expect(described_class.for_channel(:web_public)).to contain_exactly(public_item) expect(described_class.for_channel(:web_private)).to contain_exactly(private_item) expect(described_class.for_platform("android")).to contain_exactly(android_item) expect(described_class.for_platform("ios")).to contain_exactly(ios_item) expect(described_class.for_app).to contain_exactly(android_item, ios_item) end it "uses platform-specific copy and links when present" do item = create_notice!( title: "Comune", body: "Testo comune", action_url: "https://example.com/comune", action_label: "Apri", android_title: "Aggiorna su Android", android_body: "Apri Play Store", android_action_url: "https://play.google.com/store/apps/details?id=com.matchlivetv.match_live_tv", android_action_label: "Play Store", ios_title: "Aggiorna su iOS", ios_body: "Apri App Store", ios_action_url: "https://apps.apple.com/app/id000", ios_action_label: "App Store" ) android = item.as_api_json(platform: "android") ios = item.as_api_json(platform: "ios") expect(android[:title]).to eq("Aggiorna su Android") expect(android[:body]).to eq("Apri Play Store") expect(android[:action_url]).to include("play.google.com") expect(android[:action_label]).to eq("Play Store") expect(ios[:title]).to eq("Aggiorna su iOS") expect(ios[:body]).to eq("Apri App Store") expect(ios[:action_url]).to include("apps.apple.com") expect(ios[:action_label]).to eq("App Store") end it "requires at least one channel" do item = described_class.new( kind: "info", status: "draft", title: "X", body: "Y", show_on_android: false, show_on_ios: false, show_on_web_public: false, show_on_web_private: false ) expect(item).not_to be_valid expect(item.errors[:base]).to be_present end end