fix: send question answers by default and reject silent declines
4 files changed,
+53
−2
internal/equipo/copy.go+2 −0internal/equipo/question_handlers.go+8 −0internal/equipo/templates/question.html+3 −2internal/equipotest/final_question_test.go+40 −0
diff --git a/internal/equipo/copy.go b/internal/equipo/copy.go| index 91ababd0..95ee67bf 100644 |
| --- a/internal/equipo/copy.go |
| +++ b/internal/equipo/copy.go |
| @@ -250,6 +250,8 @@ var runnerDraftCopy = map[string]string{ |
| "task.group.finished": "Finished this week", |
| // COPY REVIEW REQUIRED: runner.prompt_too_large |
| "runner.prompt_too_large": "The task instructions and reproduction are too large for one turn.", |
| + // COPY REVIEW REQUIRED: question.send_answer |
| + "question.send_answer": "Send answer", |
| // COPY REVIEW REQUIRED: runner.budget |
| "runner.budget": "The task has reached its budget.", |
| // COPY REVIEW REQUIRED: runner.quota |
diff --git a/internal/equipo/question_handlers.go b/internal/equipo/question_handlers.go| index 72b10984..ccc34cb7 100644 |
| --- a/internal/equipo/question_handlers.go |
| +++ b/internal/equipo/question_handlers.go |
| @@ -34,6 +34,14 @@ func (a *application) answerQuestion(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| answer := runnerproto.QuestionAnswer{Behavior: r.PostForm.Get("behavior")} |
| + if answer.Behavior == "deny" { |
| + for key, values := range r.PostForm { |
| + if strings.HasPrefix(key, "answer_") && values[0] != "" { |
| + a.failure(w, r, ErrInvalid) |
| + return |
| + } |
| + } |
| + } |
| if raw := r.PostForm.Get("updated_input"); raw != "" { |
| answer.UpdatedInput = json.RawMessage(raw) |
| } |
diff --git a/internal/equipo/templates/question.html b/internal/equipo/templates/question.html| index e058a09f..6c11e17d 100644 |
| --- a/internal/equipo/templates/question.html |
| +++ b/internal/equipo/templates/question.html |
| @@ -1,3 +1,4 @@ |
| {{define "content"}}<h1>{{copy "task.answer"}}</h1>{{if not .Question.Fields}}<pre>{{.Question.Text}}</pre>{{end}}<form equipo-form method="post" action="/tasks/{{.TaskView.Task.ID}}/questions/{{.Question.ID}}/answer"> |
| -<input type="hidden" name="revision" value="{{.Question.Revision}}"><label>{{copy "question.behavior"}}<select rst-input name="behavior"><option value="deny">{{copy "question.deny"}}</option>{{if .Question.Fields}}<option value="allow">{{copy "question.allow"}}</option>{{end}}</select></label> |
| -{{range $i,$field:=.Question.Fields}}<label>{{$field.Prompt}}<textarea rst-input name="answer_{{$i}}" rows="4" maxlength="8192"></textarea></label>{{end}}<button rst-btn="primary">{{copy "form.save"}}</button></form>{{end}} |
| +<input type="hidden" name="revision" value="{{.Question.Revision}}"> |
| +{{range $i,$field:=.Question.Fields}}<label>{{$field.Prompt}}<textarea rst-input name="answer_{{$i}}" rows="4" maxlength="8192"></textarea></label>{{end}} |
| +{{if .Question.Fields}}<button rst-btn="primary" type="submit" name="behavior" value="allow">{{copy "question.send_answer"}}</button>{{end}}<button rst-btn type="submit" name="behavior" value="deny">{{copy "question.deny"}}</button></form>{{end}} |
diff --git a/internal/equipotest/final_question_test.go b/internal/equipotest/final_question_test.go| new file mode 100644 |
| index 00000000..0c309212 |
| --- /dev/null |
| +++ b/internal/equipotest/final_question_test.go |
| @@ -0,0 +1,40 @@ |
| +package equipotest |
| + |
| +import ( |
| + "net/url" |
| + "regexp" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func TestQuestionDefaultSubmissionSendsAnswer(t *testing.T) { |
| + f, q := questionFixture(t) |
| + server, c := taskPageServer(t, f.connectionFixture, false) |
| + path := "/tasks/" + f.a.TaskID + "/questions/" + q.ID |
| + body := taskGet(t, c, server.URL, path, 200) |
| + first := regexp.MustCompile(`<button[^>]*name="behavior"[^>]*value="([^"]+)"`).FindStringSubmatch(body) |
| + if len(first) != 2 || first[1] != "allow" || !strings.Contains(body, `value="deny"`) { |
| + t.Fatal("default action does not send answer") |
| + } |
| + res := clientPost(t, c, server.URL+path+"/answer", testOrigin, url.Values{"revision": {"1"}, "behavior": {first[1]}, "answer_0": {"Blue"}}) |
| + res.Body.Close() |
| + if res.StatusCode != 303 { |
| + t.Fatal("default answer refused", res.StatusCode) |
| + } |
| + if scalar(t, f.d, `SELECT count(*) FROM questions WHERE answer LIKE '%Blue%'`) != 1 { |
| + t.Fatal("typed answer lost") |
| + } |
| +} |
| +func TestQuestionDeclineRejectsTypedAnswer(t *testing.T) { |
| + f, q := questionFixture(t) |
| + server, c := taskPageServer(t, f.connectionFixture, false) |
| + path := "/tasks/" + f.a.TaskID + "/questions/" + q.ID + "/answer" |
| + res := clientPost(t, c, server.URL+path, testOrigin, url.Values{"revision": {"1"}, "behavior": {"deny"}, "answer_0": {"Blue"}}) |
| + res.Body.Close() |
| + if res.StatusCode != 422 { |
| + t.Fatal("decline discarded typed answer", res.StatusCode) |
| + } |
| + if scalar(t, f.d, "SELECT count(*) FROM questions WHERE state='answered'") != 0 { |
| + t.Fatal("decline persisted") |
| + } |
| +} |