GameIntegrationTest.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package cz.davza.studentadventure.integration;
  2. import cz.davza.studentadventure.Items.Book;
  3. import cz.davza.studentadventure.TestHelpers;
  4. import cz.davza.studentadventure.TestHelpers.CapturingOutputSink;
  5. import cz.davza.studentadventure.enums.CommandState;
  6. import cz.davza.studentadventure.objects.CommandResult;
  7. import cz.davza.studentadventure.objects.Game;
  8. import org.junit.jupiter.api.BeforeEach;
  9. import org.junit.jupiter.api.Test;
  10. import static org.junit.jupiter.api.Assertions.*;
  11. class GameIntegrationTest {
  12. private Game game;
  13. private CapturingOutputSink sink;
  14. @BeforeEach
  15. void setUp() {
  16. sink = new CapturingOutputSink();
  17. game = new Game(sink);
  18. }
  19. @Test
  20. void quitReturnsGameOver() {
  21. CommandResult result = game.sendCommand("quit");
  22. assertEquals(CommandState.GAME_OVER, result.getResultState());
  23. }
  24. @Test
  25. void commandAfterQuitIsRejected() {
  26. game.sendCommand("quit");
  27. CommandResult result = game.sendCommand("study 1");
  28. assertEquals(CommandState.GAME_OVER, result.getResultState());
  29. }
  30. @Test
  31. void unknownCommandReturnsFailed() {
  32. CommandResult result = game.sendCommand("fly");
  33. assertEquals(CommandState.FAILED, result.getResultState());
  34. }
  35. @Test
  36. void walkToHallwayChangesRoom() {
  37. CommandResult result = game.sendCommand("walk hallway");
  38. assertEquals(CommandState.ROOM_CHANGED, result.getResultState());
  39. assertEquals("hallway", game.getContext().getMap().getCurrentRoom().getExitCode());
  40. }
  41. @Test
  42. void walkToInvalidRoomFails() {
  43. CommandResult result = game.sendCommand("walk narnia");
  44. assertEquals(CommandState.FAILED, result.getResultState());
  45. }
  46. @Test
  47. void studyInBedroomWorks() {
  48. int intBefore = game.getContext().getPlayer().getIntelligence().getValue();
  49. game.sendCommand("study 1");
  50. assertEquals(intBefore + 5, game.getContext().getPlayer().getIntelligence().getValue());
  51. }
  52. @Test
  53. void deadlinePassedCausesGameOver() {
  54. // Advance time past day 7, 09:00
  55. for (int i = 0; i < 7; i++) game.getContext().getTime().advanceToNextMorning();
  56. game.getContext().getTime().advanceHours(2);
  57. // Any command should trigger the deadline check
  58. CommandResult result = game.sendCommand("stats");
  59. assertEquals(CommandState.GAME_OVER, result.getResultState());
  60. }
  61. @Test
  62. void stressAt100CausesGameOver() {
  63. game.getContext().getPlayer().getStress().increase(100);
  64. CommandResult result = game.sendCommand("stats");
  65. assertEquals(CommandState.GAME_OVER, result.getResultState());
  66. }
  67. @Test
  68. void fullWalkSequenceBedroomToHallway() {
  69. game.sendCommand("walk hallway");
  70. assertEquals("hallway", game.getContext().getMap().getCurrentRoom().getExitCode());
  71. }
  72. @Test
  73. void enteringClassroomWithoutBookFails() {
  74. game.sendCommand("walk hallway");
  75. CommandResult result = game.sendCommand("walk classroom");
  76. assertEquals(CommandState.FAILED, result.getResultState());
  77. // should still be in hallway
  78. assertEquals("hallway", game.getContext().getMap().getCurrentRoom().getExitCode());
  79. }
  80. @Test
  81. void fullPathToExamAndPass() {
  82. // Max out intelligence, walk to exam room, enter with correct ID, take exam
  83. game.getContext().getPlayer().getIntelligence().increase(100);
  84. game.getContext().getPlayer().getInventory().Add(new Book(), 1);
  85. game.sendCommand("walk hallway");
  86. game.sendCommand("walk classroom");
  87. game.sendCommand("walk examroom"); // triggers ID prompt
  88. String studentId = game.getContext().getPlayer().getId();
  89. game.sendCommand(studentId); // enter exam room
  90. CommandResult result = game.sendCommand("takeexam");
  91. assertEquals(CommandState.GAME_WIN, result.getResultState());
  92. }
  93. @Test
  94. void buyAndConsumeEnergyDrinkFlow() {
  95. // Walk to shop: hallway → studyroom → computerlab → shop
  96. game.sendCommand("walk hallway");
  97. game.sendCommand("walk studyroom");
  98. game.sendCommand("walk computerlab");
  99. game.sendCommand("walk shop");
  100. game.getContext().getPlayer().getEnergy().decrease(50);
  101. int energyBefore = game.getContext().getPlayer().getEnergy().getValue();
  102. game.sendCommand("buy energy_drink");
  103. game.sendCommand("consume energy_drink");
  104. assertTrue(game.getContext().getPlayer().getEnergy().getValue() > energyBefore);
  105. }
  106. @Test
  107. void computerInteractionFullFlow() {
  108. game.sendCommand("walk hallway");
  109. game.sendCommand("walk studyroom");
  110. game.sendCommand("walk computerlab");
  111. game.sendCommand("interact computer"); // → asks for password
  112. game.sendCommand("wrongpassword"); // → wrong
  113. game.sendCommand("java123"); // → correct, now active
  114. int intBefore = game.getContext().getPlayer().getIntelligence().getValue();
  115. game.sendCommand("study 1"); // study while at computer
  116. assertEquals(intBefore + 5,
  117. game.getContext().getPlayer().getIntelligence().getValue());
  118. CommandResult result = game.sendCommand("exit"); // leave computer
  119. assertEquals(CommandState.ACTION_RESUMED, result.getResultState());
  120. }
  121. }