oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "context"
7 "testing"
8 "time"
9
10 "amadan.net/rastrillo/rastrillo/harness"
11 "github.com/chromedp/cdproto/input"
12 "github.com/chromedp/chromedp"
13
14 "amadan.net/oficina/mapa/internal/mapa"
15)
16
17// Prefetch on hover (build note 70), in Chromium: resting the pointer
18// on a link to a page fetches it, and the click is answered from that
19// fetch. While the editor holds text that leaving would commit, nothing
20// is fetched ahead, and the page opened after the commit shows it.
21
22// hover rests a hovering mouse on the first element selector matches.
23func hover(selector string) chromedp.Action[chromedp.Void] {
24 return chromedp.Func(func(ctx context.Context,
25 target *chromedp.Target) error {
26 box, err := chromedp.Run(ctx, chromedp.Dimensions(selector))
27 if err != nil {
28 return err
29 }
30 quad := box.Content
31 across, down := (quad[0]+quad[4])/2, (quad[1]+quad[5])/2
32 return chromedp.Do(ctx, chromedp.MouseEvent(
33 input.DispatchMouseEventTypeMouseMoved, across, down),
34 chromedp.Sleep(600*time.Millisecond))
35 })
36}
37
38// delivered is how the browser got this page: "navigational-prefetch"
39// when a prefetch answered the click, "" when the network did.
40const delivered = `performance.getEntriesByType("navigation")[0].deliveryType`
41
42func TestHoveringALinkPrefetchesThePage(t *testing.T) {
43 rig, store := browserRig(t, 0, harness.WithHover())
44 ctx := context.Background()
45 long, err := store.CreateTeamPage(ctx, Bo, "Long notes", longBody())
46 if err != nil {
47 t.Fatal(err)
48 }
49 from, err := store.CreateTeamPage(ctx, Bo, "Jumper",
50 "Plain: [[Long notes]]")
51 if err != nil {
52 t.Fatal(err)
53 }
54 chip := `article[mapa-body] a[href="` + mapa.PagePath(long) + `"]`
55 open := func() {
56 drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900),
57 chromedp.Navigate(rig.Origin+mapa.PagePath(from)),
58 chromedp.WaitVisible(mounted))
59 }
60 open()
61 drive(t, rig, hover(chip), chromedp.Click(chip))
62 arrive(t, rig, mapa.PagePath(long))
63 if how := read(t, rig, chromedp.Evaluate[string](delivered)); how !=
64 "navigational-prefetch" {
65 t.Fatalf("the page was not prefetched: %q", how)
66 }
67 open()
68 drive(t, rig, caretAt(0), chromedp.KeyEvent(" Left."),
69 chromedp.WaitReady(`html[data-mapa-uncommitted]`),
70 hover(chip), chromedp.Click(chip))
71 arrive(t, rig, mapa.PagePath(long))
72 if how := read(t, rig, chromedp.Evaluate[string](delivered)); how != "" {
73 t.Fatalf("with text to commit the page came by %q", how)
74 }
75 recent := `[mapa-side] a[href="` + mapa.PagePath(from) + `"]`
76 drive(t, rig, chromedp.WaitReady(recent))
77}
78