Files
MusicPlayer/tests/unit/test_playlist.gd
T
Sansan c985a6ce5b Replace the broken test suite with working GdUnit4 tests
The previous tests never compiled: they used GUT's assert functions,
Godot 3 APIs and reassigned autoload functions, so no test ran.

- 90 tests: unit tests for the playlist, songs, player scoring, time
  formatting, search and the server client, plus integration tests
  that run MainWindow and SyncWindow against a fake server
- Keybind tests check that Alt+<number> and Ctrl+Alt+<number> keep
  following the player rows while players are added and removed
- run_tests.sh finds Godot and runs the suite headless (~10 seconds)
- CI runs the tests before the build job in gitea-build.yaml; the
  separate test.yml used a GitHub action that does not exist
- GdUnit4 is committed instead of ignored, since the project enables
  it, and .gitea/ is no longer ignored
- Exported builds leave out tests/ and addons/gdUnit4/
2026-10-06 08:32:56 +02:00

185 lines
5.2 KiB
GDScript

extends GdUnitTestSuite
var _server: FakeHttpTransport
var _add_to_stats: bool
var _is_debug: bool
func before_test() -> void:
_server = FakeHttpTransport.new().install()
_add_to_stats = Settings.add_to_stats
_is_debug = Settings.is_debug
Settings.add_to_stats = false
Settings.is_debug = false
Playlist.reset_playlist()
Playlist.number_of_tracks_to_preload = 1
func after_test() -> void:
Playlist.reset_playlist()
Settings.add_to_stats = _add_to_stats
Settings.is_debug = _is_debug
_server.uninstall()
func _add_songs(count: int) -> Array[SongObject]:
var songs: Array[SongObject] = []
for i: int in count:
var song: SongObject = SongObject.new()
song.song_number = i
song.soundtrack_title = "Soundtrack %d" % i
song.song_title = "Song %d" % i
Playlist.add_to_playlist(song)
songs.append(song)
return songs
func test_set_latest_played_song_advances() -> void:
_add_songs(3)
Playlist.set_latest_played_song()
assert_int(Playlist.latest_played_song).is_equal(1)
func test_set_latest_played_song_stops_at_last_song() -> void:
_add_songs(2)
Playlist.set_latest_played_song()
Playlist.set_latest_played_song()
Playlist.set_latest_played_song()
assert_int(Playlist.latest_played_song).is_equal(1)
func test_get_next_song_returns_latest_played() -> void:
var songs: Array[SongObject] = _add_songs(3)
Playlist.latest_played_song = 2
assert_object(Playlist.get_next_song()).is_same(songs[2])
func test_update_last_played_song_points_past_existing_songs() -> void:
_add_songs(4)
Playlist.update_last_played_song()
assert_int(Playlist.latest_played_song).is_equal(4)
func test_preloaded_songs_counts_unplayed_songs() -> void:
var songs: Array[SongObject] = _add_songs(3)
songs[0].has_played = true
assert_int(Playlist.get_preloaded_songs()).is_equal(2)
func test_should_preload_more_songs_when_below_target() -> void:
var songs: Array[SongObject] = _add_songs(1)
songs[0].has_played = true
assert_bool(Playlist.should_preload_more_songs()).is_true()
func test_should_not_preload_more_songs_when_target_reached() -> void:
_add_songs(1)
assert_bool(Playlist.should_preload_more_songs()).is_false()
func test_get_playlist_hides_unplayed_songs() -> void:
var songs: Array[SongObject] = _add_songs(3)
songs[0].has_played = true
assert_array(Playlist.get_playlist()).contains_exactly([songs[0]])
func test_get_playlist_shows_everything_in_debug() -> void:
Settings.is_debug = true
var songs: Array[SongObject] = _add_songs(2)
assert_array(Playlist.get_playlist()).contains_exactly(songs)
func test_song_is_answered_marks_song() -> void:
var songs: Array[SongObject] = _add_songs(2)
Playlist.song_is_answered(1)
assert_bool(songs[1].is_answered).is_true()
assert_bool(songs[0].is_answered).is_false()
func test_song_is_answered_reports_to_stats_when_enabled() -> void:
Settings.add_to_stats = true
_add_songs(2)
Playlist.song_is_answered(1)
assert_array(_server.requested_paths).contains_exactly(["/music/played?song=1"])
func test_song_is_answered_does_not_report_when_stats_disabled() -> void:
_add_songs(2)
Playlist.song_is_answered(1)
assert_array(_server.requested_paths).is_empty()
func test_points_are_recorded_on_current_song() -> void:
var songs: Array[SongObject] = _add_songs(2)
Playlist.set_currently_playing_song(1)
Playlist.add_point("lucca-id")
Playlist.add_point("crono-id")
assert_array(songs[1].players_given_point).contains_exactly(["lucca-id", "crono-id"])
assert_array(songs[0].players_given_point).is_empty()
func test_remove_point_from_current_song() -> void:
var songs: Array[SongObject] = _add_songs(1)
Playlist.set_currently_playing_song(0)
Playlist.add_point("crono-id")
Playlist.add_point("marle-id")
Playlist.remove_point("crono-id")
assert_array(songs[0].players_given_point).contains_exactly(["marle-id"])
func test_remove_player_removes_only_that_players_points() -> void:
var songs: Array[SongObject] = _add_songs(2)
songs[0].players_given_point.assign(["crono-id", "marle-id"])
songs[1].players_given_point.assign(["crono-id"])
Playlist.remove_player_from_list("marle-id")
assert_array(songs[0].players_given_point).contains_exactly(["crono-id"])
assert_array(songs[1].players_given_point).contains_exactly(["crono-id"])
func test_reset_playlist_clears_songs_and_position() -> void:
_add_songs(3)
Playlist.latest_played_song = 2
Playlist.set_currently_playing_song(1)
Playlist.reset_playlist()
assert_array(Playlist.song_object_array).is_empty()
assert_int(Playlist.latest_played_song).is_equal(0)
assert_int(Playlist.currently_playing_song).is_equal(-1)
func test_current_titles_are_hidden_until_answered() -> void:
var songs: Array[SongObject] = _add_songs(1)
Playlist.set_currently_playing_song(0)
assert_str(Playlist.get_current_soundtrack_title()).is_equal("????????")
songs[0].is_answered = true
assert_str(Playlist.get_current_soundtrack_title()).is_equal("Soundtrack 0")
assert_str(Playlist.get_current_song_title()).is_equal("Song 0")