<commit_before>require 'spec_helper'

describe Spree::Chimpy::Interface::List do
  let(:interface) { described_class.new('Members', 'customers', true, true, nil) }
  let(:api)       { double(:api) }
  let(:lists)     { double(:lists, :[] => [{"name" => "Members", "id" => "a3d3" }] ) }
  let(:key)       { 'e025fd58df5b66ebd5a709d3fcf6e600-us8' }

  before do
    Spree::Chimpy::Config.key = key
    Mailchimp::API.should_receive(:new).with(key, { timeout: 60 }).and_return(api)
    lists.stub(:list).and_return(lists)
    api.stub(:lists).and_return(lists)
  end

  context "#subscribe" do
    it "subscribes" do
      expect(lists).to receive(:subscribe).
        with('a3d3', {email: '<EMAIL>'},
              {'SIZE' => '10'}, 'html', true, true, true, true)
      interface.subscribe("<EMAIL>", 'SIZE' => '10')
    end

    it "ignores exception Mailchimp::ListInvalidImportError" do
      expect(lists).to receive(:subscribe).
        with('a3d3', {email: '<EMAIL>'},
              {}, 'html', true, true, true, true).and_raise Mailchimp::ListInvalidImportError
      expect(lambda { interface.subscribe("<EMAIL>") }).to_not raise_error(Mailchimp::ListInvalidImportError)
    end
  end

  context "#unsubscribe" do
    it "unsubscribes" do
      expect(lists).to receive(:unsubscribe).with('a3d3', { email: '<EMAIL>' })
      interface.unsubscribe("<EMAIL>")
    end

    it "ignores exception Mailchimp::EmailNotExistsError" do
      expect(lists).to receive(:unsubscribe).with('a3d3', { email: '<EMAIL>' }).and_raise Mailchimp::EmailNotExistsError
      expect(lambda { interface.unsubscribe("user<EMAIL>") }).to_not raise_error(Mailchimp::EmailNotExistsError)
    end

    it "ignores exception Mailchimp::ListNotSubscribedError" do
      expect(lists).to receive(:unsubscribe).with('a3d3', { email: '<EMAIL>' }).and_raise Mailchimp::ListNotSubscribedError
      expect(lambda { interface.unsubscribe("<EMAIL>") }).to_not raise_error(Mailchimp::ListNotSubscribedError)
    end
  end

  context "member info" do
    it "find when no errors" do
      expect(lists).to receive(:member_info).with('a3d3', [{:email=>"<EMAIL>"}]).and_return({'success_count' => 1, 'data' => [{'response' => 'foo'}]})
      expect(interface.info("<EMAIL>")).to eq({:response => 'foo'})
    end

    it "returns empty hash on error" do
      expect(lists).to receive(:member_info).with('a3d3', [{:email=>'<EMAIL>'}]).and_return({'data' => [{'error' => 'foo'}]})
      expect(interface.info("<EMAIL>")).to eq({})
    end
  end

  it "segments users" do
    expect(lists).to receive(:subscribe).
      with('a3d3', {email: '<EMAIL>'}, {'SIZE' => '10'},
            'html', true, true, true, true)
    expect(lists).to receive(:static_segments).with('a3d3').and_return([{"id" => '123', "name" => "customers"}])
    expect(lists).to receive(:static_segment_members_add).with('a3d3', 123, [{ email: "<EMAIL>"}])
    interface.subscribe("<EMAIL>", {'SIZE' => '10'}, {customer: true})
  end

  it "segments" do
    expect(lists).to receive(:static_segments).with('a3d3').and_return([{"id" => '123', "name" => "customers"}])
    expect(lists).to receive(:static_segment_members_add).with('a3d3', 123, [{email: "<EMAIL>"}, {email: "<EMAIL>"}])
    interface.segment(["<EMAIL>", "<EMAIL>"])
  end

  it "find list id" do
    interface.list_id
  end

  it "checks if merge var exists" do<commit_msg>Clean Up Failing Order Spec<commit_after>require 'spec_helper'

