require "rails_helper" RSpec.describe Mediamtx::CleanupOrphanPaths do let!(:club) { Club.create!(name: "Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") } let!(:team) { club.teams.create!(name: "Team", sport: "volleyball") } let!(:user) { User.create!(email: "coach@test.com", name: "Coach", password: "password123", role: "coach") } let!(:match) { team.matches.create!(opponent_name: "Rival", scheduled_at: 1.day.from_now) } let(:client) { instance_double(Mediamtx::Client) } def path_item(name, online: false, ready: true) { "name" => name, "online" => online, "ready" => ready } end before do allow(client).to receive(:online_path_names).and_return(Set.new) allow(client).to receive(:delete_path) allow(client).to receive(:delete_path_name) end it "rimuove path live/match_{uuid} di sessioni terminate" do session = StreamSession.create!( match: match, user: user, platform: "matchlivetv", status: "ended", ended_at: 1.hour.ago ) allow(client).to receive(:list_paths).and_return([ path_item(session.mediamtx_path_name) ]) result = described_class.new(client: client).call expect(result.removed).to eq(1) expect(client).to have_received(:delete_path).with(session) end it "rimuove path orfani senza sessione nel database" do orphan_id = SecureRandom.uuid allow(client).to receive(:list_paths).and_return([ path_item("live/match_#{orphan_id}") ]) result = described_class.new(client: client).call expect(result.removed).to eq(1) expect(client).to have_received(:delete_path_name).with("live/match_#{orphan_id}") expect(client).to have_received(:delete_path_name).with("live/match_#{orphan_id}_air") end it "non rimuove path con publisher online" do session = StreamSession.create!( match: match, user: user, platform: "matchlivetv", status: "ended", ended_at: 1.hour.ago ) allow(client).to receive(:online_path_names).and_return(Set.new([session.mediamtx_path_name])) allow(client).to receive(:list_paths).and_return([ path_item(session.mediamtx_path_name, online: true) ]) result = described_class.new(client: client).call expect(result.removed).to eq(0) expect(client).not_to have_received(:delete_path) end it "non rimuove path di sessioni ancora live" do session = StreamSession.create!( match: match, user: user, platform: "matchlivetv", status: "live" ) allow(client).to receive(:list_paths).and_return([ path_item(session.mediamtx_path_name) ]) result = described_class.new(client: client).call expect(result.removed).to eq(0) expect(client).not_to have_received(:delete_path) end it "non rimuove path con replay in elaborazione recente" do session = StreamSession.create!( match: match, user: user, platform: "matchlivetv", status: "ended", ended_at: 30.minutes.ago ) Recording.create!( stream_session: session, team: team, status: "processing", privacy_status: "public" ) allow(client).to receive(:list_paths).and_return([ path_item(session.mediamtx_path_name) ]) result = described_class.new(client: client).call expect(result.removed).to eq(0) expect(client).not_to have_received(:delete_path) end it "ignora path che non sono live/match_{uuid}" do allow(client).to receive(:list_paths).and_return([ path_item("all_others"), path_item("match_legacy_without_live_prefix") ]) result = described_class.new(client: client).call expect(result.removed).to eq(0) expect(result.skipped).to eq(2) end end