summaryrefslogtreecommitdiffstats
path: root/tests/test_goal.cpp
blob: 8e06517ebe6ed072cd3916f975d51291e6e95497 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include "goal.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int failures = 0;
static int checks = 0;

#define CHECK(cond, msg)                                                     \
    do {                                                                     \
        checks++;                                                            \
        if (!(cond)) {                                                       \
            failures++;                                                      \
            fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, (msg)); \
        }                                                                    \
    } while (0)

#define CHECK_CONT(hay, needle, msg)                                         \
    do {                                                                     \
        checks++;                                                            \
        if (!hay || !strstr(hay, needle)) {                                  \
            failures++;                                                      \
            fprintf(stderr,                                                  \
                    "FAIL %s:%d: %s (got `%s`, want substring `%s`)\n",      \
                    __FILE__, __LINE__, (msg), hay ? hay : "(null)",         \
                    (needle));                                               \
        }                                                                    \
    } while (0)

#define CHECK_NOT_CONT(hay, needle, msg)                                     \
    do {                                                                     \
        checks++;                                                            \
        if (hay && strstr(hay, needle)) {                                    \
            failures++;                                                      \
            fprintf(stderr, "FAIL %s:%d: %s (found `%s` in `%s`)\n",         \
                    __FILE__, __LINE__, (msg), (needle), hay);               \
        }                                                                    \
    } while (0)

static void test_parse_defaults(void)
{
    goal_spec spec;
    char *err = NULL;
    CHECK(goal_parse("all tests pass", &spec, &err) == 1, "parse");
    CHECK(strcmp(spec.criteria, "all tests pass") == 0, "criteria");
    CHECK(spec.max_tries == GOAL_DEFAULT_MAX_TRIES, "default cap");
    goal_spec_free(&spec);
}

static void test_parse_reads_cap(void)
{
    goal_spec spec;
    char *err = NULL;
    CHECK(goal_parse("all tests pass, stop after 3 tries", &spec, &err) == 1,
          "parse");
    CHECK(strcmp(spec.criteria, "all tests pass") == 0, "criteria");
    CHECK(spec.max_tries == 3, "cap");
    goal_spec_free(&spec);
}

static void test_parse_cap_without_tries_word(void)
{
    goal_spec spec;
    char *err = NULL;
    CHECK(goal_parse("make it green, stop after 10", &spec, &err) == 1, "parse");
    CHECK(strcmp(spec.criteria, "make it green") == 0, "criteria");
    CHECK(spec.max_tries == 10, "cap");
    goal_spec_free(&spec);
}

static void test_parse_tries_word_case_insensitive(void)
{
    goal_spec spec;
    char *err = NULL;
    CHECK(goal_parse("ship it, stop after 4 TRIES", &spec, &err) == 1,
          "uppercase TRIES accepted");
    CHECK(spec.max_tries == 4, "cap");
    goal_spec_free(&spec);
}

static void test_parse_rejections(void)
{
    goal_spec spec;
    char *err = NULL;

    err = NULL;
    CHECK(goal_parse("   ", &spec, &err) == 0, "empty input rejected");
    CHECK(err && strstr(err, "success criteria") != NULL, "empty err msg");
    free(err);

    err = NULL;
    CHECK(goal_parse("stop after 5 tries", &spec, &err) == 0,
          "missing criteria rejected");
    CHECK(err && strstr(err, "before `stop after`") != NULL, "missing err msg");
    free(err);

    err = NULL;
    CHECK(goal_parse("ship it, stop after", &spec, &err) == 0,
          "missing cap rejected");
    CHECK(err && strstr(err, "number of tries") != NULL, "missing cap err");
    free(err);

    err = NULL;
    CHECK(goal_parse("ship it, stop after 0 tries", &spec, &err) == 0,
          "zero cap rejected");
    CHECK(err && strstr(err, "at least 1 try") != NULL, "zero cap err");
    free(err);

    err = NULL;
    CHECK(goal_parse("ship it, stop after 5xyz", &spec, &err) == 0,
          "junk after the number rejected");
    CHECK(err && strstr(err, "number of tries") != NULL, "junk err msg");
    free(err);
}

