Skip to content

Comments

Add backend unit tests for controllers and WebSocket handler#47

Open
RobotHanzo wants to merge 4 commits intoautofrom
add-backend-tests-16104220138383017423
Open

Add backend unit tests for controllers and WebSocket handler#47
RobotHanzo wants to merge 4 commits intoautofrom
add-backend-tests-16104220138383017423

Conversation

@RobotHanzo
Copy link
Owner

This PR adds comprehensive unit tests for the backend controllers (GameController, SessionController, AuthController, SpeechController) and key components like GlobalWebSocketHandler and AuthSession. It also refactors JudgeDecisionStepTest to improve stability and consistency with the new tests. All tests use JUnit 5 and Mockito. Note: The build configuration (JDK 25) was preserved as requested.


PR created automatically by Jules for task 16104220138383017423 started by @RobotHanzo

- Added `GameControllerTest` covering game state, player management, and actions.
- Added `SessionControllerTest` covering session retrieval.
- Added `AuthControllerTest` covering login, guild selection, and logout.
- Added `SpeechControllerTest` covering speech flow and audio management.
- Added `GlobalWebSocketHandlerTest` covering connection handling and message broadcasting.
- Added `AuthSessionTest` covering role logic.
- Updated `JudgeDecisionStepTest` to use consistent Mockito setup and fix potential flakiness.
- Ensured tests mock static `WerewolfApplication` dependencies correctly.

Co-authored-by: RobotHanzo <36107150+RobotHanzo@users.noreply.github.com>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings February 23, 2026 08:05
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds backend unit test coverage around key controllers and WebSocket/auth session behavior, plus minor test refactoring and build-wrapper housekeeping.

Changes:

  • Added new JUnit5/Mockito tests for GameController, SessionController, AuthController, SpeechController, GlobalWebSocketHandler, and AuthSession.
  • Refactored JudgeDecisionStepTest to use @Mock + MockitoAnnotations initialization.
  • Added gradlew and removed trailing whitespace from settings.gradle.kts.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/test/kotlin/dev/robothanzo/werewolf/security/GlobalWebSocketHandlerTest.kt Adds tests for WS connection gating, ping/pong handling, and guild broadcast routing.
src/test/kotlin/dev/robothanzo/werewolf/game/steps/JudgeDecisionStepTest.kt Refactors mocking setup for stability/consistency.
src/test/kotlin/dev/robothanzo/werewolf/database/documents/AuthSessionTest.kt Adds unit tests for role-derived flags and UserRole.fromString.
src/test/kotlin/dev/robothanzo/werewolf/controller/SpeechControllerTest.kt Adds controller tests for speech flow endpoints and update broadcasts.
src/test/kotlin/dev/robothanzo/werewolf/controller/SessionControllerTest.kt Adds controller tests for session listing and session retrieval.
src/test/kotlin/dev/robothanzo/werewolf/controller/GameControllerTest.kt Adds controller tests for state transitions, role assignment, game lifecycle, and settings updates.
src/test/kotlin/dev/robothanzo/werewolf/controller/AuthControllerTest.kt Adds controller tests for guild selection, current-user info, and logout.
settings.gradle.kts Removes trailing blank line.
gradlew Adds Gradle Unix wrapper script.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +38 to +42
sessionController = SessionController(gameSessionService, identityUtils)

val mockJda = mock<JDA>()
WerewolfApplication.jda = mockJda
}
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test uses mock<JDA>() but only imports org.mockito.Mockito.mock. The reified mock<T>() helper comes from org.mockito.kotlin.mock, so this currently won’t compile. Either import/use org.mockito.kotlin.mock or call Mockito.mock(JDA::class.java).

Copilot uses AI. Check for mistakes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: Removed mock<T>() and imported org.mockito.kotlin.mock to ensure compilation and correct usage.

