Phase F follow-ups (ADR-16): - Calendars: events are now stored in calendar_events by a calendars.sync job (every 15 minutes per account, 90 days back and 400 ahead), so journals open at once and keep showing events while a source is down. Google accounts connect with OAuth (calendar.readonly) through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access. - OneNote: whole notebooks import through Microsoft Graph (Notes.Read, DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with section groups, sections, subpages, pictures and attached files. The Microsoft sign-in lives in memory for an hour and is never stored. - Readwise: an hourly sync with the person's sealed access token; new books get a page under "Readwise", new highlights are appended once. New readwise_syncs collection. - internal/oauth: signed states and one callback route per provider, routed by purpose. directory gains ImportConverted and CallerOf. - Phone app: "Today's journal" on the home screen and calendar events on journal pages. - Web clipper: `npm run package` builds Chrome/Edge, Firefox and source zips; store/ holds listing texts, permission notes and a privacy policy. - OpenAPI, generated clients, translations, compose and .env examples, and user docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
159 lines
5.7 KiB
Go
159 lines
5.7 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package readwise
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/convert"
|
|
"docwell/server/internal/directory"
|
|
)
|
|
|
|
type fakeDir struct {
|
|
pages map[string]*directory.Page
|
|
content map[string]json.RawMessage
|
|
n int
|
|
}
|
|
|
|
func newFakeDir() *fakeDir {
|
|
return &fakeDir{pages: map[string]*directory.Page{}, content: map[string]json.RawMessage{}}
|
|
}
|
|
|
|
func (f *fakeDir) CallerOf(context.Context, string) (access.Caller, error) {
|
|
return access.Caller{ID: "u1"}, nil
|
|
}
|
|
func (f *fakeDir) GetSpace(context.Context, access.Caller, string) (directory.Space, error) {
|
|
return directory.Space{ID: "sp", Workspace: "ws", Role: access.SpaceEditor}, nil
|
|
}
|
|
func (f *fakeDir) GetPage(_ context.Context, _ access.Caller, id string) (directory.Page, error) {
|
|
if p := f.pages[id]; p != nil {
|
|
return *p, nil
|
|
}
|
|
return directory.Page{}, fmt.Errorf("page not found")
|
|
}
|
|
func (f *fakeDir) add(space, parent, title string) directory.Page {
|
|
f.n++
|
|
p := &directory.Page{ID: fmt.Sprintf("p%d", f.n), Space: space, Parent: parent, Title: title}
|
|
f.pages[p.ID] = p
|
|
return *p
|
|
}
|
|
func (f *fakeDir) CreatePage(_ context.Context, _ access.Caller, space string, in directory.CreatePageInput) (directory.Page, error) {
|
|
return f.add(space, in.Parent, in.Title), nil
|
|
}
|
|
func (f *fakeDir) GetPageContent(_ context.Context, _ access.Caller, id string) (directory.PageContent, error) {
|
|
return directory.PageContent{Content: f.content[id]}, nil
|
|
}
|
|
func (f *fakeDir) SavePageContent(_ context.Context, _ access.Caller, id string, in directory.SavePageContentInput) (directory.PageContent, error) {
|
|
f.content[id] = in.Content
|
|
return directory.PageContent{Content: in.Content}, nil
|
|
}
|
|
func (f *fakeDir) ImportConverted(_ context.Context, _ access.Caller, space, parent string, pages []convert.NotionPage, _ []convert.Asset) ([]directory.Page, error) {
|
|
p := f.add(space, parent, pages[0].Title)
|
|
raw, _ := json.Marshal(pages[0].Doc)
|
|
f.content[p.ID] = raw
|
|
return []directory.Page{p}, nil
|
|
}
|
|
|
|
func TestSyncBooks(t *testing.T) {
|
|
round := 1
|
|
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Token tok" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
switch r.URL.Path {
|
|
case "/api/v2/auth/":
|
|
w.WriteHeader(http.StatusNoContent)
|
|
case "/api/v2/export/":
|
|
if round == 1 && r.URL.Query().Get("pageCursor") == "" {
|
|
_, _ = w.Write([]byte(`{"results":[{"user_book_id":1,"readable_title":"Deep Work","author":"Cal Newport","book_tags":[{"name":"books"}],` +
|
|
`"highlights":[{"id":11,"text":"Focus matters","location":12,"location_type":"location","highlighted_at":"2024-03-01T10:00:00Z","tags":[{"name":"focus"}]},` +
|
|
`{"id":12,"text":"gone","is_deleted":true}]}],"nextPageCursor":"c2"}`))
|
|
return
|
|
}
|
|
if round == 1 {
|
|
_, _ = w.Write([]byte(`{"results":[{"user_book_id":2,"readable_title":"An Article","source_url":"https://blog.example.com/a","highlights":[{"id":21,"text":"Quote"}]}],"nextPageCursor":null}`))
|
|
return
|
|
}
|
|
if r.URL.Query().Get("updatedAfter") == "" {
|
|
t.Error("second sync without updatedAfter")
|
|
}
|
|
_, _ = w.Write([]byte(`{"results":[{"user_book_id":1,"readable_title":"Deep Work","highlights":[{"id":11,"text":"Focus matters"},{"id":13,"text":"New one","note":"mine"}]}]}`))
|
|
}
|
|
}))
|
|
defer api.Close()
|
|
dir := newFakeDir()
|
|
s := New(nil, dir, Options{Secret: "x", API: api.URL, Client: api.Client()})
|
|
ctx := context.Background()
|
|
if err := s.checkToken(ctx, "tok"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.checkToken(ctx, "bad"); err == nil || !strings.Contains(err.Error(), "refused") {
|
|
t.Fatalf("bad token: %v", err)
|
|
}
|
|
r := &record{Space: "sp", Books: map[string]*book{}}
|
|
c := access.Caller{ID: "u1"}
|
|
run := func() {
|
|
books, err := s.export(ctx, "tok", r.Cursor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, b := range books {
|
|
if err := s.syncBook(ctx, c, r, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
r.Cursor = "2026-11-16T00:00:00Z"
|
|
}
|
|
run()
|
|
if len(r.Books) != 2 || r.Parent == "" || dir.pages[r.Parent].Title != "Readwise" {
|
|
t.Fatalf("books %v parent %q", r.Books, r.Parent)
|
|
}
|
|
deep := r.Books["1"]
|
|
if dir.pages[deep.Page].Parent != r.Parent || len(deep.Highlights) != 1 {
|
|
t.Fatalf("deep work = %+v", deep)
|
|
}
|
|
j := string(dir.content[deep.Page])
|
|
if !strings.Contains(j, "Focus matters") || strings.Contains(j, "gone") || !strings.Contains(j, "location 12 · 2024-03-01 · #focus") {
|
|
t.Errorf("deep work content = %s", j)
|
|
}
|
|
if a := string(dir.content[r.Books["2"].Page]); !strings.Contains(a, "https://blog.example.com/a") {
|
|
t.Errorf("article content = %s", a)
|
|
}
|
|
|
|
round = 2
|
|
run()
|
|
j = string(dir.content[deep.Page])
|
|
if strings.Count(j, "Focus matters") != 1 || !strings.Contains(j, "New one") || !strings.Contains(j, "mine") || len(deep.Highlights) != 2 {
|
|
t.Errorf("after second sync: %s, %v", j, deep.Highlights)
|
|
}
|
|
|
|
// A page moved to the trash gets a new page with the new highlights.
|
|
dir.pages[deep.Page].InTrash = true
|
|
old := deep.Page
|
|
if err := s.syncBook(ctx, c, r, apiBook{ID: 1, Title: "Deep Work", Highlights: []apiHighlight{{ID: 14, Text: "After trash"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Books["1"].Page == old || !strings.Contains(string(dir.content[r.Books["1"].Page]), "After trash") {
|
|
t.Errorf("trashed page not replaced: %+v", r.Books["1"])
|
|
}
|
|
}
|
|
|
|
func TestSealToken(t *testing.T) {
|
|
s := New(nil, newFakeDir(), Options{Secret: "a"})
|
|
got, err := s.open(s.seal("tok"))
|
|
if err != nil || got != "tok" {
|
|
t.Fatalf("open = %q, %v", got, err)
|
|
}
|
|
if _, err := New(nil, newFakeDir(), Options{Secret: "b"}).open(s.seal("tok")); err == nil {
|
|
t.Fatal("opened with another secret")
|
|
}
|
|
}
|