# deck-it - tests for the Xbox module # Copyright (C) 2026 Danilo M. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) import unittest import deckit.modules.xbox as xb PRESENCE = {"xuid": "1", "state": "Online", "devices": [{"type": "Scarlett", "titles": [ {"id": "750323071", "name": "Home", "placement": "Background", "state": "Active"}, {"id": "1234", "name": "Some Game", "placement": "Full", "state": "Active", "activity": {"richPresence": "Ranked match"}}]}]} CONSOLES = {"status": {"errorCode": "OK"}, "result": [ {"id": "CON1", "name": "Living room", "consoleType": "XboxSeriesX", "powerState": "On", "storageDevices": [{"storageDeviceName": "Internal", "freeSpaceBytes": 4e11, "totalSpaceBytes": 1e12}]}]} PROFILE = {"profileUsers": [{"id": "1", "settings": [ {"id": "Gamertag", "value": "Player"}, {"id": "GameDisplayPicRaw", "value": "https://img.example.org/p.png"}, {"id": "Gamerscore", "value": "12345"}]}]} HISTORY = {"titles": [ {"titleId": "1234", "name": "Some Game", "type": "Game", "displayImage": "https://img.example.org/c.png", "titleHistory": {"lastTimePlayed": "2026-11-16T10:00:00Z"}, "achievement": {"currentAchievements": 5, "totalAchievements": 50, "currentGamerscore": 100, "totalGamerscore": 1000, "progressPercentage": 10.0}}, {"titleId": "99", "name": "Video App", "type": "App", "displayImage": None}]} STATS = {"statlistscollection": [{"arrangebyfieldid": "1", "stats": [ {"titleid": "1234", "name": "MinutesPlayed", "value": "185"}]}]} ACHIEVEMENTS = {"achievements": [{"name": "First", "description": "Do a thing", "mediaAssets": [{"url": "https://img.example.org/a.png"}], "titleAssociations": [{"name": "Some Game"}], "rewards": [{"type": "Gamerscore", "value": "15"}], "progression": {"timeUnlocked": "2026-11-15T20:00:00Z"}}]} FRIENDS = {"people": [ {"gamertag": "zed", "displayPicRaw": "z.png", "presenceState": "Offline", "presenceText": "Offline", "isFavorite": True}, {"gamertag": "bob", "displayPicRaw": "b.png", "presenceState": "Online", "presenceText": "Some Game", "isFavorite": False}, {"gamertag": "amy", "displayPicRaw": "a.png", "presenceState": "Online", "presenceText": "Home", "isFavorite": True}]} class Summaries(unittest.TestCase): def test_foreground_is_the_full_placement_title(self): self.assertEqual(xb.foreground(PRESENCE), {"id": "1234", "name": "Some Game", "rich": "Ranked match", "device": "Scarlett"}) self.assertIsNone(xb.foreground({"state": "Offline"})) def test_foreground_takes_a_list_activity_too(self): p = {"devices": [{"titles": [{"id": 7, "name": "X", "placement": "Full", "activity": [{"richPresence": "Lobby"}]}]}]} self.assertEqual(xb.foreground(p)["rich"], "Lobby") self.assertEqual(xb.foreground(p)["id"], "7") def test_presence_has_last_seen(self): p = xb.summarize_presence({"state": "Offline", "lastSeen": {"titleName": "Home", "timestamp": "2026-11-15T22:00:00Z"}}) self.assertEqual(p, {"state": "Offline", "title": None, "last_seen": {"title": "Home", "when": "2026-11-15T22:00:00Z"}}) def test_profile(self): self.assertEqual(xb.summarize_profile(PROFILE), {"gamertag": "Player", "pic": "https://img.example.org/p.png", "gamerscore": 12345}) def test_consoles(self): c = xb.summarize_consoles(CONSOLES)[0] self.assertEqual((c["id"], c["name"], c["power"]), ("CON1", "Living room", "On")) self.assertEqual(c["storage"], [{"name": "Internal", "free": 4e11, "total": 1e12}]) def test_minutes_and_titles(self): minutes = xb.minutes_played(STATS) self.assertEqual(minutes, {"1234": 185}) t = xb.summarize_titles(HISTORY, minutes) self.assertEqual(t[0]["minutes"], 185) self.assertEqual(t[0]["achievements"], {"done": 5, "total": 50, "score": 100, "max": 1000, "pct": 10.0}) self.assertEqual((t[1]["type"], t[1]["minutes"]), ("App", None)) def test_achievements(self): a = xb.summarize_achievements(ACHIEVEMENTS)[0] self.assertEqual(a, {"name": "First", "description": "Do a thing", "icon": "https://img.example.org/a.png", "score": 15, "title": "Some Game", "unlocked": "2026-11-15T20:00:00Z"}) def test_friends_online_then_favourite_then_name(self): self.assertEqual([f["gamertag"] for f in xb.summarize_friends(FRIENDS)], ["amy", "bob", "zed"]) if __name__ == "__main__": unittest.main()