forked from TheR1D/shell_gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_roles.py
55 lines (47 loc) · 1.85 KB
/
test_roles.py
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
import json
from pathlib import Path
from unittest.mock import patch
from sgpt.config import cfg
from sgpt.role import SystemRole
from .utils import app, cmd_args, comp_args, mock_comp, runner
@patch("sgpt.handlers.handler.completion")
def test_role(completion):
completion.return_value = mock_comp('{"foo": "bar"}')
path = Path(cfg.get("ROLE_STORAGE_PATH")) / "json_gen_test.json"
path.unlink(missing_ok=True)
args = {"--create-role": "json_gen_test"}
stdin = "you are a JSON generator"
result = runner.invoke(app, cmd_args(**args), input=stdin)
completion.assert_not_called()
assert result.exit_code == 0
args = {"--list-roles": True}
result = runner.invoke(app, cmd_args(**args))
completion.assert_not_called()
assert result.exit_code == 0
assert "json_gen_test" in result.stdout
args = {"--show-role": "json_gen_test"}
result = runner.invoke(app, cmd_args(**args))
completion.assert_not_called()
assert result.exit_code == 0
assert "you are a JSON generator" in result.stdout
# Test with argument prompt.
args = {
"prompt": "generate foo, bar",
"--role": "json_gen_test",
}
result = runner.invoke(app, cmd_args(**args))
role = SystemRole.get("json_gen_test")
completion.assert_called_once_with(**comp_args(role, args["prompt"]))
assert result.exit_code == 0
generated_json = json.loads(result.stdout)
assert "foo" in generated_json
# Test with stdin prompt.
completion.return_value = mock_comp('{"foo": "bar"}')
args = {"--role": "json_gen_test"}
stdin = "generate foo, bar"
result = runner.invoke(app, cmd_args(**args), input=stdin)
completion.assert_called_with(**comp_args(role, stdin))
assert result.exit_code == 0
generated_json = json.loads(result.stdout)
assert "foo" in generated_json
path.unlink(missing_ok=True)