require 'rails_helper'

RSpec.describe PostVersion, type: :model do
  let!(:tag_name) { TagName.create!(name: 'post_version_spec_tag') }
  let!(:tag) { Tag.create!(tag_name: tag_name, category: :general) }

  let!(:post_record) do
    Post.create!(title: 'spec post', url: 'https://example.com/post-version-spec').tap do |post|
      PostTag.create!(post: post, tag: tag)
    end
  end

  let!(:post_version) do
    PostVersion.create!(
      post: post_record,
      version_no: 1,
      event_type: 'create',
      title: post_record.title,
      url: post_record.url,
      thumbnail_base: post_record.thumbnail_base,
      tags: post_record.snapshot_tag_names.join(' '),
      parent_post_ids: post_record.snapshot_parent_post_ids.join(' '),
      original_created_from: post_record.original_created_from,
      original_created_before: post_record.original_created_before,
      created_at: Time.current,
      created_by_user: nil
    )
  end

  it 'is read only after create' do
    expect do
      post_version.update!(title: 'changed')
    end.to raise_error(ActiveRecord::ReadOnlyRecord)
  end

  it 'cannot be destroyed' do
    expect do
      post_version.destroy!
    end.to raise_error(ActiveRecord::ReadOnlyRecord)
  end
end
