package mapatest import ( "crypto/sha256" "encoding/json" "fmt" "net/http" "net/url" "strings" "testing" ) // ⌘K's groups and date row (level-up §4.6), from the outside. // paletteAnswer is /search/suggest's JSON, as the palette reads it. type paletteAnswer struct { Query string `json:"query"` Date *struct { Date, Label, Href string } `json:"date"` Pages, Text, Notes []struct { Label, Href, Kind, Via string Snippet *struct { Text string `json:"text"` Marks [][2]int `json:"marks"` } `json:"snippet"` } Create string `json:"create"` } func suggestAs(t *testing.T, rig *rig, who, query string) paletteAnswer { t.Helper() var answer paletteAnswer body := rig.page(t, who, "/search/suggest?q="+url.QueryEscape(query)) if err := json.Unmarshal([]byte(body), &answer); err != nil { t.Fatalf("%s: %v", body, err) } return answer } // Gate 1, extended (L6): "Your notes" only ever holds the viewer's own // pages. Bo's holds his scratch page and none of Ana's; Ana's holds hers // and not Bo's. Byte-identity across the worlds is boSees's. func TestGate1YourNotesAreOnlyYours(t *testing.T) { busy, _ := gate1World(t, true) bo := suggestAs(t, busy, Bo, "zebracorn") if len(bo.Notes) != 1 || bo.Notes[0].Label != "zebracorn musings" { t.Fatalf("Bo's notes: %+v", bo.Notes) } ana := suggestAs(t, busy, Ana, "zebracorn") var labels []string for _, hit := range ana.Notes { labels = append(labels, hit.Label) } got := strings.Join(labels, " | ") if strings.Contains(got, "musings") || !strings.Contains(got, "Thursday 12 November 2026") || !strings.Contains(got, "Zebracorn.") { t.Fatalf("Ana's notes: %q", got) } for _, hit := range append(bo.Pages, bo.Text...) { if hit.Kind != "page" { t.Fatalf("a private page among the team's: %+v", hit) } } if day := suggestAs(t, busy, Bo, "2026-11-12"); len(day.Notes) != 0 || day.Date == nil || day.Date.Href != "/d/2026-11-12" { t.Fatalf("Bo's 12 November: %+v", day) } } // The groups and the date row, as the palette receives them: "thursday" // is today, in the viewer's zone, and the text hit's mark is the match. func TestThePaletteAnswersInGroups(t *testing.T) { busy, _ := gate1World(t, true) today := suggestAs(t, busy, Ana, "thursday") if today.Date == nil || today.Date.Label != "Thursday, 12 November" || today.Date.Href != "/d/2026-11-12" { t.Fatalf("thursday: %+v", today.Date) } answer := suggestAs(t, busy, Bo, "zebracorn f") if len(answer.Text) == 0 || answer.Text[0].Snippet == nil { t.Fatalf("in the text: %+v", answer.Text) } snip := answer.Text[0].Snippet runes := []rune(snip.Text) for _, mark := range snip.Marks { word := strings.ToLower(string(runes[mark[0]:mark[1]])) if word != "zebracorn" && word != "f" { t.Fatalf("marked %q in %q", word, snip.Text) } } page := busy.page(t, Bo, "/search?q=zebracorn") for _, want := range []string{">In the text", "zebracorn", "Your notes", "Only you see these", "zebracorn musings"} { if !strings.Contains(page, want) { t.Fatalf("the search page has no %q", want) } } } // L7: ⌘K's GETs, the date row's included, write nothing: every table's // rows hash the same before and after. func TestL7ThePaletteWritesNothing(t *testing.T) { busy, _ := gate1World(t, true) before := tableRowsHash(t, busy) for _, query := range []string{"today", "tomorrow", "next friday", "12 nov", "2026-11-20", "zebracorn", "acme", "z"} { for _, who := range []string{Ana, Bo} { for _, path := range []string{"/search/suggest", "/search"} { target := path + "?q=" + url.QueryEscape(query) code := busy.get(t, who, target).Code if code != http.StatusOK { t.Fatalf("GET %s: %d", target, code) } } } } if after := tableRowsHash(t, busy); after != before { t.Fatal("a search GET changed the database's rows") } busy.post(t, Bo, "/scratch", url.Values{"body": {"a real write"}}) if tableRowsHash(t, busy) == before { t.Fatal("the hash is vacuous: a write did not change it") } } // tableRowsHash hashes every row of every table, not the file's bytes: // the WAL and checkpoints move those without a write. func tableRowsHash(t *testing.T, rig *rig) string { t.Helper() var tables []string err := rig.db.G.Raw("SELECT name FROM sqlite_master WHERE type = " + "'table' ORDER BY name").Scan(&tables).Error if err != nil || len(tables) == 0 { t.Fatalf("tables %v: %v", tables, err) } digest := sha256.New() for _, table := range tables { rows, err := rig.db.G.Raw(`SELECT * FROM "` + table + `"`).Rows() if err != nil { t.Fatal(err) } columns, _ := rows.Columns() values := make([]any, len(columns)) pointers := make([]any, len(columns)) for at := range values { pointers[at] = &values[at] } fmt.Fprintf(digest, "%s\n", table) for rows.Next() { if err := rows.Scan(pointers...); err != nil { t.Fatal(err) } fmt.Fprintf(digest, "%v\n", values) } rows.Close() } return fmt.Sprintf("%x", digest.Sum(nil)) }