120 lines
4.4 KiB
Python
120 lines
4.4 KiB
Python
import pytest
|
|
import json
|
|
from pathlib import Path
|
|
from project import configure, read_config, write_config, handle_change_mode
|
|
|
|
CONFIG_FILE = 'tests/config.json'
|
|
|
|
|
|
@pytest.fixture(scope='function')
|
|
def setup_config():
|
|
"""Fixture to create a dummy config file before each test and remove it after."""
|
|
# Create the config file
|
|
initial_config = {"mode": "terminal",
|
|
"ollamaConfig":{"base_url": 'https://ai.fabelous.app/v1/ollama/generic', "embeddings_url": 'http://localhost:11434', "base_model": 'mistral',
|
|
"embeddings_model": 'mxbai-embed-large', "base_header":{'': ''},"" :{'': ''}}}
|
|
with open(CONFIG_FILE, 'w') as f:
|
|
json.dump(initial_config, f)
|
|
|
|
yield
|
|
|
|
# remove the config file after the test
|
|
if Path(CONFIG_FILE).exists():
|
|
Path(CONFIG_FILE).unlink()
|
|
|
|
def test_configure(monkeypatch):
|
|
inputs = iter([
|
|
"I want gui", # Incorrect value
|
|
'terminal', # Correct input after re-prompt
|
|
'https://ai.fabelous.app/v1/ollama/generic', # Base LLM URL
|
|
'http://localhost:11434', # Embeddings URL
|
|
'mistral', # Base model
|
|
'mxbai-embed-large', # Embeddings model
|
|
'Authorization', # Base Model authentication key
|
|
'Token xzy', # Base Model authentication value
|
|
'Authorization', # Embeddings key for authentication
|
|
'Token xzy'# Embeddings value for authentication
|
|
])
|
|
|
|
monkeypatch.setattr('builtins.input', lambda _: next(inputs))
|
|
|
|
config = configure()
|
|
|
|
# Expected configurations based on the inputs
|
|
expected_config = {"mode": "terminal",
|
|
"ollamaConfig":{"base_url": 'https://ai.fabelous.app/v1/ollama/generic', "embeddings_url": 'http://localhost:11434', "base_model": 'mistral',
|
|
"embeddings_model": 'mxbai-embed-large', "base_header":{'Authorization': 'Token xzy'}
|
|
,"embeddings_header" :{'Authorization': 'Token xzy'}}}
|
|
|
|
|
|
assert config['mode'] == expected_config['mode'], "Mode configuration does not match."
|
|
assert config['ollamaConfig'] == expected_config['ollamaConfig'], "OllamaConfig does not match."
|
|
|
|
|
|
def test_read_config(setup_config, monkeypatch):
|
|
"""Test with both existing and non-existing config files."""
|
|
|
|
# non existing test file
|
|
monkeypatch.setattr('project.CONFIG_FILE', 'non_existing_config.json')
|
|
|
|
result = read_config()
|
|
|
|
assert result is None, "The function should return None for a non-existing config file."
|
|
|
|
# existing test file
|
|
monkeypatch.setattr('project.CONFIG_FILE', CONFIG_FILE)
|
|
|
|
config = read_config()
|
|
|
|
assert isinstance(config, dict), "The returned configuration is not a dictionary."
|
|
assert set(config.keys()) == {'mode', 'ollamaConfig'}, "The returned configuration does not contain expected keys."
|
|
|
|
|
|
|
|
|
|
|
|
def test_handle_change_mode(setup_config, monkeypatch):
|
|
"""Test to correctly update the config for valid modes."""
|
|
monkeypatch.setattr('project.CONFIG_FILE', CONFIG_FILE)
|
|
|
|
class Args:
|
|
pass
|
|
|
|
args = Args()
|
|
args.m = 'gui'
|
|
updated_config = handle_change_mode(args)
|
|
assert updated_config['mode'] == 'gui', "Mode should be updated to 'gui'"
|
|
|
|
args.m = 'terminal'
|
|
updated_config = handle_change_mode(args)
|
|
assert updated_config['mode'] == 'terminal', "Mode should be updated to 'terminal'"
|
|
|
|
args.m = 'invalid_mode'
|
|
with pytest.raises(SystemExit) as e:
|
|
handle_change_mode(args)
|
|
assert "Not a valid mode option. Only 'gui' and 'terminal' are valid" in str(e.value)
|
|
|
|
|
|
|
|
def test_write_config(monkeypatch):
|
|
"""Test to ensure changes are written to the config file correctly."""
|
|
monkeypatch.setattr('project.CONFIG_FILE', CONFIG_FILE)
|
|
new_config = {
|
|
"mode": "terminal",
|
|
"ollamaConfig": {
|
|
"base_url": "http://localhost:11434",
|
|
"embeddings_url": "https://ai.fabelous.app/v1/ollama/generic",
|
|
"base_model": "mistral",
|
|
"embeddings_model": "mxbai-embed-large",
|
|
"base_header": '{"Authorization": "Token xzy"}',
|
|
"embeddings_header": '{"Authorization": "Token xzy"}'
|
|
}
|
|
}
|
|
write_config(new_config)
|
|
|
|
# Read the file directly to check if the write was successful
|
|
with open(CONFIG_FILE, 'r') as f:
|
|
config = json.load(f)
|
|
assert config == new_config, "The config file was not updated correctly"
|
|
Path(CONFIG_FILE).unlink()
|