test: fill coverage gaps in lobbyService and gameService

- joinLobby: addPlayer returns falsy (race condition fallback)
- joinLobby: lobby disappears between addPlayer and final fetch
- createLobby: non-unique-violation errors re-thrown immediately
- createGameSession: unexpected DB errors propagate correctly
This commit is contained in:
lila 2026-04-24 10:11:36 +02:00
parent 76192667e0
commit 4ece995385
2 changed files with 34 additions and 0 deletions

View file

@ -126,6 +126,14 @@ describe("createGameSession", () => {
expect(mockGetDistractors).toHaveBeenCalledTimes(3); expect(mockGetDistractors).toHaveBeenCalledTimes(3);
}); });
it("propagates unexpected errors from getGameTerms", async () => {
mockGetGameTerms.mockRejectedValue(new Error("connection refused"));
await expect(createGameSession(validRequest)).rejects.toThrow(
"connection refused",
);
});
}); });
describe("evaluateAnswer", () => { describe("evaluateAnswer", () => {

View file

@ -87,6 +87,14 @@ describe("createLobby", () => {
"Could not generate a unique lobby code", "Could not generate a unique lobby code",
); );
}); });
it("re-throws non-unique-violation errors immediately", async () => {
const dbError = new Error("connection refused");
mockCreateLobby.mockRejectedValue(dbError);
await expect(createLobby("user-1")).rejects.toThrow("connection refused");
expect(mockCreateLobby).toHaveBeenCalledTimes(1);
});
}); });
describe("joinLobby", () => { describe("joinLobby", () => {
@ -173,4 +181,22 @@ describe("joinLobby", () => {
"Lobby is full", "Lobby is full",
); );
}); });
it("throws ConflictError when addPlayer returns falsy (race condition)", async () => {
mockAddPlayer.mockResolvedValue(undefined);
await expect(joinLobby("ABC123", "user-2")).rejects.toThrow(
"Lobby is no longer available",
);
});
it("throws AppError when lobby disappears after addPlayer succeeds", async () => {
mockGetLobbyByCodeWithPlayers
.mockResolvedValueOnce(fakeLobbyWithPlayers)
.mockResolvedValueOnce(undefined);
await expect(joinLobby("ABC123", "user-2")).rejects.toThrow(
"Lobby disappeared during join",
);
});
}); });