require 'rails_helper'

RSpec.describe 'Nostr Settings', type: :feature do
  let(:user) { create :user, cn: 'jimmy', ou: 'kosmos.org' }

  before do
    login_as user, scope: :user

    allow_any_instance_of(User).to receive(:dn)
      .and_return("cn=#{user.cn},ou=kosmos.org,cn=users,dc=kosmos,dc=org")
    allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(nil)
  end

  describe 'Adding a nostr pubkey' do
    scenario 'Without nostr browser extension available' do
      visit setting_path(:nostr)
      expect(page).to have_content("No browser extension found")
      expect(page).to have_css('button[data-settings--nostr-pubkey-target=setPubkey]:disabled')
    end

    # scenario 'Successfully saving a key' do
    #   Note: Needs a more complex JS testing setup (maybe poltergeist), not
    #   worth it for now
    # end
  end

  context "With pubkey configured" do
    before do
      allow_any_instance_of(User).to receive(:nostr_pubkey)
        .and_return("ce273cbfb0d4e3e06930773a337c1459e4849efd4cb4c751b906a561c98a6d09")
    end

    scenario 'Remove nostr pubkey from account' do
      visit setting_path(:nostr)
      expect(page).to have_field("nostr_public_key",
        with: "npub1ecnne0as6n37q6fswuarxlq5t8jgf8hafj6vw5deq6jkrjv2d5ysnehu73",
        disabled: true)

      expect(LdapManager::UpdateNostrKey).to receive(:call).with(
        dn: "cn=jimmy,ou=kosmos.org,cn=users,dc=kosmos,dc=org",
        pubkey: nil
      )

      click_link "Remove"
    end
  end
end
