Files
manual_slop/tests/test_saved_presets_sim.py

169 lines
4.7 KiB
Python

import pytest
import time
import tomli_w
import os
import json
import shutil
from pathlib import Path
from src.api_hook_client import ApiHookClient
@pytest.fixture(scope="session", autouse=True)
def test_env_setup():
temp_workspace = Path("tests/artifacts/live_gui_workspace")
if temp_workspace.exists():
try: shutil.rmtree(temp_workspace)
except: pass
temp_workspace.mkdir(parents=True, exist_ok=True)
config_path = temp_workspace / "config.toml"
manual_slop_path = temp_workspace / "manual_slop.toml"
# Create minimal project file
manual_slop_path.write_text("[project]\nname = 'TestProject'\n", encoding="utf-8")
# Create local config.toml
config_path.write_text(tomli_w.dumps({
"projects": {
"paths": [str(manual_slop_path.absolute())],
"active": str(manual_slop_path.absolute())
},
"ai": {
"provider": "gemini",
"model": "gemini-2.5-flash-lite"
}
}))
yield
# Cleanup handled by live_gui fixture usually, but we can be explicit
if config_path.exists(): config_path.unlink()
def test_preset_switching(live_gui):
client = ApiHookClient()
# Paths for presets
temp_workspace = Path("tests/artifacts/live_gui_workspace")
global_presets_path = temp_workspace / "presets.toml"
project_presets_path = temp_workspace / "project_presets.toml"
manual_slop_path = temp_workspace / "manual_slop.toml"
# Cleanup before test
if global_presets_path.exists(): global_presets_path.unlink()
if project_presets_path.exists(): project_presets_path.unlink()
try:
# Create a global preset
global_presets_path.write_text(tomli_w.dumps({
"presets": {
"TestGlobal": {
"system_prompt": "Global Prompt",
"temperature": 0.7
}
}
}))
# Create a project preset
project_presets_path.write_text(tomli_w.dumps({
"presets": {
"TestProject": {
"system_prompt": "Project Prompt",
"temperature": 0.3
},
"TestGlobal": { # Override
"system_prompt": "Overridden Prompt",
"temperature": 0.5
}
}
}))
# Switch to the local project to ensure context is correct
client.push_event("custom_callback", {
"callback": "_switch_project",
"args": [str(manual_slop_path.absolute())]
})
time.sleep(2)
# Trigger reload of presets (just in case)
client.push_event("custom_callback", {
"callback": "_refresh_from_project",
"args": []
})
time.sleep(2) # Wait for processing
# Apply Global Preset (should use override from project if available in merged list)
client.push_event("custom_callback", {
"callback": "_apply_preset",
"args": ["TestGlobal", "global"]
})
time.sleep(1)
# Verify state
state = client.get_gui_state()
assert state["global_preset_name"] == "TestGlobal"
assert state["global_system_prompt"] == "Overridden Prompt"
assert state["temperature"] == 0.5
# Apply Project Preset
client.push_event("custom_callback", {
"callback": "_apply_preset",
"args": ["TestProject", "project"]
})
time.sleep(1)
state = client.get_gui_state()
assert state["project_preset_name"] == "TestProject"
assert state["project_system_prompt"] == "Project Prompt"
assert state["temperature"] == 0.3
# Select "None"
client.push_event("custom_callback", {
"callback": "_apply_preset",
"args": ["None", "global"]
})
time.sleep(1)
state = client.get_gui_state()
assert not state.get("global_preset_name") # Should be None or ""
# Prompt remains from previous application
assert state["global_system_prompt"] == "Overridden Prompt"
finally:
# Cleanup
if global_presets_path.exists(): global_presets_path.unlink()
if project_presets_path.exists(): project_presets_path.unlink()
def test_preset_manager_modal(live_gui):
client = ApiHookClient()
temp_workspace = Path("tests/artifacts/live_gui_workspace")
global_presets_path = temp_workspace / "presets.toml"
project_presets_path = temp_workspace / "project_presets.toml"
# Open Modal
client.set_value("show_preset_manager_modal", True)
time.sleep(1)
# Create New Preset via Modal Logic (triggering the callback directly for reliability in headless)
client.push_event("custom_callback", {
"callback": "_cb_save_preset",
"args": ["ModalPreset", "Modal Content", 0.9, 1.0, 4096, "global"]
})
time.sleep(2)
# Verify file exists
assert global_presets_path.exists()
with open(global_presets_path, "rb") as f:
import tomllib
data = tomllib.load(f)
assert "ModalPreset" in data["presets"]
assert data["presets"]["ModalPreset"]["temperature"] == 0.9
# Delete Preset via Modal Logic
client.push_event("custom_callback", {
"callback": "_cb_delete_preset",
"args": ["ModalPreset", "global"]
})
time.sleep(2)
# Verify file content
with open(global_presets_path, "rb") as f:
data = tomllib.load(f)
assert "ModalPreset" not in data["presets"]