Comment on lines +58 to +62
val guildMock = mock<Guild>()
whenever(guildMock.name).thenReturn("Test Guild")
whenever(guildMock.iconUrl).thenReturn("icon.png")
whenever(guildMock.getMemberById("user1")).thenReturn(mock<Member>())

Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue as above: mock<Guild>() / mock<Member>() requires org.mockito.kotlin.mock (or use Mockito.mock(Guild::class.java) etc.). As written, this test won’t compile.

Copilot uses AI. Check for mistakes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: Imported org.mockito.kotlin.mock to resolve the mock<Guild>() compilation issue.

Comment on lines 30 to 50
@InjectMocks
private lateinit var speechController: SpeechController

@Mock
private lateinit var gameSessionService: GameSessionService

@Mock
private lateinit var speechService: SpeechService

@Mock
private lateinit var policeService: PoliceService

@Mock
private lateinit var jda: JDA

@BeforeEach
fun setup() {
MockitoAnnotations.openMocks(this)
WerewolfApplication.jda = jda
speechController = SpeechController(gameSessionService, speechService, policeService)
}
Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@InjectMocks is declared but the controller is then instantiated manually in setup(). This is redundant and can confuse future readers about how the object under test is created. Either rely on @InjectMocks or remove it and keep manual construction.

Copilot uses AI. Check for mistakes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: Removed the redundant @InjectMocks annotation since I am manually instantiating the controller in the setup() method.

Comment on lines 54 to 63
// Assume isAdmin logic - isAdmin is an extension function, mocking it might be hard.
// It likely checks permissions.
// If it's an extension function, we cannot easily mock it unless we mock the Member interface completely
// and isAdmin calls member methods.
// dev.robothanzo.werewolf.utils.isAdmin checks member.hasPermission(Administrator) or is owner.

// However, isAdmin is an extension function.
// Let's assume it returns false for mock unless we configure it.
// But for this test, we just check if it returns OK.

Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This inline commentary about difficulties mocking isAdmin() is speculative/noisy. Since the test doesn’t assert the admin path, consider removing the commentary and (optionally) explicitly stubbing memberMock.hasPermission(Permission.ADMINISTRATOR) if you want deterministic coverage of role selection.

Copilot uses AI. Check for mistakes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: Removed the speculative comments about isAdmin to reduce noise.

Comment on lines 37 to 49
val user = AuthSession(userId = "user1", guildId = "123", role = UserRole.SPECTATOR)
whenever(session.attributes).thenReturn(mapOf("user" to user))
whenever(session.uri).thenReturn(URI("ws://localhost:8080/ws?guildId=123"))

handler.afterConnectionEstablished(session)

verify(session, never()).close(any())
}

@Test
fun testConnectionEstablished_NoUser() {
whenever(session.attributes).thenReturn(emptyMap())

Copy link

Copilot AI Feb 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebSocketSession.attributes is a mutable map (Spring exposes it as a Java Map, which Kotlin maps to MutableMap). Stubbing it with mapOf(...)/emptyMap() returns a read-only Map and will fail type-checking. Use mutableMapOf(...) (or hashMapOf(...)) for these stubs.

Copilot uses AI. Check for mistakes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed: Updated whenever(session.attributes).thenReturn(...) to use mutableMapOf(...) to match the expected MutableMap type.

RobotHanzo and others added 2 commits February 24, 2026 12:41
…hSessionTest.kt

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…etHandlerTest.kt

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@RobotHanzo
Copy link
Owner Author

@copilot open a new pull request to apply changes based on the comments in this thread

Copy link

Copilot AI commented Feb 24, 2026

@RobotHanzo I've opened a new pull request, #48, to work on those changes. Once the pull request is ready, I'll request review from you.

- Fix mock imports in SessionControllerTest
- Remove redundant @Injectmocks in SpeechControllerTest
- Remove noisy comments in AuthControllerTest
- Use mutableMapOf for session attributes in GlobalWebSocketHandlerTest
- Revert JDK version to 25 and re-enable jdave dependencies

Co-authored-by: RobotHanzo <36107150+RobotHanzo@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants