# @Author: dileep
# @Last Modified by:   dileep

import random
import pytest
from microbial_ai.regulation import Event, Action, Memory


@pytest.fixture
def random_action():
    return Action(type='fixed', phi={'rxn1': (random.random(), '+')})


@pytest.fixture
def random_event(random_action):
    return Event(state=random.randint(0, 100), action=random_action,
                 next_state=random.randint(0, 100), reward=random.random())


@pytest.mark.usefixtures("random_event")
class TestMemory:
    """
        Tests for the Memory class
    """
    def test_initialization(self):
        memory = Memory(1000)
        assert memory.capacity == 1000
        assert memory.idx == 0

    def test_add_event(self, random_event):
        memory = Memory(1000)
        memory.add_event(random_event)
        assert len(memory.memory) == 1
        assert memory.idx == 1
        for _ in range(1500):
            memory.add_event(random_event)
        assert len(memory.memory) == memory.capacity
        assert memory.idx == (1000 - 500 + 1)

    def test_sample(self, random_event):
        memory = Memory(1000)
        with pytest.raises(ValueError):
            memory.sample(100)
        for _ in range(400):
            

        assert len(memory.sample(200)) == 200

memory.add_event(random_event)