import json import os import tempfile from unittest.mock import patch import pytest from time_tracker.core.config import load_config, save_config def test_load_config_creates_default_if_not_exists(): """ creates default when file doesn't exist""" with tempfile.NamedTemporaryFile(suffix='.json', delete=False) as tmp: tmp_path = tmp.name # Remove the file so it doesn't exist os.unlink(tmp_path) # Mock the config file path with patch('time_tracker.core.config.CONFIG_FILE', tmp_path): config = load_config() # Check default values assert config['tracking_enabled'] is True # Updated to match actual defaults expected_work_apps = ['PyCharm', 'VS Code', 'Visual Studio', 'Sublime Text', 'Atom', 'IntelliJ IDEA'] assert config['window_rules']['work'] == expected_work_apps expected_distraction_apps = ['YouTube', 'Twitter', 'Facebook', 'Instagram', 'TikTok', 'Discord', 'Slack'] assert config['window_rules']['distraction'] == expected_distraction_apps assert config.get('current_task_id') is None # Verify file was created assert os.path.exists(tmp_path) def test_save_and_load_config(): """Test saving and loading config preserves data""" with tempfile.NamedTemporaryFile(suffix='.json', delete=False) as tmp: tmp_path = tmp.name # Clean up after ourselves if os.path.exists(tmp_path): os.unlink(tmp_path) test_config = { 'tracking_enabled': False, 'current_task_id': 'TEST-123', 'window_rules': { 'work': ['Editor'], 'distraction': ['Social Media'] } } # Mock the config file path with patch('time_tracker.core.config.CONFIG_FILE', tmp_path): save_config(test_config) loaded_config = load_config() assert loaded_config['tracking_enabled'] == False assert loaded_config['current_task_id'] == 'TEST-123' assert loaded_config['window_rules']['work'] == ['Editor'] assert loaded_config['window_rules']['distraction'] == ['Social Media'] def test_load_config_preserves_existing_values(): """Test that loading config doesn't overwrite existing values unnecessarily""" with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as tmp: initial_config = { 'tracking_enabled': True, 'current_task_id': 'EXISTING-123', 'window_rules': { 'work': ['Custom Editor'], 'distraction': ['Gaming App'] } } json.dump(initial_config, tmp) tmp_path = tmp.name # Mock the config file path with patch('time_tracker.core.config.CONFIG_FILE', tmp_path): loaded_config = load_config() assert loaded_config['tracking_enabled'] == True assert loaded_config['current_task_id'] == 'EXISTING-123' assert loaded_config['window_rules']['work'] == ['Custom Editor'] assert loaded_config['window_rules']['distraction'] == ['Gaming App']