package web
import (
"encoding/json"
"strings"
"testing"
)
// preflightBody renders the base of a three-rung strata for a viewer
// who may accept, with the stack's pre-flight as given.
func preflightBody(t *testing.T, stack string) string {
t.Helper()
var st stackView
if err := json.Unmarshal([]byte(stack), &st); err != nil {
t.Fatal(err)
}
d := &landingDetail{landingRow: landingRow{ID: "aa11bb22cc33", Branch: "r1", Target: "trunk", State: "open", Title: "rung one"}}
d.CanAccept, d.MayLand, d.Mergeable, d.Stack = true, true, "clean", st
var buf strings.Builder
if err := landingPage.ExecuteTemplate(&buf, "landing-body", pageData{Repo: "quarry", Landing: d}); err != nil {
t.Fatal(err)
}
return buf.String()
}
const threeRungs = `"segments":[
{"id":"aa11bb22cc33","branch":"r1","target":"trunk","state":"open","position":1,"landable":true,"preflight":"clean"},
{"id":"bb22cc33dd44","branch":"r2","target":"r1","state":"open","position":2,"preflight":"%s"},
{"id":"cc33dd44ee55","branch":"r3","target":"r2","state":"open","position":3,"preflight":"%s"}]`
// A walk that would stop at rung 2 is offered as far as it would get —
// "land up to 1/3" — and the whole strata is not offered, since
// pressing it would stop there anyway (1f8c4148). One that would land
// every rung keeps the whole-strata control.
func TestTheLandControlOffersOnlyHowFarTheWalkGets(t *testing.T) {
stops := preflightBody(t, `{"top":"cc33dd44ee55","base":"aa11bb22cc33","preflight":"stops","stopsAt":2,"landTo":"aa11bb22cc33","landCount":1,`+
strings.Replace(strings.Replace(threeRungs, "%s", "conflict", 1), "%s", "not-reached", 1)+`}`)
if !strings.Contains(stops, `data-hook="land-up-to" data-count="1"`) || !strings.Contains(stops, `/landing/aa11bb22cc33/land"`) {
t.Fatal("FAIL: a walk stopping at 2 does not offer landing up to rung 1")
}
if strings.Contains(stops, `/landing/cc33dd44ee55/land"`) {
t.Fatal("FAIL: the whole strata is offered though the walk would stop at rung 2")
}
lands := preflightBody(t, `{"top":"cc33dd44ee55","base":"aa11bb22cc33","preflight":"lands","landTo":"cc33dd44ee55","landCount":3,`+
strings.Replace(strings.Replace(threeRungs, "%s", "clean", 1), "%s", "clean", 1)+`}`)
if !strings.Contains(lands, `/landing/cc33dd44ee55/land"`) || strings.Contains(lands, `data-hook="land-up-to"`) {
t.Fatal("FAIL: a strata that would land whole is not offered whole")
}
t.Log("ok: stops at 2 offers up to 1 only; lands whole offers the whole strata")
}