"""Jita price provider tests."""

from unittest.mock import patch

from django.test import TestCase

from shop.market import best_price, jita_prices


class BestPriceTests(TestCase):
    def test_buy_is_highest(self):
        orders = [{"price": 100.0}, {"price": 200.0}, {"price": 1.0}]
        self.assertEqual(best_price(orders, is_buy=True), 200)

    def test_sell_is_lowest(self):
        orders = [{"price": 120.0}, {"price": 99.0}]
        self.assertEqual(best_price(orders, is_buy=False), 99)

    def test_empty_is_none(self):
        self.assertIsNone(best_price([], is_buy=True))


class JitaPricesTests(TestCase):
    @patch("shop.market._fetch_orders")
    def test_best_of_book_at_jita_station(self, mock_fetch):
        mock_fetch.return_value = [
            {"is_buy_order": True, "price": 7_900_000.0, "location_id": 60003760},
            {"is_buy_order": True, "price": 1.0, "location_id": 60003760},          # 1-ISK bid ignored (not max)
            {"is_buy_order": False, "price": 8_499_000.0, "location_id": 60003760},
            {"is_buy_order": False, "price": 8_000_000.0, "location_id": 30000142},  # other station, ignored
        ]
        self.assertEqual(jita_prices(671), {"buy": 7_900_000, "sell": 8_499_000})

    @patch("shop.market._fetch_orders")
    def test_missing_side_is_none(self, mock_fetch):
        mock_fetch.return_value = [
            {"is_buy_order": False, "price": 8_499_000.0, "location_id": 60003760},
        ]
        self.assertEqual(jita_prices(671), {"buy": None, "sell": 8_499_000})
