require 'rails_helper'

RSpec.describe MuxLiveStream, type: :model do
  let(:broadcast) { build(:broadcast, name: "My Broadcast") }
  let(:broadcast_2) { create(:broadcast, :with_stream, skip_create_callback: true, name: "My Broadcast") }
  let(:live_stream_data) { OpenStruct.new(data: OpenStruct.new(id: "stream_id", stream_key: "stream_key")) }
  let(:live_stream_playback_data) { OpenStruct.new(data: OpenStruct.new(id: "playback_id")) }

  it "creates live stream and live stream playback URL" do
    allow_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:create_live_stream).and_return(live_stream_data)
    allow_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:create_live_stream_playback_id).and_return(live_stream_playback_data)

    live_stream = MuxLiveStream.new

    expect(live_stream.id).to eq "stream_id"
    expect(live_stream.key).to eq "stream_key"
    expect(live_stream.playback_id).to eq "playback_id"
  end

  it "destroys live stream" do
    expect_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:delete_live_stream).with(broadcast_2.stream_uid)

    stream = MuxLiveStream.new
    stream.destroy_stream(broadcast_2.stream_uid)
  end

  context "when reduced latency is enabled via the environment" do
    it "turns on reduced latency mode when creating the live stream" do
      allow(ENV).to receive(:[]).with("MUX_REDUCED_LATENCY_ENABLED").and_return("true")
      allow(ENV).to receive(:[]).with("MUX_TEST_MODE_DISABLED").and_return("false")
      request = instance_double(MuxRuby::CreateLiveStreamRequest)
      allow(request).to receive(:reduced_latency=)
      allow(request).to receive(:new_asset_settings=)
      allow(request).to receive(:playback_policy=)
      allow(request).to receive(:test=)
      allow(MuxRuby::CreateLiveStreamRequest).to receive(:new).and_return(request)
      allow_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:create_live_stream).and_return(live_stream_data)
      allow_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:create_live_stream_playback_id).and_return(live_stream_playback_data)

      live_stream = MuxLiveStream.new

      expect(live_stream.id).to eq "stream_id"
      expect(request).to have_received(:reduced_latency=).with(true)
    end
  end

  context "when test mode is enabled" do
    it "creates live streams in test mode" do
      allow(ENV).to receive(:[]).with("MUX_REDUCED_LATENCY_ENABLED").and_return("true")
      allow(ENV).to receive(:[]).with("MUX_TEST_MODE_DISABLED").and_return(nil)
      request = instance_double(MuxRuby::CreateLiveStreamRequest)
      allow(request).to receive(:reduced_latency=)
      allow(request).to receive(:new_asset_settings=)
      allow(request).to receive(:playback_policy=)
      allow(request).to receive(:test=)
      allow(MuxRuby::CreateLiveStreamRequest).to receive(:new).and_return(request)
      allow_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:create_live_stream).and_return(live_stream_data)
      allow_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:create_live_stream_playback_id).and_return(live_stream_playback_data)

      live_stream = MuxLiveStream.new

      expect(live_stream.id).to eq "stream_id"
      expect(request).to have_received(:reduced_latency=).with(true)
      expect(request).to have_received(:test=).with(true)
    end
  end

  context "when test mode is disabled" do
    it "creates live streams in test mode" do
      allow(ENV).to receive(:[]).with("MUX_REDUCED_LATENCY_ENABLED").and_return("true")
      allow(ENV).to receive(:[]).with("MUX_TEST_MODE_DISABLED").and_return("true")
      request = instance_double(MuxRuby::CreateLiveStreamRequest)
      allow(request).to receive(:reduced_latency=)
      allow(request).to receive(:new_asset_settings=)
      allow(request).to receive(:playback_policy=)
      allow(request).to receive(:test=)
      allow(MuxRuby::CreateLiveStreamRequest).to receive(:new).and_return(request)
      allow_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:create_live_stream).and_return(live_stream_data)
      allow_any_instance_of(MuxRuby::LiveStreamsApi).to receive(:create_live_stream_playback_id).and_return(live_stream_playback_data)

      live_stream = MuxLiveStream.new

      expect(live_stream.id).to eq "stream_id"
      expect(request).to have_received(:reduced_latency=).with(true)
      expect(request).to have_received(:test=).with(false)
    end
  end
end
