111 lines
3.6 KiB
Python
111 lines
3.6 KiB
Python
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from cthulhu import settings
|
|
from cthulhu.backends.toml_backend import Backend
|
|
|
|
|
|
LEGACY_SETTINGS = """format-version = 2
|
|
|
|
[profiles.default.metadata]
|
|
display-name = "Default"
|
|
internal-name = "default"
|
|
|
|
[profiles.default.keybindings]
|
|
keyboard-layout = "desktop"
|
|
desktop-modifier-keys = ["Insert", "KP_Insert"]
|
|
|
|
[profiles.default.ai-assistant]
|
|
enabled = false
|
|
provider = "ollama"
|
|
api-key-file = ""
|
|
ollama-model = "llama3.2-vision"
|
|
ollama-endpoint = "http://localhost:11434"
|
|
confirmation-required = true
|
|
action-timeout = 30
|
|
screenshot-quality = "medium"
|
|
max-context-length = 4000
|
|
|
|
[profiles.default.ocr]
|
|
language-code = "eng"
|
|
scale-factor = 3
|
|
grayscale-image = false
|
|
invert-image = false
|
|
black-white-image = false
|
|
black-white-threshold = 200
|
|
color-calculation = false
|
|
color-calculation-max = 3
|
|
copy-to-clipboard = false
|
|
|
|
[profiles.default.plugins]
|
|
active-plugins = ["PluginManager", "OCR"]
|
|
plugin-sources = []
|
|
"""
|
|
|
|
|
|
class LegacyTomlSchemaMigrationTests(unittest.TestCase):
|
|
def test_backend_migrates_legacy_nested_profile_schema_on_read(self):
|
|
with tempfile.TemporaryDirectory() as tempDir:
|
|
Path(tempDir, "user-settings.toml").write_text(
|
|
LEGACY_SETTINGS,
|
|
encoding="utf-8",
|
|
)
|
|
|
|
backend = Backend(tempDir)
|
|
|
|
self.assertEqual(backend.availableProfiles(), [["Default", "default"]])
|
|
|
|
general = backend.getGeneral("default")
|
|
|
|
self.assertEqual(general["profile"], ["Default", "default"])
|
|
self.assertEqual(
|
|
general["keyboardLayout"],
|
|
settings.GENERAL_KEYBOARD_LAYOUT_DESKTOP,
|
|
)
|
|
self.assertEqual(general["cthulhuModifierKeys"], settings.DESKTOP_MODIFIER_KEYS)
|
|
self.assertEqual(general["activePlugins"], ["PluginManager", "OCR"])
|
|
self.assertEqual(general["aiProvider"], settings.AI_PROVIDER_OLLAMA)
|
|
self.assertFalse(general["aiAssistantEnabled"])
|
|
self.assertEqual(general["ocrLanguageCode"], "eng")
|
|
self.assertEqual(backend.getKeybindings("default"), {})
|
|
|
|
def test_saving_after_legacy_read_rewrites_current_schema(self):
|
|
with tempfile.TemporaryDirectory() as tempDir:
|
|
settingsPath = Path(tempDir, "user-settings.toml")
|
|
settingsPath.write_text(LEGACY_SETTINGS, encoding="utf-8")
|
|
|
|
backend = Backend(tempDir)
|
|
general = backend.getGeneral("default")
|
|
backend.saveProfileSettings("default", dict(general), {}, {})
|
|
|
|
savedSettings = settingsPath.read_text(encoding="utf-8")
|
|
|
|
self.assertIn('profile = ["Default", "default"]', savedSettings)
|
|
self.assertIn('activePlugins = ["PluginManager", "OCR"]', savedSettings)
|
|
self.assertNotIn("format-version = 2", savedSettings)
|
|
self.assertNotIn("[profiles.default.metadata]", savedSettings)
|
|
|
|
def test_legacy_profile_keybindings_are_preserved(self):
|
|
legacySettings = LEGACY_SETTINGS.replace(
|
|
'desktop-modifier-keys = ["Insert", "KP_Insert"]',
|
|
'desktop-modifier-keys = ["Insert", "KP_Insert"]\ncustom-binding = "kb:cthulhu+x"',
|
|
)
|
|
|
|
with tempfile.TemporaryDirectory() as tempDir:
|
|
Path(tempDir, "user-settings.toml").write_text(
|
|
legacySettings,
|
|
encoding="utf-8",
|
|
)
|
|
|
|
backend = Backend(tempDir)
|
|
|
|
self.assertEqual(
|
|
backend.getKeybindings("default"),
|
|
{"custom-binding": "kb:cthulhu+x"},
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|