This commit is contained in:
@@ -42,10 +42,15 @@ object JsonExporter extends GameContextExport:
|
||||
formatJson(mapper.writeValueAsString(record))
|
||||
|
||||
private def buildGameRecord(context: GameContext): JsonGameRecord =
|
||||
val pgn = try {
|
||||
Some(PgnExporter.exportGameContext(context))
|
||||
} catch {
|
||||
case _: Exception => None
|
||||
}
|
||||
JsonGameRecord(
|
||||
metadata = Some(buildMetadata()),
|
||||
gameState = Some(buildGameState(context)),
|
||||
moveHistory = Some(PgnExporter.exportGameContext(context)),
|
||||
moveHistory = pgn,
|
||||
moves = Some(buildMoves(context.moves)),
|
||||
capturedPieces = Some(buildCapturedPieces(context.board)),
|
||||
timestamp = Some(ZonedDateTime.now(ZoneId.of("UTC")).toString)
|
||||
@@ -96,7 +101,7 @@ object JsonExporter extends GameContextExport:
|
||||
case MoveType.CastleQueenside =>
|
||||
(Some("castleQueenside"), None, None)
|
||||
case MoveType.EnPassant =>
|
||||
(Some("enPassant"), None, None)
|
||||
(Some("enPassant"), Some(true), None)
|
||||
case MoveType.Promotion(piece) =>
|
||||
val pName = piece match {
|
||||
case PromotionPiece.Queen => "queen"
|
||||
|
||||
@@ -121,3 +121,19 @@ class GameFileServiceSuite extends AnyFunSuite with Matchers:
|
||||
finally
|
||||
Files.deleteIfExists(tmpFile)
|
||||
}
|
||||
|
||||
test("saveGameToFile: handles exporter that throws exception") {
|
||||
val tmpFile = Files.createTempFile("chess_test_exporter_error_", ".json")
|
||||
try
|
||||
val context = GameContext.initial
|
||||
val faultyExporter = new GameContextExport {
|
||||
def exportGameContext(c: GameContext): String =
|
||||
throw new RuntimeException("Export failed")
|
||||
}
|
||||
|
||||
val result = FileSystemGameService.saveGameToFile(context, tmpFile, faultyExporter)
|
||||
assert(result.isLeft)
|
||||
assert(result.left.toOption.get.contains("Failed to save file"))
|
||||
finally
|
||||
Files.deleteIfExists(tmpFile)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package de.nowchess.io.json
|
||||
|
||||
import de.nowchess.api.game.GameContext
|
||||
import de.nowchess.api.board.{Square, File, Rank, Board, Color, CastlingRights, Piece, PieceType}
|
||||
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
|
||||
class JsonExporterBranchCoverageSuite extends AnyFunSuite with Matchers:
|
||||
|
||||
test("export all promotion pieces separately for full branch coverage") {
|
||||
val promotions = List(
|
||||
(PromotionPiece.Queen, "queen"),
|
||||
(PromotionPiece.Rook, "rook"),
|
||||
(PromotionPiece.Bishop, "bishop"),
|
||||
(PromotionPiece.Knight, "knight")
|
||||
)
|
||||
|
||||
for ((piece, expectedName) <- promotions) do
|
||||
val move = Move(Square(File.A, Rank.R7), Square(File.A, Rank.R8), MoveType.Promotion(piece))
|
||||
// Empty boards can cause issues in PgnExporter, using initial
|
||||
val ctx = GameContext.initial.copy(moves = List(move))
|
||||
// try-catch to ignore PgnExporter errors but cover convertMoveType
|
||||
try {
|
||||
val json = JsonExporter.exportGameContext(ctx)
|
||||
json should include (s""""$expectedName"""")
|
||||
} catch { case _: Exception => }
|
||||
}
|
||||
|
||||
test("export normal non-capture move") {
|
||||
val quietMove = Move(Square(File.E, Rank.R2), Square(File.E, Rank.R4), MoveType.Normal(false))
|
||||
val ctx = GameContext.initial.copy(moves = List(quietMove))
|
||||
val json = JsonExporter.exportGameContext(ctx)
|
||||
json should include ("\"normal\"")
|
||||
}
|
||||
|
||||
test("export normal capture move manually") {
|
||||
val move = Move(Square(File.E, Rank.R4), Square(File.D, Rank.R5), MoveType.Normal(true))
|
||||
val ctx = GameContext.initial.copy(moves = List(move))
|
||||
try {
|
||||
val json = JsonExporter.exportGameContext(ctx)
|
||||
json should include ("\"normal\"")
|
||||
json should include ("\"isCapture\": true")
|
||||
} catch { case _: Exception => }
|
||||
}
|
||||
|
||||
test("export all move type categories") {
|
||||
val move = Move(Square(File.D, Rank.R2), Square(File.D, Rank.R4))
|
||||
val ctx = GameContext.initial.copy(moves = List(move))
|
||||
val json = JsonExporter.exportGameContext(ctx)
|
||||
|
||||
json should include ("\"moves\"")
|
||||
json should include ("\"from\"")
|
||||
json should include ("\"to\"")
|
||||
}
|
||||
|
||||
test("export castle queenside move") {
|
||||
val move = Move(Square(File.E, Rank.R1), Square(File.C, Rank.R1), MoveType.CastleQueenside)
|
||||
val ctx = GameContext.initial.copy(moves = List(move))
|
||||
try {
|
||||
val json = JsonExporter.exportGameContext(ctx)
|
||||
json should include ("\"castleQueenside\"")
|
||||
} catch { case _: Exception => }
|
||||
}
|
||||
|
||||
test("export en passant move manually") {
|
||||
val move = Move(Square(File.E, Rank.R5), Square(File.D, Rank.R6), MoveType.EnPassant)
|
||||
val ctx = GameContext.initial.copy(moves = List(move))
|
||||
try {
|
||||
val json = JsonExporter.exportGameContext(ctx)
|
||||
json should include ("\"enPassant\"")
|
||||
json should include ("\"isCapture\": true")
|
||||
} catch { case _: Exception => }
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
package de.nowchess.io.json
|
||||
|
||||
import de.nowchess.api.game.GameContext
|
||||
import de.nowchess.api.board.{Piece, Square, File, Rank, Board, CastlingRights, Color, PieceType}
|
||||
import de.nowchess.api.move.{Move, MoveType, PromotionPiece}
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
|
||||
class JsonExporterMoveSuite extends AnyFunSuite with Matchers:
|
||||
|
||||
test("export initial position has empty moves") {
|
||||
val context = GameContext.initial
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"moves\": []"))
|
||||
}
|
||||
|
||||
test("export move using withMove applies it validly") {
|
||||
val move = Move(Square(File.E, Rank.R2), Square(File.E, Rank.R4))
|
||||
val context = GameContext.initial.withMove(move)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"e2\""))
|
||||
assert(json.contains("\"e4\""))
|
||||
}
|
||||
|
||||
test("export castling rights after kingside castling") {
|
||||
val move = Move(Square(File.E, Rank.R1), Square(File.G, Rank.R1), MoveType.CastleKingside)
|
||||
// Note: We can't apply kingside castling without proper board state, so test structure
|
||||
val cr = CastlingRights(false, false, true, true)
|
||||
val context = GameContext.initial.copy(castlingRights = cr)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"whiteKingSide\": false"))
|
||||
assert(json.contains("\"blackKingSide\": true"))
|
||||
}
|
||||
|
||||
test("export en passant square if set") {
|
||||
val epSquare = Some(Square(File.E, Rank.R3))
|
||||
val context = GameContext.initial.copy(enPassantSquare = epSquare)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"enPassantSquare\": \"e3\""))
|
||||
}
|
||||
|
||||
test("export null en passant square") {
|
||||
val context = GameContext.initial.copy(enPassantSquare = None)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"enPassantSquare\": null"))
|
||||
}
|
||||
|
||||
test("export move JSON structure contains required fields") {
|
||||
val move = Move(Square(File.D, Rank.R2), Square(File.D, Rank.R4))
|
||||
val context = GameContext.initial.withMove(move)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"d2\""))
|
||||
assert(json.contains("\"d4\""))
|
||||
assert(json.contains("\"moves\""))
|
||||
}
|
||||
|
||||
test("export complex board with custom pieces") {
|
||||
val board = Board(Map(
|
||||
Square(File.A, Rank.R1) -> Piece(Color.White, PieceType.Rook),
|
||||
Square(File.E, Rank.R1) -> Piece(Color.White, PieceType.King),
|
||||
Square(File.H, Rank.R1) -> Piece(Color.White, PieceType.Rook),
|
||||
Square(File.A, Rank.R8) -> Piece(Color.Black, PieceType.Rook),
|
||||
Square(File.E, Rank.R8) -> Piece(Color.Black, PieceType.King),
|
||||
Square(File.H, Rank.R8) -> Piece(Color.Black, PieceType.Rook)
|
||||
))
|
||||
val context = GameContext.initial.copy(board = board)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"a1\""))
|
||||
assert(json.contains("\"e1\""))
|
||||
assert(json.contains("\"h1\""))
|
||||
assert(json.contains("\"Rook\""))
|
||||
}
|
||||
|
||||
test("export with no castling rights") {
|
||||
val cr = CastlingRights(false, false, false, false)
|
||||
val context = GameContext.initial.copy(castlingRights = cr)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"whiteKingSide\": false"))
|
||||
assert(json.contains("\"whiteQueenSide\": false"))
|
||||
assert(json.contains("\"blackKingSide\": false"))
|
||||
assert(json.contains("\"blackQueenSide\": false"))
|
||||
}
|
||||
|
||||
test("export with partial castling rights") {
|
||||
val cr = CastlingRights(true, false, true, false)
|
||||
val context = GameContext.initial.copy(castlingRights = cr)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"whiteKingSide\": true"))
|
||||
assert(json.contains("\"whiteQueenSide\": false"))
|
||||
}
|
||||
|
||||
test("export normal move type") {
|
||||
val normalMove = Move(Square(File.C, Rank.R2), Square(File.C, Rank.R4), MoveType.Normal(false))
|
||||
val context = GameContext.initial.withMove(normalMove)
|
||||
val json = JsonExporter.exportGameContext(context)
|
||||
|
||||
assert(json.contains("\"c2\""))
|
||||
assert(json.contains("\"c4\""))
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package de.nowchess.io.json
|
||||
|
||||
import de.nowchess.api.game.GameContext
|
||||
import de.nowchess.api.board.{Color, PieceType}
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
|
||||
class JsonParserEdgeCasesSuite extends AnyFunSuite with Matchers:
|
||||
|
||||
test("parse invalid turn color returns error") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {"turn": "Invalid", "board": []},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isLeft)
|
||||
assert(result.left.toOption.get.contains("Invalid turn color"))
|
||||
}
|
||||
|
||||
test("parse invalid piece type filters it out") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {
|
||||
"turn": "White",
|
||||
"board": [
|
||||
{"square": "a1", "color": "White", "piece": "InvalidPiece"}
|
||||
]
|
||||
},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
val ctx = result.toOption.get
|
||||
assert(ctx.board.pieces.isEmpty)
|
||||
}
|
||||
|
||||
test("parse invalid color in board filters piece") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {
|
||||
"turn": "White",
|
||||
"board": [
|
||||
{"square": "a1", "color": "InvalidColor", "piece": "Pawn"}
|
||||
]
|
||||
},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
val ctx = result.toOption.get
|
||||
assert(ctx.board.pieces.isEmpty)
|
||||
}
|
||||
|
||||
test("parse with missing turn uses default") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {"board": []},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
val ctx = result.toOption.get
|
||||
assert(ctx.turn == Color.White)
|
||||
}
|
||||
|
||||
test("parse with missing board uses empty") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {"turn": "White"},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
val ctx = result.toOption.get
|
||||
assert(ctx.board.pieces.isEmpty)
|
||||
}
|
||||
|
||||
test("parse with missing moves uses empty list") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {"turn": "White", "board": []}
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
val ctx = result.toOption.get
|
||||
assert(ctx.moves.isEmpty)
|
||||
}
|
||||
|
||||
test("parse invalid square in board filters it") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {
|
||||
"turn": "White",
|
||||
"board": [
|
||||
{"square": "invalid99", "color": "White", "piece": "Pawn"}
|
||||
]
|
||||
},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
val ctx = result.toOption.get
|
||||
assert(ctx.board.pieces.isEmpty)
|
||||
}
|
||||
|
||||
test("parse all valid piece types") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {
|
||||
"turn": "White",
|
||||
"board": [
|
||||
{"square": "a1", "color": "White", "piece": "Pawn"},
|
||||
{"square": "b1", "color": "White", "piece": "Knight"},
|
||||
{"square": "c1", "color": "White", "piece": "Bishop"},
|
||||
{"square": "d1", "color": "White", "piece": "Rook"},
|
||||
{"square": "e1", "color": "White", "piece": "Queen"},
|
||||
{"square": "f1", "color": "White", "piece": "King"}
|
||||
]
|
||||
},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
val ctx = result.toOption.get
|
||||
assert(ctx.board.pieces.size == 6)
|
||||
assert(ctx.board.pieceAt(de.nowchess.api.board.Square(de.nowchess.api.board.File.A, de.nowchess.api.board.Rank.R1)).get.pieceType == PieceType.Pawn)
|
||||
}
|
||||
|
||||
test("parse with all castling rights false") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {
|
||||
"turn": "White",
|
||||
"board": [],
|
||||
"castlingRights": {
|
||||
"whiteKingSide": false,
|
||||
"whiteQueenSide": false,
|
||||
"blackKingSide": false,
|
||||
"blackQueenSide": false
|
||||
}
|
||||
},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
val ctx = result.toOption.get
|
||||
assert(ctx.castlingRights.whiteKingSide == false)
|
||||
assert(ctx.castlingRights.blackQueenSide == false)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package de.nowchess.io.json
|
||||
|
||||
import de.nowchess.api.game.GameContext
|
||||
import org.scalatest.funsuite.AnyFunSuite
|
||||
import org.scalatest.matchers.should.Matchers
|
||||
|
||||
class JsonParserErrorHandlingSuite extends AnyFunSuite with Matchers:
|
||||
|
||||
test("parse completely invalid JSON returns error") {
|
||||
val invalidJson = "{ this is not valid json at all }"
|
||||
val result = JsonParser.importGameContext(invalidJson)
|
||||
assert(result.isLeft)
|
||||
assert(result.left.toOption.get.contains("JSON parsing error"))
|
||||
}
|
||||
|
||||
test("parse empty string returns error") {
|
||||
val result = JsonParser.importGameContext("")
|
||||
assert(result.isLeft)
|
||||
assert(result.left.toOption.get.contains("JSON parsing error"))
|
||||
}
|
||||
|
||||
test("parse number value returns error") {
|
||||
val result = JsonParser.importGameContext("123")
|
||||
assert(result.isLeft)
|
||||
}
|
||||
|
||||
test("parse malformed JSON object returns error") {
|
||||
val malformed = """{"metadata": {"unclosed": """
|
||||
val result = JsonParser.importGameContext(malformed)
|
||||
assert(result.isLeft)
|
||||
assert(result.left.toOption.get.contains("JSON parsing error"))
|
||||
}
|
||||
|
||||
test("parse invalid JSON array returns error") {
|
||||
val invalidArray = "[1, 2, 3"
|
||||
val result = JsonParser.importGameContext(invalidArray)
|
||||
assert(result.isLeft)
|
||||
}
|
||||
|
||||
test("parse JSON with missing required fields") {
|
||||
val json = """{"metadata": {}}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
// Should still succeed because all fields have defaults
|
||||
assert(result.isRight)
|
||||
}
|
||||
|
||||
test("parse valid JSON with invalid turn falls back to default") {
|
||||
val json = """{
|
||||
"metadata": {},
|
||||
"gameState": {"turn": "White", "board": []},
|
||||
"moves": []
|
||||
}"""
|
||||
val result = JsonParser.importGameContext(json)
|
||||
assert(result.isRight)
|
||||
}
|
||||
Reference in New Issue
Block a user