Files
OrionToolbar/tests/test_oriontoolbar.py
2026-10-23 11:04:51 +01:00

91 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""Source-contract and macOS runtime smoke tests. No VM state is changed."""
import os
import re
import shutil
import subprocess
import sys
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
CONTROLLER = (ROOT/'OrionToolbar/OrionVMController.swift').read_text()
VIEW = (ROOT/'OrionToolbar/OrionMenuView.swift').read_text()
APP = (ROOT/'OrionToolbar/OrionToolbarApp.swift').read_text()
class SourceContracts(unittest.TestCase):
def test_menu_bar_app(self):
self.assertIn('MenuBarExtra', APP)
self.assertIn('.menuBarExtraStyle(.window)', APP)
def test_vm_discovery(self):
self.assertIn('func discoverVMs()', CONTROLLER)
self.assertIn('runPrlctl(["list", "-a", "-j"])', CONTROLLER)
def test_refresh_probes_on_startup_and_timer(self):
self.assertGreaterEqual(CONTROLLER.count('await refreshServiceAvailability()'), 2)
self.assertIn('func refreshServiceAvailability()', CONTROLLER)
def test_ssh_only_if_reachable(self):
self.assertEqual(VIEW.count('vm.reachableSSH.contains(server.id)'), 2)
self.assertIn('server.sshUser.isEmpty', VIEW)
def test_mailpit_only_if_reachable(self):
self.assertEqual(VIEW.count('vm.reachableMailpit.contains(server.id)'), 2)
self.assertIn('server.mailpitPort != nil', VIEW)
def test_running_filter_for_probes(self):
block = CONTROLLER.split('func refreshServiceAvailability()', 1)[1].split('private nonisolated static func tcpPortOpen',1)[0]
self.assertIn('"running"', block)
self.assertIn('reachableSSH = Set(', block)
self.assertIn('reachableMailpit = Set(', block)
def test_probe_timeouts(self):
self.assertIn('"-G", "2", "-z"', CONTROLLER)
self.assertRegex(CONTROLLER, r'timeoutInterval\s*=\s*3')
def test_actions(self):
for name in ('openSSH','openMailpit','operate','hideVM','restoreVM','reorderVM','reorderServer','integrate','removeDefinition'):
with self.subTest(name=name):
self.assertRegex(CONTROLLER, r'func\s+' + name + r'\s*\(')
def test_vm_operations(self):
for operation in ('"start"','"stop"','"restart"','"force-stop"'):
with self.subTest(operation=operation):
self.assertIn(operation, VIEW + CONTROLLER)
def test_persisted_order_and_visibility(self):
self.assertIn('orionVMOrder.v1', CONTROLLER)
self.assertIn('orionHiddenVMIDs.v1', CONTROLLER)
self.assertIn('UserDefaults.standard', CONTROLLER)
def test_drag_and_drop(self):
self.assertGreaterEqual(VIEW.count('.onDrag'), 2)
self.assertGreaterEqual(VIEW.count('.onDrop'), 2)
def test_icons(self):
self.assertIn('VM.app', CONTROLLER)
self.assertIn('func icon(for machine:', CONTROLLER)
self.assertIn('func icon(for server:', CONTROLLER)
def test_settings(self):
self.assertIn('OrionSettingsWindowPresenter', VIEW)
self.assertIn('SettingsView()', VIEW)
def test_confirmation_before_force_stop(self):
self.assertIn('pendingForceStop', VIEW)
self.assertIn('.confirmationDialog("Force stop virtual machine?', VIEW)
def test_button_spacing(self):
self.assertIn('.padding(.leading, 12)', VIEW)
def test_foreground_activation_contract(self):
# Source-level contract only; actual window z-order requires manual UI test.
self.assertIn('NSWorkspace.shared', CONTROLLER)
self.assertIn('func openSSH', CONTROLLER)
self.assertIn('func openMailpit', CONTROLLER)
@unittest.skipUnless(sys.platform == 'darwin' and os.getenv('ORION_LIVE') == '1', 'Set ORION_LIVE=1 on macOS')
class LiveReadOnly(unittest.TestCase):
def test_parallels_list_json(self):
import json
executable = shutil.which('prlctl') or '/Applications/Parallels Desktop.app/Contents/MacOS/prlctl'
if not Path(executable).exists(): self.skipTest('Parallels CLI not installed')
p = subprocess.run([executable,'list','-a','-j'], capture_output=True, text=True, timeout=20)
self.assertEqual(p.returncode,0,p.stderr)
data = json.loads(p.stdout)
self.assertIsInstance(data,list)
for vm in data:
self.assertIn('uuid',vm)
self.assertIn('name',vm)
self.assertIn('status',vm)
def test_xcode_project(self):
self.assertTrue((ROOT/'OrionToolbar.xcodeproj/project.pbxproj').is_file())
if __name__ == '__main__': unittest.main()