- Public pages follow "#b-…" and "#r-…" links like the page view does, and their block and record links copy the public address. The retry logic is shared as followLink (Packages/editor/src/hashlinks.ts). - Search indexes each database record as its own document (from the page's content_json), returned with a "record" field and opened beside its page. Filter-only listings stay pages only; removed records and trashed pages drop out. The index mapping version is bumped, so it rebuilds once. - Phone app: search lists records and opens them through a new "follow" bridge message. - Guide updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
230 lines
7.2 KiB
Go
230 lines
7.2 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package search
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"log/slog"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func newIndex(t *testing.T) *Index {
|
|
t.Helper()
|
|
x, err := OpenMemory(nil, slog.New(slog.NewTextHandler(io.Discard, nil)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = x.Close() })
|
|
return x
|
|
}
|
|
|
|
func TestQueryScopesAndRanks(t *testing.T) {
|
|
x := newIndex(t)
|
|
put := func(id, ws, space, title, text string) {
|
|
if err := x.Put(id, doc{Workspace: ws, Space: space, Title: title, Text: text}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
put("p1", "w1", "s1", "Deployment guide", "How we ship the server to production.")
|
|
put("p2", "w1", "s1", "Meeting notes", "We talked about the deployment window.")
|
|
put("p3", "w1", "s2", "Deployment secrets", "Private space.")
|
|
put("p4", "w2", "s9", "Deployment elsewhere", "Another workspace.")
|
|
|
|
ctx := context.Background()
|
|
hits, total, err := x.Query(ctx, "w1", []string{"s1"}, "deployment", Filter{}, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if total != 2 || len(hits) != 2 {
|
|
t.Fatalf("hits = %+v (total %d)", hits, total)
|
|
}
|
|
if hits[0].ID != "p1" {
|
|
t.Errorf("a title match must rank first: %+v", hits)
|
|
}
|
|
for _, h := range hits {
|
|
if h.ID == "p3" || h.ID == "p4" {
|
|
t.Errorf("%s is outside the caller's spaces", h.ID)
|
|
}
|
|
}
|
|
|
|
// Typing: the last word matches as a prefix.
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "meet", Filter{}, 10); len(hits) != 1 || hits[0].ID != "p2" {
|
|
t.Errorf("prefix = %+v", hits)
|
|
}
|
|
// One wrong letter is forgiven.
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "deploymant", Filter{}, 10); len(hits) == 0 {
|
|
t.Error("fuzzy match failed")
|
|
}
|
|
if hits, _, _ := x.Query(ctx, "w1", nil, "deployment", Filter{}, 10); len(hits) != 0 {
|
|
t.Error("no visible spaces must find nothing")
|
|
}
|
|
|
|
if err := x.Remove("p1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "guide", Filter{}, 10); len(hits) != 0 {
|
|
t.Errorf("removed page still found: %+v", hits)
|
|
}
|
|
}
|
|
|
|
func TestQueryFilters(t *testing.T) {
|
|
x := newIndex(t)
|
|
put := func(id, creator, editor, updated, title string) {
|
|
if err := x.Put(id, newDoc(pageRow{
|
|
ID: id, Workspace: "w1", Space: "s1", Title: title, Creator: creator, LastEditor: editor, Updated: updated,
|
|
})); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
put("old", "ann", "ann", "2026-01-10 09:00:00.000Z", "Roadmap draft")
|
|
put("mid", "ann", "bob", "2026-06-15 12:00:00.000Z", "Roadmap review")
|
|
put("new", "cat", "cat", "2026-10-30 18:30:00.000Z", "Roadmap final")
|
|
|
|
ctx := context.Background()
|
|
ids := func(f Filter, text string) []string {
|
|
t.Helper()
|
|
hits, _, err := x.Query(ctx, "w1", []string{"s1"}, text, f, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out []string
|
|
for _, h := range hits {
|
|
out = append(out, h.ID)
|
|
}
|
|
return out
|
|
}
|
|
day := func(s string) time.Time { v, _ := time.Parse(time.DateOnly, s); return v }
|
|
|
|
if got := ids(Filter{Author: "bob"}, "roadmap"); len(got) != 1 || got[0] != "mid" {
|
|
t.Errorf("last editor = %v", got)
|
|
}
|
|
if got := ids(Filter{Author: "ann"}, "roadmap"); len(got) != 2 {
|
|
t.Errorf("creator or editor = %v", got)
|
|
}
|
|
if got := ids(Filter{After: day("2026-06-01")}, "roadmap"); len(got) != 2 {
|
|
t.Errorf("after = %v", got)
|
|
}
|
|
if got := ids(Filter{After: day("2026-06-01"), Before: day("2026-07-01")}, "roadmap"); len(got) != 1 || got[0] != "mid" {
|
|
t.Errorf("range = %v", got)
|
|
}
|
|
// No words but a filter: newest first.
|
|
if got := ids(Filter{After: day("2026-01-01")}, ""); len(got) != 3 || got[0] != "new" || got[2] != "old" {
|
|
t.Errorf("filter only = %v", got)
|
|
}
|
|
if got := ids(Filter{}, ""); len(got) != 0 {
|
|
t.Errorf("nothing asked = %v", got)
|
|
}
|
|
}
|
|
|
|
func TestOpenRebuildsOldMapping(t *testing.T) {
|
|
dir := t.TempDir()
|
|
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
|
x, err := Open(dir, nil, log)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := x.Put("p1", doc{Workspace: "w1", Space: "s1", Title: "Kept?"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := x.idx.SetInternal([]byte(mappingKey), []byte("1")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_ = x.Close()
|
|
|
|
x, err = Open(dir, nil, log)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer x.Close()
|
|
if n, _ := x.Count(); n != 0 {
|
|
t.Errorf("an index with an old mapping must start over, has %d pages", n)
|
|
}
|
|
if v, _ := x.idx.GetInternal([]byte(mappingKey)); string(v) != mappingVersion {
|
|
t.Errorf("mapping version = %q", v)
|
|
}
|
|
}
|
|
|
|
func TestSnippet(t *testing.T) {
|
|
text := "Zażółć gęślą jaźń. The quick brown fox jumps over the lazy dog."
|
|
start := len("Zażółć gęślą jaźń. The quick ")
|
|
snippet, marks := Snippet(text, [][2]int{{start, start + len("brown")}}, 30)
|
|
if len(marks) != 1 {
|
|
t.Fatalf("marks = %v (%q)", marks, snippet)
|
|
}
|
|
runes := []rune(snippet)
|
|
if got := string(runes[marks[0][0]:marks[0][1]]); got != "brown" {
|
|
t.Errorf("mark covers %q in %q", got, snippet)
|
|
}
|
|
if s, m := Snippet("", nil, 10); s != "" || len(m) != 0 {
|
|
t.Error("empty text")
|
|
}
|
|
if s, _ := Snippet("short", nil, 100); s != "short" {
|
|
t.Errorf("short = %q", s)
|
|
}
|
|
}
|
|
|
|
func TestRecordsAreFoundOnTheirOwn(t *testing.T) {
|
|
x := newIndex(t)
|
|
content := func(rows string) json.RawMessage {
|
|
return json.RawMessage(`{"type":"doc","content":[{"type":"database","attrs":{"id":"d1","data":{
|
|
"columns":[{"id":"t","name":"Name","type":"title"},{"id":"n","name":"Notes","type":"text"}],
|
|
"rows":[` + rows + `]}}}]}`)
|
|
}
|
|
index := func(p pageRow) {
|
|
t.Helper()
|
|
b := x.idx.NewBatch()
|
|
if err := x.indexPage(b, p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := x.idx.Batch(b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
page := pageRow{
|
|
ID: "p1", Workspace: "w1", Space: "s1", Title: "Launch plan", Updated: "2026-11-01 10:00:00.000Z",
|
|
ContentText: "Launch plan\nInvite pilots ask the beta group\nOrder cake\n",
|
|
ContentJSON: content(`{"id":"r1","cells":{"t":"Invite pilots","n":"ask the beta group"}},{"id":"r2","cells":{"t":"Order cake"}}`),
|
|
}
|
|
index(page)
|
|
ctx := context.Background()
|
|
hits, _, err := x.Query(ctx, "w1", []string{"s1"}, "pilots", Filter{}, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var rec *Hit
|
|
for i, h := range hits {
|
|
if h.Record != nil {
|
|
rec = &hits[i]
|
|
}
|
|
}
|
|
if rec == nil || rec.ID != "p1" || rec.Title != "Launch plan" || *rec.Record != (RecordHit{Database: "d1", Row: "r1", Title: "Invite pilots"}) {
|
|
t.Fatalf("record hit = %+v in %+v", rec, hits)
|
|
}
|
|
if n, _ := x.Count(); n != 1 {
|
|
t.Errorf("count = %d, records must not count as pages", n)
|
|
}
|
|
// Listing by a filter alone lists pages only.
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "", Filter{Author: ""}.withAfter(), 10); len(hits) != 1 || hits[0].Record != nil {
|
|
t.Errorf("filter only = %+v", hits)
|
|
}
|
|
|
|
// A deleted record goes from the index with the next save.
|
|
page.ContentText = "Launch plan\nOrder cake\n"
|
|
page.ContentJSON = content(`{"id":"r2","cells":{"t":"Order cake"}}`)
|
|
index(page)
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "pilots", Filter{}, 10); len(hits) != 0 {
|
|
t.Errorf("deleted record still found: %+v", hits)
|
|
}
|
|
// A trashed page takes its records along.
|
|
page.DeletedAt = "2026-11-02 10:00:00.000Z"
|
|
index(page)
|
|
if hits, _, _ := x.Query(ctx, "w1", []string{"s1"}, "cake", Filter{}, 10); len(hits) != 0 {
|
|
t.Errorf("trashed page's record still found: %+v", hits)
|
|
}
|
|
}
|
|
|
|
func (f Filter) withAfter() Filter { f.After = time.Unix(1, 0); return f }
|