From 16724922ed6676ac0ee22103b50fee4dead3da18 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 5 Oct 2026 19:54:36 +0200 Subject: [PATCH] Test that the song cache fills up to the cache size setting Covers a cache size above 1: songs are preloaded until the setting is reached, playing a song tops the cache up again, and the cache keeps filling after a failed download once the server answers again. --- tests/fakes/fake_http_transport.gd | 13 +++++++++ tests/integration/test_main_window.gd | 39 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/tests/fakes/fake_http_transport.gd b/tests/fakes/fake_http_transport.gd index 7f65d22..7521215 100644 --- a/tests/fakes/fake_http_transport.gd +++ b/tests/fakes/fake_http_transport.gd @@ -12,6 +12,7 @@ var requested_paths: Array[String] = [] var requested_methods: Array[int] = [] var _responses: Dictionary = {} +var _successes_left: Dictionary = {} var _previous_transport: HttpTransport @@ -39,6 +40,13 @@ func respond_json(path: String, data: Variant, response_code: int = 200) -> void ## Answers requests to [param path] with [param body] as-is. func respond_bytes(path: String, body: PackedByteArray, response_code: int = 200) -> void: _responses[path] = {"code": response_code, "body": body} + _successes_left.erase(path) + + +## Lets [param successes] more requests to [param path] succeed, then answers 503 until +## a new response is registered for the path. +func fail_after(path: String, successes: int) -> void: + _successes_left[path] = successes ## Returns how many times [param path] has been requested. @@ -50,6 +58,11 @@ func request(url: String, method: int, on_complete: Callable) -> void: var path: String = url.trim_prefix(Settings.default_path) requested_paths.append(path) requested_methods.append(method) + if _successes_left.has(path): + if _successes_left[path] == 0: + on_complete.call(503, PackedByteArray()) + return + _successes_left[path] -= 1 if _responses.has(path): on_complete.call(_responses[path].code, _responses[path].body) else: diff --git a/tests/integration/test_main_window.gd b/tests/integration/test_main_window.gd index 6cad167..c2094f9 100644 --- a/tests/integration/test_main_window.gd +++ b/tests/integration/test_main_window.gd @@ -119,3 +119,42 @@ func test_failed_song_info_retries_only_that_step() -> void: assert_int(Playlist.song_object_array.size()).is_equal(songs_before + 1) assert_str(Playlist.song_object_array.back().song_title).is_equal("Dancing Mad") assert_bool(Playlist.is_song_downloading).is_false() + + +func test_preloads_as_many_songs_as_the_cache_setting() -> void: + Playlist.number_of_tracks_to_preload = 3 + + _runner.invoke("fetch_song") + + assert_int(Playlist.get_preloaded_songs()).is_equal(3) + assert_bool(Playlist.is_song_downloading).is_false() + + +func test_playing_a_song_tops_the_cache_up_again() -> void: + Playlist.number_of_tracks_to_preload = 3 + _runner.invoke("fetch_song") + + await _play_first_song() + + assert_int(Playlist.get_preloaded_songs()).is_equal(3) + assert_int(Playlist.song_object_array.size()).is_equal(4) + + +func test_cache_keeps_filling_after_a_failed_download() -> void: + _window.song_fetch_retry_seconds = 0.05 + Playlist.number_of_tracks_to_preload = 3 + var mp3: PackedByteArray = FileAccess.get_file_as_bytes("res://tests/data/short.mp3") + # One more download works, then the server stops answering + _server.fail_after("/music/rand", 1) + + _runner.invoke("fetch_song") + await await_millis(150) + + assert_int(Playlist.get_preloaded_songs()).is_equal(2) + assert_bool(Playlist.is_song_downloading).is_true() + + _server.respond_bytes("/music/rand", mp3) + await await_millis(150) + + assert_int(Playlist.get_preloaded_songs()).is_equal(3) + assert_bool(Playlist.is_song_downloading).is_false()