require 'rails_helper'

RSpec.describe 'Wiki conflict handling', type: :request do
  let!(:user) { create_member_user! }

  def auth_headers user
    { 'X-Transfer-Code' => user.inheritance_code }
  end

  it 'returns 409 when base_revision_id is stale' do
    page =
        Wiki::Commit.create_content!(
            tag_name: TagName.create!(name: 'wiki_conflict_request'),
            body: 'first',
            created_by_user: user,
            message: 'init')

    stale_id = page.current_revision.id

    Wiki::Commit.content!(
        page:,
        body: 'second',
        created_user: user,
        message: 'other edit',
        base_revision_id: stale_id)

    put "/wiki/#{ page.id }",
        params: {
          title: 'wiki_conflict_request',
          body: 'third',
          message: 'stale',
          base_revision_id: stale_id,
        },
        headers: auth_headers(user)

    expect(response).to have_http_status(:conflict)

    page.reload
    expect(page.body).to eq('second')
    expect(page.current_revision.message).to eq('other edit')
  end
end
