"""
Tests for Py2048
"""
import unittest
from py2048 import Game2048

class TestGame2048(unittest.TestCase):
    
    def test_init(self):
        """Test game initialization"""
        game = Game2048()
        self.assertEqual(game.size, 4)
        self.assertEqual(game.score, 0)
        self.assertEqual(len(game.grid), 4)
        self.assertEqual(len(game.grid[0]), 4)
    
    def test_slide_row(self):
        """Test row sliding logic"""
        game = Game2048()
        # Test basic slide
        self.assertEqual(game._slide_row([2, 0, 0, 0]), [2, 0, 0, 0])
        # Test merge
        self.assertEqual(game._slide_row([2, 2, 0, 0]), [4, 0, 0, 0])
        # Test no merge
        self.assertEqual(game._slide_row([2, 4, 0, 0]), [2, 4, 0, 0])
        # Test multiple merges
        self.assertEqual(game._slide_row([2, 2, 4, 4]), [4, 8, 0, 0])
    
    def test_has_won(self):
        """Test win condition"""
        game = Game2048()
        self.assertFalse(game.has_won())
        game.grid[0][0] = 2048
        self.assertTrue(game.has_won())
    
    def test_can_move(self):
        """Test move availability"""
        game = Game2048()
        # New game should always be able to move
        self.assertTrue(game.can_move())
        
        # Full grid with no merges possible
        game.grid = [
            [2, 4, 2, 4],
            [4, 2, 4, 2],
            [2, 4, 2, 4],
            [4, 2, 4, 2]
        ]
        self.assertFalse(game.can_move())
    
    def test_move_left(self):
        """Test left move"""
        game = Game2048()
        game.grid = [
            [2, 2, 0, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0]
        ]
        moved = game.move_left()
        self.assertTrue(moved)
        self.assertEqual(game.grid[0][0], 4)

if __name__ == '__main__':
    unittest.main()
