<commit_before>require 'headless'
require 'selenium-webdriver'
require 'json'
require 'pp'
require 'fileutils'
require 'ostruct'

Before do
  $headless ||= false
  unless $headless
    $headless = Headless.new
    $headless.start

    profile_dir = File.expand_path('features/profile')
    profile = Selenium::WebDriver::Firefox::Profile.new(profile_dir)

    extensions = {
      bbt: Dir['zotero-better-*.xpi'].first,
      dbb: Dir['tmp/zotero-debug-*.xpi'].first,
    }
    extensions.values.each{|xpi|
      profile.add_extension(xpi)
    }
    profile['extensions.zotero.httpServer.enabled'] = true;
    profile['extensions.zotero.debug.store'] = true;
    profile['extensions.zotero.debug.log'] = true;
    profile['extensions.zotero.translators.better-bibtex.debug'] = true;
    profile['extensions.zotero.translators.better-bibtex.attachmentRelativePath'] = true

    profile['browser.download.dir'] = "/tmp/webdriver-downloads"
    profile['browser.download.folderList'] = 2
    profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
    profile['pdfjs.disabled'] = true

    BROWSER = Selenium::WebDriver.for :firefox, :profile => profile
    sleep 2
    DBB = JSONRPCClient.new('http://localhost:23119/debug-bridge')
    DBB.bootstrap('Zotero.BetterBibTeX')
    BBT = JSONRPCClient.new('http://localhost:23119/debug-bridge/better-bibtex')
  end

  BBT.reset
  sleep 1
  throw 'Library not empty!' unless BBT.librarySize == 0
end
at_exit do
  $headless.destroy if $headless
end

#After do |s|
#  STDOUT.puts 'log: ' + DBB.log
#end

#Given /^that ([^\s]+) is set to (.*)$/ do |pref, value|
#  if value =~ /^['"](.*)['"]$/
#    ZOTERO.setCharPref(pref, $1)
#  elsif ['false', 'true'].include?(value.downcase)
#    ZOTERO.setBoolPref(pref, value.downcase == 'true')
#  elsif value.downcase == 'null'
#    ZOTERO.setCharPref(pref, nil)
#  else
#    ZOTERO.setIntPref(pref, Integer(value))
#  end
#end

When /^I import ([0-9]+) references? (with ([0-9]+) attachments? )?from '([^']+)'$/ do |references, dummy, attachments, filename|
  bib = File.expand_path(File.join(File.dirname(__FILE__), '..', filename))

  entries = OpenStruct.new({start: BBT.librarySize})

  BBT.import(bib)

  start = Time.now

  expected = references.to_i + attachments.to_i

  while !entries.now || entries.now != entries.new
    sleep 2
    entries.now = entries.new || entries.start
    #STDOUT.puts entries.now
    entries.new = BBT.librarySize

    elapsed = Time.now - start
    if elapsed > 3
      processed = entries.new - entries.start
      remaining = expected - processed
      speed = processed / elapsed
      timeleft = (Time.mktime(0)+((expected - processed) / speed)).strftime("%H:%M:%S")
      STDOUT.puts "Slow import: #{processed} entries @ #{speed.round(1)} entries/sec, #{timeleft} remaining"
    end
  end



  expect(entries.now - entries.start).to eq(references.to_i + attachments.to_i)
end

Then /^write the library to '([^']+)'$/ do |filename|
  open(filename, 'w'){|f| f.write(JSON.pretty_generate(BBT.getAll, :indent => '  ')) }
end

Then /^the library should match '([^']+)'$/ do |filename|
  expected = File.expand_path(File.join(File.dirname(__FILE__), '..', filename))

  case File.extname(expected)
    when '.json'
      expected = JSON.parse(open(expected).read)
      expected = expected['items'] if expected.is_a?(Hash) && expected['items']<commit_msg>4 seconds is OK on my build system<commit_after>require 'headless'
require 'selenium-webdriver'
require 'json'
require 'pp'
require 'fileutils'
require 'ostruct'

