package handlers
import (
"fmt"
"net/http"
"strings"
"testing"
"github.com/mixeme/selfpost/internal/store"
)
// The Outbound log and the page of one message, rendered from the real journal
// by the handlers. Their markup is held to the mockups by the guards in
// internal/web/view; what is checked here is the behaviour of the pages — the
// polled region, the paging, the scoping.
// regionOf cuts the polled region (the table and the foot under it) out of a
// body: from the table's container to the end of the foot.
func regionOf(t *testing.T, body string) string {
t.Helper()
start := strings.Index(body, `
")
return body[start : foot+end+len("
")]
}
// The fragment answers with the markup the page holds for the same region, so
// the first poll changes nothing a person sees: the table, byte for byte, and
// the foot, which only gains the attribute that swaps it into place by its id.
func TestOutLogFragmentIsTheRegionOfThePage(t *testing.T) {
h, _ := serverWithDelivery(t)
page := regionOf(t, getBody(t, h.HandleDeliveries, "/outbound/log?domain=bs.example.ru"))
fragment := getBody(t, h.HandleDeliveriesRows, "/outbound/log/fragment?domain=bs.example.ru")
if !strings.Contains(fragment, `id="out-log-foot" hx-swap-oob="true"`) {
t.Errorf("the fragment's foot is not marked to be swapped in by its id:\n%s", fragment)
}
swapped := strings.Replace(fragment, ` hx-swap-oob="true"`, "", 1)
if swapped != page {
t.Errorf("the fragment is not the page's polled region\npage:\n%s\nfragment:\n%s", page, fragment)
}
// The poll asks for the same filters again, and never replaces the filter form.
has(t, "region", page, `data-poll`, `aria-live="polite"`, `hx-trigger="load"`, `hx-swap="outerHTML"`,
`hx-get="/outbound/log/fragment?domain=bs.example.ru"`)
lacks(t, "fragment", fragment, `