summaryrefslogtreecommitdiff
path: root/tests/context_test.py
blob: 1cc033001ec088ac968215b4a7e755f9fc3fe920 (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
from context import make_ai_context, make_prompt

default_config = {
  "engine": "chat",
  "options": {
    "model": "gpt-4o",
    "endpoint_url": "https://api.openai.com/v1/chat/completions",
    "max_tokens": "0",
    "max_completion_tokens": "0",
    "temperature": "1",
    "request_timeout": "20",
    "stream": "1",
    "enable_auth": "1",
    "token_file_path": "",
    "selection_boundary": "",
    "initial_prompt": "You are a general assistant.",
  },
  "ui": {
    "open_chat_command": "preset_below",
    "scratch_buffer_keep_open": "0",
    "populate_options": "0",
    "code_syntax_enabled": "1",
    "paste_mode": "1",
  },
}

def test_default_config():
    actual_context = make_ai_context({
        'config_default': default_config,
        'config_extension': {},
        'user_instruction': 'translate to Slovak',
        'user_selection': 'Hello world!',
        'command_type': 'chat',
    })
    expected_context = {
        'config': default_config,
        'prompt': 'translate to Slovak:\nHello world!',
    }
    assert expected_context == actual_context

def test_param_config():
    actual_config = make_ai_context({
        'config_default': default_config,
        'config_extension': {
            'options': {
                'max_tokens': '1000',
            },
        },
        'user_instruction': 'hello',
        'user_selection': '',
        'command_type': 'chat',
    })['config']
    assert '1000' == actual_config['options']['max_tokens']
    assert 'gpt-4o' == actual_config['options']['model']

def test_role_config():
    context = make_ai_context({
        'config_default': default_config,
        'config_extension': {},
        'user_instruction': '/test-role-simple user instruction',
        'user_selection': 'selected text',
        'command_type': 'chat',
    })
    actual_config = context['config']
    actual_prompt = context['prompt']
    assert 'o1-preview' == actual_config['options']['model']
    assert 'simple role prompt:\nuser instruction:\nselected text' == actual_prompt

def test_role_config_different_commands():
    base = {
        'config_default': default_config,
        'config_extension': {},
        'user_instruction': '/test-role hello',
        'user_selection': '',
    }
    context  = make_ai_context({ **base, 'command_type': 'chat' })
    actual_config = context['config']
    actual_prompt = context['prompt']
    assert 'model-common' == actual_config['options']['model']
    assert '0' == actual_config['ui']['paste_mode']
    assert 'preset_tab' == actual_config['ui']['open_chat_command']
    assert 'hello' == actual_prompt
    assert 'https://localhost/chat' == actual_config['options']['endpoint_url']
    assert 'chat' == actual_config['engine']

    context  = make_ai_context({ **base, 'command_type': 'complete' })
    actual_config = context['config']
    actual_prompt = context['prompt']
    assert 'model-common' == actual_config['options']['model']
    assert '0' == actual_config['ui']['paste_mode']
    assert 'hello' == actual_prompt
    assert 'https://localhost/complete' == actual_config['options']['endpoint_url']
    assert 'complete' == actual_config['engine']

    context  = make_ai_context({ **base, 'command_type': 'edit' })
    actual_config = context['config']
    actual_prompt = context['prompt']
    assert 'model-common' == actual_config['options']['model']
    assert '0' == actual_config['ui']['paste_mode']
    assert 'hello' == actual_prompt
    assert 'https://localhost/edit' == actual_config['options']['endpoint_url']
    assert 'complete' == actual_config['engine']

def test_multiple_role_configs():
    context = make_ai_context({
        'config_default': default_config,
        'config_extension': {},
        'user_instruction': '/test-role /test-role-simple hello',
        'user_selection': '',
        'command_type': 'chat',
    })
    actual_config = context['config']
    actual_prompt = context['prompt']
    assert 'o1-preview' == actual_config['options']['model']
    assert 'https://localhost/chat' == actual_config['options']['endpoint_url']
    assert 'simple role prompt:\nhello' == actual_prompt

def test_chat_only_role():
    context = make_ai_context({
        'config_default': default_config,
        'config_extension': {},
        'user_instruction': '/chat-only-role',
        'user_selection': '',
        'command_type': 'chat',
    })
    actual_config = context['config']
    assert 'preset_tab' == actual_config['options']['open_chat_command']

def test_default_roles():
    base = {
        'config_default': default_config,
        'config_extension': {},
        'user_instruction': '/chat-only-role',
        'user_selection': '',
        'command_type': 'chat',
    }
    context = make_ai_context({ **base, 'user_instruction': '/right hello world!' })
    assert 'preset_right' == context['config']['ui']['open_chat_command']

    context = make_ai_context({ **base, 'user_instruction': '/tab' })
    assert 'preset_tab' == context['config']['ui']['open_chat_command']

def test_user_prompt():
    assert 'fix grammar: helo word' == make_prompt( '', 'fix grammar: helo word', '', '')
    assert 'fix grammar:\nhelo word' == make_prompt( '', 'fix grammar', 'helo word', '')

def test_role_prompt():
    assert 'fix grammar:\nhelo word' == make_prompt( 'fix grammar', 'helo word', '', '')
    assert 'fix grammar:\nhelo word' == make_prompt( 'fix grammar', '', 'helo word', '')
    assert 'fix grammar:\nand spelling:\nhelo word' == make_prompt( 'fix grammar', 'and spelling', 'helo word', '')

def test_selection_prompt():
    assert 'fix grammar:\nhelo word' == make_prompt( '', '', 'fix grammar:\nhelo word', '')

def test_selection_boundary():
    assert 'fix grammar:\n###\nhelo word\n###' == make_prompt( '', 'fix grammar', 'helo word', '###')
    assert 'fix grammar:\n###\nhelo word\n###' == make_prompt( 'fix grammar', '', 'helo word', '###')