| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func groupLabels(hits []Hit) string { |
| 9 | var out []string |
| 10 | for _, hit := range hits { |
| 11 | out = append(out, hit.Label) |
| 12 | } |
| 13 | return strings.Join(out, " | ") |
| 14 | } |
| 15 | |
| 16 | // ⌘K's groups (level-up §4.6): team titles and aliases under Pages, team |
| 17 | // pages found by their words under In the text, and the viewer's own |
| 18 | // daily and scratch pages under Your notes, each page once. |
| 19 | func TestSearchAnswersInGroups(t *testing.T) { |
| 20 | store := testStore(t) |
| 21 | index := NewIndex(store) |
| 22 | mustCreate(t, store, "Brightwater Labs", "") |
| 23 | old := mustCreate(t, store, "Bright ideas", "") |
| 24 | store.Rename(ctx(), ana, old.ID, "Halden Robotics") |
| 25 | mustCreate(t, store, "Q4 renewals", "The biggest is Brightwater Labs.") |
| 26 | mustCreate(t, store, "Pilot", "Brightwater pilot.") |
| 27 | store.SaveDaily(ctx(), ana, "2026-11-12", 0, "Tom: Brightwater signs.") |
| 28 | store.CreateScratch(ctx(), ana, "brightwater musings") |
| 29 | store.CreateScratch(ctx(), bo, "brightwater, as Bo has it") |
| 30 | got := search(t, index, scopeOf(t, store, ana), "brigh") |
| 31 | if groupLabels(got.Pages) != "Brightwater Labs | Halden Robotics" || |
| 32 | got.Pages[1].Via != "Bright ideas" || got.Pages[0].Via != "" { |
| 33 | t.Fatalf("pages: %+v", got.Pages) |
| 34 | } |
| 35 | if groupLabels(got.Text) != "Pilot | Q4 renewals" { |
| 36 | t.Fatalf("in the text: %q", groupLabels(got.Text)) |
| 37 | } |
| 38 | notes := groupLabels(got.Notes) |
| 39 | if !strings.Contains(notes, "Thursday 12 November 2026") || |
| 40 | !strings.Contains(notes, "brightwater musings") || |
| 41 | strings.Contains(notes, "Bo") || len(got.Notes) != 2 { |
| 42 | t.Fatalf("Ana's notes: %q", notes) |
| 43 | } |
| 44 | for _, hit := range got.Notes { |
| 45 | if hit.Kind == KindPage || hit.Snippet == nil { |
| 46 | t.Fatalf("a note that is not Ana's own, or unmarked: %+v", hit) |
| 47 | } |
| 48 | } |
| 49 | if got.Create != "brigh" || got.Count() != 6 { |
| 50 | t.Fatalf("create %q, count %d", got.Create, got.Count()) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Each group holds at most the limit, so one group's many hits do not |
| 55 | // crowd out the others. |
| 56 | func TestEachGroupHasItsOwnLimit(t *testing.T) { |
| 57 | store := testStore(t) |
| 58 | index := NewIndex(store) |
| 59 | for _, title := range []string{"Kelp one", "Kelp two", "Kelp three"} { |
| 60 | mustCreate(t, store, title, "") |
| 61 | } |
| 62 | for _, title := range []string{"Alpha", "Beta", "Gamma"} { |
| 63 | mustCreate(t, store, title, "kelp farming") |
| 64 | } |
| 65 | for range 3 { |
| 66 | store.CreateScratch(ctx(), ana, "kelp notes") |
| 67 | } |
| 68 | results, err := index.Search(ctx(), scopeOf(t, store, ana), "kelp", 2) |
| 69 | if err != nil || len(results.Pages) != 2 || len(results.Text) != 2 || |
| 70 | len(results.Notes) != 2 { |
| 71 | t.Fatalf("%+v %v", results, err) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // The date row takes the place of the note for that day: one row a day. |
| 76 | func TestTheDateRowReplacesThatDaysNote(t *testing.T) { |
| 77 | store := testStore(t) |
| 78 | index := NewIndex(store) |
| 79 | store.SaveDaily(ctx(), ana, "2026-11-12", 0, "written") |
| 80 | results := search(t, index, scopeOf(t, store, ana), "2026-11-12") |
| 81 | if len(results.Notes) != 1 { |
| 82 | t.Fatalf("the day is not among Ana's notes: %+v", results.Notes) |
| 83 | } |
| 84 | results.withDate(dateRow(nil, "2026-11-12", "2026-11-14")) |
| 85 | if len(results.Notes) != 0 || results.Date == nil || |
| 86 | results.Count() != 1 { |
| 87 | t.Fatalf("%+v", results) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // The snippet marks what the query typed of each word, in UTF-16 units, |
| 92 | // and the parts a template renders join back to the text. |
| 93 | func TestTheSnippetMarksTheMatch(t *testing.T) { |
| 94 | store := testStore(t) |
| 95 | index := NewIndex(store) |
| 96 | mustCreate(t, store, "Zed", "😀 Ünïcode ünion and unity, unbound.") |
| 97 | hit := search(t, index, Scope{}, "un").Text[0] |
| 98 | snip := hit.Snippet |
| 99 | if snip.Text != "😀 Ünïcode ünion and unity, unbound." { |
| 100 | t.Fatalf("text %q", snip.Text) |
| 101 | } |
| 102 | var marked []string |
| 103 | for _, part := range snip.Parts() { |
| 104 | if part.Marked { |
| 105 | marked = append(marked, part.Text) |
| 106 | } |
| 107 | } |
| 108 | if strings.Join(marked, ",") != "Ün,ün,un,un" { |
| 109 | t.Fatalf("marked %q, marks %v", marked, snip.Marks) |
| 110 | } |
| 111 | // The emoji is two UTF-16 units, so "Ün" starts at 3. |
| 112 | if snip.Marks[0] != [2]int{3, 5} { |
| 113 | t.Fatalf("marks %v", snip.Marks) |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // A long block is cut around the match, and the marks follow the cut. |
| 118 | func TestALongSnippetIsCutAroundTheMatch(t *testing.T) { |
| 119 | store := testStore(t) |
| 120 | index := NewIndex(store) |
| 121 | body := strings.Repeat("filler ", 60) + "zebracorn " + |
| 122 | strings.Repeat("after ", 60) |
| 123 | mustCreate(t, store, "Long", body) |
| 124 | snip := search(t, index, Scope{}, "zebra").Text[0].Snippet |
| 125 | runes := []rune(snip.Text) |
| 126 | if runes[0] != '…' || runes[len(runes)-1] != '…' || |
| 127 | len(runes) != snippetWidth+2 || len(snip.Marks) != 1 { |
| 128 | t.Fatalf("%q %v", snip.Text, snip.Marks) |
| 129 | } |
| 130 | at := snip.Marks[0] |
| 131 | if string(runes[at[0]:at[1]]) != "zebra" { |
| 132 | t.Fatalf("marked %q", string(runes[at[0]:at[1]])) |
| 133 | } |
| 134 | } |
| 135 | |