# frozen_string_literal: true

require './test/test_helper'
require 'minitest/mock'
require './services/ticker'

module Services
  class TickerTest < Minitest::Test
    def setup
      @request_fixture = File.read('./test/fixtures/sp500.json')
    end

    def test_performance
      log = MiniTest::Mock.new
      log.expect(:log, 'nil')

      Services::Ticker.sp500('5d').stub(:log, log) do |ticker|
        ticker.stub(:request, @request_fixture) do
          assert(ticker.performance)
        end
      end
    end

    def test_stats
      perf = MiniTest::Mock.new

      perf.expect(:performance, '1D')
      perf.expect(:performance, '5D')
      perf.expect(:performance, '1M')
      perf.expect(:performance, '3M')
      perf.expect(:performance, '6M')
      perf.expect(:performance, '1Y')
      perf.expect(:performance, '5Y')
      perf.expect(:performance, '10Y')
      perf.expect(:performance, 'YTD')
      perf.expect(:performance, 'MAX')

      nasdaq = Services::Ticker.nasdaq

      Services::Ticker.stub(:new, perf) do
        assert_equal(Services::Ticker::RANGE.keys.count, nasdaq.stats.values.compact.count)
      end
    end

    def test_in_date_range?
      ticker = Services::Ticker.new('bigc', Services::Ticker::RANGE['1Y'])
      ticker.stubs(:timestamp).returns(1.year.ago.to_i)

      assert ticker.in_date_range?
    end
  end
end
