require 'rails_helper'


RSpec.describe 'NicoTags', type: :request do
  describe 'GET /tags/nico' do
    it 'returns tags and next_cursor when overflowing limit' do
      create_list(:tag, 21, :nico)
      get '/tags/nico', params: { limit: 20 }
      expect(response).to have_http_status(:ok)
      expect(json['tags'].size).to eq(20)
      expect(json['next_cursor']).to be_present
    end
  end

  describe 'PATCH /tags/nico/:id' do
    let(:member) { create(:user, :member) }
    let(:nico_tag) { create(:tag, :nico) }

    it '401 when not logged in' do
      sign_out
      patch "/tags/nico/#{nico_tag.id}", params: { tags: 'a b' }
      expect(response).to have_http_status(:unauthorized)
    end

    it '403 when not member' do
      sign_in_as(create(:user, role: 'guest'))
      patch "/tags/nico/#{nico_tag.id}", params: { tags: 'a b' }
      expect(response).to have_http_status(:forbidden)
    end

    it '400 when target is not nico category' do
      sign_in_as(member)
      non_nico = create(:tag, :general)
      patch "/tags/nico/#{non_nico.id}", params: { tags: 'a b' }
      expect(response).to have_http_status(:bad_request)
    end
  end
end
