require 'rails_helper'

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

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

  it 'restores wiki page to previous version' do
    pending '#337 で対応予定'

    page =
        Wiki::Commit.create_content!(
            tag_name: TagName.create!(name: 'wiki_restore_page'),
            body: 'v1',
            created_by_user: user,
            message: 'init')

    v1 = page.wiki_versions.order(:version_no).last

    Wiki::Commit.content!(
        page:,
        body: 'v2',
        created_user: user,
        message: 'edit',
        base_revision_id: page.current_revision.id)

    post "/wiki/#{ page.id }/restore",
         params: { version_no: v1.version_no },
         headers: auth_headers(user)

    expect(response).to have_http_status(:ok)
    expect(page.reload.body).to eq('v1')
    expect(page.wiki_versions.order(:version_no).last.event_type).to eq('restore')
  end
end
