Files
SanctuaryCommander/test/home_test.py
T
2026-11-13 22:29:30 -05:00

100 lines
4.1 KiB
Python

from html.parser import HTMLParser
import json
import os
from pathlib import Path
import runpy
import subprocess
import sys
import tempfile
import unittest
ROOT = Path(__file__).resolve().parents[1]
class HomeElements(HTMLParser):
def __init__(self):
super().__init__()
self.links = []
self.buttons = []
self.headings = []
self.selection_url = None
def handle_starttag(self, tag, attrs):
attrs = dict(attrs)
if tag == 'a':
self.links.append(attrs.get('href'))
if tag == 'button':
self.buttons.append(attrs)
if tag in ('h1', 'h2'):
self.headings.append(tag)
if 'data-selection-url' in attrs:
self.selection_url = attrs['data-selection-url']
class HomeTest(unittest.TestCase):
def setUp(self):
self.directory = tempfile.TemporaryDirectory()
self.addCleanup(self.directory.cleanup)
self.workdir = Path(self.directory.name)
(self.workdir / 'config.json').write_text(json.dumps({
'database': {'type': 'SQLite', 'name': 'test.db'},
'upload_location': 'uploads'
}))
subprocess.run([sys.executable, str(ROOT / 'sc_database_generator.py')],
cwd=self.workdir, check=True, capture_output=True)
original = Path.cwd()
try:
os.chdir(self.workdir)
self.app = runpy.run_path(str(ROOT / 'sc_webserver.py'))['app']
finally:
os.chdir(original)
self.app.config['TESTING'] = True
self.client = self.app.test_client()
def test_working_actions_are_grouped_and_all_destinations_work(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
elements = HomeElements()
elements.feed(response.get_data(as_text=True))
self.assertCountEqual(elements.links, ['/', '/media', '/upload/photo', '/upload/video',
'/configure/stream', '/youtube/download', '/youtube/jobs'])
self.assertEqual(elements.buttons, []) # No deceptively enabled placeholder controls.
self.assertEqual(elements.headings.count('h1'), 1)
self.assertEqual(elements.selection_url, '/media/status')
for link in elements.links:
with self.subTest(link=link):
self.assertEqual(self.client.get(link).status_code, 200)
self.assertIn(b'COMING SOON', response.data)
self.assertIn(b'TV & screen controls', response.data)
self.assertIn(b'<title>Sanctuary Commander</title>', response.data)
def test_home_uses_local_assets_and_preserves_original_background(self):
home = self.client.get('/')
for asset in ('style.css', 'home.css', 'home.js'):
self.assertIn(f'/static/{asset}'.encode(), home.data)
with self.client.get(f'/static/{asset}') as response:
self.assertEqual(response.status_code, 200)
self.assertNotIn(b'cdn.jsdelivr', home.data)
with self.client.get('/static/style.css') as response:
self.assertIn(b'background-color: #ebe1c7 !important', response.data)
with self.client.get('/static/home.css') as response:
self.assertIn(b'@media (max-width: 640px)', response.data)
self.assertIn(b'prefers-reduced-motion', response.data)
self.assertIn(b':focus-visible', response.data)
def test_home_navigation_survives_database_failure(self):
# Only the optional status panel depends on the database, not navigation.
from unittest.mock import patch
import sqlite3
with patch('utilities.database.sqlite.SQLite.query', side_effect=sqlite3.OperationalError('Unavailable')):
self.assertEqual(self.client.get('/').status_code, 200)
with self.assertLogs(self.app.logger, level='ERROR'):
response = self.client.get('/media/status')
self.assertEqual(response.status_code, 503)
self.assertEqual(response.json['status'], 'FAILED')
if __name__ == '__main__':
unittest.main()