RSpec.describe PostTag, type: :model do
  describe '#sections' do
    it 'loads sections by post_id and tag_id' do
      post_tag = create(:post_tag)
      section = create(:post_tag_section,
                       post: post_tag.post,
                       tag: post_tag.tag,
                       begin_ms: 1000,
                       end_ms: 2000)

      expect(post_tag.sections).to contain_exactly(section)
    end

    it 'does not load sections for another tag on the same post' do
      post = create(:post)
      tag = create(:tag)
      other_tag = create(:tag)

      post_tag = create(:post_tag, post:, tag:)
      create(:post_tag_section,
             post:,
             tag: other_tag,
             begin_ms: 1000,
             end_ms: 2000)

      expect(post_tag.sections).to be_empty
    end

    it 'allows open-ended sections' do
      post_tag = create(:post_tag)
      section = create(:post_tag_section,
                       post: post_tag.post,
                       tag: post_tag.tag,
                       begin_ms: 1000,
                       end_ms: nil)

      expect(section).to be_valid
      expect(post_tag.sections).to contain_exactly(section)
    end
  end
end
