File size: 2,846 Bytes
b701455
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import importlib
import os

import pytest


def test_set_and_get_last_seed(tmp_path, monkeypatch):
    monkeypatch.setenv("LD_SETTINGS_STORE_PATH", str(tmp_path / "settings_store.json"))
    # Import SettingsStore directly from file to avoid package-level side-effects
    import importlib.util
    spec = importlib.util.spec_from_file_location('settings_store_module', os.path.join(os.getcwd(), 'src', 'Core', 'SettingsStore.py'))
    SS = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(SS)

    assert SS.get_last_seed() is None
    SS.set_last_seed(424242)
    assert SS.get_last_seed() == 424242


def test_append_and_get_history(tmp_path, monkeypatch):
    monkeypatch.setenv("LD_SETTINGS_STORE_PATH", str(tmp_path / "settings_store.json"))
    # Import module directly from file
    import importlib.util
    spec = importlib.util.spec_from_file_location('settings_store_module', os.path.join(os.getcwd(), 'src', 'Core', 'SettingsStore.py'))
    SS = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(SS)

    assert SS.get_history() == []
    entry = SS.append_snapshot({"settings": {"prompt": "x", "width": 64}})
    hist = SS.get_history()
    assert isinstance(hist, list) and len(hist) == 1
    assert hist[0]["settings"]["prompt"] == "x"
    assert "id" in hist[0] and "ts" in hist[0]


def test_preferences_default_and_round_trip(tmp_path, monkeypatch):
    monkeypatch.setenv("LD_SETTINGS_STORE_PATH", str(tmp_path / "settings_store.json"))
    import importlib.util
    spec = importlib.util.spec_from_file_location('settings_store_module', os.path.join(os.getcwd(), 'src', 'Core', 'SettingsStore.py'))
    SS = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(SS)

    assert SS.get_preferences() == {"torch_compile": False, "vae_autotune": False}

    stored = SS.set_preferences({"torch_compile": True, "vae_autotune": True})
    assert stored == {"torch_compile": True, "vae_autotune": True}
    assert SS.get_preferences() == {"torch_compile": True, "vae_autotune": True}


def test_migrate_from_last_seed_txt(tmp_path, monkeypatch):
    store_path = tmp_path / "settings_store.json"
    monkeypatch.setenv("LD_SETTINGS_STORE_PATH", str(store_path))

    # Create legacy last_seed.txt in same directory
    basedir = store_path.parent
    basedir.mkdir(parents=True, exist_ok=True)
    legacy = basedir / "last_seed.txt"
    legacy.write_text("999999")

    # Import module directly from file
    import importlib.util
    spec = importlib.util.spec_from_file_location('settings_store_module', os.path.join(os.getcwd(), 'src', 'Core', 'SettingsStore.py'))
    SS = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(SS)

    res = SS.migrate_from_last_seed_txt()
    assert res == 999999
    assert SS.get_last_seed() == 999999
    assert not legacy.exists()