Given(/^a search catalog$/) do
  # TODO: ensure db is loaded with *some* releases
  visit SearchPage
end

When(/^I attempt to search for a single character$/) do
  on(SearchPage) do |page|
    page.search_for = 'a'
  end
end

Then(/^I should see some autocomplete options$/) do
  on(SearchPage) do |page|
    page.wait_until { page.release_suggest_elements.size > 0 || page.artist_suggest_elements.size > 0 }
  end
end

When(/^I search for a release that exists$/) do
  @release_that_exists = "Dap Dippin' with Sharon Jones & The Dap-Kings"
  # TODO: ensure DB has the release we are about to search
  # DB.load_with { release: @release_that_exists, other_data: 'blahblahblah' }
  on(SearchPage) do |page|
    page.search_for = @release_that_exists
    page.submit
  end
end

When(/^I search for a partial phrase "(.*?)"$/) do |phrase|
  on(SearchPage) do |page|
    page.search_for = phrase
    page.submit
  end
end

Given(/^the release "(.*?)" is present$/) do |name|
  # TODO: like above, ensure this release is in DB
end

Then(/^I should see it as a result$/) do
  on(ResultsPage) do |page|
    page.wait_until { page.result_elements.size > 0 }
    results = page.result_elements.map(&:text)
    puts "Results: #{results}"
    expect(results).to include @release_that_exists
  end
end

Then(/^I should see "(.*?)" as a result$/) do |name|
  on(ResultsPage) do |page|
    page.wait_until { page.result_elements.size > 0 }
    results = page.result_elements.map(&:text)
    puts "Results: #{results}"
    expect(results).to include name
  end
end
