require "rails_helper"

RSpec.describe AnalysisNotification, type: :model do
  describe "#build" do
    context "for audio type" do
      it "returns AudioAnalysisNotification subclass" do
        notification = described_class.build("audio", "job_id")

        expect(notification).to be_a(AnalysisNotification::AudioAnalysisNotification)
        expect(notification.job_id).to eq "job_id"
      end
    end

    context "for video type" do
      it "returns VideoAnalysisNotification subclass" do
        notification = described_class.build("video", "job_id")

        expect(notification).to be_a(AnalysisNotification::VideoAnalysisNotification)
        expect(notification.job_id).to eq "job_id"
      end
    end
  end

  describe AnalysisNotification::VideoAnalysisNotification do
    let!(:video) { create(:video, analysis_uid: "job_id") }

    subject { described_class.new("job_id") }

    describe "#video" do
      it "fetches video using job id" do
        expect(subject.video).to eq video
      end
    end

    describe "#success!" do
      it "updates status of the video" do
        subject.success!
        expect(video.reload).to be_analysis_success
      end
    end

    describe "#failure!" do
      it "updates status of the video" do
        subject.failure!
        expect(video.reload).to be_analysis_failure
      end
    end
  end

  describe AnalysisNotification::AudioAnalysisNotification do
    let!(:video) { create(:video, audio_analysis_uid: "job_id") }

    subject { described_class.new("job_id") }

    describe "#video" do
      it "fetches video using job id" do
        expect(subject.video).to eq video
      end
    end

    describe "#success!" do
      it "updates status of the video" do
        subject.success!
        expect(video.reload).to be_audio_analysis_success
      end
    end

    describe "#failure!" do
      it "updates status of the video" do
        subject.failure!
        expect(video.reload).to be_audio_analysis_failure
      end
    end
  end
end
