<commit_before>def siri_path(attributes = {})
  attributes = {
    referential: 'test'
  }.merge(attributes.delete_if { |k,v| v.nil? })

  url_for(attributes.merge(path: "siri"))
end

def send_siri_request(request, attributes = {})
  response = RestClient.post siri_path(attributes), request, {content_type: :xml}
  save_siri_messages request: request, response: response.body
  @last_siri_request = request
  @last_siri_response = response.body
end

def save_siri_messages(messages = {})
  return unless ENV['SIRI_DEBUG']

  @siri_timestamp ||= Time.now.strftime("%Y%m%d%H%M%S")
  @siri_message_id ||= 0
  @siri_message_id += 1

  messages.each do |type, content|
    file = "log/siri-message-#{@siri_timestamp}-#{@siri_message_id}-#{type}"
    File.write file, content, mode: "wb"
  end
end

Given(/^a SIRI server (?:"([^"]*)" )?waits (GetStopMonitoring) request on "([^"]*)" to respond with$/) do |name, message_type, url, response|
  name ||= "default"
  SIRIServer.create(name, url).expect_request(message_type, response).start
end

Given(/^the SIRI server waits (GetStopMonitoring) request to respond with$/) do |message_type, response|
  SIRIServer.find("default").expect_request(message_type, response)
end

When(/^the SIRI server has received a (GetStopMonitoring) request$/) do |message_type|
  SIRIServer.find("default").wait_request message_type
end

When(/^the SIRI server has received (\d+) (GetStopMonitoring) requests$/) do |count, message_type|
  SIRIServer.find("default").wait_request message_type, count.to_i
end

When(/^I send this SIRI request(?: to the Referential "([^"]*)")?$/) do |referential, request|
  send_siri_request request, referential: referential
end

Then(/^I should receive this SIRI response$/) do |expected_xml|
  save_siri_messages expected: normalized_xml(expected_xml), received: normalized_xml(@last_siri_response), received_raw: @last_siri_response
  expect(normalized_xml(@last_siri_response)).to eq(normalized_xml(expected_xml))
end

Then(/^I should receive a SIRI GetStopMonitoringResponse with$/) do |expected|
  document = REXML::Document.new(@last_siri_response)

  expected_values = {}
  expected.raw.each do |row|
    expected_values[row[0]] = row[1] unless row[2] && row[2] =~ /^TODO/
  end

  actual_values = {}
  expected_values.keys.each do |xpath|
    node = REXML::XPath.first(document, xpath, { "siri" => "http://www.siri.org.uk/siri" })
    xml_value = node.text if node
    actual_values[xpath] = xml_value
  end

  expect(actual_values).to eq(expected_values)
end

When(/^I send a SIRI GetStopMonitoring request with$/) do |attributes|
  default_attributes = {
    "RequestorRef" => "test",
    "MonitoringRef" => "NINOXE:StopPoint:SP:24:LOC"
  }
  attributes = default_attributes.merge(attributes.rows_hash)<commit_msg>Commit<commit_after>def siri_path(attributes = {})
  attributes = {
    referential: 'test'
  }.merge(attributes.delete_if { |k,v| v.nil? })

  url_for(attributes.merge(path: "siri"))
end

def send_siri_request(request, attributes = {})
  response = RestClient.post siri_path(attributes), request, {content_type: :xml}
  save_siri_messages request: request, response: response.body
  @last_siri_request = request
  @last_siri_response = response.body
end

def save_siri_messages(messages = {})
  return unless ENV['SIRI_DEBUG']

  @siri_timestamp ||= Time.now.strftime("%Y%m%d%H%M%S")
  @siri_message_id ||= 0
  @siri_message_id += 1

  messages.each do |type, content|
    file = "log/siri-message-#{@siri_timestamp}-#{@siri_message_id}-#{type}"
    File.write file, content, mode: "wb"
  end
end

Given(/^a SIRI server (?:"([^"]*)" )?waits (([^"]*)) request on "([^"]*)" to respond with$/) do |name, message_type, url, response|
  name ||= "default"
  SIRIServer.create(name, url).expect_request(message_type, response).start
end

Given(/^the SIRI server waits (GetStopMonitoring) request to respond with$/) do |message_type, response|
  SIRIServer.find("default").expect_request(message_type, response)
end

When(/^the SIRI server has received a (GetStopMonitoring) request$/) do |message_type|
  SIRIServer.find("default").wait_request message_type
end

When(/^the SIRI server has received (\d+) (GetStopMonitoring) requests$/) do |count, message_type|
  SIRIServer.find("default").wait_request message_type, count.to_i
end

When(/^I send this SIRI request(?: to the Referential "([^"]*)")?$/) do |referential, request|
  send_siri_request request, referential: referential
end

Then(/^I should receive this SIRI response$/) do |expected_xml|
  save_siri_messages expected: normalized_xml(expected_xml), received: normalized_xml(@last_siri_response), received_raw: @last_siri_response
  expect(normalized_xml(@last_siri_response)).to eq(normalized_xml(expected_xml))
end

When(/^I receive this GeneralMessageRequesst$/) do |message_type|
  SIRIServer.find("default").wait_request message_type
end

Then(/^I should receive a SIRI GetStopMonitoringResponse with$/) do |expected|
  document = REXML::Document.new(@last_siri_response)

  expected_values = {}
  expected.raw.each do |row|
    expected_values[row[0]] = row[1] unless row[2] && row[2] =~ /^TODO/
  end

  actual_values = {}
  expected_values.keys.each do |xpath|
    node = REXML::XPath.first(document, xpath, { "siri" => "http://www.siri.org.uk/siri" })
    xml_value = node.text if node
    actual_values[xpath] = xml_value
  end

  expect(actual_values).to eq(expected_values)
end

When(/^I send a SIRI GetStopMonitoring request with$/) do |attributes|
  default_attributes = {
    "RequestorRef" => "test",
    "MonitoringRef" => "NINOXE:StopPoint:SP:24:LOC"
  }
  attributes = default_attributes.merge(attributes.rows_hash)
{'fraction_non_alphanumeric': 0.14290748898678415, 'fraction_numerical': 0.0028193832599118945, 'mean_word_length': 5.516647531572905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 20}, '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': '5543357', 'n_tokens_mistral': 2004, 'n_tokens_neox': 1971, 'n_words': 425}