more tests for grouping map chunks

This commit is contained in:
Adam 2022-04-20 22:58:06 +02:00
parent 8ae4e934f7
commit 2a3b0de953

View file

@ -12,11 +12,11 @@ import dev.adamko.kafkatorio.schema.common.MapTilePosition
import dev.adamko.kafkatorio.schema.common.PrototypeName
import dev.adamko.kafkatorio.schema.common.SurfaceIndex
import dev.adamko.kafkatorio.schema.common.Tick
import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype
import dev.adamko.kafkatorio.schema.packets.KafkatorioPacket
import dev.adamko.kafkatorio.schema.packets.MapChunkUpdate
import dev.adamko.kafkatorio.schema.packets.MapChunkUpdateKey
import dev.adamko.kafkatorio.schema.packets.PrototypesUpdate
import dev.adamko.kafkatorio.schema.prototypes.FactorioPrototype
import dev.adamko.kotka.kxs.serde
import io.kotest.assertions.withClue
import io.kotest.core.spec.style.FunSpec
@ -32,81 +32,17 @@ import org.apache.kafka.streams.TopologyTestDriver
class GroupMapChunksTest : FunSpec({
test("basic test") {
val streamsBuilder = StreamsBuilder()
test("proto, then map chunk") {
val prototypesTopicName = "kafkatorio.packet.prototypes"
val mapChunkUpdatesTopicName = "kafkatorio.packet.map-chunk"
val scenario = Scenario()
val topology = groupMapChunks(streamsBuilder)
val testDriver = TopologyTestDriver(topology)
val prototypesTestInputTopic: TestInputTopic<FactorioServerId, KafkatorioPacket> =
testDriver.createInputTopic(
prototypesTopicName,
jsonMapper.serde<FactorioServerId>().serializer(),
jsonMapper.serde<KafkatorioPacket>().serializer(),
scenario.pipePrototypePacket(
PrototypesUpdate(listOf(testMockTile))
)
val serverId = FactorioServerId("test-server-id")
scenario.pipeMapChunkPacket(testMapChunkUpdate)
prototypesTestInputTopic.pipeInput(
serverId,
KafkatorioPacket(
modVersion = "1.2.3",
tick = Tick(22u),
PrototypesUpdate(
listOf(
FactorioPrototype.MapTile(
PrototypeName("fake-proto"),
layer = 1u,
mapColour = Colour(0.3f, 0.2f, 0.1f, 1.0f),
collisionMasks = listOf("fake-mask"),
order = "fake-order",
canBeMined = false,
)
)
)
)
)
val mapChunkUpdatesInputTopic: TestInputTopic<FactorioServerId, KafkatorioPacket> =
testDriver.createInputTopic(
mapChunkUpdatesTopicName,
jsonMapper.serde<FactorioServerId>().serializer(),
jsonMapper.serde<KafkatorioPacket>().serializer(),
)
mapChunkUpdatesInputTopic.pipeInput(
serverId,
KafkatorioPacket(
modVersion = "1.2.3",
tick = Tick(44u),
MapChunkUpdate(
key = MapChunkUpdateKey(
MapChunkPosition(11, 22),
SurfaceIndex(1u),
),
tileDictionary = MapTileDictionary(
tilesXY = mapOf(
"1" to mapOf("2" to PrototypeKey(33))
),
protos = mapOf(PrototypeName("fake-proto") to PrototypeKey(33))
)
)
)
)
val outputTopic: TestOutputTopic<ServerMapChunkId, ServerMapChunkTiles<ColourHex>> =
testDriver
.createOutputTopic(
TOPIC_GROUPED_MAP_CHUNKS_STATE,
kxsBinary.serde<ServerMapChunkId>().deserializer(),
kxsBinary.serde<ServerMapChunkTiles<ColourHex>>().deserializer(),
)
val records = outputTopic.readRecordsToList()
val records = scenario.mapTilesOutputTopic.readRecordsToList()
withClue(records) {
records shouldHaveSize 1
@ -114,7 +50,7 @@ class GroupMapChunksTest : FunSpec({
records.map { it.key to it.value }.first() should { (key, value) ->
key.shouldNotBeNull()
key shouldBeEqualToComparingFields ServerMapChunkId(
serverId = serverId,
serverId = FactorioServerId("test-server-id"),
chunkPosition = MapChunkPosition(0, 0),
surfaceIndex = SurfaceIndex(1u),
chunkSize = ChunkSize.CHUNK_512,
@ -129,4 +65,148 @@ class GroupMapChunksTest : FunSpec({
}
}
}
})
test("map chunk, then proto") {
val scenario = Scenario()
scenario.pipePrototypePacket(
PrototypesUpdate(listOf(testMockTile.copy(mapColour = COLOUR_ONE)))
)
scenario.pipeMapChunkPacket(testMapChunkUpdate)
var records = scenario.mapTilesOutputTopic.readRecordsToList()
withClue(records) {
records shouldHaveSize 1
records.map { it.key to it.value }.first() should { (key, value) ->
key.shouldNotBeNull()
key shouldBeEqualToComparingFields ServerMapChunkId(
serverId = FactorioServerId("test-server-id"),
chunkPosition = MapChunkPosition(0, 0),
surfaceIndex = SurfaceIndex(1u),
chunkSize = ChunkSize.CHUNK_512,
)
value.shouldNotBeNull()
value shouldBeEqualToComparingFields ServerMapChunkTiles(
chunkId = key,
map = mapOf(
MapTilePosition(1, 2) to ColourHex(25u, 25u, 25u, 25u)
)
)
}
}
scenario.pipePrototypePacket(
PrototypesUpdate(listOf(testMockTile.copy(mapColour = COLOUR_TWO)))
)
records = scenario.mapTilesOutputTopic.readRecordsToList()
withClue(records) {
records shouldHaveSize 1
records.map { it.key to it.value }.first() should { (key, value) ->
key.shouldNotBeNull()
key shouldBeEqualToComparingFields ServerMapChunkId(
serverId = FactorioServerId("test-server-id"),
chunkPosition = MapChunkPosition(0, 0),
surfaceIndex = SurfaceIndex(1u),
chunkSize = ChunkSize.CHUNK_512,
)
value.shouldNotBeNull()
value shouldBeEqualToComparingFields ServerMapChunkTiles(
chunkId = key,
map = mapOf(
MapTilePosition(1, 2) to ColourHex(51u, 51u, 51u, 51u)
)
)
}
}
}
}) {
class Scenario(
streamsBuilder: StreamsBuilder = StreamsBuilder(),
prototypesTopicName: String = "kafkatorio.packet.prototypes",
mapChunkUpdatesTopicName: String = "kafkatorio.packet.map-chunk",
private val serverId: FactorioServerId = FactorioServerId("test-server-id"),
) {
private val topology = groupMapChunks(streamsBuilder)
private val testDriver: TopologyTestDriver = TopologyTestDriver(topology)
private val prototypesTestInputTopic: TestInputTopic<FactorioServerId, KafkatorioPacket> =
testDriver.createInputTopic(
prototypesTopicName,
jsonMapper.serde<FactorioServerId>().serializer(),
jsonMapper.serde<KafkatorioPacket>().serializer(),
)
private val mapChunkUpdatesInputTopic: TestInputTopic<FactorioServerId, KafkatorioPacket> =
testDriver.createInputTopic(
mapChunkUpdatesTopicName,
jsonMapper.serde<FactorioServerId>().serializer(),
jsonMapper.serde<KafkatorioPacket>().serializer(),
)
val mapTilesOutputTopic: TestOutputTopic<ServerMapChunkId, ServerMapChunkTiles<ColourHex>> =
testDriver.createOutputTopic(
TOPIC_GROUPED_MAP_CHUNKS_STATE,
kxsBinary.serde<ServerMapChunkId>().deserializer(),
kxsBinary.serde<ServerMapChunkTiles<ColourHex>>().deserializer(),
)
fun pipePrototypePacket(prototypeUpdate: PrototypesUpdate) {
prototypesTestInputTopic.pipeInput(
serverId,
KafkatorioPacket(
modVersion = "1.2.3",
tick = Tick(22u),
data = prototypeUpdate,
)
)
}
fun pipeMapChunkPacket(mapChunkUpdate: MapChunkUpdate) {
mapChunkUpdatesInputTopic.pipeInput(
serverId,
KafkatorioPacket(
modVersion = "1.2.3",
tick = Tick(44u),
data = mapChunkUpdate
)
)
}
}
companion object {
val COLOUR_ONE = Colour(0.1f, 0.1f, 0.1f, 0.1f)
val COLOUR_TWO = Colour(0.2f, 0.2f, 0.2f, 0.2f)
val testMockTile = FactorioPrototype.MapTile(
PrototypeName("fake-proto"),
layer = 1u,
mapColour = Colour(0.3f, 0.2f, 0.1f, 1.0f),
collisionMasks = listOf("fake-mask"),
order = "fake-order",
canBeMined = false,
)
val testMapChunkUpdate = MapChunkUpdate(
key = MapChunkUpdateKey(
MapChunkPosition(11, 22),
SurfaceIndex(1u),
),
tileDictionary = MapTileDictionary(
tilesXY = mapOf(
"1" to mapOf("2" to PrototypeKey(33))
),
protos = mapOf(PrototypeName("fake-proto") to PrototypeKey(33))
)
)
}
}