Files
MusicPlayer/tests/integration/test_song_loader.gd
T
Sansan a0428e3475 Follow the GDScript style guide in every script
- Types on every parameter, return value and variable, and snake_case names
  (the player name limit is the constant MAX_NAME_LENGTH).
- Spelling fixes: *_triggered, character_selected, separator, caught_errors.
  The server's catched_errors key stays as the server sends it.
- Signals are connected with signal.connect() instead of by name.
- Code is ordered signals, constants, exported vars, vars, @onready vars,
  functions, with @onready on one line and two blank lines between functions.
- Every script starts with a ## description. Template comments, old LOC notes
  and commented-out code are gone, apart from the local play code.
- Lines are at most 100 characters, apart from the local play path.
- The screenshot tour turns off the blinking text cursor so pictures match.
2026-10-06 11:53:26 +02:00

128 lines
3.9 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).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)