74 lines
2.5 KiB
Python
74 lines
2.5 KiB
Python
#!/usr/bin/env python
|
|
# coding=utf-8
|
|
from __future__ import absolute_import
|
|
|
|
import unittest
|
|
import mock
|
|
import threading
|
|
import time
|
|
|
|
from octoprint_tailscale_funnel.status_monitor import StatusMonitor
|
|
|
|
|
|
class TestStatusMonitor(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
# Create a mock plugin
|
|
self.mock_plugin = mock.Mock()
|
|
self.mock_plugin._logger = mock.Mock()
|
|
self.mock_plugin.tailscale_interface = mock.Mock()
|
|
self.mock_plugin._settings = mock.Mock()
|
|
self.mock_plugin._plugin_manager = mock.Mock()
|
|
|
|
self.status_monitor = StatusMonitor(self.mock_plugin, interval=0.1) # Short interval for testing
|
|
|
|
def test_init(self):
|
|
# Test that the status monitor is initialized correctly
|
|
self.assertEqual(self.status_monitor.plugin, self.mock_plugin)
|
|
self.assertEqual(self.status_monitor.interval, 0.1)
|
|
self.assertIsNone(self.status_monitor._thread)
|
|
|
|
def test_start(self):
|
|
# Test that start creates and starts a thread
|
|
self.status_monitor.start()
|
|
self.assertIsNotNone(self.status_monitor._thread)
|
|
self.assertTrue(self.status_monitor._thread.is_alive())
|
|
|
|
# Clean up
|
|
self.status_monitor.stop()
|
|
|
|
def test_stop(self):
|
|
# Test that stop stops the thread
|
|
self.status_monitor.start()
|
|
self.assertTrue(self.status_monitor._thread.is_alive())
|
|
|
|
self.status_monitor.stop()
|
|
self.assertIsNone(self.status_monitor._thread)
|
|
|
|
@mock.patch('octoprint_tailscale_funnel.status_monitor.time')
|
|
def test_monitor_loop(self, mock_time):
|
|
# Setup mocks
|
|
mock_time.sleep = mock.Mock() # Don't actually sleep during testing
|
|
|
|
# Mock the tailscale interface
|
|
self.mock_plugin.tailscale_interface.is_funnel_enabled.return_value = True
|
|
self.mock_plugin._settings.get_boolean.return_value = False # Different from funnel status
|
|
|
|
# Start the monitor
|
|
self.status_monitor.start()
|
|
|
|
# Give it a moment to run
|
|
time.sleep(0.2)
|
|
|
|
# Stop the monitor
|
|
self.status_monitor.stop()
|
|
|
|
# Assert that the methods were called
|
|
self.mock_plugin.tailscale_interface.is_funnel_enabled.assert_called()
|
|
self.mock_plugin._settings.set_boolean.assert_called_with(["enabled"], True)
|
|
self.mock_plugin._settings.save.assert_called()
|
|
self.mock_plugin._plugin_manager.send_plugin_message.assert_called()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main() |