Files
Docwell/Server/internal/search/scans_test.go
T
tajniak81andClaude Opus 5.5 c98b127f1e
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
Add columns, suggesting mode, verified pages, scanned PDF search and server translations
- 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>
2026-11-17 08:38:08 +01:00

153 lines
3.9 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package search
import (
"bytes"
"context"
"errors"
"io"
"log/slog"
"sync"
"testing"
"github.com/go-pdf/fpdf"
)
var errOff = errors.New("off")
// fakeScanner reads every scan as the same text, for workspaces turned on.
type fakeScanner struct {
mu sync.Mutex
on map[string]bool
fail error
reads int
}
func (s *fakeScanner) ReadsScans(_ context.Context, ws string) bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.on[ws]
}
func (s *fakeScanner) ReadScan(_ context.Context, ws string, _ []byte) (string, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.reads++
if !s.on[ws] {
return "", errOff
}
if s.fail != nil {
return "", s.fail
}
return "Signed lease agreement for the warehouse", nil
}
// scannedPDF is a PDF with a drawing and no text layer.
func scannedPDF(t *testing.T) []byte {
t.Helper()
doc := fpdf.New("P", "mm", "A4", "")
doc.AddPage()
doc.Rect(20, 20, 80, 40, "F")
var buf bytes.Buffer
if err := doc.Output(&buf); err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
func TestScannedPDFsWaitForAI(t *testing.T) {
f := &fakeFiles{
pages: map[string]pageRow{"page00000000001": pageLinking("page00000000001", "Leases", "scan00000000001")},
atts: []attachmentRow{
{ID: "scan00000000001", Page: "page00000000001", File: "scan_abc.pdf", Name: "Lease.pdf", Size: 2000, Updated: "2026-11-01 10:00:00.000Z"},
},
files: map[string][]byte{"scan00000000001": scannedPDF(t)},
}
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)
}
if n := x.ScansWaiting(); n != 1 {
t.Fatalf("waiting %d, want 1", n)
}
find := func() []Hit {
t.Helper()
hits, _, err := x.Query(ctx, "w1", []string{"s1"}, "warehouse", Filter{}, 10)
if err != nil {
t.Fatal(err)
}
return hits
}
// Without a reader, or with the workspace turned off, the scan waits.
if err := x.ReadScans(ctx); err != nil {
t.Fatal(err)
}
sc := &fakeScanner{on: map[string]bool{}}
x.UseScans(sc, errOff)
if err := x.ReadScans(ctx); err != nil {
t.Fatal(err)
}
if sc.reads != 0 || x.ScansWaiting() != 1 || len(find()) != 0 {
t.Fatalf("read while off: reads %d, waiting %d", sc.reads, x.ScansWaiting())
}
// Turned on: read once, indexed under its page, off the queue.
sc.on["w1"] = true
if err := x.ReadScans(ctx); err != nil {
t.Fatal(err)
}
hits := find()
if len(hits) != 1 || hits[0].File == nil || hits[0].File.Name != "Lease.pdf" {
t.Fatalf("hits %+v", hits)
}
if x.ScansWaiting() != 0 || sc.reads != 1 {
t.Fatalf("waiting %d, reads %d", x.ScansWaiting(), sc.reads)
}
// A later file pass does not queue it again.
if err := x.CatchUpFiles(ctx); err != nil {
t.Fatal(err)
}
if x.ScansWaiting() != 0 {
t.Fatal("queued again")
}
}
func TestScannedPDFsAreGivenUpAfterFailures(t *testing.T) {
f := &fakeFiles{
pages: map[string]pageRow{"page00000000001": pageLinking("page00000000001", "Leases", "scan00000000001")},
atts: []attachmentRow{
{ID: "scan00000000001", Page: "page00000000001", File: "scan_abc.pdf", Name: "Lease.pdf", Size: 2000, Updated: "2026-11-01 10:00:00.000Z"},
},
files: map[string][]byte{"scan00000000001": scannedPDF(t)},
}
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.CatchUpFiles(ctx); err != nil {
t.Fatal(err)
}
sc := &fakeScanner{on: map[string]bool{"w1": true}, fail: errors.New("provider down")}
x.UseScans(sc, errOff)
for i := 1; i <= maxScanTries; i++ {
if err := x.ReadScans(ctx); err != nil {
t.Fatal(err)
}
}
if sc.reads != maxScanTries || x.ScansWaiting() != 0 {
t.Fatalf("reads %d, waiting %d", sc.reads, x.ScansWaiting())
}
}