Files
remark42/e2e/e2e_test.go
T
Dmitry VerkhoturovandGitHub 0b651dddd4 Make backend tests wait on conditions instead of durations (#2190)
* Make backend tests wait on conditions instead of durations

The backend workflow has a long tail of runs that fail once and pass on
a rerun. Every one of them comes down to a test assuming an operation
finishes within some duration rather than waiting for the state it
needs. Three were reproducible and each was reproduced against the old
code before being changed: TestServerAuthHooks minted a token that lived
one second and never tested expiry, so a slow runner turned the first
POST into a 401; TestServerApp_AnonMode saw "connection refused" because
waitForHTTPServerStart returned silently after three seconds and left a
later assertion to fail with something unrelated; TestFsStore_Cleanup
slept 200ms against a 300ms ttl that Cleanup widens to 400ms with its
commit grace, so roughly 100ms of stall collected an image meant to
survive.

Fixed sleeps before asserting on asynchronous work are replaced with
polls on the condition itself, using require.Eventually and
require.EventuallyWithT, and require.Never where the assertion is that
something did not happen. Polling closures assert on the CollectT they
are handed rather than on t, since testify runs them on another
goroutine, and polls that issue HTTP requests stay under the rate limit
on the routes they poll through.

Where a test needs time to have passed, the clock input is pinned
instead: staging ages are stamped with os.Chtimes on both sides of the
cleanup boundary right before each call, which also makes the 100ms
commit grace an exact case rather than something no assertion reaches,
and the RSS tests set store.Comment.Timestamp explicitly rather than
racing the wall clock into the first 100ms of a second so pubDate
matches.

chooseUnusedPort takes a port from the kernel's ephemeral range. Picking
at random out of a fixed 10000-port window let two package binaries,
which go test ./... runs concurrently, land on the same number between
the probe closing and the server binding. The start helpers fail naming
the port they waited on, and the SSL tests wait on the redirect port as
well as the TLS one.

Arbitrary budgets that nothing tests are gone: ten HTTP clients with a
one-second timeout against bolt-backed import and export, the "should
take about 100msec" assertions, and a one-second bound on noticing an
already cancelled context. Shutdown stays bounded at ten seconds so a
hang is still caught.

Two assertions get stronger. TestServerAuthHooks accepted 403 or 401
from a blocked user, an alternative that existed only because the short
token could expire mid-test; it is deterministically 403 now.
TestAdmin_BlockedList asserted two users blocked while one carried the
same 150ms ttl the next step waits to lapse, so the halves raced each
other.

goleak stops reporting the regexp2 clock goroutine, which chroma pulls
in for syntax highlighting and which lives for up to a second after the
last match with a timeout; it ends on its own but a binary finishing
inside that window was reported as leaking, and this suite now finishes
sooner. The ignore for net/http.(*Server).Shutdown goes the other way:
it no longer matches anything, with both packages run fifteen times each
under CPU oversubscription to confirm.

Two gaps the change would otherwise have opened are covered directly
rather than left to the side effects that used to cover them. The
one-second token was the only thing exercising the authenticator's
ClaimsUpd hook on refresh, so TestServerApp_ClaimsUpd now calls the hook
itself and checks admin, blocked, email and restricted-name
impersonation, including the two pass-through cases. Lifting the
open-route limit removed the last incidental exercise of the rate
limiter, so TestRateLimiter drives a burst past the allowance and checks
the refusals and that the limit is per client. Both run without a wall
clock, and both were confirmed to fail when the behaviour they cover is
removed.

Production code is untouched. The two sleeps outside test code, the 429
backoff in cmd/cleanup.go and the submit poll in store/image/image.go,
are left alone: no CI failure implicates them.

Test sleeps drop from 67 to 21, all of them either inside a
testing/synctest bubble or a poll interval. The suite runs in about 22
seconds instead of 46, mostly because
TestPublic_FindCommentsCtrl_ConsistentCount no longer paces a hundred
subtests with an 80ms sleep each to stay under the open route limit. The
300s per-package budget now matches across both workflows, the race_test
target and the documented command, and CLAUDE.md records the convention.

with '#' will be ignored, and an empty message aborts the commit. # #
Date: Sat Aug 22 01:12:31 2026 +0100 # # interactive rebase in progress;
onto 7c312da1 # Last command done (1 command done): # reword deb6cbf1 #
Make backend tests wait on conditions instead of durations # Next
command to do (1 remaining command): # reword 262e6dc2 # Apply go fix
under Go 1.27 # You are currently editing a commit while rebasing branch
'fix/backend-test-flakiness' on '7c312da1'. # # Changes to be committed:
.github/workflows/release.yml # modified: CLAUDE.md # modified: Makefile
modified: backend/_example/memory_store/server/rpc_test.go # modified:
backend/app/cmd/import_test.go # modified:
backend/app/cmd/server_test.go # modified: backend/app/main_test.go #
modified: backend/app/rest/api/admin_test.go # modified:
backend/app/rest/api/middleware_test.go # modified:
backend/app/rest/api/migrator_test.go # modified:
backend/app/rest/api/rest_private_test.go # modified:
backend/app/rest/api/rest_public_test.go # modified:
backend/app/rest/api/rest_test.go # modified:
backend/app/rest/api/rss_test.go # modified:
backend/app/rest/proxy/image_test.go # modified:
backend/app/store/image/fs_store_test.go # modified:
backend/app/store/service/service_test.go # modified:
docs/backlog/api-tests-deadlock-on-macos.md #

* Apply go fix under Go 1.27

Go 1.27 extends go fix with the modernizers, so `go fix ./...` now
rewrites patterns the language has since replaced. Running it across all
three modules produces this: legacy sync/atomic calls on plain integers
become the atomic types (notify.Service.closed, image.Service.term and
submitCount, and several test counters), reverse index loops become
slices.Backward, a Split-then-index becomes strings.Cut, counted loops
become range over an int, and interface{} becomes any in the e2e suite.

The example module needed no changes. The e2e module is behind a build
tag, so it only matches with `go fix -tags e2e ./...`.

One knock-on: prealloc can see the bound of a loop once it is written as
range over an int, so the slice it feeds is now preallocated.

with '#' will be ignored, and an empty message aborts the commit. # #
Date: Sat Aug 22 01:32:09 2026 +0100 # # interactive rebase in progress;
onto 7c312da1 # Last commands done (2 commands done): # reword deb6cbf1
262e6dc2 # Apply go fix under Go 1.27 # No commands remaining. # You are
currently editing a commit while rebasing branch
'fix/backend-test-flakiness' on '7c312da1'. # # Changes to be committed:
backend/app/migrator/native.go # modified: backend/app/notify/notify.go
backend/app/rest/api/rest_private_test.go # modified:
backend/app/store/comment.go # modified:
backend/app/store/image/image.go # modified:
backend/app/store/service/service_test.go # modified:
backend/app/store/service/title_test.go # modified: e2e/e2e_test.go #
modified: e2e/widgets_test.go #
2026-08-21 22:17:44 -05:00

626 lines
21 KiB
Go

//go:build e2e
// Package e2e drives the remark42 widget in a real browser through playwright.
//
// The suite talks to the stack in compose-e2e-test.yml at the repository root and brings it
// up itself when nothing is listening. Files:
//
// - e2e_test.go: TestMain, shared helpers, constants
// - auth_test.go: dev, anonymous and email sign-in
// - comment_test.go: post, reply, edit, delete
// - vote_test.go: voting and its failure path
// - thread_test.go: sorting and collapse persistence
// - iframe_test.go: the iframe's color scheme and reveal handshake
// - widgets_test.go: last-comments, counter and the profile iframe
package e2e
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/mxschmitt/playwright-go"
"github.com/stretchr/testify/require"
)
const (
// what the browser asks for. these have to be names rather than 127.0.0.1: the dev oauth2
// server binds whatever host it reads out of REMARK_URL, and a loopback bind inside the
// container cannot be published, so the browser resolves the names back with
// --host-resolver-rules and reaches the published ports
baseURL = "http://remark42:8080"
shortEditURL = "http://remark42-shortedit:8081"
// what this process asks for, since it is not the browser and has no resolver rules
probeURL = "http://127.0.0.1:8080"
shortEditProbeURL = "http://127.0.0.1:8081"
mailpitURL = "http://127.0.0.1:8025"
composeFile = "../compose-e2e-test.yml"
// every comment form carries this label, whatever mode it is in
commentFormSel = `form[aria-label="New comment"]`
// traces of failed tests land here; CI uploads the directory
traceDir = "traces"
// generous because CI runners are slower and less predictable than a laptop
waitTimeout = 15 * time.Second
// what a single locator call inside a poll body gets. playwright's own default is 30s,
// twice the budget of the loops here, so without this a missing element would block one
// attempt for longer than the whole loop and then report the wrong thing
pollTimeout = time.Second
)
var (
pw *playwright.Playwright
browser playwright.Browser
startedStack bool
// engines beyond chromium, launched on demand for the rendering tests
extraBrowsers = map[string]playwright.Browser{}
extraBrowsersMu sync.Mutex
// distinguishes this run's threads from those a previous run left in the database.
// a rerun is a fresh process, so without E2E_RUN_ID it would get its own threads and a
// failure caused by ordering or by state an earlier test left behind would pass on the
// second attempt whatever the code did
runID = firstNonEmpty(os.Getenv("E2E_RUN_ID"), fmt.Sprintf("%d", time.Now().UnixNano()))
authGate sync.Mutex
lastAuthCall time.Time
contextSeq atomic.Int64
// the default client has no timeout, so a port that accepts and then stalls would block
// a probe well past its own deadline and leave TestMain looking hung
probeClient = &http.Client{Timeout: 5 * time.Second}
)
// everything under /auth/ is rate limited to 2 requests a second, and that figure is a bare
// literal at backend/app/rest/api/rest.go:242 rather than a setting, so the suite has to pace
// itself: the widget calls /auth/status on every load, and again on visibilitychange or
// window focus while an oauth popup sign-in is pending. without this the limiter starts
// answering 429 and the widget renders as signed out
func pauseForAuthLimit() {
const spacing = 700 * time.Millisecond
authGate.Lock()
defer authGate.Unlock()
if wait := spacing - time.Since(lastAuthCall); wait > 0 {
time.Sleep(wait)
}
lastAuthCall = time.Now()
}
func TestMain(m *testing.M) {
if err := ensureStack(); err != nil {
log.Printf("[ERROR] stack not ready: %v", err)
teardown(1)
}
if err := playwright.Install(installOpts("chromium")); err != nil {
log.Printf("[ERROR] failed to install playwright: %v", err)
teardown(1)
}
var err error
if pw, err = playwright.Run(); err != nil {
log.Printf("[ERROR] failed to start playwright: %v", err)
teardown(1)
}
headless := os.Getenv("E2E_HEADLESS") != "false"
var slowMo float64
if !headless {
slowMo = 50 // slow the visible browser down enough to follow
}
browser, err = pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
Headless: playwright.Bool(headless),
SlowMo: playwright.Float(slowMo),
Args: []string{
"--host-resolver-rules=MAP remark42 127.0.0.1, MAP remark42-shortedit 127.0.0.1",
},
})
if err != nil {
_ = pw.Stop()
log.Printf("[ERROR] failed to launch browser: %v", err)
teardown(1)
}
code := m.Run()
for _, b := range extraBrowsers {
_ = b.Close()
}
_ = browser.Close()
_ = pw.Stop()
teardown(code)
}
// ensureStack waits for a running stack and starts one with compose when there is none
func ensureStack() error {
// every service, not just the first: compose-dev-backend.yml and `make rundev` also
// publish 8080, and adopting one of those would run the suite against a developer's own
// database and fail later as unexplained locator timeouts
if stackReady(2 * time.Second) {
return nil
}
log.Printf("[INFO] no complete stack on 127.0.0.1, bringing one up from %s", composeFile)
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
defer cancel()
// set before running, not after: a failed `up` leaves containers behind, and teardown
// has to know to remove them
startedStack = true
// --build matters: the image tag this compose file uses is the same one the dev compose
// files produce, so without it the suite can quietly test an image from another checkout
cmd := exec.CommandContext(ctx, "docker", "compose", "-f", composeFile,
"up", "-d", "--build", "--quiet-pull", "--wait")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("docker compose up: %w\n%s", err, out)
}
if !stackReady(waitTimeout) {
return fmt.Errorf("compose reported the stack healthy but it does not answer")
}
return nil
}
func firstNonEmpty(values ...string) string {
for _, v := range values {
if v != "" {
return v
}
}
return ""
}
// stackReady reports whether every service the suite needs answers
func stackReady(timeout time.Duration) bool {
for _, url := range []string{
probeURL + "/ping",
shortEditProbeURL + "/ping",
mailpitURL + "/api/v1/messages",
} {
if err := serverReady(url, timeout); err != nil {
return false
}
}
return true
}
func teardown(code int) {
if startedStack && os.Getenv("E2E_KEEP") == "" {
composeDown()
}
os.Exit(code)
}
// composeDown is separate so its context is closed before teardown calls os.Exit. it is
// bounded because it runs after m.Run, where `go test -timeout` can no longer rescue a
// wedged daemon and the binary would otherwise hang until the CI job times out
func composeDown() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
cmd := exec.CommandContext(ctx, "docker", "compose", "-f", composeFile, "down", "-v")
if out, err := cmd.CombinedOutput(); err != nil {
log.Printf("[WARN] docker compose down: %v\n%s", err, out)
}
}
func serverReady(url string, timeout time.Duration) error {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
resp, err := probeClient.Get(url)
if err == nil {
_ = resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return nil
}
}
time.Sleep(100 * time.Millisecond)
}
return fmt.Errorf("%s not ready after %v", url, timeout)
}
// newPage opens an isolated context, so cookies and storage never leak between tests. the
// context records a trace, kept only when the test fails, since a CI-only failure otherwise
// leaves nothing to look at from the browser side
func newPage(t *testing.T) playwright.Page {
t.Helper()
return newPageOn(t, browser)
}
func newPageOn(t *testing.T, b playwright.Browser) playwright.Page {
t.Helper()
ctx, err := b.NewContext()
require.NoError(t, err)
// the reveal timers start when the iframe element is created, so the tests that bound
// them have to measure from there rather than from anything this process can time
require.NoError(t, ctx.AddInitScript(playwright.Script{Content: playwright.String(iframeMarkScript)}))
tracing := ctx.Tracing().Start(playwright.TracingStartOptions{
Screenshots: playwright.Bool(true),
Snapshots: playwright.Bool(true),
}) == nil
// a test that opens two contexts would otherwise have them write the same file, and
// cleanup runs last-in-first-out, so the surviving trace would be of the page that was
// only setting the scenario up
seq := contextSeq.Add(1)
t.Cleanup(func() {
if tracing {
if !t.Failed() {
_ = ctx.Tracing().Stop()
_ = ctx.Close()
return
}
// say so rather than swallowing it: this runs only on a test that already failed,
// and a silently missing trace is what the reader goes looking for
// the pid distinguishes attempts: a rerun is a fresh process that shares runID
// with the attempt it is retrying and restarts its own counter, so without it the
// rerun would overwrite the trace of the attempt that actually failed
name := strings.ReplaceAll(t.Name(), "/", "-")
path := filepath.Join(traceDir, fmt.Sprintf("%s-%d-%d.zip", name, os.Getpid(), seq))
if serr := ctx.Tracing().Stop(path); serr != nil {
t.Logf("could not write the trace to %s: %v", path, serr)
}
}
_ = ctx.Close()
})
page, err := ctx.NewPage()
require.NoError(t, err)
debug := os.Getenv("E2E_DEBUG") != ""
page.OnResponse(func(r playwright.Response) {
switch {
case r.Status() == http.StatusTooManyRequests:
// the rate limiter answers the widget, not the test, so without this the failure
// arrives as an unexplained locator timeout
log.Printf("[WARN] rate limited: %s", r.URL())
case debug && r.Status() >= 400:
body, _ := r.Text()
log.Printf("[DEBUG] HTTP %d %s %s", r.Status(), r.URL(), body)
}
})
return page
}
// installOpts asks for the browser system libraries on CI only: install-deps shells out to
// apt with sudo, which is right for a runner and wrong for someone's laptop
func installOpts(browsers ...string) *playwright.RunOptions {
return &playwright.RunOptions{
Browsers: browsers,
WithDeps: os.Getenv("CI") != "",
}
}
// engines lists the browsers the rendering tests run in. hiding the frame until its
// document reports itself inited guards against a flash that was a WebKit one, so chromium
// alone would test the engine that never had the bug. E2E_BROWSERS narrows the set
func engines() []string {
if v := os.Getenv("E2E_BROWSERS"); v != "" {
return strings.Split(v, ",")
}
return []string{"chromium", "firefox", "webkit"}
}
// browserFor launches an engine once and keeps it for the rest of the run
func browserFor(t *testing.T, name string) playwright.Browser {
t.Helper()
if name == "chromium" {
return browser
}
extraBrowsersMu.Lock()
defer extraBrowsersMu.Unlock()
if b, ok := extraBrowsers[name]; ok {
return b
}
require.NoError(t, playwright.Install(installOpts(name)))
var bt playwright.BrowserType
switch name {
case "firefox":
bt = pw.Firefox
case "webkit":
bt = pw.WebKit
default:
t.Fatalf("unknown browser %q", name)
}
b, err := bt.Launch(playwright.BrowserTypeLaunchOptions{
Headless: playwright.Bool(os.Getenv("E2E_HEADLESS") != "false"),
})
require.NoError(t, err)
extraBrowsers[name] = b
return b
}
// renderURL is the thread url for the rendering tests. it uses the address directly rather
// than the mapped hostname, because --host-resolver-rules is a chromium flag and these tests
// need no dev oauth2, which is the only reason the hostname exists
func renderURL(t *testing.T) string {
t.Helper()
return threadURLOn(t, probeURL)
}
// threadURL gives each test its own comment thread. the demo page passes
// window.location.href as remark_config.url, and remark42 keys comments by it, so a unique
// query string isolates a test without resetting the database between runs
func threadURL(t *testing.T) string {
t.Helper()
return threadURLOn(t, baseURL)
}
// threadURLOn is threadURL against a given instance, for the tests that do not use the
// default one. every thread url goes through here so the rewriting below cannot be missed
func threadURLOn(t *testing.T, base string) string {
t.Helper()
// underscores are left in on purpose: collapse persistence keys off the page url, and a
// url carrying the separator its storage used to join on is the case that broke it. only
// the "/" of a subtest name is dropped, having no business in a query value
name := strings.ReplaceAll(strings.ToLower(t.Name()), "/", "-")
return fmt.Sprintf("%s/web/?e2e=%s-%s", base, name, runID)
}
// openThread loads the demo page on this test's own thread and waits for the widget
func openThread(t *testing.T, page playwright.Page) playwright.FrameLocator {
t.Helper()
return openURL(t, page, threadURL(t))
}
func openURL(t *testing.T, page playwright.Page, url string) playwright.FrameLocator {
t.Helper()
pauseForAuthLimit()
_, err := page.Goto(url, playwright.PageGotoOptions{
WaitUntil: playwright.WaitUntilStateDomcontentloaded,
})
require.NoError(t, err)
return widget(t, page)
}
// reload re-navigates and returns the widget once the thread itself has loaded.
//
// waiting for the comment form is not enough here: root.tsx renders it as soon as the user
// fetch resolves, while the thread keeps its own preloader until /find answers. an assertion
// that a comment is absent would otherwise pass against a thread that has not rendered yet
func reload(t *testing.T, page playwright.Page) playwright.FrameLocator {
t.Helper()
pauseForAuthLimit()
_, err := page.ExpectResponse("**/api/v1/find**", func() error {
_, rerr := page.Reload()
return rerr
}, playwright.PageExpectResponseOptions{Timeout: playwright.Float(float64(waitTimeout.Milliseconds()))})
require.NoError(t, err)
frame := widget(t, page)
// the response has arrived; give preact the frame it needs to swap the preloader out
waitHidden(t, frame.Locator(`[role="list"] .preloader`))
return frame
}
// postComment types into the form the widget is currently showing and waits for the comment
// to appear in the thread
func postComment(t *testing.T, frame playwright.FrameLocator, text string) {
t.Helper()
postCommentMatching(t, frame, text, text)
}
// postCommentMatching posts source and waits for a comment carrying marker. the two differ
// whenever the source has markdown in it, since the thread shows the rendered result
func postCommentMatching(t *testing.T, frame playwright.FrameLocator, source, marker string) playwright.Locator {
t.Helper()
submitForm(t, frame.Locator(commentFormSel).First(), source)
posted := comment(frame, marker)
waitVisible(t, posted)
return posted
}
// submitForm fills a comment form and submits it. the submit label is Send, Reply or Save
// depending on the form's mode, so match on the type rather than the text
func submitForm(t *testing.T, form playwright.Locator, text string) {
t.Helper()
require.NoError(t, form.Locator("textarea").Fill(text))
require.NoError(t, form.Locator(`button[type="submit"]`).Click())
}
// comment locates the comment carrying the text.
//
// only sound for asserting that something IS there. comments render through an
// IntersectionObserver (components/root/in-view), so one below the fold is an empty
// placeholder article and no text filter matches it: an absence assertion written this way
// passes whether the comment is gone or merely off screen. use articleCount for absence
func comment(frame playwright.FrameLocator, text string) playwright.Locator {
return frame.Locator("article", playwright.FrameLocatorLocatorOptions{HasText: text})
}
// articleCount counts the comments in the thread, including any the viewport has not reached
// yet, which still occupy an empty article element.
//
// it is an absence oracle only after a reload. before one the widget keeps a deleted
// comment's node and only swaps its text, and the backend prunes a deleted comment from the
// tree only when it has no replies (store/service/tree.go)
func articleCount(t *testing.T, frame playwright.FrameLocator) int {
t.Helper()
n, err := frame.Locator("article").Count()
require.NoError(t, err)
return n
}
// replyForm returns the form the widget opened last, which is the reply or edit form.
//
// it waits for a second form to exist first: playwright auto-waits for the element a locator
// resolves to, not for a better one to appear, so filling .Last() too early would type into
// the top-level form and post a root comment instead of a reply
func replyForm(t *testing.T, frame playwright.FrameLocator) playwright.Locator {
t.Helper()
eventually(t, waitTimeout, "the reply or edit form did not open", func() bool {
n, err := frame.Locator(commentFormSel).Count()
return err == nil && n > 1
})
return frame.Locator(commentFormSel).Last()
}
// actions returns the action bar of the comment carrying the text
func actions(frame playwright.FrameLocator, text string) playwright.Locator {
return comment(frame, text).Locator(".comment-actions").First()
}
// widget returns the widget's iframe once its own content has rendered. it waits on the
// comment form rather than the auth panel, because .auth is only present while signed out
func widget(t *testing.T, page playwright.Page) playwright.FrameLocator {
t.Helper()
frame := page.FrameLocator("#remark42 iframe")
waitVisible(t, frame.Locator(commentFormSel).First())
return frame
}
func waitVisible(t *testing.T, loc playwright.Locator) {
t.Helper()
require.NoError(t, loc.WaitFor(playwright.LocatorWaitForOptions{
State: playwright.WaitForSelectorStateVisible,
Timeout: playwright.Float(float64(waitTimeout.Milliseconds())),
}))
}
func waitHidden(t *testing.T, loc playwright.Locator) {
t.Helper()
require.NoError(t, loc.WaitFor(playwright.LocatorWaitForOptions{
State: playwright.WaitForSelectorStateHidden,
Timeout: playwright.Float(float64(waitTimeout.Milliseconds())),
}))
}
// pollText reads an element's text with a timeout short enough to be used inside eventually
func pollText(loc playwright.Locator) (string, error) {
return loc.InnerText(playwright.LocatorInnerTextOptions{
Timeout: playwright.Float(float64(pollTimeout.Milliseconds())),
})
}
// pollAttr reads an attribute with the same short timeout
func pollAttr(loc playwright.Locator, name string) (string, error) {
return loc.GetAttribute(name, playwright.LocatorGetAttributeOptions{
Timeout: playwright.Float(float64(pollTimeout.Milliseconds())),
})
}
// eventually polls fn until it returns true, for assertions playwright's own auto-waiting
// does not cover, such as a value read out of the page with Evaluate
func eventually(t *testing.T, timeout time.Duration, msg string, fn func() bool) {
t.Helper()
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
if fn() {
return
}
time.Sleep(50 * time.Millisecond)
}
t.Fatalf("condition not met within %v: %s", timeout, msg)
}
// pageFetch issues a request from the page's own session, so it carries the browser's cookies
// and the XSRF header the widget's own calls carry. body may be nil for a bodyless request
func pageFetch(t *testing.T, page playwright.Page, method, url string, body any) (status int, respBody string) {
t.Helper()
payload := ""
if body != nil {
raw, err := json.Marshal(body)
require.NoError(t, err)
payload = string(raw)
}
res, err := page.Evaluate(`async ({method, url, payload}) => {
const xsrf = document.cookie.split('; ').find((c) => c.startsWith('XSRF-TOKEN='));
const resp = await fetch(url, {
method,
credentials: 'include',
headers: {
'Content-Type': 'application/json',
'X-XSRF-TOKEN': xsrf ? xsrf.slice(xsrf.indexOf('=') + 1) : '',
},
body: payload === '' ? undefined : payload,
});
return {status: resp.status, body: await resp.text()};
}`, map[string]any{"method": method, "url": url, "payload": payload})
require.NoError(t, err)
out, ok := res.(map[string]any)
require.True(t, ok, "unexpected shape from the page: %#v", res)
switch v := out["status"].(type) {
case int:
status = v
case float64:
status = int(v)
default:
t.Fatalf("unexpected status type %T in %#v", v, out)
}
respBody, _ = out["body"].(string)
return status, respBody
}
// mailpitMessage returns the newest message sent to the address, with its body
func mailpitMessage(t *testing.T, to string) string {
t.Helper()
type item struct {
ID string `json:"ID"`
To []struct {
Address string `json:"Address"`
} `json:"To"`
}
var found string
eventually(t, waitTimeout, "no message for "+to, func() bool {
resp, err := probeClient.Get(mailpitURL + "/api/v1/messages?limit=50")
if err != nil {
return false
}
defer func() { _ = resp.Body.Close() }()
var list struct {
Messages []item `json:"messages"`
}
if err := json.NewDecoder(resp.Body).Decode(&list); err != nil {
return false
}
for _, msg := range list.Messages {
for _, addr := range msg.To {
if strings.EqualFold(addr.Address, to) {
found = msg.ID
return true
}
}
}
return false
})
resp, err := probeClient.Get(fmt.Sprintf("%s/api/v1/message/%s", mailpitURL, found))
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }()
var body struct {
Text string `json:"Text"`
HTML string `json:"HTML"`
}
require.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
return body.Text + body.HTML
}