Spaces:
Running
Running
File size: 2,512 Bytes
1d0b04b 23040f5 1d0b04b 23040f5 1d0b04b 23040f5 1d0b04b 23040f5 1d0b04b 23040f5 1d0b04b 23040f5 1d0b04b 23040f5 1d0b04b 23040f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | """Unit tests for the Retro Alpha engine."""
import pytest
import engine
def test_new_game():
state = engine.new_game()
assert state.cash_balance == 1_000_000
assert state.total_value() == 1_000_000
assert state.year == engine.STARTING_YEAR
assert state.month == engine.STARTING_MONTH
assert state.months_elapsed == 0
assert all(state.portfolio[a] == 0.0 for a in engine.ASSETS)
assert state.game_over is False
def test_trade_buy():
state = engine.new_game()
engine.execute_player_trade(state, "nifty_50", "buy", 0.5)
assert state.cash_balance < 1_000_000
assert state.portfolio["nifty_50"] > 0
assert len(state.ledger) == 1
def test_trade_sell():
state = engine.new_game()
engine.execute_player_trade(state, "nifty_50", "buy", 0.5)
engine.execute_player_trade(state, "nifty_50", "sell", 0.5)
assert state.cash_balance > 0
def test_trade_invalid_asset():
state = engine.new_game()
with pytest.raises(ValueError):
engine.execute_player_trade(state, "banana", "buy", 0.1)
def test_advance_month():
state = engine.new_game()
news = {"headline": "Test", "impact": {a: 0.0 for a in engine.ASSETS}, "duration_months": 1}
engine.advance_month(state, news, [])
assert state.months_elapsed == 1
assert state.month == engine.STARTING_MONTH + 1
assert state.year == engine.STARTING_YEAR
def test_advance_year_rollover():
state = engine.new_game()
state.month = 12
news = {"headline": "Test", "impact": {a: 0.0 for a in engine.ASSETS}, "duration_months": 1}
engine.advance_month(state, news, [])
assert state.month == 1
assert state.year == engine.STARTING_YEAR + 1
def test_game_over_at_120_months():
state = engine.new_game()
state.months_elapsed = 119
news = {"headline": "Test", "impact": {a: 0.0 for a in engine.ASSETS}, "duration_months": 1}
engine.advance_month(state, news, [])
assert state.months_elapsed == 120
assert state.game_over is True
def test_cash_price_stable():
state = engine.new_game()
for _ in range(20):
state.months_elapsed += 1
engine.random_walk(state)
assert abs(state.prices["cash"] - 1.0) < 1e-9
def test_winning_threshold():
state = engine.new_game()
state.cash_balance = 3_000_000
state.months_elapsed = 119
news = {"headline": "Test", "impact": {a: 0.0 for a in engine.ASSETS}, "duration_months": 1}
engine.advance_month(state, news, [])
assert state.won is True
|