describe Spree::Chimpy::Interface::List do
  let(:interface) { described_class.new('Members', 'customers', true, true, nil) }
  let(:api)       { double(:api) }
  let(:lists)     { double(:lists, :[] => [{"name" => "Members", "id" => "a3d3" }] ) }
  let(:key)       { 'e025fd58df5b66ebd5a709d3fcf6e600-us8' }

  before do
    Spree::Chimpy::Config.key = key
    Mailchimp::API.should_receive(:new).with(key, { timeout: 60 }).and_return(api)
    lists.stub(:list).and_return(lists)
    api.stub(:lists).and_return(lists)
  end

  context "#subscribe" do
    it "subscribes" do
      expect(lists).to receive(:subscribe).
        with('a3d3', {email: '<EMAIL>'},
              {'SIZE' => '10'}, 'html', true, true, true, true)
      interface.subscribe("<EMAIL>", 'SIZE' => '10')
    end

    it "ignores exception Mailchimp::ListInvalidImportError" do
      expect(lists).to receive(:subscribe).
        with('a3d3', {email: '<EMAIL>'},
              {}, 'html', true, true, true, true).and_raise Mailchimp::ListInvalidImportError
      expect(lambda { interface.subscribe("<EMAIL>") }).to_not raise_error(Mailchimp::ListInvalidImportError)
    end
  end

  context "#unsubscribe" do
    it "unsubscribes" do
      expect(lists).to receive(:unsubscribe).with('a3d3', { email: '<EMAIL>' })
      interface.unsubscribe("<EMAIL>")
    end

    it "ignores exception Mailchimp::EmailNotExistsError" do
      expect(lists).to receive(:unsubscribe).with('a3d3', { email: '<EMAIL>' }).and_raise Mailchimp::EmailNotExistsError
      expect(lambda { interface.unsubscribe("<EMAIL>") }).to_not raise_error(Mailchimp::EmailNotExistsError)
    end

    it "ignores exception Mailchimp::ListNotSubscribedError" do
      expect(lists).to receive(:unsubscribe).with('a3d3', { email: '<EMAIL>' }).and_raise Mailchimp::ListNotSubscribedError
      expect(lambda { interface.unsubscribe("<EMAIL>") }).to_not raise_error(Mailchimp::ListNotSubscribedError)
    end
  end

  context "member info" do
    it "find when no errors" do
      expect(lists).to receive(:member_info).with('a3d3', [{:email=>"<EMAIL>"}]).and_return({'success_count' => 1, 'data' => [{'response' => 'foo'}]})
      expect(interface.info("<EMAIL>")).to eq({:response => 'foo'})
    end

    it "returns empty hash on error" do
      expect(lists).to receive(:member_info).with('a3d3', [{:email=>'<EMAIL>'}]).and_return({'data' => [{'error' => 'foo'}]})
      expect(interface.info("<EMAIL>")).to eq({})
    end
  end

  it "segments users" do
    expect(lists).to receive(:subscribe).
      with('a3d3', {email: '<EMAIL>'}, {'SIZE' => '10'},
            'html', true, true, true, true)
    expect(lists).to receive(:static_segments).with('a3d3').and_return([{"id" => 123, "name" => "customers"}])
    expect(lists).to receive(:static_segment_members_add).with('a3d3', 123, [{:email => "<EMAIL>"}])
    interface.subscribe("<EMAIL>", {'SIZE' => '10'}, {customer: true})
  end

  it "segments" do
    expect(lists).to receive(:static_segments).with('a3d3').and_return([{"id" => '123', "name" => "customers"}])
    expect(lists).to receive(:static_segment_members_add).with('a3d3', 123, [{email: "<EMAIL>"}, {email: "<EMAIL>"}])
    interface.segment(["<EMAIL>", "<EMAIL>"])
  end

  it "find list id" do
    interface.list_id
  end

  it "checks if merge var exists" do
{'fraction_non_alphanumeric': 0.19277465205803967, 'fraction_numerical': 0.019840094758661535, 'mean_word_length': 4.3147128245476, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5877099', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2277, 'n_words': 447}