<commit_before>require "blinkbox/common_config"
require "blinkbox/onix2_processor/service"

context Blinkbox::Onix2Processor::Service do
  describe "#intialize" do
    before :each do
      @options = Blinkbox::CommonConfig.new
    end

    it "must create pending assets & mapping updates queues if they are not present" do
      queue = stub_const("Blinkbox::CommonMessaging::Queue", double(Blinkbox::CommonMessaging::Queue))
      allow(queue).to receive(:new)
      described_class.new(@options)
      expect(queue).to have_received(:new).with("Marvin.onix2_processor.pending_assets", exchange: "Marvin", bindings: anything, prefetch: kind_of(Integer))
      pending "Mapping definitions"
      expect(queue).to have_received(:new).with("Marvin.onix2_processor.mapping_updates", exchange: "Mapping", bindings: anything)
    end

    it "must not start if logging options are missing" do
      opts = double(@options)
      allow(opts).to receive(:tree).with(:logging).and_return({})
      expect {
        described_class.new(opts)
      }.to raise_error(ArgumentError)
    end
  end

  describe "#start" do
    before :each do
      @service = described_class.allocate
      allow(@service).to receive(:process_epub)
      @queue = instance_double(Blinkbox::CommonMessaging::Queue)
      def @queue.subscribe(&block)
        @subscribe_block = block
      end
      @service.instance_variable_set(:'@queue', @queue)

      @logger = instance_double(Blinkbox::CommonLogging)
      @service.instance_variable_set(:'@logger', @logger)
      allow(@logger).to receive(:error)
      @service.start
    end

    def fake_publish(metadata, obj)
      @queue.instance_variable_get(:'@subscribe_block').call(metadata, obj)
    end

    it "must send IngestionFilePendingV2 messages for processing" do
      metadata = { headers: {} }
      obj = Blinkbox::CommonMessaging::IngestionFilePendingV2.allocate
      expect(fake_publish(metadata, obj)).to eq(:ack)
      expect(@service).to have_received(:process_message).with(metadata, obj)
    end

    it "must log an error and reject other messages" do
      metadata = { headers: {} }
      obj = Blinkbox::CommonMessaging::IngestionBookMetadataV2.allocate
      expect(fake_publish(metadata, obj)).to eq(:reject)
      expect(@service).to_not have_received(:process_message).with(metadata, obj)
      expect(@logger).to have_received(:error)
    end
  end

  describe "#process_message" do<commit_msg>Removed nasty Unit tests for #start<commit_after>require "blinkbox/common_config"
require "blinkbox/onix2_processor/service"

context Blinkbox::Onix2Processor::Service do
  describe "#intialize" do
    before :each do
      @options = Blinkbox::CommonConfig.new
    end

    it "must create pending assets queue if they are not present" do
      queue = stub_const("Blinkbox::CommonMessaging::Queue", double(Blinkbox::CommonMessaging::Queue))
      allow(queue).to receive(:new)
      exchange = stub_const("Blinkbox::CommonMessaging::Exchange", double(Blinkbox::CommonMessaging::Exchange))
      allow(exchange).to receive(:new)
      described_class.new(@options)
      expect(queue).to have_received(:new).with("Marvin.onix2_processor.pending_assets", exchange: "Marvin", bindings: anything, prefetch: kind_of(Integer))
    end

    it "must not start if logging options are missing" do
      opts = double(@options)
      allow(opts).to receive(:tree).with(:logging).and_return({})
      expect {
        described_class.new(opts)
      }.to raise_error(ArgumentError)
    end
  end

  describe "#start" do
    before :each do
      @service = described_class.allocate
      allow(@service).to receive(:process_message)
      @queue = instance_double(Blinkbox::CommonMessaging::Queue)
      def @queue.subscribe(opts = {}, &block)
        @subscribe_block = block
      end
      @service.instance_variable_set(:'@queue', @queue)

      @logger = instance_double(Blinkbox::CommonLogging)
      @service.instance_variable_set(:'@logger', @logger)
      allow(@logger).to receive(:error)
      @service.start
    end

    def fake_publish(metadata, obj)
      @queue.instance_variable_get(:'@subscribe_block').call(metadata, obj)
    end
  end

  describe "#process_message" do
{'fraction_non_alphanumeric': 0.11686775490891886, 'fraction_numerical': 0.0023657440264963333, 'mean_word_length': 4.051373954599761, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5791161', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1223, 'n_words': 263}