extends TestCase func _parse(args: Array[String]) -> ServerConfig: return ServerConfig.parse(PackedStringArray(args)) func test_without_server_flag_nothing_is_parsed() -> void: var config: ServerConfig = _parse(["--seed=5", "--bots=3"]) assert_false(config.server) assert_eq(config.bots, 0) assert_true(config.is_valid()) func test_defaults() -> void: var config: ServerConfig = _parse(["--server"]) assert_true(config.server) assert_true(config.is_valid()) assert_false(config.has_seed) assert_eq(config.port, ServerConfig.DEFAULT_PORT) assert_eq(config.ticks, 0) assert_false(config.unthrottled) func test_all_options() -> void: var config: ServerConfig = _parse([ "--path", "/ignored", "--server", "--seed=-17", "--port=4000", "--players=3", "--bots=20", "--ticks=900", "--unthrottled", "--log-every=30", "--bot-difficulty=hard", ]) assert_true(config.is_valid(), str(config.errors)) assert_true(config.has_seed) assert_eq(config.match_seed, -17) assert_eq(config.port, 4000) assert_eq(config.players, 3) assert_eq(config.bots, 20) assert_eq(config.ticks, 900) assert_true(config.unthrottled) assert_eq(config.log_every, 30) assert_eq(config.bot_difficulty, BotBrain.HARD) assert_eq(config.to_dictionary()["bot_difficulty"], "hard") func test_listen_is_off_by_default_and_refuses_idle_slots() -> void: assert_false(_parse(["--server"]).listen) var config: ServerConfig = _parse(["--server", "--listen", "--port=4001", "--bots=24"]) assert_true(config.is_valid(), str(config.errors)) assert_true(config.listen) assert_eq(config.to_dictionary()["listen"], true) assert_false(_parse(["--server", "--listen", "--players=2"]).is_valid()) func test_bot_difficulty_defaults_to_mixed_and_rejects_unknown_names() -> void: assert_eq(_parse(["--server"]).bot_difficulty, -1) assert_eq(_parse(["--server", "--bot-difficulty=mixed"]).bot_difficulty, -1) assert_eq(_parse(["--server", "--bot-difficulty=easy"]).bot_difficulty, BotBrain.EASY) var bad: ServerConfig = _parse(["--server", "--bot-difficulty=brutal"]) assert_false(bad.is_valid()) func test_large_seed_round_trips() -> void: var config: ServerConfig = _parse(["--server", "--seed=9223372036854775807"]) assert_eq(config.match_seed, 9223372036854775807) assert_eq(config.to_dictionary()["seed"], 9223372036854775807) func test_unknown_argument_is_an_error() -> void: var config: ServerConfig = _parse(["--server", "--bot=3"]) assert_false(config.is_valid()) assert_true(config.errors[0].contains("--bot=3")) func test_non_numeric_value_is_an_error() -> void: var config: ServerConfig = _parse(["--server", "--seed=abc"]) assert_false(config.is_valid()) assert_false(config.has_seed) func test_ranges_are_checked() -> void: assert_false(_parse(["--server", "--port=0"]).is_valid()) assert_false(_parse(["--server", "--port=70000"]).is_valid()) assert_false(_parse(["--server", "--bots=-1"]).is_valid()) assert_false(_parse(["--server", "--ticks=-1"]).is_valid()) assert_false(_parse(["--server", "--fault=meteor"]).is_valid()) var too_many: String = "--bots=%d" % (Tuning.SOLO_PLAYERS + 1) assert_false(_parse(["--server", too_many]).is_valid()) var full: Array[String] = ["--server", "--players=4", "--bots=%d" % (Tuning.SOLO_PLAYERS - 4)] assert_true(_parse(full).is_valid()) func test_discovery_options() -> void: var config: ServerConfig = _parse([ "--server", "--listen", "--name= Harbour Light ", "--list=lists.example.org", "--list=192.0.2.4:3000", "--list=[2001:db8::1]:4000", "--no-lan", ]) assert_true(config.is_valid(), str(config.errors)) assert_eq(config.server_name, "Harbour Light") assert_false(config.lan) assert_eq(config.lists, [ {"host": "lists.example.org", "port": NetDiscovery.DEFAULT_LIST_PORT}, {"host": "192.0.2.4", "port": 3000}, {"host": "2001:db8::1", "port": 4000}, ] as Array[Dictionary]) assert_true(_parse(["--server", "--listen"]).lan, "LAN discovery is on by default") func test_bad_discovery_options() -> void: for wrong: String in ["--list=", "--list=host:", "--list=host:0", "--list=host:99999", "--list=[::1", "--list=[::1]x"]: assert_false(_parse(["--server", "--listen", wrong]).is_valid(), wrong) assert_false(_parse(["--server", "--list=192.0.2.4"]).is_valid(), "a list server needs --listen") assert_false(_parse(["--server", "--no-lan"]).is_valid()) func test_soak_options() -> void: var config: ServerConfig = _parse(["--server", "--listen", "--matches=2", "--world-hash-every=300"]) assert_true(config.is_valid(), str(config.errors)) assert_eq(config.matches, 2) assert_eq(config.world_hash_every, 300) assert_eq(config.to_dictionary()["matches"], 2) assert_false(_parse(["--server", "--world-hash-every=300"]).is_valid(), "hashes are only logged when listening") assert_false(_parse(["--server", "--matches=-1"]).is_valid()) func test_config_file_and_command_line_order() -> void: var path: String = "user://test_server.cfg" var file: FileAccess = FileAccess.open(path, FileAccess.WRITE) file.store_string("# a comment\n\nlisten\nport = 24700\nname = Harbour Light\nhealth-port = 24700\nno-lan\n") file.close() var config: ServerConfig = _parse(["--server", "--config=" + path, "--port=24702"]) assert_true(config.is_valid(), str(config.errors)) assert_true(config.listen) assert_false(config.lan) assert_eq(config.port, 24702, "the command line after --config wins") assert_eq(config.server_name, "Harbour Light") assert_eq(config.health_port, 24700) assert_eq(config.health_bind, "127.0.0.1") DirAccess.remove_absolute(ProjectSettings.globalize_path(path)) func test_config_file_problems_are_errors() -> void: assert_false(_parse(["--server", "--config=user://no_such_file.cfg"]).is_valid()) var path: String = "user://test_bad_server.cfg" var file: FileAccess = FileAccess.open(path, FileAccess.WRITE) file.store_string("listen\nprot = 5\nconfig = other.cfg\n") file.close() var config: ServerConfig = _parse(["--server", "--config=" + path]) assert_eq(config.errors.size(), 2, str(config.errors)) DirAccess.remove_absolute(ProjectSettings.globalize_path(path)) func test_health_and_stop_options() -> void: var config: ServerConfig = _parse(["--server", "--listen", "--health-port=8080", "--health-bind=0.0.0.0", "--stop-file=/tmp/x"]) assert_true(config.is_valid(), str(config.errors)) assert_eq([config.health_port, config.health_bind, config.stop_file], [8080, "0.0.0.0", "/tmp/x"]) assert_false(_parse(["--server", "--health-bind=localhost"]).is_valid()) assert_false(_parse(["--server", "--health-port=70000"]).is_valid()) func test_team_size_picks_solo_or_duos() -> void: assert_eq(_parse(["--server"]).team_size, 1, "solo by default") var duos: ServerConfig = _parse(["--server", "--team-size=2", "--bots=23", "--players=1"]) assert_true(duos.is_valid(), str(duos.errors)) assert_eq(duos.team_size, Tuning.DUO_TEAM_SIZE) assert_eq(duos.to_dictionary()["team_size"], 2) for bad: String in ["--team-size=0", "--team-size=3", "--team-size=two"]: assert_false(_parse(["--server", bad]).is_valid(), bad)