Files
MusicPlayer/tests/integration/test_update_popup.gd
SansanandClaude Opus 5.5 f39865fffc
Build / test (push) Successful in 1m53s
Build / build (push) Successful in 1m15s
Update the game from inside it (#13)
"Version X is available" in the top bar is now a button that opens an
Update popup:
- Update and restart gets the download link from the server (the redirect
  is not followed, as the Gitea link needs no token), downloads the new game
  next to the running one with a progress bar, checks it (complete, and an
  MZ or ELF file), swaps it in and restarts. The quiz is saved first and
  continues. On Windows the running .exe is renamed to .old, put back if
  the swap fails, and deleted at the next start.
- Download in browser opens the .exe, the Linux package, or the release
  page on other platforms.
- From the editor, an unwritable folder or another platform, only the
  browser download is offered, with the reason.

The message now fits between the tabs and the buttons in every theme; in
Nord and Christmas it ran into Sync. The macOS export preset is removed,
and the unused download_client_* functions are replaced by
fetch_client_download_url.

Closes #13

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 21:23:26 +02:00

161 lines
4.9 KiB
GDScript

extends MainWindowTestSuite
## Getting the newest version from "Version X is available" in the top bar: updating in
## place and restarting, or downloading in the browser.
const LINK: String = "https://gitea.example/releases/download/99.0.0/MusicPlayer_99.0.0.exe"
var _top_bar: TopBar
var _popup: UpdatePopup
var _updater: FakeUpdater
var _opened: Array[String] = []
var _dir: String
## Downloads nothing: hands over a prepared file, or fails, at once.
class FakeUpdater:
extends Updater
var downloaded: PackedByteArray = [0x4D, 0x5A, 0x90, 0x00]
var fail_with: String = ""
var urls: Array[String] = []
var started: Array[String] = []
var quits: int = 0
func _init() -> void:
super()
start_process = func(path: String, _arguments: PackedStringArray) -> int:
started.append(path)
return 1
quit = func() -> void: quits += 1
func download(url: String, on_progress: Callable, on_done: Callable,
on_failed: Callable) -> void:
urls.append(url)
if !fail_with.is_empty():
on_failed.call(fail_with)
return
on_progress.call(downloaded.size() / 2, downloaded.size())
var file: FileAccess = FileAccess.open(update_path(), FileAccess.WRITE)
file.store_buffer(downloaded)
file.close()
on_done.call(update_path())
func _before_window_starts() -> void:
_server.respond_json("/download", "99.0.0")
func before_test() -> void:
# The quiz is saved before restarting, next to the settings
OS.set_environment(Settings.CONFIG_DIR_ENV, create_temp_dir("update_%d" % randi()))
await super()
_top_bar = _window.get_node("TopBar")
_popup = _window.get_node("UpdatePopup")
_opened = []
_popup.open_url = func(url: String) -> Error:
_opened.append(url)
return OK
_dir = create_temp_dir("game_%d" % randi())
_updater = FakeUpdater.new()
_updater.in_editor = false
_updater.os_name = "Windows"
_updater.executable_path = _dir.path_join("MusicPlayer.exe")
var file: FileAccess = FileAccess.open(_updater.executable_path, FileAccess.WRITE)
file.store_string("old game")
file.close()
_popup.add_child(_updater)
_popup.updater = _updater
func _open_popup() -> void:
_top_bar.update_button.pressed.emit()
func test_clicking_the_message_opens_the_popup() -> void:
_open_popup()
assert_bool(_popup.visible).is_true()
assert_str(_popup.title_label.text).is_equal("Version 99.0.0 is available")
assert_str(_popup.info_label.text).contains(Settings.version)
assert_bool(_popup.update_now_button.visible).is_true()
func test_update_downloads_swaps_and_restarts() -> void:
_server.respond_redirect("/download/windows", LINK)
_open_popup()
_popup.update_now()
assert_array(_updater.urls).contains_exactly([LINK])
var exe: String = _updater.executable_path
assert_array(FileAccess.get_file_as_bytes(exe)).is_equal(_updater.downloaded)
assert_str(FileAccess.get_file_as_string(exe + Updater.OLD_SUFFIX)).is_equal("old game")
assert_array(_updater.started).contains_exactly([exe])
assert_int(_updater.quits).is_equal(1)
# The quiz continues after the restart
assert_bool(FileAccess.file_exists(QuizSave.path())).is_true()
func test_failed_download_says_why_and_keeps_the_game() -> void:
_server.respond_redirect("/download/windows", LINK)
_updater.fail_with = "The download failed. Check the connection and try again."
_open_popup()
_popup.update_now()
assert_str(_popup.status_label.text).is_equal(_updater.fail_with)
assert_bool(_popup.update_now_button.disabled).is_false()
assert_bool(_popup.browser_button.disabled).is_false()
assert_str(FileAccess.get_file_as_string(_updater.executable_path)).is_equal("old game")
assert_int(_updater.quits).is_equal(0)
func test_no_download_on_the_server_says_so() -> void:
_open_popup()
_popup.update_now()
assert_array(_updater.urls).is_empty()
assert_str(_popup.status_label.text).contains("no download")
assert_int(_updater.quits).is_equal(0)
func test_browser_gets_the_package_on_linux() -> void:
var package: String = LINK.replace(".exe", "_linux.tar.gz")
_server.respond_redirect("/download/linux-package", package)
_updater.os_name = "Linux"
_open_popup()
_popup.download_in_browser()
assert_array(_opened).contains_exactly([package])
func test_browser_gets_the_release_page_on_other_platforms() -> void:
_updater.os_name = "macOS"
_open_popup()
_popup.download_in_browser()
assert_array(_opened).contains_exactly([Updater.RELEASES_PAGE])
assert_int(_server.count("/download/windows")).is_equal(0)
func test_browser_gets_the_release_page_when_the_server_has_no_link() -> void:
_open_popup()
_popup.download_in_browser()
assert_array(_opened).contains_exactly([Updater.RELEASES_PAGE])
func test_from_the_editor_only_the_browser_is_offered() -> void:
_updater.in_editor = true
_open_popup()
assert_bool(_popup.update_now_button.visible).is_false()
assert_bool(_popup.reason_label.visible).is_true()
assert_str(_popup.reason_label.text).contains("editor")
assert_bool(_popup.browser_button.visible).is_true()