Files
MusicPlayer/tests/unit/test_song_files.gd
T
Sansan e4352a9f34
Build / test (push) Successful in 1m25s
Build / build (push) Successful in 1m12s
Save the quiz and continue it after a restart; load characters from a folder
The quiz is saved to current_quiz.json in the config folder after every
change, and the game goes on where it was when it starts again: the
players with their points and characters, every song with who got a point
for it and whether it was played, answered or is playing, and the songs
downloaded ahead with their files. The song that was playing is ready to
play again. Songs whose files are gone are downloaded again.

On a server, the saved songs are checked against the server's playlist. If
it changed, e.g. after a sync, or another server is used, the quiz starts
over with the same players. A quiz from a music folder continues offline,
including how far a prepared quiz has come. A new game is saved like any
other change, and a file that can't be read starts a new quiz.

Characters can be read from a folder picked in Settings instead of the
server, for this session. A music folder with a characters folder brings
its own characters, e.g. for a themed quiz; that folder is not counted as
a soundtrack. When the characters come from somewhere else, players pick
their characters again.

Closes #11, closes #44
2026-10-09 18:08:50 +02:00

110 lines
3.5 KiB
GDScript

extends GdUnitTestSuite
## Downloaded songs are kept in files in the songs folder, not in memory.
var _snapshot: AutoloadSnapshot
var _config_dir_before: String
var _mp3: PackedByteArray
func before_test() -> void:
_snapshot = AutoloadSnapshot.take([Playlist])
_config_dir_before = OS.get_environment(Settings.CONFIG_DIR_ENV)
OS.set_environment(Settings.CONFIG_DIR_ENV, create_temp_dir("song_files"))
Playlist.clear_song_files()
Playlist.song_object_array.clear()
_mp3 = FileAccess.get_file_as_bytes("res://tests/data/short.mp3")
func after_test() -> void:
Playlist.clear_song_files()
OS.set_environment(Settings.CONFIG_DIR_ENV, _config_dir_before)
_snapshot.restore()
func _add_song() -> SongObject:
var song_object: SongObject = SongObject.new()
Playlist.store_song(song_object, _mp3)
Playlist.add_to_playlist(song_object)
return song_object
func test_stored_song_is_a_file_and_plays_back() -> void:
var song_object: SongObject = _add_song()
assert_object(song_object.song).is_null()
assert_bool(FileAccess.file_exists(song_object.song_path)).is_true()
var stream: AudioStream = Playlist.get_song(0)
assert_object(stream).is_instanceof(AudioStreamMP3)
assert_float(stream.get_length()).is_greater(0.0)
func test_every_song_gets_a_file_of_its_own() -> void:
var first: SongObject = _add_song()
var second: SongObject = _add_song()
assert_str(first.song_path).is_not_equal(second.song_path)
assert_array(DirAccess.get_files_at(Playlist.songs_folder())).has_size(2)
func test_downloading_a_song_again_replaces_its_file() -> void:
var song_object: SongObject = _add_song()
var old_path: String = song_object.song_path
Playlist.add_song_to_song_object(0, _mp3)
assert_str(song_object.song_path).is_not_equal(old_path)
assert_object(Playlist.get_song(0)).is_not_null()
func test_resetting_the_playlist_deletes_the_files() -> void:
_add_song()
_add_song()
Playlist.reset_playlist()
assert_array(DirAccess.get_files_at(Playlist.songs_folder())).is_empty()
func test_missing_file_gives_no_song_so_it_is_downloaded_again() -> void:
var song_object: SongObject = _add_song()
DirAccess.remove_absolute(song_object.song_path)
assert_object(Playlist.get_song(0)).is_null()
func test_song_stays_in_memory_when_it_cannot_be_saved() -> void:
# A file where the folder should be, so the folder can't be made
var blocker: String = create_temp_dir("song_files_blocked").path_join("not_a_folder")
FileAccess.open(blocker, FileAccess.WRITE).store_string("x")
OS.set_environment(Settings.CONFIG_DIR_ENV, blocker)
var song_object: SongObject = _add_song()
assert_str(song_object.song_path).is_empty()
assert_object(Playlist.get_song(0)).is_instanceof(AudioStreamMP3)
func test_new_songs_never_write_over_files_kept_from_a_saved_quiz() -> void:
var kept: SongObject = _add_song()
var kept_bytes: PackedByteArray = FileAccess.get_file_as_bytes(kept.song_path)
# As after a restart: the count of files written starts over
Playlist._song_files_written = 0
var new_song: SongObject = _add_song()
assert_str(new_song.song_path).is_not_equal(kept.song_path)
assert_array(FileAccess.get_file_as_bytes(kept.song_path)).is_equal(kept_bytes)
assert_array(DirAccess.get_files_at(Playlist.songs_folder())).has_size(2)
func test_clearing_can_keep_the_files_a_saved_quiz_uses() -> void:
var kept: SongObject = _add_song()
var dropped: SongObject = _add_song()
Playlist.clear_song_files(PackedStringArray([kept.song_path]))
assert_bool(FileAccess.file_exists(kept.song_path)).is_true()
assert_bool(FileAccess.file_exists(dropped.song_path)).is_false()