require 'rails_helper'

RSpec.describe UsdaFood do

  let!(:data) do
    {
      salted_butter: create(:usda_food, long_description: 'Salted Butter'),
      unsalted_butter: create(:usda_food, long_description: 'Unsalted Butter'),
      flour: create(:usda_food, long_description: 'Flour'),
      bread_flour: create(:usda_food, long_description: 'Bread Flour'),
      sugar: create(:usda_food, long_description: 'Sugar,Granulated'),
      mustard: create(:usda_food, long_description: 'HONEY MUSTARD DIPPING SAUCE')
    }
  end

  def items(*syms)
    Array.wrap(syms).map { |s| data[s] }
  end

  it 'can be found by single tokens' do
    r = UsdaFood.matches_tokens(:long_description, ['sal'])
    expect(r.length).to eq 1
    expect(r).to contain_exactly *items(:salted_butter)

    r = UsdaFood.matches_tokens(:long_description, ['flour'])
    expect(r.length).to eq 2
    expect(r).to contain_exactly *items(:flour, :bread_flour)
  end

  it 'can be found by multiple tokens' do
    r = UsdaFood.matches_tokens(:long_description, ['sal', 'butter'])
    expect(r.length).to eq 1
    expect(r).to contain_exactly *items(:salted_butter)

    r = UsdaFood.matches_tokens(:long_description, ['butter', 'sal'])
    expect(r.length).to eq 1
    expect(r).to contain_exactly *items(:salted_butter)
  end

  it 'treats commas like spaces' do
    r = UsdaFood.matches_tokens(:long_description, ['gran', 'sugar'])
    expect(r.length).to eq 1
    expect(r).to contain_exactly *items(:sugar)
  end

end