oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "strings"
5 "testing"
6)
7
8func labels(results Results) string {
9 var out []string
10 for _, hit := range results.All() {
11 out = append(out, hit.Label)
12 }
13 return strings.Join(out, " | ")
14}
15
16func search(t *testing.T, index *Index, scope Scope, query string) Results {
17 t.Helper()
18 results, err := index.Search(ctx(), scope, query, 20)
19 if err != nil {
20 t.Fatal(err)
21 }
22 return results
23}
24
25func TestTitlesRankFirstThenBodies(t *testing.T) {
26 store := testStore(t)
27 index := NewIndex(store)
28 mustCreate(t, store, "Pilot notes", "")
29 mustCreate(t, store, "Autopilot", "")
30 mustCreate(t, store, "Q1 plan", "The pilot pilot pilot starts.")
31 mustCreate(t, store, "Budget", "A pilot is mentioned once, among "+
32 "many other words about money and plans and dates.")
33 mustCreate(t, store, "Pilot", "")
34 got := labels(search(t, index, Scope{}, "pilot"))
35 want := "Pilot | Pilot notes | Q1 plan | Budget"
36 if got != want {
37 t.Fatalf("got %q\nwant %q", got, want)
38 }
39}
40
41func TestAnAliasFindsItsPageAndSaysSo(t *testing.T) {
42 store := testStore(t)
43 index := NewIndex(store)
44 page := mustCreate(t, store, "Acme", "")
45 store.Rename(ctx(), ana, page.ID, "Brightwater")
46 results := search(t, index, Scope{}, "acm")
47 if len(results.All()) != 1 || results.Pages[0].Label != "Brightwater" ||
48 results.Pages[0].Via != "Acme" {
49 t.Fatalf("hits %+v", results.All())
50 }
51}
52
53func TestEveryWordMustMatchTheLastAsAPrefix(t *testing.T) {
54 store := testStore(t)
55 index := NewIndex(store)
56 mustCreate(t, store, "One", "renewal in march")
57 mustCreate(t, store, "Two", "renewal in april")
58 if got := labels(search(t, index, Scope{}, "renewal mar")); got != "One" {
59 t.Fatalf("got %q", got)
60 }
61}
62
63func TestOneLetterSearchesTitlesOnly(t *testing.T) {
64 store := testStore(t)
65 index := NewIndex(store)
66 mustCreate(t, store, "Quarterly", "")
67 mustCreate(t, store, "Other", "quiet quokka")
68 if got := labels(search(t, index, Scope{}, "q")); got != "Quarterly" {
69 t.Fatalf("got %q", got)
70 }
71}
72
73func TestPrivatePagesAreSearchedByTheirOwnerOnly(t *testing.T) {
74 store := testStore(t)
75 index := NewIndex(store)
76 store.SaveDaily(ctx(), ana, "2026-11-12", 0, "zebracorn sighting")
77 store.CreateScratch(ctx(), ana, "zebracorn draft")
78 anaHits := labels(search(t, index, scopeOf(t, store, ana), "zebracorn"))
79 if !strings.Contains(anaHits, "Thursday 12 November 2026") ||
80 !strings.Contains(anaHits, "zebracorn draft") {
81 t.Fatalf("Ana's own: %q", anaHits)
82 }
83 store.SetTimeZone(ctx(), bo, "UTC")
84 if got := search(t, index, scopeOf(t, store, bo), "zebracorn"); len(
85 got.All()) != 0 {
86 t.Fatalf("Bo found %q", labels(got))
87 }
88 if got := labels(search(t, index, scopeOf(t, store, ana),
89 "2026-11")); got != "Thursday 12 November 2026" {
90 t.Fatalf("a date prefix: %q", got)
91 }
92}
93
94func TestCreateIsOfferedOnlyForAFreeTitle(t *testing.T) {
95 store := testStore(t)
96 index := NewIndex(store)
97 mustCreate(t, store, "Acme", "")
98 for query, want := range map[string]string{"acme": "",
99 " New idea ": "New idea", "2026-11-12": "", "a|b": ""} {
100 if got := search(t, index, Scope{}, query).Create; got != want {
101 t.Errorf("%q offers %q, want %q", query, got, want)
102 }
103 }
104}
105
106func TestTheIndexFollowsEditsDeletesAndRenames(t *testing.T) {
107 store := testStore(t)
108 index := NewIndex(store)
109 page := mustCreate(t, store, "Acme", "alpha")
110 search(t, index, Scope{}, "alpha")
111 store.Commit(ctx(), bo, page.ID, page.Revision, "beta")
112 if len(search(t, index, Scope{}, "alpha").All()) != 0 ||
113 len(search(t, index, Scope{}, "beta").All()) != 1 {
114 t.Fatal("an edit did not reach the index")
115 }
116 store.Rename(ctx(), bo, page.ID, "Zenith")
117 if labels(search(t, index, Scope{}, "zen")) != "Zenith" {
118 t.Fatal("a rename did not reach the titles")
119 }
120 store.Delete(ctx(), bo, page.ID)
121 if len(search(t, index, Scope{}, "beta").All()) != 0 {
122 t.Fatal("a deleted page is still found")
123 }
124}
125
126func TestSuggestOffersTitlesAndOwnPendingTexts(t *testing.T) {
127 store := testStore(t)
128 index := NewIndex(store)
129 mustCreate(t, store, "Halden", "see [[Halcyon days]]")
130 store.CreateScratch(ctx(), ana, "[[Halberd plans]]")
131 anaScope := scopeOf(t, store, ana)
132 found, _ := index.Suggest(ctx(), anaScope, "hal", 8)
133 if len(found) != 3 || found[0].Text != "Halden" || found[0].Pending ||
134 found[1].Text != "Halcyon days" || found[2].Text != "Halberd plans" ||
135 !found[2].Pending {
136 t.Fatalf("Ana's suggestions: %+v", found)
137 }
138 boFound, _ := index.Suggest(ctx(), Scope{}, "hal", 8)
139 for _, suggestion := range boFound {
140 if suggestion.Text == "Halberd plans" {
141 t.Fatal("Bo is offered Ana's private pending link")
142 }
143 }
144}
145
146// Review M3: a reader never pays for someone else's write. The writer's
147// own request catches the index up after its commit, so when Bo reads,
148// Ana's write is already in, and Bo's read does no catch-up at all.
149func TestTheWriterCatchesTheIndexUpNotTheReader(t *testing.T) {
150 store := testStore(t)
151 index := NewIndex(store)
152 mustCreate(t, store, "Acme", "alpha")
153 search(t, index, Scope{}, "alpha")
154 before := index.CatchUps()
155 if _, err := store.CreateScratch(ctx(), ana, "private alpha"); err != nil {
156 t.Fatal(err)
157 }
158 mustCreate(t, store, "Beta", "alpha beta")
159 afterWrites := index.CatchUps()
160 if afterWrites != before+2 {
161 t.Fatalf("the writers caught the index up %d times, want 2",
162 afterWrites-before)
163 }
164 if got := labels(search(t, index, Scope{}, "beta")); got != "Beta" {
165 t.Fatalf("Bo's read does not see the write: %q", got)
166 }
167 search(t, index, scopeOf(t, store, bo), "alpha")
168 if index.CatchUps() != afterWrites {
169 t.Fatal("a reader caught the index up")
170 }
171}
172
173// Review L6: a query of hundreds of words is cut to a bounded few, and
174// still answers by its first words.
175func TestALongQueryIsBounded(t *testing.T) {
176 store := testStore(t)
177 index := NewIndex(store)
178 mustCreate(t, store, "Acme", "alpha beta")
179 query := "alpha beta" + strings.Repeat(" alpha", 300)
180 if got := labels(search(t, index, Scope{}, query)); got != "Acme" {
181 t.Fatalf("a long query: %q", got)
182 }
183 if words := queryWords(query); len(words) != maxQueryWords {
184 t.Fatalf("%d words kept", len(words))
185 }
186}
187