package cz.davza.studentadventure.items; import cz.davza.studentadventure.Items.Computer; import cz.davza.studentadventure.TestHelpers; import cz.davza.studentadventure.enums.CommandState; import cz.davza.studentadventure.objects.CommandResult; import cz.davza.studentadventure.objects.GameData; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class ComputerTest { private Computer computer; private GameData context; private static final String PASSWORD = "java123"; @BeforeEach void setUp() { context = TestHelpers.buildGameData(); computer = new Computer(PASSWORD); // Navigate to computer lab where study is allowed context.getMap().setNextRoom(context.getMap().getCurrentRoom().getExit("hallway")); context.getMap().commitRoomChange(); context.getMap().setNextRoom(context.getMap().getCurrentRoom().getExit("studyroom")); context.getMap().commitRoomChange(); context.getMap().setNextRoom(context.getMap().getCurrentRoom().getExit("computerlab")); context.getMap().commitRoomChange(); } @Test void interactWhileLockedRequestsPassword() { CommandResult result = computer.Interact(context); assertEquals(CommandState.ACTION_PENDING, result.getResultState()); assertTrue(result.getResultMessage().contains("locked")); } @Test void exitWhileAtPasswordPromptResumesGame() { computer.Interact(context); CommandResult result = computer.handleCommand("exit", context); assertEquals(CommandState.ACTION_RESUMED, result.getResultState()); } @Test void wrongPasswordReturnsFailed() { computer.Interact(context); CommandResult result = computer.handleCommand("wrongpass", context); assertEquals(CommandState.FAILED, result.getResultState()); } @Test void wrongPasswordKeepsComputerLocked() { computer.Interact(context); computer.handleCommand("wrongpass", context); // Should still be locked — interacting again should ask for password CommandResult result = computer.Interact(context); assertTrue(result.getResultMessage().contains("locked")); } @Test void correctPasswordUnlocksComputer() { computer.Interact(context); CommandResult result = computer.handleCommand(PASSWORD, context); // Should return success (handler already on computer) assertEquals(CommandState.SUCCESS, result.getResultState()); } @Test void afterUnlockInteractGoesActiveNotPassword() { computer.Interact(context); computer.handleCommand(PASSWORD, context); // Next interact should go straight to active state CommandResult result = computer.Interact(context); assertEquals(CommandState.ACTION_PENDING, result.getResultState()); assertFalse(result.getResultMessage().contains("locked")); } @Test void exitWhileActiveResumesGame() { computer.Interact(context); computer.handleCommand(PASSWORD, context); CommandResult result = computer.handleCommand("exit", context); assertEquals(CommandState.ACTION_RESUMED, result.getResultState()); } @Test void studyCommandWorksWhileActiveOnComputer() { computer.Interact(context); computer.handleCommand(PASSWORD, context); int intBefore = context.getPlayer().getIntelligence().getValue(); computer.handleCommand("study 1", context); assertEquals(intBefore + 5, context.getPlayer().getIntelligence().getValue()); } @Test void unknownCommandWhileActiveReturnsFailed() { computer.Interact(context); computer.handleCommand(PASSWORD, context); CommandResult result = computer.handleCommand("sleep", context); assertEquals(CommandState.FAILED, result.getResultState()); } }