CI / go (Media) (push) Successful in 13m40s
CI / go (Server) (push) Failing after 23m20s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 10m35s
Docs / build (push) Canceled after 0s
Docs / deploy (push) Canceled after 0s
CI / desktop (ubuntu-latest) (push) Failing after 3h4m42s
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
CI / mobile (push) Failing after 9m19s
CI / docker (push) Successful in 2h2m57s
- Columns: /columns puts 2 to 4 columns side by side, with widths dragged between them; they stack on phones. HTML export and import keep them (also Notion column lists and Confluence page layouts). - Suggesting mode: edits become suggested insertions and deletions that editors accept or reject one by one or all at once. Text only. - Verified pages: editors verify a page for a while or without end; the verifier is notified when it runs out (migration 1764700000). - Scanned PDFs attached to pages are read by AI for search where a workspace turns it on. - The server writes error messages, emails, digests and phone notifications in English, Polish, German, French and Spanish; the apps send their language with each request. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
233 lines
7.4 KiB
Go
233 lines
7.4 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package search
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"log/slog"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/go-pdf/fpdf"
|
|
|
|
"docwell/server/internal/convert"
|
|
"docwell/server/internal/store/pocketbase"
|
|
)
|
|
|
|
// fakeFiles is a PocketBase with pages, attachments and their files.
|
|
type fakeFiles struct {
|
|
mu sync.Mutex
|
|
pages map[string]pageRow
|
|
atts []attachmentRow
|
|
files map[string][]byte // attachment id -> bytes
|
|
downloads int
|
|
}
|
|
|
|
func (f *fakeFiles) server(t *testing.T) *pocketbase.Client {
|
|
t.Helper()
|
|
reply := func(w http.ResponseWriter, v any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(v)
|
|
}
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("POST /api/collections/_superusers/auth-with-password", func(w http.ResponseWriter, r *http.Request) {
|
|
reply(w, map[string]string{"token": "su"})
|
|
})
|
|
mux.HandleFunc("GET /api/collections/pages/records", func(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
items := []pageRow{}
|
|
for _, p := range f.pages {
|
|
items = append(items, p)
|
|
}
|
|
reply(w, map[string]any{"items": items, "totalItems": len(items), "totalPages": 1})
|
|
})
|
|
mux.HandleFunc("GET /api/collections/pages/records/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
p, ok := f.pages[r.PathValue("id")]
|
|
if !ok {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
reply(w, map[string]any{"message": "not found"})
|
|
return
|
|
}
|
|
reply(w, p)
|
|
})
|
|
mux.HandleFunc("GET /api/collections/attachments/records", func(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
reply(w, map[string]any{"items": f.atts, "totalItems": len(f.atts), "totalPages": 1})
|
|
})
|
|
mux.HandleFunc("GET /api/collections/attachments/records/{id}", func(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
for _, a := range f.atts {
|
|
if a.ID == r.PathValue("id") {
|
|
reply(w, a)
|
|
return
|
|
}
|
|
}
|
|
w.WriteHeader(http.StatusNotFound)
|
|
reply(w, map[string]any{"message": "not found"})
|
|
})
|
|
mux.HandleFunc("POST /api/files/token", func(w http.ResponseWriter, r *http.Request) {
|
|
reply(w, map[string]string{"token": "ft"})
|
|
})
|
|
mux.HandleFunc("GET /api/files/attachments/{id}/{file}", func(w http.ResponseWriter, r *http.Request) {
|
|
f.mu.Lock()
|
|
defer f.mu.Unlock()
|
|
f.downloads++
|
|
_, _ = w.Write(f.files[r.PathValue("id")])
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
t.Cleanup(srv.Close)
|
|
return pocketbase.New(srv.URL, "su@x.test", "pw")
|
|
}
|
|
|
|
func pdfWith(t *testing.T, text string) []byte {
|
|
t.Helper()
|
|
doc := fpdf.New("P", "mm", "A4", "")
|
|
doc.AddPage()
|
|
doc.SetFont("Helvetica", "", 12)
|
|
doc.Cell(40, 10, text)
|
|
var buf bytes.Buffer
|
|
if err := doc.Output(&buf); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return buf.Bytes()
|
|
}
|
|
|
|
// pageLinking is a page whose content links to the attachments ids.
|
|
func pageLinking(id, title string, ids ...string) pageRow {
|
|
var nodes []string
|
|
for _, a := range ids {
|
|
nodes = append(nodes, `{"type":"fileAttachment","attrs":{"src":"/api/v1/files/`+a+`","name":"f"}}`)
|
|
}
|
|
return pageRow{
|
|
ID: id, Workspace: "w1", Space: "s1", Title: title, ContentText: "Some notes", Updated: "2026-11-01 10:00:00.000Z",
|
|
ContentJSON: json.RawMessage(`{"type":"doc","content":[` + strings.Join(nodes, ",") + `]}`),
|
|
}
|
|
}
|
|
|
|
func TestAttachmentText(t *testing.T) {
|
|
word, err := convert.DOCX("Plan", convert.Node{Type: "doc", Content: []convert.Node{
|
|
{Type: "paragraph", Content: []convert.Node{{Type: "text", Text: "Quarterly budget forecast for marketing"}}},
|
|
}}, convert.DOCXOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f := &fakeFiles{
|
|
pages: map[string]pageRow{
|
|
"page00000000001": pageLinking("page00000000001", "Finance", "pdf000000000001", "doc000000000001", "img000000000001"),
|
|
// Links to nothing: its attachment is uploaded but not on the page.
|
|
"page00000000002": pageLinking("page00000000002", "Drafts"),
|
|
},
|
|
atts: []attachmentRow{
|
|
{ID: "pdf000000000001", Page: "page00000000001", File: "policy_abc.pdf", Name: "Expense policy.pdf", Size: 2000, Updated: "2026-11-01 10:00:00.000Z"},
|
|
{ID: "doc000000000001", Page: "page00000000001", File: "plan_abc.docx", Name: "Plan.docx", Size: 2000, Updated: "2026-11-01 10:00:01.000Z"},
|
|
{ID: "img000000000001", Page: "page00000000001", File: "pic_abc.png", Name: "pic.png", Size: 10, Updated: "2026-11-01 10:00:02.000Z"},
|
|
{ID: "orphan000000001", Page: "page00000000002", File: "orphan_abc.txt", Name: "orphan.txt", Size: 10, Updated: "2026-11-01 10:00:03.000Z"},
|
|
},
|
|
files: map[string][]byte{
|
|
"pdf000000000001": pdfWith(t, "Receipts are reimbursed within thirty days"),
|
|
"doc000000000001": word,
|
|
"img000000000001": []byte("PNG"),
|
|
"orphan000000001": []byte("Zanzibar travel notes"),
|
|
},
|
|
}
|
|
x, err := OpenMemory(f.server(t), slog.New(slog.NewTextHandler(io.Discard, nil)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = x.Close() })
|
|
ctx := context.Background()
|
|
if err := x.CatchUp(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := x.CatchUpFiles(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
query := func(text string) []Hit {
|
|
t.Helper()
|
|
hits, _, err := x.Query(ctx, "w1", []string{"s1"}, text, Filter{}, 10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return hits
|
|
}
|
|
|
|
hits := query("reimbursed")
|
|
if len(hits) != 1 || hits[0].ID != "page00000000001" || hits[0].Title != "Finance" ||
|
|
hits[0].File == nil || hits[0].File.ID != "pdf000000000001" || hits[0].File.Name != "Expense policy.pdf" {
|
|
t.Fatalf("pdf hit %+v", hits)
|
|
}
|
|
if !strings.Contains(hits[0].Snippet, "reimbursed") || len(hits[0].Highlights) == 0 {
|
|
t.Errorf("snippet %q %v", hits[0].Snippet, hits[0].Highlights)
|
|
}
|
|
if hits := query("forecast"); len(hits) != 1 || hits[0].File == nil || hits[0].File.Name != "Plan.docx" {
|
|
t.Errorf("word hit %+v", hits)
|
|
}
|
|
// A file name is searchable too.
|
|
if hits := query("expense"); len(hits) != 1 || hits[0].File == nil {
|
|
t.Errorf("name hit %+v", hits)
|
|
}
|
|
// Uploaded but not linked from its page: not found.
|
|
if hits := query("zanzibar"); len(hits) != 0 {
|
|
t.Errorf("unlinked file found: %+v", hits)
|
|
}
|
|
// Pages are counted without their files.
|
|
if n, _ := x.Count(); n != 2 {
|
|
t.Errorf("count %d", n)
|
|
}
|
|
|
|
// Files are read once: another pass downloads nothing.
|
|
before := f.downloads
|
|
if err := x.CatchUpFiles(ctx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if f.downloads != before {
|
|
t.Errorf("read again: %d downloads", f.downloads-before)
|
|
}
|
|
if before != 3 {
|
|
t.Errorf("downloads %d, want 3 (the picture is not read)", before)
|
|
}
|
|
|
|
// Taking the PDF out of the page drops it at the page's next indexing.
|
|
f.mu.Lock()
|
|
f.pages["page00000000001"] = pageLinking("page00000000001", "Finance", "doc000000000001")
|
|
p := f.pages["page00000000001"]
|
|
f.mu.Unlock()
|
|
batch := x.idx.NewBatch()
|
|
if err := x.indexPage(batch, p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := x.idx.Batch(batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hits := query("reimbursed"); len(hits) != 0 {
|
|
t.Errorf("unlinked pdf still found: %+v", hits)
|
|
}
|
|
if hits := query("forecast"); len(hits) != 1 {
|
|
t.Errorf("word file lost: %+v", hits)
|
|
}
|
|
|
|
// A trashed page takes its files with it.
|
|
p.DeletedAt = "2026-11-02 00:00:00.000Z"
|
|
batch = x.idx.NewBatch()
|
|
if err := x.indexPage(batch, p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := x.idx.Batch(batch); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if hits := query("forecast"); len(hits) != 0 {
|
|
t.Errorf("trashed page's file found: %+v", hits)
|
|
}
|
|
}
|