defmodule Ibex.MessageTest do
  use ExUnit.Case
  doctest Ibex
  require Ibex.Message

  test "adds nul to field" do
    result = Ibex.Message.make_field(<<71>>)
    assert(result == <<71, 0>>)
  end

  test "prefixes length to message" do
    msg_code = <<71>>
    version = <<2>>
    uhh = <<2>>
    last = <<0>>

    msg_code = Ibex.Message.make_field(msg_code)
    assert(msg_code == <<71, 0>>)
    version = Ibex.Message.make_field(version)
    assert(version == <<2, 0>>)
    uhh = Ibex.Message.make_field(uhh)
    assert(uhh == <<2, 0>>)
    last = Ibex.Message.make_field(last)
    assert(last == <<0, 0>>)

    msg = <<msg_code::binary, version::binary, uhh::binary, last::binary>>
    l = Ibex.Message.get_msg_length(msg)
    made_msg = Ibex.Message.make_msg(msg, l)
    assert(made_msg == <<0, 0, 0, 9, 71, 0, 2, 0, 2, 0, 0, 0, 0>>)
  end

  test "convert to IB API format" do
    test_msg = Ibex.Messages.Outbound.ReqIds.t()
    converted = IBMessage.to_ib_api(test_msg)
    assert(converted = <<0, 0, 0, 4, 8, 0, 50, 0, 0>>)
  end

  test "request contract details" do
    test_msg = Ibex.Messages.Outbound.ReqContractDetails.t("1001", "AAPL", "STK")
    converted = IBMessage.to_ib_api(test_msg)

    assert(
      converted ==
        <<0, 0, 0, 19, 49, 48, 48, 49, 0, 56, 0, 57, 0, 65, 65, 80, 76, 0, 83, 84, 75, 0, 0>>
    )
  end
end