|
| 1 | +import org.scalatest.funsuite.AnyFunSuite |
| 2 | +import io.circe.parser.parse |
| 3 | +import io.circe.syntax._ // For .asJson |
| 4 | +import io.circe.Json // For explicit Json type usage |
| 5 | +import JsonFormats._ // Your encoders |
| 6 | + |
| 7 | +// Assuming UnitedWeStandApp is in the default package |
| 8 | +// Assuming Participant and Society case classes are in the default package |
| 9 | + |
| 10 | +class UnitedWeStandAppTest extends AnyFunSuite { |
| 11 | + |
| 12 | + test( |
| 13 | + "UnitedWeStandApp.runCycles output should be valid JSON and contain non-empty participants array" |
| 14 | + ) { |
| 15 | + // Directly call runCycles and serialize, to avoid capturing stdout |
| 16 | + val society = UnitedWeStandApp.runCycles(cycleCount = 5) // Use a small cycle count for testing |
| 17 | + val jsonString = society.asJson.noSpaces |
| 18 | + |
| 19 | + // 1. Check if the string is valid JSON |
| 20 | + val parsedJsonResult = parse(jsonString) |
| 21 | + assert( |
| 22 | + parsedJsonResult.isRight, |
| 23 | + s"Output should be valid JSON. Parsing error: ${parsedJsonResult.left.getOrElse("")}" |
| 24 | + ) |
| 25 | + |
| 26 | + // 2. Perform checks on the parsed JSON structure |
| 27 | + val json = parsedJsonResult.getOrElse( |
| 28 | + throw new RuntimeException( |
| 29 | + "JSON parsing failed in test, this should not happen due to the assert above." |
| 30 | + ) |
| 31 | + ) |
| 32 | + |
| 33 | + // Check for the presence of the 'participants' array |
| 34 | + val participantsCursor = json.hcursor.downField("participants") |
| 35 | + assert( |
| 36 | + participantsCursor.succeeded, |
| 37 | + "JSON should contain a 'participants' field." |
| 38 | + ) |
| 39 | + |
| 40 | + // Check if 'participants' is an array and is non-empty |
| 41 | + val participantsArray = participantsCursor.as[List[Json]] |
| 42 | + assert( |
| 43 | + participantsArray.isRight, |
| 44 | + "'participants' field should be an array." |
| 45 | + ) |
| 46 | + assert( |
| 47 | + participantsArray.getOrElse(Nil).nonEmpty, |
| 48 | + "Participants array should not be empty." |
| 49 | + ) |
| 50 | + |
| 51 | + // 3. Optionally, check structure of a participant (if needed, more detailed) |
| 52 | + val firstParticipantOpt = participantsArray.getOrElse(Nil).headOption |
| 53 | + assert( |
| 54 | + firstParticipantOpt.isDefined, |
| 55 | + "Participant array should have at least one participant." |
| 56 | + ) |
| 57 | + |
| 58 | + val firstParticipantJson = firstParticipantOpt.get |
| 59 | + assert( |
| 60 | + firstParticipantJson.hcursor.downField("uuid").as[String].isRight, |
| 61 | + "Participant should have a 'uuid' string field." |
| 62 | + ) |
| 63 | + assert( |
| 64 | + firstParticipantJson.hcursor.downField("opinion").as[Boolean].isRight, |
| 65 | + "Participant should have an 'opinion' boolean field." |
| 66 | + ) |
| 67 | + } |
| 68 | +} |
0 commit comments