require "rails_helper"

RSpec.describe StreamNotificationsController, type: :controller do
  render_views

  let!(:broadcast) { create(:broadcast, :with_stream, skip_create_callback: true, name: "Live Stream") }
  let(:active_status) { { type: "video.live_stream.active", object: { id: "mux_stream" } } }
  let(:disconnected_status) { { type: "video.live_stream.disconnected", object: { id: "mux_stream" } } }
  let(:idle_status) { { type: "video.live_stream.idle", object: { id: "mux_stream" } } }
  let(:idle_status_for_unknown_broadcast) { { type: "video.live_stream.idle", object: { id: "unknown-id" } } }
  let(:asset_ready) do
    {
      type: "video.asset.static_renditions.ready",
      object: { id: "asset_uid" },
      data: {
        playback_ids: [
          { id: "playback_uid" }
        ],
        static_renditions: {
          files: [{ name: "high.mp4" }]
        }
      },
      stream_notification: {
        data: {
          live_stream_id: "mux_stream"
        }
      }
    }
  end
  let(:full_live_stream_ready) do
    {
      type: "video.asset.ready",
      object: { id: "active_asset_uid" },
      data: {
        playback_ids: [
            { id: "full_live_stream_playback_uid" }
        ]
      },
      stream_notification: {
        data: {
          live_stream_id: "mux_stream"
        }
      }
    }
  end

  describe "#create" do
    before do
      allow(BroadcastsChannel).to receive(:broadcast_stream_updates).with(be_kind_of(Broadcast))
      allow(BroadcastsChannel).to receive(:stream_recording_ready)
    end

    it "updates the broadcast when active status is received in notification" do
      post :create, params: active_status

      expect(broadcast.reload).to be_active
      expect(BroadcastsChannel).to have_received(:broadcast_stream_updates).with(be_kind_of(Broadcast))
    end

    it "updates the broadcast when disconnected status is received in notification" do
      post :create, params: disconnected_status

      expect(broadcast.reload).to be_streamer_disconnected
      expect(BroadcastsChannel).to have_received(:broadcast_stream_updates).with(be_kind_of(Broadcast))
    end

    it "updates the broadcast when idle status is received in notification" do
      post :create, params: idle_status

      expect(broadcast.reload).to be_idle
      expect(BroadcastsChannel).to have_received(:broadcast_stream_updates).with(be_kind_of(Broadcast))
    end

    it "creates a broadcast recording when static_renditions.ready is received in notification" do
      expect do
        post :create, params: asset_ready
      end.to change(BroadcastRecording, :count).by(1)

      expect(BroadcastsChannel).to have_received(:stream_recording_ready)
    end

    it "stores full livestream playback uid and updates the broadcast" do
      post :create, params: full_live_stream_ready

      expect(Broadcast.last.full_live_stream_playback_uid).to eq "full_live_stream_playback_uid"
      expect(BroadcastsChannel).to have_received(:broadcast_stream_updates).with(be_kind_of(Broadcast))
    end

    it "returns OK response even for non-existing broadcast" do
      post :create, params: idle_status_for_unknown_broadcast

      expect(response).to be_successful
    end
  end

  after do
    # Set the callback again or it will affect other test cases where the callback is required
    Broadcast.set_callback(:create, :after, :create_mux_live_stream)
  end
end
