package web
import (
"bytes"
"strings"
"testing"
)
// The decide form's Enter default is whichever submit button comes
// first. Update from <target> merges into the proposer's branch, so it
// must never be that button: Accept leads when it is offered, and
// Update submits through its own form and so is never a candidate.
func TestUpdateIsNeverTheDecideFormsDefaultSubmit(t *testing.T) {
for _, unmet := range [][]string{nil, {"quarry"}} {
d := landingDetail{
landingRow: landingRow{ID: strings.Repeat("a", 64), Branch: "work",
Target: "trunk", State: "open"},
CanAccept: true, Mergeable: "clean", Behind: 2, Unmet: unmet,
}
var buf bytes.Buffer
if err := noHooks.template(landingPage).Execute(&buf, pageData{Title: "Landing", Repo: "quarry",
Section: "landings", Landing: &d}); err != nil {
t.Fatal(err)
}
html := buf.String()
start := strings.Index(html, `class="d-act acting"`)
if start < 0 {
t.Fatalf("FAIL: unmet=%v: no decide form", unmet)
}
form := html[start:]
form = form[:strings.Index(form, "</form>")]
first := strings.Index(form, `type="submit"`)
upd := strings.Index(form, `/update"`)
if upd < 0 {
t.Fatalf("FAIL: unmet=%v: update control missing", unmet)
}
if first >= 0 {
tag := form[strings.LastIndex(form[:first], "<button"):]
tag = tag[:strings.Index(tag, ">")]
if strings.Contains(tag, `/update"`) {
t.Errorf("FAIL: unmet=%v: update is the default submit: %s", unmet, tag)
}
}
tag := form[strings.LastIndex(form[:upd], "<button"):]
tag = tag[:strings.Index(tag, ">")]
if !strings.Contains(tag, ` form="`) {
t.Errorf("FAIL: unmet=%v: update belongs to the decide form: %s", unmet, tag)
}
if !strings.Contains(html, `<form id="l-update"`) {
t.Errorf("FAIL: unmet=%v: no separate update form", unmet)
}
if unmet == nil {
if g := strings.Index(form, `data-accept="go"`); g < 0 || g > upd {
t.Errorf("FAIL: accept does not lead update (accept %d, update %d)", g, upd)
}
}
}
}