require "rails_helper"

RSpec.describe ProjectsChannel, type: :channel do
  let(:user) { create(:user) }
  let(:project) { create(:project, account: user.primary_account) }

  before do
    stub_connection current_user: user
  end

  it "successfully subscribes to project stream" do
    subscribe id: project.id
    expect(subscription).to be_confirmed
    expect(subscription).to have_stream_for(project)
  end

  describe ".broadcast_video_analysis_update" do
    it "broadcasts to the project channel" do
      video = create(:video, project: project)
      content = ApplicationController.render partial: "video_analyses/video_status_updated", locals: { video: video }

      expect {
        ProjectsChannel.broadcast_video_analysis_update(video)
      }.to have_broadcasted_to(project).with(event: "video_status_update", content: content)
    end
  end

  describe ".conference_recording_ready" do
    it "broadcasts to the project channel" do
      recording_file = Rack::Test::UploadedFile.new(Rails.root.join("spec", "fixtures", "files", "video_file.mp4"), "video/mp4")
      zoom_meeting = create(:zoom_meeting, project: project, recording: recording_file)
      video_url = 'http://url.to.video/video.mp4'
      allow(zoom_meeting.recording).to receive(:service_url).and_return(video_url)

      expect {
        ProjectsChannel.conference_recording_ready(project, zoom_meeting.recording)
      }.to have_broadcasted_to(project).with { |data|
        expect(data['content']).to include(video_url)
        expect(data['event']).to eq('conference_recording_ready')
      }
    end
  end
end
