//go:build browser package mapatest import ( "context" "testing" "time" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/input" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // Prefetch on hover (build note 70), in Chromium: resting the pointer // on a link to a page fetches it, and the click is answered from that // fetch. While the editor holds text that leaving would commit, nothing // is fetched ahead, and the page opened after the commit shows it. // hover rests a hovering mouse on the first element selector matches. func hover(selector string) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { box, err := chromedp.Run(ctx, chromedp.Dimensions(selector)) if err != nil { return err } quad := box.Content across, down := (quad[0]+quad[4])/2, (quad[1]+quad[5])/2 return chromedp.Do(ctx, chromedp.MouseEvent( input.DispatchMouseEventTypeMouseMoved, across, down), chromedp.Sleep(600*time.Millisecond)) }) } // delivered is how the browser got this page: "navigational-prefetch" // when a prefetch answered the click, "" when the network did. const delivered = `performance.getEntriesByType("navigation")[0].deliveryType` func TestHoveringALinkPrefetchesThePage(t *testing.T) { rig, store := browserRig(t, 0, harness.WithHover()) ctx := context.Background() long, err := store.CreateTeamPage(ctx, Bo, "Long notes", longBody()) if err != nil { t.Fatal(err) } from, err := store.CreateTeamPage(ctx, Bo, "Jumper", "Plain: [[Long notes]]") if err != nil { t.Fatal(err) } chip := `article[mapa-body] a[href="` + mapa.PagePath(long) + `"]` open := func() { drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), chromedp.Navigate(rig.Origin+mapa.PagePath(from)), chromedp.WaitVisible(mounted)) } open() drive(t, rig, hover(chip), chromedp.Click(chip)) arrive(t, rig, mapa.PagePath(long)) if how := read(t, rig, chromedp.Evaluate[string](delivered)); how != "navigational-prefetch" { t.Fatalf("the page was not prefetched: %q", how) } open() drive(t, rig, caretAt(0), chromedp.KeyEvent(" Left."), chromedp.WaitReady(`html[data-mapa-uncommitted]`), hover(chip), chromedp.Click(chip)) arrive(t, rig, mapa.PagePath(long)) if how := read(t, rig, chromedp.Evaluate[string](delivered)); how != "" { t.Fatalf("with text to commit the page came by %q", how) } recent := `[mapa-side] a[href="` + mapa.PagePath(from) + `"]` drive(t, rig, chromedp.WaitReady(recent)) }