Files
MusicPlayer/tests/integration/test_song_loader.gd
T
Sansan 240c331ab2
Build / test (push) Successful in 53s
Build / build (push) Successful in 1m13s
Cache character pictures and songs on disk
Character pictures are kept in characters/ next to the settings, with an
index.json of when the server last changed each one and when it was
downloaded. Only new or changed pictures are downloaded, using the server's new
/character/details. Older servers fall back to the name list, and pictures are
downloaded again after 7 days.

Downloaded songs are written to songs/ and only their paths are kept in memory.
The folder is emptied when the game starts and when the playlist resets.
2026-10-07 21:57:27 +02:00

130 lines
4.0 KiB
GDScript

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.use_low_played_mode = false
_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)
func test_low_played_mode_uses_its_endpoint() -> void:
Settings.use_low_played_mode = true
_server.respond_bytes("/music/rand/low",
FileAccess.get_file_as_bytes("res://tests/data/short.mp3"))
_loader.fetch_song()
assert_int(_server.count("/music/rand/low")).is_equal(1)
assert_int(_server.count("/music/rand")).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)