oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "crypto/sha256"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
10 "testing"
11)
12
13// ⌘K's groups and date row (level-up §4.6), from the outside.
14
15// paletteAnswer is /search/suggest's JSON, as the palette reads it.
16type paletteAnswer struct {
17 Query string `json:"query"`
18 Date *struct {
19 Date, Label, Href string
20 } `json:"date"`
21 Pages, Text, Notes []struct {
22 Label, Href, Kind, Via string
23 Snippet *struct {
24 Text string `json:"text"`
25 Marks [][2]int `json:"marks"`
26 } `json:"snippet"`
27 }
28 Create string `json:"create"`
29}
30
31func suggestAs(t *testing.T, rig *rig, who, query string) paletteAnswer {
32 t.Helper()
33 var answer paletteAnswer
34 body := rig.page(t, who, "/search/suggest?q="+url.QueryEscape(query))
35 if err := json.Unmarshal([]byte(body), &answer); err != nil {
36 t.Fatalf("%s: %v", body, err)
37 }
38 return answer
39}
40
41// Gate 1, extended (L6): "Your notes" only ever holds the viewer's own
42// pages. Bo's holds his scratch page and none of Ana's; Ana's holds hers
43// and not Bo's. Byte-identity across the worlds is boSees's.
44func TestGate1YourNotesAreOnlyYours(t *testing.T) {
45 busy, _ := gate1World(t, true)
46 bo := suggestAs(t, busy, Bo, "zebracorn")
47 if len(bo.Notes) != 1 || bo.Notes[0].Label != "zebracorn musings" {
48 t.Fatalf("Bo's notes: %+v", bo.Notes)
49 }
50 ana := suggestAs(t, busy, Ana, "zebracorn")
51 var labels []string
52 for _, hit := range ana.Notes {
53 labels = append(labels, hit.Label)
54 }
55 got := strings.Join(labels, " | ")
56 if strings.Contains(got, "musings") ||
57 !strings.Contains(got, "Thursday 12 November 2026") ||
58 !strings.Contains(got, "Zebracorn.") {
59 t.Fatalf("Ana's notes: %q", got)
60 }
61 for _, hit := range append(bo.Pages, bo.Text...) {
62 if hit.Kind != "page" {
63 t.Fatalf("a private page among the team's: %+v", hit)
64 }
65 }
66 if day := suggestAs(t, busy, Bo, "2026-11-12"); len(day.Notes) != 0 ||
67 day.Date == nil || day.Date.Href != "/d/2026-11-12" {
68 t.Fatalf("Bo's 12 November: %+v", day)
69 }
70}
71
72// The groups and the date row, as the palette receives them: "thursday"
73// is today, in the viewer's zone, and the text hit's mark is the match.
74func TestThePaletteAnswersInGroups(t *testing.T) {
75 busy, _ := gate1World(t, true)
76 today := suggestAs(t, busy, Ana, "thursday")
77 if today.Date == nil || today.Date.Label != "Thursday, 12 November" ||
78 today.Date.Href != "/d/2026-11-12" {
79 t.Fatalf("thursday: %+v", today.Date)
80 }
81 answer := suggestAs(t, busy, Bo, "zebracorn f")
82 if len(answer.Text) == 0 || answer.Text[0].Snippet == nil {
83 t.Fatalf("in the text: %+v", answer.Text)
84 }
85 snip := answer.Text[0].Snippet
86 runes := []rune(snip.Text)
87 for _, mark := range snip.Marks {
88 word := strings.ToLower(string(runes[mark[0]:mark[1]]))
89 if word != "zebracorn" && word != "f" {
90 t.Fatalf("marked %q in %q", word, snip.Text)
91 }
92 }
93 page := busy.page(t, Bo, "/search?q=zebracorn")
94 for _, want := range []string{">In the text</h2>", "<mark>zebracorn</mark>",
95 "Your notes", "Only you see these", "zebracorn musings"} {
96 if !strings.Contains(page, want) {
97 t.Fatalf("the search page has no %q", want)
98 }
99 }
100}
101
102// L7: ⌘K's GETs, the date row's included, write nothing: every table's
103// rows hash the same before and after.
104func TestL7ThePaletteWritesNothing(t *testing.T) {
105 busy, _ := gate1World(t, true)
106 before := tableRowsHash(t, busy)
107 for _, query := range []string{"today", "tomorrow", "next friday",
108 "12 nov", "2026-11-20", "zebracorn", "acme", "z"} {
109 for _, who := range []string{Ana, Bo} {
110 for _, path := range []string{"/search/suggest", "/search"} {
111 target := path + "?q=" + url.QueryEscape(query)
112 code := busy.get(t, who, target).Code
113 if code != http.StatusOK {
114 t.Fatalf("GET %s: %d", target, code)
115 }
116 }
117 }
118 }
119 if after := tableRowsHash(t, busy); after != before {
120 t.Fatal("a search GET changed the database's rows")
121 }
122 busy.post(t, Bo, "/scratch", url.Values{"body": {"a real write"}})
123 if tableRowsHash(t, busy) == before {
124 t.Fatal("the hash is vacuous: a write did not change it")
125 }
126}
127
128// tableRowsHash hashes every row of every table, not the file's bytes:
129// the WAL and checkpoints move those without a write.
130func tableRowsHash(t *testing.T, rig *rig) string {
131 t.Helper()
132 var tables []string
133 err := rig.db.G.Raw("SELECT name FROM sqlite_master WHERE type = " +
134 "'table' ORDER BY name").Scan(&tables).Error
135 if err != nil || len(tables) == 0 {
136 t.Fatalf("tables %v: %v", tables, err)
137 }
138 digest := sha256.New()
139 for _, table := range tables {
140 rows, err := rig.db.G.Raw(`SELECT * FROM "` + table + `"`).Rows()
141 if err != nil {
142 t.Fatal(err)
143 }
144 columns, _ := rows.Columns()
145 values := make([]any, len(columns))
146 pointers := make([]any, len(columns))
147 for at := range values {
148 pointers[at] = &values[at]
149 }
150 fmt.Fprintf(digest, "%s\n", table)
151 for rows.Next() {
152 if err := rows.Scan(pointers...); err != nil {
153 t.Fatal(err)
154 }
155 fmt.Fprintf(digest, "%v\n", values)
156 }
157 rows.Close()
158 }
159 return fmt.Sprintf("%x", digest.Sum(nil))
160}
161