aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-11-16 17:21:07 +0100
committerDanilo M. <danix@danix.xyz>2026-11-16 17:21:07 +0100
commit5e18a133f779d5f344e592b4e3c122c43b2a4982 (patch)
treeb856ec03b2f97e4a7ba8abfcff4c06893454f306 /tests
parentab02a2e982d145333d843fe84087b5fdd0e05b38 (diff)
downloaddeck-it-5e18a133f779d5f344e592b4e3c122c43b2a4982.tar.gz
deck-it-5e18a133f779d5f344e592b4e3c122c43b2a4982.zip
fix(xbox): keep the reader alive and the flags right
- run() survives any exception from tick(); _http turns http.client.HTTPException into OSError. - Signing out or in again resets the slow-read clock, so the profile is read at once instead of after 10 minutes. - After stop() nothing sets status.xbox back to 1; its compare and write is under a lock. - api() uses a copy of the auth taken under the lock; command() maps a missing auth to "not signed in". - Action and sign-in errors carry Xbox Live's own message when the HTTP error body has one. - type_of caches only a 404; other errors retry on the next tick. - Sign-in: start_login is locked, a poller only ends its own flow, and a failed token save ends it as "failed". - state() is loading only until the first presence read. - Drop a stale placeholder comment. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_xbox.py65
1 files changed, 62 insertions, 3 deletions
diff --git a/tests/test_xbox.py b/tests/test_xbox.py
index 2eeea8d..8a14bb6 100644
--- a/tests/test_xbox.py
+++ b/tests/test_xbox.py
@@ -191,7 +191,7 @@ class Login(Base):
def test_start_returns_the_code_and_state_shows_it(self):
x = self.xbox(signed_in=False)
- x._wait_login = lambda: None # the test drives login_step itself
+ x._wait_login = lambda login: None # the test drives login_step itself
view = x.start_login({})
self.assertEqual((view["code"], view["url"]), ("ABCD-1234", "https://microsoft.com/devicelogin"))
self.assertEqual(x.state()["login"]["code"], "ABCD-1234")
@@ -199,7 +199,7 @@ class Login(Base):
def test_pending_then_done_saves_the_token(self):
x = self.xbox(signed_in=False)
- x._wait_login = lambda: None
+ x._wait_login = lambda login: None
x.start_login({})
self.fake.fail["/oauth2/v2.0/token"] = xb.HttpError(400, {"error": "authorization_pending"})
self.assertEqual(x.login_step(), "pending")
@@ -211,12 +211,24 @@ class Login(Base):
def test_declined_or_expired_ends_it(self):
x = self.xbox(signed_in=False)
- x._wait_login = lambda: None
+ x._wait_login = lambda login: None
x.start_login({})
self.fake.fail["/oauth2/v2.0/token"] = xb.HttpError(400, {"error": "expired_token"})
self.assertEqual(x.login_step(), "failed")
self.assertIsNone(x.state()["login"])
+ def test_an_old_poller_leaves_a_newer_code(self):
+ x = self.xbox(signed_in=False)
+ x._wait_login = lambda login: None
+ x.start_login({})
+ old = x.login
+ old["expires"] = 0
+ x.start_login({})
+ new = x.login
+ self.assertIsNot(new, old)
+ self.assertEqual(x.login_step(old), "failed")
+ self.assertIs(x.login, new)
+
class Tick(Base):
def test_a_tick_reads_everything_and_marks_the_game(self):
@@ -286,6 +298,46 @@ class Tick(Base):
self.assertFalse(x.state()["signed_in"])
self.assertEqual(self.status.read_text(), "0")
+ def test_sign_in_again_reads_the_profile_at_once(self):
+ x = self.xbox()
+ x.tick()
+ self.fake.fail["/oauth2/v2.0/token"] = xb.HttpError(400, {"error": "invalid_grant"})
+ x.auth = None
+ x.tick()
+ x._wait_login = lambda login: None
+ x.start_login({})
+ self.assertEqual(x.login_step(), "done")
+ x.tick()
+ self.assertEqual(x.state()["profile"]["gamertag"], "Player")
+
+ def test_a_throttled_type_lookup_is_not_cached(self):
+ self.fake.answers["userpresence"] = {"state": "Online", "devices": [{"titles": [
+ {"id": "555", "name": "New Game", "placement": "Full"}]}]}
+ self.fake.fail["titleid(555)"] = xb.HttpError(429, {})
+ x = self.xbox()
+ x.tick()
+ self.assertNotIn("555", x.types)
+ x.tick()
+ self.assertEqual(x.state()["playing"]["id"], "555")
+
+ def test_a_failing_tick_does_not_end_the_reader(self):
+ x = self.xbox()
+
+ def tick():
+ x.stopping = True
+ x.wake.set()
+ raise RuntimeError("boom")
+ x.tick = tick
+ x.run()
+ self.assertEqual((x.offline, x.reason), (True, "boom"))
+
+ def test_nothing_after_stop_sets_playing(self):
+ x = self.xbox()
+ x.tick()
+ x.stop()
+ x._set_playing({"id": "1234", "name": "Some Game"})
+ self.assertEqual(self.status.read_text(), "0")
+
def test_stop_writes_zero(self):
x = self.xbox()
x.tick()
@@ -316,6 +368,13 @@ class Commands(Base):
with self.assertRaisesRegex(xb.ModuleError, "Console is off"):
x.command("off", {"console": "CON1"})
+ def test_an_xccs_http_error_message_surfaces(self):
+ x = self.xbox()
+ x.tick()
+ self.fake.fail["/commands"] = xb.HttpError(400, {"status": {"errorCode": "Bad", "errorMessage": "Console not found"}})
+ with self.assertRaisesRegex(xb.ModuleError, "Console not found"):
+ x.command("wake", {"console": "CON1"})
+
def test_make_lists_the_actions(self):
m = xb.make({"xbox": {"client_id": ""}})
self.assertEqual(set(m.actions), {"login", *xb.COMMANDS})