summaryrefslogtreecommitdiff
path: root/tests/test_configuration.py
blob: 3e0ad79b52104dfacb262a07c14d8b745a62ebfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
Tests for the configuration service.
"""

import tempfile
from pathlib import Path
from unittest.mock import Mock

from lsfg_vk.configuration import ConfigurationService
from lsfg_vk.config_schema import ConfigurationManager


def test_parse_script_content():
    """Test parsing of script content with current environment variable format"""
    
    # Test script content matching current format
    script_content = """#!/bin/bash
# lsfg-vk launch script generated by decky-lossless-scaling-vk plugin
# This script sets up the environment for lsfg-vk to work with the plugin configuration
export PROTON_USE_WOW64=1
export SteamDeck=0
export DXVK_FRAME_RATE=18
export LSFG_PROCESS=decky-lsfg-vk
exec "$@"
"""
    
    script_values = ConfigurationManager.parse_script_content(script_content)
    
    assert script_values["enable_wow64"] is True
    assert script_values["disable_steamdeck_mode"] is True  # SteamDeck=0 means disable
    assert script_values["dxvk_frame_rate"] == 18


def test_parse_script_content_minimal():
    """Test parsing when only required exports are present"""
    
    script_content = """#!/bin/bash
# lsfg-vk launch script generated by decky-lossless-scaling-vk plugin
export LSFG_PROCESS=decky-lsfg-vk
exec "$@"
"""
    
    script_values = ConfigurationManager.parse_script_content(script_content)
    
    # Should be empty dict since no tracked env vars are present
    assert script_values == {}


def test_merge_config_with_script():
    """Test merging TOML config with script environment variables"""
    
    # Get defaults
    toml_config = ConfigurationManager.get_defaults()
    
    # Script values from parsing
    script_values = {
        "enable_wow64": True,
        "disable_steamdeck_mode": True,
        "dxvk_frame_rate": 30
    }
    
    merged = ConfigurationManager.merge_config_with_script(toml_config, script_values)
    
    # TOML fields should be preserved
    assert merged["multiplier"] == 1  # default from TOML
    assert merged["flow_scale"] == 0.8  # default from TOML
    assert merged["performance_mode"] is True  # default from TOML
    
    # Script fields should be overlaid
    assert merged["enable_wow64"] is True
    assert merged["disable_steamdeck_mode"] is True
    assert merged["dxvk_frame_rate"] == 30
    
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_home = Path(temp_dir)
        
        service = ConfigurationService(logger=mock_logger)
        service.user_home = temp_home
        service.lsfg_script_path = temp_home / "lsfg"
        
        script_content = """#!/bin/bash

# export ENABLE_LSFG=1
export LSFG_MULTIPLIER=2
export LSFG_FLOW_SCALE=1.0
# export LSFG_HDR=1
# export LSFG_PERF_MODE=1
# export MESA_VK_WSI_PRESENT_MODE=immediate # - disable vsync

exec "$@"
"""
        
        config = service._parse_script_content(script_content)
        
        assert config["enable_lsfg"] is False
        assert config["multiplier"] == 2
        assert config["flow_scale"] == 1.0
        assert config["hdr"] is False
        assert config["perf_mode"] is False
        assert config["immediate_mode"] is False


def test_generate_script_content():
    """Test script content generation"""
    mock_logger = Mock()
    
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_home = Path(temp_dir)
        
        service = ConfigurationService(logger=mock_logger)
        service.user_home = temp_home
        service.lsfg_script_path = temp_home / "lsfg"
        
        # Test with no toggles enabled
        config = {
            "enable_wow64": False,
            "disable_steamdeck_mode": False
        }
        content = service._generate_script_content(config)
        
        assert "#!/bin/bash" in content
        assert "export LSFG_PROCESS=decky-lsfg-vk" in content
        assert "export PROTON_USE_WOW64=1" not in content
        assert "export SteamDeck=0" not in content
        assert 'exec "$@"' in content
        
        # Test with both toggles enabled
        config = {
            "enable_wow64": True,
            "disable_steamdeck_mode": True
        }
        content = service._generate_script_content(config)
        
        assert "#!/bin/bash" in content
        assert "export PROTON_USE_WOW64=1" in content
        assert "export SteamDeck=0" in content
        assert "export LSFG_PROCESS=decky-lsfg-vk" in content
        assert 'exec "$@"' in content


def test_config_roundtrip():
    """Test that we can write config and read it back correctly"""
    mock_logger = Mock()
    
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_home = Path(temp_dir)
        
        service = ConfigurationService(logger=mock_logger)
        service.user_home = temp_home
        service.lsfg_script_path = temp_home / "lsfg"
        
        # Update config
        result = service.update_config(
            dll="/path/to/dll",
            multiplier=3,
            flow_scale=1.5,
            performance_mode=False,
            hdr_mode=True,
            experimental_present_mode="immediate",
            dxvk_frame_rate=30,
            enable_wow64=True,
            disable_steamdeck_mode=False
        )
        
        assert result["success"] is True
        
        # Read it back
        read_result = service.get_config()
        
        assert read_result["success"] is True
        config = read_result["config"]
        assert config["dll"] == "/path/to/dll"
        assert config["multiplier"] == 3
        assert config["flow_scale"] == 1.5
        assert config["performance_mode"] is False
        assert config["hdr_mode"] is True
        assert config["experimental_present_mode"] == "immediate"
        assert config["dxvk_frame_rate"] == 30
        assert config["enable_wow64"] is True
        assert config["disable_steamdeck_mode"] is False
        
        assert read_result["success"] is True
        config = read_result["config"]
        
        assert config["enable_lsfg"] is True
        assert config["multiplier"] == 3
        assert config["flow_scale"] == 1.5
        assert config["hdr"] is True
        assert config["perf_mode"] is False
        assert config["immediate_mode"] is True