static void test_first_attempt_prompt(void)
{
    goal_spec spec;
    spec.criteria = xstrdup("all tests pass");
    spec.max_tries = 5;
    char *prompt = goal_prompt(&spec, 1, NULL);
    CHECK_CONT(prompt, "all tests pass", "states the goal");
    CHECK_NOT_CONT(prompt, "NOT yet met", "no feedback on first attempt");
    CHECK_CONT(prompt, "fully met", "asks to keep going");
    free(prompt);
    goal_spec_free(&spec);
}

static void test_later_attempt_prompt(void)
{
    goal_spec spec;
    spec.criteria = xstrdup("all tests pass");
    spec.max_tries = 3;
    char *prompt = goal_prompt(&spec, 2, "the build still fails on x86");
    CHECK_CONT(prompt, "attempt 2 of 3", "attempt framing");
    CHECK_CONT(prompt, "NOT yet met", "verdict phrasing");
    CHECK_CONT(prompt, "the build still fails on x86", "includes feedback");
    free(prompt);

    char *no_fb = goal_prompt(&spec, 4, NULL);
    CHECK_CONT(no_fb, "attempt 4 of 3", "attempt number");
    CHECK_CONT(no_fb, "no specific feedback was given", "fallback feedback");
    free(no_fb);
    goal_spec_free(&spec);
}

static void test_verdict_met(void)
{
    goal_verdict v;
    goal_parse_verdict("MET\nAll tests pass and the suite is green.", &v);
    CHECK(v.met, "met detected");
    CHECK_CONT(v.reason, "green", "reason captured");
    free(v.reason);
}

static void test_verdict_not_met(void)
{
    goal_verdict v;
    goal_parse_verdict("NOT MET\nTwo tests still fail in the linux job.", &v);
    CHECK(!v.met, "not met detected");
    CHECK_CONT(v.reason, "still fail", "reason captured");
    free(v.reason);
}

static void test_verdict_garbage(void)
{
    goal_verdict v;
    goal_parse_verdict("I think we are close! Let me keep going.", &v);
    CHECK(!v.met, "conservative on garbage");
    CHECK_CONT(v.reason, "close", "first line is the reason");
    free(v.reason);
}

static void test_verdict_unmet_is_not_met(void)
{
    /* "UNMET" contains the substring "MET"; it must not read as met. */
    goal_verdict v;
    goal_parse_verdict("UNMET\nStill failing.", &v);
    CHECK(!v.met, "UNMET is not met");
    free(v.reason);

    goal_parse_verdict("NOT UNMET", &v);
    CHECK(!v.met, "NOT UNMET is not met");
    free(v.reason);

    goal_parse_verdict("unmet", &v);
    CHECK(!v.met, "lowercase unmet is not met");
    free(v.reason);

    goal_parse_verdict("MET", &v);
    CHECK(v.met, "plain MET is met");
    free(v.reason);
}

static void test_verdict_no_reason(void)
{
    goal_verdict v;
    goal_parse_verdict("MET", &v);
    CHECK(v.met, "bare MET is met");
    CHECK_CONT(v.reason, "did not give a reason", "fallback reason");
    free(v.reason);
}

static void test_preamble(void)
{
    const char *p = goal_evaluator_preamble();
    CHECK_CONT(p, "MET", "mentions MET token");
    CHECK_CONT(p, "NOT MET", "mentions NOT MET token");
    CHECK_CONT(p, "strict", "strict reviewer");
}

int main(void)
{
    test_parse_defaults();
    test_parse_reads_cap();
    test_parse_cap_without_tries_word();
    test_parse_tries_word_case_insensitive();
    test_parse_rejections();
    test_first_attempt_prompt();
    test_later_attempt_prompt();
    test_verdict_met();
    test_verdict_not_met();
    test_verdict_garbage();
    test_verdict_unmet_is_not_met();
    test_verdict_no_reason();
    test_preamble();
    printf("%d checks, %d failures\n", checks, failures);
    return failures ? 1 : 0;
}