Files
DeepTutor/tests/runtime/test_update_worker.py
evan188199-tech 3a192f6a53 fix(runtime): log swallowed launcher, init and update-worker failures (#1704)
* fix: log singleton reset failures instead of swallowing them

_reset_runtime_singletons() (deeptutor_cli/init_cmd.py and
deeptutor/runtime/launcher.py) swallowed every exception while dropping
cached PathService/RuntimeSettings/ModelCatalog singletons. A failed
PathService reset leaves the previous home's cached paths in place, so
deeptutor init --home / launcher start silently keep writing into the
old workspace. Log PathService failures at ERROR and cache-clear
failures at WARNING so the misdirection is observable.

* fix(launcher): log handoff failure when mark_failed fails too

_handoff_pending_update wrapped the fallback store.mark_failed in a bare
'except Exception: pass'. When the handoff itself failed and recording
the failure failed again (corrupt state, IO error), the launcher shut
down with no persistent trace of either error. Keep the minimal fix:
emit a fallback log naming the update job, the handoff error and the
recording error before returning False.

* fix(update): log and skip recovery when job state is unreadable

The update worker's recovery path wrapped store.load() in a bare
except-pass: with a corrupt state.json the failed-update bookkeeping and
the app restore were skipped silently, leaving the job stuck and no
trace in worker.log. Load failures now append a durable record (load
error plus the original failure) to the store log and skip the round
without touching the damaged state.

* test(codebuddy): make credential fixture expiry relative to now

test_load_credentials_parses_session wrote a hard-coded expiresAt of
1791055241000 (2026-10-03 15:40:41 UTC) and asserted is_expired() is
False. Once that instant passed the assertion flipped, so the test now
fails on every Python version for every branch.

Derive expiresAt / refreshExpiresAt from time.time() so the fixture
always describes a live session, and assert against the value written.

---------

Co-authored-by: XZH <cto@xzh.ai>
2026-10-07 20:37:37 +08:00

100 lines
3.2 KiB
Python

from __future__ import annotations
from pathlib import Path
import pytest
from deeptutor.runtime.update_worker import build_update_command, run_update_worker
from deeptutor.services.app_update import UpdateJob, UpdateJobStore
def _handoff_job(tmp_path: Path) -> tuple[UpdateJobStore, UpdateJob]:
store = UpdateJobStore(tmp_path / "update")
pending = store.create(current_version="1.6.1", target_version="1.7.0")
home = tmp_path / "home"
home.mkdir()
job = store.prepare_handoff(
pending.id,
home=home,
restart_argv=["start", "--home", str(home.resolve())],
)
return store, job
def test_worker_updates_then_restarts(tmp_path: Path) -> None:
store, job = _handoff_job(tmp_path)
captured: dict[str, object] = {}
def run(command: list[str], cwd: Path, log_path: Path) -> int:
captured.update(command=command, cwd=cwd, log_path=log_path)
return 0
def restart(value: UpdateJob, log_path: Path) -> None:
captured.update(restart=value, restart_log=log_path)
result = run_update_worker(
store_root=store.root,
parent_pid=123,
wait_for_parent=lambda pid: captured.update(parent_pid=pid),
command_runner=run,
restart_launcher=restart,
)
assert result == 0
assert captured["parent_pid"] == 123
assert captured["command"] == build_update_command("1.7.0")
assert isinstance(captured["restart"], UpdateJob)
assert store.load().status == "restarting"
assert store.load().restart_count == 1
def test_worker_failure_is_durable_and_restores_app(tmp_path: Path) -> None:
store, _job = _handoff_job(tmp_path)
restarted: list[UpdateJob] = []
result = run_update_worker(
store_root=store.root,
parent_pid=123,
wait_for_parent=lambda _pid: None,
command_runner=lambda _command, _cwd, _log: 7,
restart_launcher=lambda value, _log: restarted.append(value),
)
assert result == 1
assert store.load().status == "failed"
assert store.load().error == "pip exited with status 7"
assert restarted and restarted[0].status == "failed"
def test_worker_recovery_reports_and_skips_on_corrupt_state(tmp_path: Path) -> None:
store, _job = _handoff_job(tmp_path)
restarted: list[UpdateJob] = []
corrupt_state = "{ not valid json"
def run_and_corrupt(command: list[str], cwd: Path, log_path: Path) -> int:
store.state_path.write_text(corrupt_state, encoding="utf-8")
return 7
result = run_update_worker(
store_root=store.root,
parent_pid=123,
wait_for_parent=lambda _pid: None,
command_runner=run_and_corrupt,
restart_launcher=lambda value, _log: restarted.append(value),
)
assert result == 1
assert restarted == []
assert store.state_path.read_text(encoding="utf-8") == corrupt_state
log = store.log_path.read_text(encoding="utf-8")
assert "recovery aborted" in log
assert "could not be loaded" in log
assert "pip exited with status 7" in log
def test_update_command_rejects_non_stable_or_injected_versions() -> None:
with pytest.raises(ValueError):
build_update_command("1.7.0rc1")
with pytest.raises(ValueError):
build_update_command("1.7.0; touch /tmp/nope")