extends GdUnitTestSuite ## Downloading songs into the Playlist ahead of time, against a fake server. const SONG_INFO: Dictionary = { "SongNo": 0, "Soundtrack": "Chrono Trigger", "Song": "Corridors of Time", } var _snapshot: AutoloadSnapshot var _server: FakeHttpTransport var _loader: SongLoader func before_test() -> void: _snapshot = AutoloadSnapshot.take([Settings, Playlist]) Playlist.reset_playlist() Playlist.is_song_downloading = false Playlist.number_of_tracks_to_preload = 1 Settings.random_mode = Settings.RandomMode.NORMAL _server = FakeHttpTransport.new().install() _server.respond_json("/music/list", []) _server.respond_bytes("/music/rand", FileAccess.get_file_as_bytes("res://tests/data/short.mp3")) _server.respond_bytes("/music/addQue", PackedByteArray()) _server.respond_json("/music/info", SONG_INFO) _loader = auto_free(SongLoader.new()) add_child(_loader) func after_test() -> void: _server.uninstall() _snapshot.restore() func test_load_song_list_adds_songs_already_played_on_the_server() -> void: _server.respond_json("/music/list", [ {"SongNo": 0, "Soundtrack": "Chrono Trigger", "Song": "Corridors of Time"}, ]) _server.respond_json("/music/info", {"SongNo": 1, "Soundtrack": "Final Fantasy VI", "Song": "Dancing Mad"}) var signals: GdUnitSignalAssert = assert_signal(monitor_signals(_loader)) _loader.load_song_list() await signals.is_emitted("song_list_loaded") var first: SongObject = Playlist.song_object_array[0] assert_str(first.soundtrack_title).is_equal("Chrono Trigger") assert_bool(first.has_played).is_true() assert_bool(first.is_answered).is_true() func test_load_song_list_then_preloads_a_new_song() -> void: _loader.load_song_list() assert_int(_server.count("/music/rand")).is_equal(1) assert_int(Playlist.song_object_array.size()).is_equal(1) func test_fetch_song_downloads_queues_and_adds_the_song() -> void: var signals: GdUnitSignalAssert = assert_signal(monitor_signals(_loader)) _loader.fetch_song() await signals.is_emitted("song_added") assert_array(_server.requested_paths) \ .contains_exactly(["/music/rand", "/music/addQue", "/music/info"]) var song: SongObject = Playlist.song_object_array[0] assert_str(song.song_title).is_equal("Corridors of Time") assert_object(song.song).override_failure_message("Songs are kept on disk").is_null() assert_str(song.song_path.get_base_dir()).is_equal(Playlist.songs_folder()) assert_object(Playlist.get_song(0)).is_not_null() assert_bool(Playlist.is_song_downloading).is_false() func test_fetch_song_keeps_going_until_the_cache_is_full() -> void: Playlist.number_of_tracks_to_preload = 3 _loader.fetch_song() assert_int(Playlist.song_object_array.size()).is_equal(3) assert_int(_server.count("/music/rand")).is_equal(3) @warning_ignore("unused_parameter") func test_each_random_mode_uses_its_endpoint(mode: Settings.RandomMode, path: String, test_parameters: Array = [ [Settings.RandomMode.NORMAL, "/music/rand"], [Settings.RandomMode.LOW_PLAYED, "/music/rand/low"], [Settings.RandomMode.CLASSIC, "/music/rand/classic"], ]) -> void: Settings.random_mode = mode _server.respond_bytes(path, FileAccess.get_file_as_bytes("res://tests/data/short.mp3")) _loader.fetch_song() assert_int(_server.count(path)).is_equal(1) for other: String in ["/music/rand", "/music/rand/low", "/music/rand/classic"]: if other != path: assert_int(_server.count(other)).override_failure_message(other).is_equal(0) func test_fetch_song_if_idle_does_nothing_while_downloading() -> void: Playlist.is_song_downloading = true _loader.fetch_song_if_idle() assert_int(_server.count("/music/rand")).is_equal(0) func test_fill_cache_does_nothing_when_the_cache_is_full() -> void: _loader.fetch_song() _loader.fill_cache() assert_int(_server.count("/music/rand")).is_equal(1) func test_fill_cache_downloads_after_the_cache_size_grows() -> void: _loader.fetch_song() Playlist.number_of_tracks_to_preload = 2 _loader.fill_cache() assert_int(_server.count("/music/rand")).is_equal(2) func test_failed_step_is_retried_alone() -> void: _loader.retry_seconds = 0.05 _server.fail_after("/music/info", 0) _loader.fetch_song() assert_int(Playlist.song_object_array.size()).is_equal(0) _server.respond_json("/music/info", SONG_INFO) await get_tree().create_timer(0.2).timeout assert_int(_server.count("/music/rand")).is_equal(1) assert_int(_server.count("/music/addQue")).is_equal(1) assert_int(Playlist.song_object_array.size()).is_equal(1)