# deck-it - tests for stats # 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.stats as stats class Stats(unittest.TestCase): def test_state_shape(self): st = stats.make({}).state() for key in ("cpu", "memUsed", "memTotal", "net", "uptime", "host"): self.assertIn(key, st) self.assertIsInstance(st["cpu"], int) def test_build_discovers_stats(self): from deckit.modules import build names = [m["name"] for m in build({"modules": {}}).names()] self.assertIn("stats", names) class Intel(unittest.TestCase): def test_parse_bytes(self): self.assertEqual(stats._parse_bytes("12G"), 12 * 1024 ** 3) self.assertEqual(stats._parse_bytes("512M"), 512 * 1024 ** 2) self.assertEqual(stats._parse_bytes(2048), 2048) self.assertIsNone(stats._parse_bytes("nope")) def test_vram_scan_counts_each_client_once(self): import tempfile root = Path(tempfile.mkdtemp()) rows = [ ("100", "3", "drm-driver:\txe\ndrm-client-id:\t7\ndrm-resident-vram0:\t1024 MiB\n"), ("100", "4", "drm-driver:\txe\ndrm-client-id:\t7\ndrm-resident-vram0:\t1024 MiB\n"), ("200", "5", "drm-driver:\txe\ndrm-client-id:\t8\ndrm-resident-vram0:\t512 MiB\n"), ("300", "6", "drm-driver:\ti915\ndrm-client-id:\t9\ndrm-resident-vram0:\t999 MiB\n"), ] for pid, fd, body in rows: d = root / pid / "fdinfo" d.mkdir(parents=True, exist_ok=True) (d / fd).write_text(body) self.assertEqual(stats._intel_vram_used(root), (1024 + 512) * 1024 ** 2) if __name__ == "__main__": unittest.main()