package mapatest import ( "encoding/json" "net/http" "net/http/httptest" "testing" ) // Prefetch on hover (build note 70): every screen names Mapa's ruleset, // which prefetches pages and days and never prerenders. func TestTheSpeculationRulesetPrefetchesPagesAndDays(t *testing.T) { rig := newRig(t) for _, path := range []string{"/", "/pages", "/settings"} { header := rig.get(t, Ana, path).Header().Get("Speculation-Rules") if header != `"/speculation-rules"` { t.Errorf("GET %s: Speculation-Rules %q", path, header) } } // Signed out, as the browser may fetch it: it says nothing private. recorder := httptest.NewRecorder() rig.built.Mux.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/speculation-rules", nil)) if kind := recorder.Header().Get("Content-Type"); recorder.Code != 200 || kind != "application/speculationrules+json" { t.Fatalf("the ruleset: %d %q", recorder.Code, kind) } var rules map[string][]struct { Source string Eagerness string Where map[string]any } if err := json.Unmarshal(recorder.Body.Bytes(), &rules); err != nil { t.Fatal(err) } if len(rules) != 1 || len(rules["prefetch"]) != 1 { t.Fatalf("the ruleset is not one prefetch rule: %s", recorder.Body.String()) } rule := rules["prefetch"][0] if rule.Source != "document" || rule.Eagerness != "moderate" { t.Fatalf("the rule: %+v", rule) } } // A prefetch is a GET, so it writes nothing (gate 10): a teammate who // has never written prefetches a page, two days and Today, and no // table changes: no member row, no draft, no event. func TestAPrefetchWritesNothing(t *testing.T) { rig := newRig(t) page := rig.createPage(t, Ana, "Acme", "Customer since 2024.") before, events := snapshot(t, rig), rig.count(t, "events") drafts := rig.count(t, "drafts") for _, path := range []string{page, "/d/2026-11-12", "/d/1999-01-01", "/"} { recorder := httptest.NewRecorder() request := httptest.NewRequest(http.MethodGet, path, nil) request.Header.Set("Sec-Purpose", "prefetch") rig.built.As(Bo).ServeHTTP(recorder, request) if recorder.Code != http.StatusOK { t.Fatalf("prefetching %s: %d", path, recorder.Code) } } if snapshot(t, rig) != before || rig.count(t, "events") != events || rig.count(t, "drafts") != drafts { t.Fatal("a prefetch wrote to the database") } }