extends TestCase ## The command log from issue M8: what a match was given, kept so it can be replayed. func _command(tick: int, buttons: int) -> PlayerCommand: return PlayerCommand.from_input(tick, Vector2(0.5, -1.0), 1.25, -0.3, buttons, 2, 1) func _log() -> CommandLog: var rules: MatchRules = MatchRules.new() rules.max_players = 6 rules.lobby_ticks = 12 rules.fill_with_bots = false # A seed too large for a JSON number to hold exactly. var commands: CommandLog = CommandLog.new(0x7FFFFFFFFFFFFFF1, 2, rules) commands.record(0, 1, _command(0, PlayerCommand.BUTTON_JUMP)) commands.record(0, -3, _command(0, 0)) commands.record(7, 2, _command(9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD)) commands.final_tick = 8 commands.final_hash = "abc123" commands.winners = [-3] as Array[int] commands.notes = {"bots": 4} return commands func test_records_keep_their_tick_handle_and_command() -> void: var commands: CommandLog = _log() assert_eq(commands.count(), 3) assert_eq(commands.at_tick_of(2), 7) assert_eq(commands.handle_of(1), -3) assert_eq(commands.handle_of(2), 2) assert_eq(commands.command_of(2).encode(), _command(9, PlayerCommand.BUTTON_FIRE | PlayerCommand.BUTTON_BUILD).encode()) func test_round_trips_through_bytes() -> void: var commands: CommandLog = _log() var back: CommandLog = CommandLog.from_bytes(commands.to_bytes()) assert_not_null(back) assert_eq(back.session_seed, 0x7FFFFFFFFFFFFFF1, "the whole 63-bit seed survives") assert_eq(back.humans, 2) assert_eq(back.rules.max_players, 6) assert_eq(back.rules.lobby_ticks, 12) assert_false(back.rules.fill_with_bots) assert_eq(back.rules.countdown_ticks, commands.rules.countdown_ticks) assert_eq(back.count(), 3) for index: int in 3: assert_eq(back.at_tick_of(index), commands.at_tick_of(index)) assert_eq(back.handle_of(index), commands.handle_of(index)) assert_eq(back.command_of(index).encode(), commands.command_of(index).encode()) assert_eq(back.final_tick, 8) assert_eq(back.final_hash, "abc123") assert_eq(back.winners, [-3] as Array[int]) assert_eq(int(back.notes["bots"]), 4) func test_an_empty_log_round_trips() -> void: var back: CommandLog = CommandLog.from_bytes(CommandLog.new(5, 0).to_bytes()) assert_not_null(back) assert_eq(back.count(), 0) assert_eq(back.session_seed, 5) func test_saves_and_loads_a_file() -> void: var path: String = "user://test_command_log/roundtrip.tclog" assert_eq(_log().save(path), OK) var back: CommandLog = CommandLog.load_from(path) assert_not_null(back) assert_eq(back.count(), 3) DirAccess.remove_absolute(path) assert_null(CommandLog.load_from(path), "a missing file is no log") func test_bad_bytes_are_refused_without_errors() -> void: var good: PackedByteArray = _log().to_bytes() assert_null(CommandLog.from_bytes(PackedByteArray())) assert_null(CommandLog.from_bytes("TWCL".to_ascii_buffer())) var wrong_magic: PackedByteArray = good.duplicate() wrong_magic[0] = 0x58 assert_null(CommandLog.from_bytes(wrong_magic)) # Cut short anywhere: never a log with records missing, never an engine error. for size: int in range(0, good.size(), 7): var cut: CommandLog = CommandLog.from_bytes(good.slice(0, size)) assert_true(cut == null or cut.count() == 3, "cut at %d bytes" % size) # A header length that points past the end. var long_header: PackedByteArray = good.duplicate() long_header.encode_u32(4, 0x7FFFFFFF) assert_null(CommandLog.from_bytes(long_header)) # A header that isn't JSON. var head_size: int = good.decode_u32(4) var not_json: PackedByteArray = good.duplicate() for index: int in range(8, 8 + head_size): not_json[index] = 0x7B assert_null(CommandLog.from_bytes(not_json)) # A raw length that is huge, or not a whole number of records. var huge: PackedByteArray = good.duplicate() huge.encode_u32(8 + head_size, 0x7FFFFFF0) assert_null(CommandLog.from_bytes(huge)) var ragged: PackedByteArray = good.duplicate() ragged.encode_u32(8 + head_size, CommandLog.RECORD_SIZE * 3 + 1) assert_null(CommandLog.from_bytes(ragged)) # Records that don't decompress to the promised length. var short: PackedByteArray = good.duplicate() short.encode_u32(8 + head_size, CommandLog.RECORD_SIZE * 40) assert_null(CommandLog.from_bytes(short)) ## H5: a header whose fields are the wrong kind of JSON is refused, never an error. func test_a_header_with_the_wrong_types_is_refused_without_errors() -> void: var good: PackedByteArray = _log().to_bytes() var head_size: int = good.decode_u32(4) var head: Dictionary = JSON.parse_string(good.slice(8, 8 + head_size).get_string_from_utf8()) var records: PackedByteArray = good.slice(8 + head_size) var bad_rule: Dictionary = head.duplicate(true) bad_rule["rules"]["max_players"] = [1, 2] var bad_winner: Dictionary = head.duplicate(true) bad_winner["winners"] = [{"x": 1}] var bad_tick: Dictionary = head.duplicate(true) bad_tick["final_tick"] = {"x": 1} for changed: Dictionary in [bad_rule, bad_winner]: assert_null(CommandLog.from_bytes(_with_head(changed, records))) var read: CommandLog = CommandLog.from_bytes(_with_head(bad_tick, records)) assert_not_null(read, "a bad final tick alone still leaves a usable log") assert_eq(read.final_tick, 0) assert_eq(read.count(), 3) func _with_head(head: Dictionary, records: PackedByteArray) -> PackedByteArray: var text: PackedByteArray = JSON.stringify(head).to_utf8_buffer() var out: StreamPeerBuffer = StreamPeerBuffer.new() out.big_endian = false out.put_data(CommandLog.MAGIC.to_ascii_buffer()) out.put_u32(text.size()) out.put_data(text) out.put_data(records) return out.data_array