fc9454832f
The index had no trace of anything I have read: BookOrbit holds 234
annotations across 511 books and none of it reached knox, so recall about books
was empty.
Entries are chapters, not highlights. A book produces a handful of dense,
readable entries instead of hundreds of fragments, which is what a reader
actually asks for ("what did I highlight in this chapter").
Uses the annotations export endpoint rather than the Postgres schema: it is the
contract BookOrbit maintains, and it already carries book, chapter, note, style
and origin. Login is one POST per sweep (the endpoint is throttled at 3/min).
Fingerprint is (book, chapter); the content hash is the rendered body, so a new
highlight updates the entry and appends a log version rather than creating a
second copy of the chapter. The watcher deliberately has no mtime-style skip:
a highlight edited in place keeps the same latest timestamp, and skipping on
that would hide the edit.
Wired as 'knox bookorbit', into the full 'knox ingest' sweep, and a 30-minute
ticker inside LocalSourcesEnabled (the server node replicates only).
179 lines
6.0 KiB
Go
179 lines
6.0 KiB
Go
package ingest
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// bookorbitServer stands in for the BookOrbit API: one login endpoint and one
|
|
// export endpoint, as the ingester uses them.
|
|
func bookorbitServer(t *testing.T, rows *[]boAnnotation) *httptest.Server {
|
|
t.Helper()
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/v1/auth/login", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
var body struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
if body.Username != "reader" || body.Password != "secret" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
_, _ = w.Write([]byte(`{"message":"bad credentials"}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"accessToken":"token-123","accessTokenExpiresAt":"2026-01-01T00:15:00Z"}`))
|
|
})
|
|
mux.HandleFunc("/api/v1/annotations/export", func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Bearer token-123" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
_ = json.NewEncoder(w).Encode(*rows)
|
|
})
|
|
return httptest.NewServer(mux)
|
|
}
|
|
|
|
func newTestBookOrbit(srv *httptest.Server) *BookOrbitIngester {
|
|
return &BookOrbitIngester{
|
|
Version: "bookorbit/v1",
|
|
BaseURL: srv.URL,
|
|
Username: "reader",
|
|
Password: "secret",
|
|
client: srv.Client(),
|
|
}
|
|
}
|
|
|
|
func TestBookOrbitGroupsByChapter(t *testing.T) {
|
|
rows := []boAnnotation{
|
|
{ID: 1, BookID: 10, BookTitle: "The Timeless Way", ChapterTitle: "Patterns", Text: "Each pattern describes a problem", Origin: "web", Style: "highlight", Color: "yellow", HighlightedAt: "2026-01-02T10:00:00.000Z"},
|
|
{ID: 2, BookID: 10, BookTitle: "The Timeless Way", ChapterTitle: "Patterns", Text: "which occurs over and over", Note: "the same problem, not the same solution", Origin: "kobo", Style: "highlight", Color: "yellow", HighlightedAt: "2026-01-03T09:00:00.000Z"},
|
|
{ID: 3, BookID: 10, BookTitle: "The Timeless Way", ChapterTitle: "Quality", Text: "There is one timeless way", Origin: "web", Style: "highlight", Color: "yellow", HighlightedAt: "2026-01-04T09:00:00.000Z"},
|
|
{ID: 4, BookID: 20, BookTitle: "Another Book", Text: "No chapter here", Origin: "web", Style: "highlight", Color: "yellow", HighlightedAt: "2026-02-01T09:00:00.000Z"},
|
|
}
|
|
srv := bookorbitServer(t, &rows)
|
|
defer srv.Close()
|
|
|
|
results, err := newTestBookOrbit(srv).IngestAll()
|
|
if err != nil {
|
|
t.Fatalf("ingest: %v", err)
|
|
}
|
|
if len(results) != 3 {
|
|
t.Fatalf("results = %d, want 3 chapters (2 in book 10, 1 book-level in book 20)", len(results))
|
|
}
|
|
|
|
byTitle := map[string]*IngestResult{}
|
|
for _, r := range results {
|
|
byTitle[r.Title] = r
|
|
}
|
|
|
|
patterns, ok := byTitle["The Timeless Way — Patterns"]
|
|
if !ok {
|
|
t.Fatalf("missing chapter entry; got %v", keysOf(byTitle))
|
|
}
|
|
if !strings.Contains(patterns.Content, "Each pattern describes a problem") ||
|
|
!strings.Contains(patterns.Content, "Note: the same problem, not the same solution") {
|
|
t.Fatalf("chapter content missing quote or note:\n%s", patterns.Content)
|
|
}
|
|
if patterns.CreatedAt != "2026-01-03T09:00:00.000Z" {
|
|
t.Fatalf("CreatedAt = %q, want the latest highlight in the chapter", patterns.CreatedAt)
|
|
}
|
|
if patterns.Provenance["count"] != 2 {
|
|
t.Fatalf("provenance count = %v, want 2", patterns.Provenance["count"])
|
|
}
|
|
|
|
// A book with no chapters still produces one entry, titled by the book.
|
|
if _, ok := byTitle["Another Book"]; !ok {
|
|
t.Fatalf("missing book-level entry; got %v", keysOf(byTitle))
|
|
}
|
|
}
|
|
|
|
func TestBookOrbitFingerprintAndHashAreStable(t *testing.T) {
|
|
rows := []boAnnotation{
|
|
{ID: 1, BookID: 10, BookTitle: "Book", ChapterTitle: "One", Text: "first", HighlightedAt: "2026-01-01T00:00:00Z"},
|
|
}
|
|
srv := bookorbitServer(t, &rows)
|
|
defer srv.Close()
|
|
ing := newTestBookOrbit(srv)
|
|
|
|
first, err := ing.IngestAll()
|
|
if err != nil {
|
|
t.Fatalf("first: %v", err)
|
|
}
|
|
second, err := ing.IngestAll()
|
|
if err != nil {
|
|
t.Fatalf("second: %v", err)
|
|
}
|
|
if first[0].Fingerprint != second[0].Fingerprint {
|
|
t.Fatalf("fingerprint changed between identical sweeps: %q vs %q", first[0].Fingerprint, second[0].Fingerprint)
|
|
}
|
|
if first[0].ContentHash != second[0].ContentHash {
|
|
t.Fatalf("content hash changed between identical sweeps")
|
|
}
|
|
}
|
|
|
|
func TestBookOrbitNewHighlightChangesContentHash(t *testing.T) {
|
|
rows := []boAnnotation{
|
|
{ID: 1, BookID: 10, BookTitle: "Book", ChapterTitle: "One", Text: "first", HighlightedAt: "2026-01-01T00:00:00Z"},
|
|
}
|
|
srv := bookorbitServer(t, &rows)
|
|
defer srv.Close()
|
|
ing := newTestBookOrbit(srv)
|
|
|
|
before, err := ing.IngestAll()
|
|
if err != nil {
|
|
t.Fatalf("before: %v", err)
|
|
}
|
|
|
|
rows = append(rows, boAnnotation{ID: 2, BookID: 10, BookTitle: "Book", ChapterTitle: "One", Text: "second", HighlightedAt: "2026-01-02T00:00:00Z"})
|
|
after, err := ing.IngestAll()
|
|
if err != nil {
|
|
t.Fatalf("after: %v", err)
|
|
}
|
|
if len(after) != 1 {
|
|
t.Fatalf("results = %d, want the chapter still merged into one entry", len(after))
|
|
}
|
|
if before[0].ContentHash == after[0].ContentHash {
|
|
t.Fatal("content hash unchanged after adding a highlight, so the entry would never update")
|
|
}
|
|
if before[0].Fingerprint != after[0].Fingerprint {
|
|
t.Fatal("fingerprint changed when the chapter gained a highlight; it must be stable")
|
|
}
|
|
}
|
|
|
|
func TestBookOrbitLoginFailureIsAnError(t *testing.T) {
|
|
srv := bookorbitServer(t, &[]boAnnotation{})
|
|
defer srv.Close()
|
|
ing := newTestBookOrbit(srv)
|
|
ing.Password = "wrong"
|
|
|
|
if _, err := ing.IngestAll(); err == nil {
|
|
t.Fatal("expected an error for bad credentials, got nil (a silent zero would hide a broken robot login)")
|
|
}
|
|
}
|
|
|
|
func TestBookOrbitUnconfiguredIsNoop(t *testing.T) {
|
|
ing := &BookOrbitIngester{Version: "bookorbit/v1"}
|
|
results, err := ing.IngestAll()
|
|
if err != nil || results != nil {
|
|
t.Fatalf("unconfigured ingester = (%v, %v), want (nil, nil)", results, err)
|
|
}
|
|
}
|
|
|
|
func keysOf(m map[string]*IngestResult) []string {
|
|
out := make([]string, 0, len(m))
|
|
for k := range m {
|
|
out = append(out, k)
|
|
}
|
|
return out
|
|
}
|