require 'rails_helper'

RSpec.describe PostImplication, type: :model do
  let!(:post_record) do
    Post.create!(
      title: 'post',
      url: 'https://example.com/post-implication-post'
    )
  end

  let!(:parent_post) do
    Post.create!(
      title: 'parent post',
      url: 'https://example.com/post-implication-parent'
    )
  end

  it 'is valid with post and parent_post' do
    implication = described_class.new(
      post: post_record,
      parent_post:
    )

    expect(implication).to be_valid
  end

  it 'does not allow same post as parent_post' do
    implication = described_class.new(
      post: post_record,
      parent_post: post_record
    )

    expect(implication).not_to be_valid
    expect(implication.errors[:parent_post_id]).to be_present
  end

  it 'does not allow duplicate pair' do
    described_class.create!(
      post: post_record,
      parent_post:
    )

    duplicate = described_class.new(
      post: post_record,
      parent_post:
    )

    expect(duplicate).not_to be_valid
    expect(duplicate.errors[:post_id]).to be_present
  end
end