Before do
  $headless ||= false
  unless $headless
    $headless = Headless.new
    $headless.start

    profile_dir = File.expand_path('features/profile')
    profile = Selenium::WebDriver::Firefox::Profile.new(profile_dir)

    extensions = {
      bbt: Dir['zotero-better-*.xpi'].first,
      dbb: Dir['tmp/zotero-debug-*.xpi'].first,
    }
    extensions.values.each{|xpi|
      profile.add_extension(xpi)
    }
    profile['extensions.zotero.httpServer.enabled'] = true;
    profile['extensions.zotero.debug.store'] = true;
    profile['extensions.zotero.debug.log'] = true;
    profile['extensions.zotero.translators.better-bibtex.debug'] = true;
    profile['extensions.zotero.translators.better-bibtex.attachmentRelativePath'] = true

    profile['browser.download.dir'] = "/tmp/webdriver-downloads"
    profile['browser.download.folderList'] = 2
    profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
    profile['pdfjs.disabled'] = true

    BROWSER = Selenium::WebDriver.for :firefox, :profile => profile
    sleep 2
    DBB = JSONRPCClient.new('http://localhost:23119/debug-bridge')
    DBB.bootstrap('Zotero.BetterBibTeX')
    BBT = JSONRPCClient.new('http://localhost:23119/debug-bridge/better-bibtex')
  end

  BBT.reset
  sleep 1
  throw 'Library not empty!' unless BBT.librarySize == 0
end
at_exit do
  $headless.destroy if $headless
end

#After do |s|
#  STDOUT.puts 'log: ' + DBB.log
#end

#Given /^that ([^\s]+) is set to (.*)$/ do |pref, value|
#  if value =~ /^['"](.*)['"]$/
#    ZOTERO.setCharPref(pref, $1)
#  elsif ['false', 'true'].include?(value.downcase)
#    ZOTERO.setBoolPref(pref, value.downcase == 'true')
#  elsif value.downcase == 'null'
#    ZOTERO.setCharPref(pref, nil)
#  else
#    ZOTERO.setIntPref(pref, Integer(value))
#  end
#end

When /^I import ([0-9]+) references? (with ([0-9]+) attachments? )?from '([^']+)'$/ do |references, dummy, attachments, filename|
  bib = File.expand_path(File.join(File.dirname(__FILE__), '..', filename))

  entries = OpenStruct.new({start: BBT.librarySize})

  BBT.import(bib)

  start = Time.now

  expected = references.to_i + attachments.to_i

  while !entries.now || entries.now != entries.new
    sleep 2
    entries.now = entries.new || entries.start
    #STDOUT.puts entries.now
    entries.new = BBT.librarySize

    elapsed = Time.now - start
    if elapsed > 5
      processed = entries.new - entries.start
      remaining = expected - processed
      speed = processed / elapsed
      timeleft = (Time.mktime(0)+((expected - processed) / speed)).strftime("%H:%M:%S")
      STDOUT.puts "Slow import (#{elapsed}): #{processed} entries @ #{speed.round(1)} entries/sec, #{timeleft} remaining"
    end
  end



  expect(entries.now - entries.start).to eq(references.to_i + attachments.to_i)
end

Then /^write the library to '([^']+)'$/ do |filename|
  open(filename, 'w'){|f| f.write(JSON.pretty_generate(BBT.getAll, :indent => '  ')) }
end

Then /^the library should match '([^']+)'$/ do |filename|
  expected = File.expand_path(File.join(File.dirname(__FILE__), '..', filename))

  case File.extname(expected)
    when '.json'
      expected = JSON.parse(open(expected).read)
      expected = expected['items'] if expected.is_a?(Hash) && expected['items']
{'fraction_non_alphanumeric': 0.16549192364170337, 'fraction_numerical': 0.006901615271659324, 'mean_word_length': 3.8270729978738482, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4918995', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2308, 'n_words': 453}