File size: 3,330 Bytes
3860419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from unittest import mock

from gpt_engineer.applications.cli import learning
from gpt_engineer.applications.cli.learning import Learning
from gpt_engineer.core.default.disk_memory import DiskMemory
from gpt_engineer.core.prompt import Prompt


def test_human_review_input_no_concent_returns_none():
    with mock.patch.object(learning, "check_collection_consent", return_value=False):
        result = learning.human_review_input()

    assert result is None


def test_human_review_input_consent_code_ran_no_comments():
    with (
        mock.patch.object(learning, "check_collection_consent", return_value=True),
        mock.patch("builtins.input", return_value="y"),
    ):
        result = learning.human_review_input()

    assert result.raw == "y, y, "
    assert result.ran is True
    assert result.works is None
    assert result.comments == ""


def test_human_review_input_consent_code_ran_not_perfect_but_useful_no_comments():
    with (
        mock.patch.object(learning, "check_collection_consent", return_value=True),
        mock.patch("builtins.input", side_effect=["y", "n", "y", ""]),
    ):
        result = learning.human_review_input()

    assert result.raw == "y, n, y"
    assert result.ran is True
    assert result.works is True
    assert result.comments == ""


def test_check_collection_consent_yes():
    gpte_consent_mock = mock.Mock()
    gpte_consent_mock.exists.return_value = True
    gpte_consent_mock.read_text.return_value = "true"

    with mock.patch.object(learning, "Path", return_value=gpte_consent_mock):
        result = learning.check_collection_consent()

    assert result is True


def test_check_collection_consent_no_ask_collection_consent():
    with mock.patch.object(learning, "Path") as gpte_consent_mock:
        gpte_consent_mock.exists.return_value = True
        gpte_consent_mock.read_text.return_value = "false"

        with mock.patch.object(learning, "ask_collection_consent", return_value=True):
            result = learning.check_collection_consent()

    assert result is True


def test_ask_collection_consent_yes():
    with mock.patch("builtins.input", return_value="y"):
        result = learning.ask_collection_consent()

    assert result is True


def test_ask_collection_consent_no():
    with mock.patch("builtins.input", return_value="n"):
        result = learning.ask_collection_consent()

    assert result is False


def test_extract_learning():
    review = learning.Review(
        raw="y, n, y",
        ran=True,
        works=True,
        perfect=False,
        comments="The code is not perfect",
    )
    memory = mock.Mock(spec=DiskMemory)
    memory.to_json.return_value = {"prompt": "prompt"}

    result = learning.extract_learning(
        Prompt("prompt"),
        "model_name",
        0.01,
        ("prompt_tokens", "completion_tokens"),
        memory,
        review,
    )

    assert isinstance(result, Learning)


def test_get_session():
    with mock.patch.object(learning, "Path") as path_mock:
        # can be better tested with pyfakefs.
        path_mock.return_value.__truediv__.return_value.exists.return_value = False

        with mock.patch.object(learning, "random") as random_mock:
            random_mock.randint.return_value = 42
            result = learning.get_session()

        assert result == "42"