package web
import (
"regexp"
"strings"
"testing"
)
// Every decision control lives in ONE form, and that form also holds
// the override reason — an input marked required, inside a <details>
// that is closed until an owner opens it. HTML constraint validation
// runs over the whole form whoever pressed it, so an empty override
// blocked Update, Accept, Land and Auto alike: the browser refused to
// submit and could not even say why, because the field it objected to
// was not focusable ("An invalid form control with name='override' is
// not focusable"). Update, which sets no busy signal, did nothing at
// all; Land when green, which sets one on click, span forever.
//
// Only the override's own button needs the reason, so every other
// submit in the form skips validation.
func TestDecisionControlsSkipTheOverrideValidation(t *testing.T) {
// The premise: the override field is required and starts hidden.
if !strings.Contains(landingSource, `name="override"`) ||
!strings.Contains(landingSource, `required aria-label="override reason"`) {
t.Fatal("the override reason is no longer a required field; this test's premise is stale")
}
buttons := regexp.MustCompile(`<button type="submit"[^>]*formaction="[^"]*"[^>]*>`).
FindAllString(landingSource, -1)
if len(buttons) < 5 {
t.Fatalf("found only %d submit buttons; the form was restructured", len(buttons))
}
for _, b := range buttons {
override := strings.Contains(b, `$pressed = 'override'`)
novalidate := strings.Contains(b, "formnovalidate")
switch {
case override && novalidate:
t.Error("the override button skips validation, so it can land red with no reason")
case !override && !novalidate:
action := regexp.MustCompile(`formaction="([^"]*)"`).FindStringSubmatch(b)
t.Errorf("%s does not set formnovalidate, so an unfilled override reason "+
"silently blocks it", action[1])
}
}
}