- Search can be narrowed by space, author and last change; the index records a mapping version and rebuilds when it changes. - Editors can pin up to 12 pages to a space's home. - Find and replace in the editor (Ctrl+F), with match case. - Pages can be set to full width and a serif or mono font. - Tailwind now scans the editor package, so its classes are generated. - Fix golangci-lint findings, including push device names that were saved before being trimmed and capped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
167 lines
4.9 KiB
Go
167 lines
4.9 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package search
|
|
|
|
import (
|
|
"context"
|
|
"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)
|
|
}
|
|
}
|