feat: ingest Brevier reading copies and phone notes (#53)
feat: ingest Brevier reading copies and phone notes (#53) Co-authored-by: David Gwilliam <dhgwilliam@gmail.com> Co-committed-by: David Gwilliam <dhgwilliam@gmail.com>
This commit was merged in pull request #53.
This commit is contained in:
@@ -0,0 +1,221 @@
|
||||
package ingest
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/pierrec/lz4/v4"
|
||||
)
|
||||
|
||||
// writeFile creates a file under dir, creating parent directories.
|
||||
func writeFile(t *testing.T, path string, data []byte) {
|
||||
t.Helper()
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(path, data, 0o644); err != nil {
|
||||
t.Fatalf("write %s: %v", path, err)
|
||||
}
|
||||
}
|
||||
|
||||
func copyText(title, source, read, body string) string {
|
||||
return "---\ntitle: \"" + title + "\"\nsource: \"" + source + "\"\nread: " + read + "\n---\n\n" + body + "\n"
|
||||
}
|
||||
|
||||
func compressLZ4(t *testing.T, text string) []byte {
|
||||
t.Helper()
|
||||
var buf bytes.Buffer
|
||||
w := lz4.NewWriter(&buf)
|
||||
if _, err := w.Write([]byte(text)); err != nil {
|
||||
t.Fatalf("lz4 write: %v", err)
|
||||
}
|
||||
if err := w.Close(); err != nil {
|
||||
t.Fatalf("lz4 close: %v", err)
|
||||
}
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
// TestBrevierReadsPlainAndCompressedCopies: Brevier writes plain .md only when
|
||||
// the reader switches the setting on; older copies stay compressed. Reading
|
||||
// just one form would silently drop half the archive.
|
||||
func TestBrevierReadsPlainAndCompressedCopies(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
plain := filepath.Join(dir, "example.com", "2026-11-07-plain.md")
|
||||
packed := filepath.Join(dir, "example.com", "2026-11-08-packed.md.lz4")
|
||||
writeFile(t, plain, []byte(copyText("Plain page", "https://example.com/plain", "2026-11-07T10:00:00Z", "plain body")))
|
||||
writeFile(t, packed, compressLZ4(t, copyText("Packed page", "https://example.com/packed", "2026-11-08T11:00:00-08:00", "packed body")))
|
||||
|
||||
ing := &BrevierIngester{Version: "test", Archive: dir}
|
||||
results, err := ing.IngestAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ingest: %v", err)
|
||||
}
|
||||
if len(results) != 2 {
|
||||
t.Fatalf("results = %d, want 2", len(results))
|
||||
}
|
||||
|
||||
byTitle := map[string]*IngestResult{}
|
||||
for _, r := range results {
|
||||
byTitle[r.Title] = r
|
||||
}
|
||||
if r := byTitle["Plain page"]; r == nil || r.Content != "plain body" {
|
||||
t.Fatalf("plain copy = %+v", r)
|
||||
}
|
||||
if r := byTitle["Packed page"]; r == nil || r.Content != "packed body" {
|
||||
t.Fatalf("compressed copy = %+v", r)
|
||||
}
|
||||
if got := byTitle["Packed page"].CreatedAt; got != "2026-11-08T19:00:00Z" {
|
||||
t.Fatalf("CreatedAt = %q, want the read stamp normalised to UTC", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBrevierOneEntryPerPageAcrossDays: a page read on two days has two copies.
|
||||
// They must share a fingerprint so the entry updates rather than duplicating,
|
||||
// and the newer copy must sort last so it wins.
|
||||
func TestBrevierOneEntryPerPageAcrossDays(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
src := "https://example.com/article"
|
||||
writeFile(t, filepath.Join(dir, "example.com", "2026-11-01-article.md"),
|
||||
[]byte(copyText("Article", src, "2026-11-01T09:00:00Z", "first read")))
|
||||
writeFile(t, filepath.Join(dir, "example.com", "2026-11-05-article.md"),
|
||||
[]byte(copyText("Article", src, "2026-11-05T09:00:00Z", "second read, changed")))
|
||||
|
||||
results, err := (&BrevierIngester{Version: "test", Archive: dir}).IngestAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ingest: %v", err)
|
||||
}
|
||||
if len(results) != 2 {
|
||||
t.Fatalf("results = %d, want both copies (they are versions)", len(results))
|
||||
}
|
||||
if results[0].Fingerprint != results[1].Fingerprint {
|
||||
t.Fatalf("fingerprints differ across days: %q vs %q", results[0].Fingerprint, results[1].Fingerprint)
|
||||
}
|
||||
if results[0].ContentHash == results[1].ContentHash {
|
||||
t.Fatal("content hash unchanged although the text changed")
|
||||
}
|
||||
if results[0].CreatedAt > results[1].CreatedAt {
|
||||
t.Fatalf("copies are not oldest-first: %q then %q", results[0].CreatedAt, results[1].CreatedAt)
|
||||
}
|
||||
if results[1].Content != "second read, changed" {
|
||||
t.Fatalf("newest copy is not last: %q", results[1].Content)
|
||||
}
|
||||
}
|
||||
|
||||
func notesPage(title, source, created, body string) string {
|
||||
return "---\ntitle: \"" + title + "\"\nsource: \"" + source + "\"\ncreated: " + created + "\n---\n\n# " + title + "\n\n" + body + "\n"
|
||||
}
|
||||
|
||||
func TestBrevierPhoneNotesFileAndZip(t *testing.T) {
|
||||
inbox := t.TempDir()
|
||||
writeFile(t, filepath.Join(inbox, "single.md"),
|
||||
[]byte(notesPage("Single", "https://example.com/single", "2026-11-02", "> a quote\n\nmy note")))
|
||||
|
||||
var buf bytes.Buffer
|
||||
zw := zip.NewWriter(&buf)
|
||||
for _, page := range []struct{ name, title, src, body string }{
|
||||
{"a.md", "A", "https://example.com/a", "> quote a"},
|
||||
{"b.md", "B", "https://example.com/b", "> quote b"},
|
||||
} {
|
||||
w, err := zw.Create(page.name)
|
||||
if err != nil {
|
||||
t.Fatalf("zip create: %v", err)
|
||||
}
|
||||
if _, err := w.Write([]byte(notesPage(page.title, page.src, "2026-11-03", page.body))); err != nil {
|
||||
t.Fatalf("zip write: %v", err)
|
||||
}
|
||||
}
|
||||
if _, err := zw.Create("images/picture.png"); err != nil {
|
||||
t.Fatalf("zip image: %v", err)
|
||||
}
|
||||
if err := zw.Close(); err != nil {
|
||||
t.Fatalf("zip close: %v", err)
|
||||
}
|
||||
writeFile(t, filepath.Join(inbox, "Brevier-notes.zip"), buf.Bytes())
|
||||
|
||||
results, err := (&BrevierIngester{Version: "test", Inbox: inbox}).IngestAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ingest: %v", err)
|
||||
}
|
||||
if len(results) != 3 {
|
||||
t.Fatalf("results = %d, want 3 (one file + two zip pages, images skipped)", len(results))
|
||||
}
|
||||
for _, r := range results {
|
||||
if r.SourceID != "brevier-notes" {
|
||||
t.Fatalf("%q came from source %q", r.Title, r.SourceID)
|
||||
}
|
||||
if r.ContentType != "reading/notes" {
|
||||
t.Fatalf("%q has content type %q", r.Title, r.ContentType)
|
||||
}
|
||||
}
|
||||
bySource := map[string]*IngestResult{}
|
||||
for _, r := range results {
|
||||
bySource[r.SourcePath] = r
|
||||
}
|
||||
if got := bySource["https://example.com/single"].CreatedAt; got != "2026-11-02T00:00:00Z" {
|
||||
t.Fatalf("created date not normalised: %q", got)
|
||||
}
|
||||
if got := bySource["https://example.com/a"].CreatedAt; got != "2026-11-03T00:00:00Z" {
|
||||
t.Fatalf("zip page created date not normalised: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBrevierMultiPageNotesFile covers the desktop shape: one file, one
|
||||
// `## [title](url)` section per page.
|
||||
func TestBrevierMultiPageNotesFile(t *testing.T) {
|
||||
inbox := t.TempDir()
|
||||
text := "# Your notes\n\n## [First](https://example.com/1)\n\n> quote one\n\n## [Second](https://example.com/2)\n\n> quote two\n"
|
||||
writeFile(t, filepath.Join(inbox, "Brevier-notes.md"), []byte(text))
|
||||
|
||||
results, err := (&BrevierIngester{Version: "test", Inbox: inbox}).IngestAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ingest: %v", err)
|
||||
}
|
||||
if len(results) != 2 {
|
||||
t.Fatalf("results = %d, want 2 sections", len(results))
|
||||
}
|
||||
if results[0].SourcePath != "https://example.com/1" || results[1].SourcePath != "https://example.com/2" {
|
||||
t.Fatalf("sources = %q, %q", results[0].SourcePath, results[1].SourcePath)
|
||||
}
|
||||
if results[0].Content != "> quote one" {
|
||||
t.Fatalf("section body = %q", results[0].Content)
|
||||
}
|
||||
}
|
||||
|
||||
// TestBrevierSavedArticleWithoutAddress: Android's Save writes the article with
|
||||
// no front matter, so there is no URL to key on. It is still worth keeping, and
|
||||
// re-saving an unchanged page must dedupe.
|
||||
func TestBrevierSavedArticleWithoutAddress(t *testing.T) {
|
||||
inbox := t.TempDir()
|
||||
text := "# A Saved Title\n\nsome article text\n"
|
||||
writeFile(t, filepath.Join(inbox, "A-Saved-Title.md"), []byte(text))
|
||||
|
||||
ing := &BrevierIngester{Version: "test", Inbox: inbox}
|
||||
first, err := ing.IngestAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ingest: %v", err)
|
||||
}
|
||||
second, err := ing.IngestAll()
|
||||
if err != nil {
|
||||
t.Fatalf("ingest again: %v", err)
|
||||
}
|
||||
if len(first) != 1 || len(second) != 1 {
|
||||
t.Fatalf("results = %d / %d, want 1 each", len(first), len(second))
|
||||
}
|
||||
if first[0].Fingerprint != second[0].Fingerprint {
|
||||
t.Fatal("re-reading the same saved article produced a different fingerprint")
|
||||
}
|
||||
if first[0].Title != "A Saved Title" {
|
||||
t.Fatalf("title = %q, want the heading", first[0].Title)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrevierMissingDirectoriesAreNoop(t *testing.T) {
|
||||
ing := &BrevierIngester{Version: "test", Archive: filepath.Join(t.TempDir(), "nope"), Inbox: filepath.Join(t.TempDir(), "nope")}
|
||||
results, err := ing.IngestAll()
|
||||
if err != nil || results != nil {
|
||||
t.Fatalf("missing dirs = (%v, %v), want (nil, nil)", results, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user