require "spec_helper"
require_relative "../../app/models/duration_timecode"

describe DurationTimecode do
  describe ".parse" do
    it "returns a new instance from a HH:MM:SS formatted string" do
      duration = described_class.parse("00:30:20")

      expect(duration).to be_a(described_class)
      expect(duration.hours).to be_zero
      expect(duration.minutes).to eq(30)
      expect(duration.seconds).to eq(20)
    end

    context "with an invalid format" do
      it "raises an error" do
        expect {
          described_class.parse("bad format")
        }.to raise_error(ArgumentError)
      end
    end
  end

  describe ".from_seconds" do
    it "returns a new instance from total seconds" do
      duration = described_class.from_seconds(3662)

      expect(duration.hours).to eq(1)
      expect(duration.minutes).to eq(1)
      expect(duration.seconds).to eq(2)
    end
  end

  describe "#to_i" do
    it "returns the total number of seconds" do
      duration = described_class.parse("01:01:02")

      expect(duration.to_i).to eq(3662)
    end
  end

  describe "#to_s" do
    it "returns a string in HH:MM:SS format" do
      duration = described_class.new(0, 15, 30)

      expect(duration.to_s).to eq("00:15:30")
    end
  end

  describe "#+" do
    it "returns an instance representing the sum" do
      duration1 = described_class.new(0, 15, 40)
      duration2 = described_class.new(0, 3, 25)

      sum = duration1 + duration2

      expect(sum.to_s).to eq("00:19:05")
    end
  end
end
