#!/usr/bin/env python3
"""Synthetic checks only; no serial device opened, no board required."""
import builtins
from pathlib import Path
import unittest
from unittest.mock import patch
import uart_diagnostics as diag


class PreflightTests(unittest.TestCase):
    def test_usb_permission_case(self):
        result = diag.assess(diag.DEMO_CASES[0])
        self.assertEqual(result["transport"], "USB")
        self.assertIn("current_user_lacks_read_write_access", result["findings"])

    def test_console_case_does_not_verify_header(self):
        result = diag.assess(diag.DEMO_CASES[1])
        self.assertIn("selected_device_is_named_as_kernel_console", result["findings"])
        self.assertEqual(result["pin_mapping"], "not_verified")

    def test_missing_metadata_is_unknown(self):
        result = diag.assess(diag.DEMO_CASES[2])
        self.assertEqual(result["transport"], "unconfirmed")
        self.assertIn("kernel_console_configuration_unknown", result["findings"])

    def test_missing_device_is_not_ready(self):
        result = diag.assess(diag.DEMO_CASES[3])
        self.assertIn("device_missing_or_unreadable", result["findings"])
        self.assertFalse(result["port_opened"])

    def test_console_name_match_is_exact(self):
        observation = dict(diag.DEMO_CASES[1], kernel_cmdline="console=ttyTHS10,115200")
        self.assertNotIn("selected_device_is_named_as_kernel_console", diag.assess(observation)["findings"])
        observation["kernel_cmdline"] = "console=/dev/ttyTHS1,115200 console=tty0"
        self.assertIn("selected_device_is_named_as_kernel_console", diag.assess(observation)["findings"])

    def test_non_character_device_is_rejected_by_assessment(self):
        observation = dict(diag.DEMO_CASES[0], character_device=False)
        self.assertIn("not_a_character_device", diag.assess(observation)["findings"])

    def test_collection_only_reads_allowlisted_information(self):
        # All filesystem metadata is mocked: this is not a Linux/device test.
        paths = []
        def fake_read(path):
            paths.append(path)
            return "synthetic"
        with patch.object(diag.sys, "platform", "linux"), \
             patch.object(Path, "resolve", lambda value: value), \
             patch.object(Path, "stat", side_effect=FileNotFoundError), \
             patch.object(Path, "exists", return_value=False), \
             patch.object(diag, "read_info", fake_read), \
             patch.object(builtins, "open", side_effect=AssertionError("unexpected open")), \
             patch.object(Path, "open", side_effect=AssertionError("unexpected open")):
            diag.inspect("/dev/ttyTHS1")
        self.assertEqual(paths, ["/proc/cmdline", "/proc/device-tree/model", "/etc/nv_tegra_release"])

    def test_arbitrary_paths_are_rejected(self):
        with patch.object(diag.sys, "platform", "linux"), patch.object(Path, "resolve", lambda value: value):
            for value in ["/etc/passwd", "/dev/null", "ttyTHS1", "/dev/ttyTHS1/extra"]:
                with self.assertRaises(ValueError):
                    diag.inspect(value)


if __name__ == "__main__":
    unittest.main(verbosity=2)
