Files
Docwell/Server/internal/convert/vault_test.go
T
tajniak81andClaude Opus 5.5 3d1cf362d7 Add calendars, a web clipper, more importers and workspace snapshots
Phase F of the AFFiNE plan (ADR-16):

- Calendars: people subscribe to iCal feeds (webcal, Google's secret
  address, Outlook) or CalDAV accounts in account settings; their
  events show at the top of journal pages in the reader's time zone.
  Repeats, moved and cancelled occurrences, IANA and Windows zones
  are handled. Events are read on demand and kept in memory for 15
  minutes; passwords are sealed with DOCWELL_SECRET_KEY; sources
  follow the webhook address rules. New calendar_accounts collection.
- Importers: zips of Markdown (Obsidian vaults, Bear TextBundles and
  .bear2bk, Readwise's Markdown, Docwell's own space export) keep
  folders as pages, resolve [[wikilinks]] and embeds by name, and turn
  front matter and #tags into properties and tags. OneNote .mht
  exports, Readwise CSV, and any other CSV as a database page.
- Exports: PNG of a page drawn in the browser, print styles that print
  only the page in the light theme, and workspace snapshots (records,
  Yjs states, JSON and files in one zip) that restore as a new
  workspace. Old ids are swapped byte for byte inside the Yjs updates,
  so links point at the restored pages and comments stay anchored.
- Web-Clipper/: a Manifest V3 extension for Chrome, Edge and Firefox
  that saves the article or the selection as Markdown through the
  import API with a personal API token.
- OpenAPI, generated clients, translations and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 11:56:03 +01:00

206 lines
8.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package convert
import (
"reflect"
"strings"
"testing"
)
func pageNamed(t *testing.T, pages []NotionPage, title string) (int, NotionPage) {
t.Helper()
for i, p := range pages {
if p.Title == title {
return i, p
}
}
var titles []string
for _, p := range pages {
titles = append(titles, p.Title)
}
t.Fatalf("no page %q in %v", title, titles)
return -1, NotionPage{}
}
func TestDetectArchive(t *testing.T) {
cases := map[string]struct {
files map[string][]byte
want VaultKind
}{
"obsidian": {map[string][]byte{"Vault/.obsidian/app.json": []byte("{}"), "Vault/Note.md": []byte("x")}, VaultObsidian},
"bear": {map[string][]byte{"Note.textbundle/text.markdown": []byte("# Note"), "Note.textbundle/info.json": []byte("{}")}, VaultBear},
"notion": {map[string][]byte{"Home 0123456789abcdef0123456789abcdef.md": []byte("# Home")}, VaultNotion},
"markdown": {map[string][]byte{"docs/a.md": []byte("# A"), "docs/b.md": []byte("# B")}, VaultMarkdown},
"html": {map[string][]byte{"Home.html": []byte("<h1>Home</h1>")}, VaultNotion},
}
for name, c := range cases {
got, err := DetectArchive(zipOf(t, c.files))
if err != nil || got != c.want {
t.Errorf("%s: got %q, %v; want %q", name, got, err, c.want)
}
}
}
func TestReadVaultObsidian(t *testing.T) {
img := pngOf(t, 4, 4)
data := zipOf(t, map[string][]byte{
"My Vault/.obsidian/app.json": []byte("{}"),
"My Vault/.trash/Old.md": []byte("gone"),
"My Vault/Home.md": []byte("---\ntags: [project, \"#work\"]\nstatus: active\ndue: 2026-12-01\npriority: 2\ndone: false\naliases: [Start]\n---\n" +
"# Home\n\nSee [[Projects/Plan|the plan]] and [[Ideas#Later]], also [[Missing]].\n\n" +
"![[diagram.png|300]]\n\n> [!warning] Careful\n> Mind the gap.\n\nInline #idea and `#notatag` %%hidden%% text ^block1\n\n" +
"```\n[[not a link]] #nottag\n```\n"),
"My Vault/Ideas.md": []byte("Some ==marked== ideas. Link back to [Home](Home.md)."),
"My Vault/Projects/Plan.md": []byte("# Plan\n\nThe plan."),
"My Vault/Projects/Projects.md": []byte("Folder note for projects."),
"My Vault/Archive/2025/Old plan.md": []byte("old"),
"My Vault/attachments/diagram.png": img,
"My Vault/attachments/unused.pdf": []byte("%PDF"),
"My Vault/Drawing.excalidraw.md": []byte("excalidraw"),
"My Vault/Projects/notes for me.txt": []byte("an attachment, not a page"),
})
pages, assets, err := ReadVault(data, VaultObsidian)
if err != nil {
t.Fatal(err)
}
if len(pages) != 7 {
var titles []string
for _, p := range pages {
titles = append(titles, p.Title)
}
t.Fatalf("got %d pages %v, want Home, Ideas, Projects, Archive, 2025, Old plan, Plan", len(pages), titles)
}
homeAt, home := pageNamed(t, pages, "Home")
ideasAt, _ := pageNamed(t, pages, "Ideas")
projectsAt, projects := pageNamed(t, pages, "Projects")
_, plan := pageNamed(t, pages, "Plan")
archiveAt, _ := pageNamed(t, pages, "Archive")
yearAt, year := pageNamed(t, pages, "2025")
_, old := pageNamed(t, pages, "Old plan")
if home.Parent != -1 || plan.Parent != projectsAt || projects.Parent != -1 || year.Parent != archiveAt || old.Parent != yearAt {
t.Errorf("tree: home %d plan %d projects %d year %d old %d", home.Parent, plan.Parent, projects.Parent, year.Parent, old.Parent)
}
if !strings.Contains(asJSON(t, projects.Doc), "Folder note for projects") {
t.Errorf("folder note is not the folder's page: %s", asJSON(t, projects.Doc))
}
for i, p := range pages {
if p.Parent >= i {
t.Errorf("page %q comes before its parent", p.Title)
}
}
if want := []string{"project", "work", "idea"}; !reflect.DeepEqual(home.Tags, want) {
t.Errorf("tags = %v, want %v", home.Tags, want)
}
wantProps := map[string]any{"status": "active", "due": "2026-12-01", "priority": float64(2), "done": false}
if !reflect.DeepEqual(home.Props, wantProps) {
t.Errorf("props = %#v, want %#v", home.Props, wantProps)
}
j := asJSON(t, home.Doc)
for _, want := range []string{
`"text":"the plan"`, `"href":"` + PageRef(planIndex(pages)) + `"`,
`"text":"Ideas \u003e Later"`, `"href":"` + PageRef(ideasAt) + `"`,
`"src":"asset:0"`, `"type":"callout"`, `"text":"Careful","marks":[{"type":"bold"}]}]},{"type":"paragraph","content":[{"type":"text","text":"Mind the gap."`,
`[[not a link]] #nottag`, `#notatag`,
} {
if !strings.Contains(j, want) {
t.Errorf("Home lacks %s:\n%s", want, j)
}
}
for _, unwanted := range []string{"hidden", "^block1", `"text":"Home"`, "[[Missing]]"} {
if strings.Contains(j, unwanted) {
t.Errorf("Home still has %s:\n%s", unwanted, j)
}
}
if !strings.Contains(j, `also Missing.`) {
t.Errorf("an unresolved wikilink should keep its text:\n%s", j)
}
_, ideas := pageNamed(t, pages, "Ideas")
if ij := asJSON(t, ideas.Doc); !strings.Contains(ij, "Some marked ideas") || !strings.Contains(ij, `"href":"`+PageRef(homeAt)+`"`) {
t.Errorf("Ideas: %s", ij)
}
if len(assets) != 1 || assets[0].Name != "diagram.png" {
t.Errorf("assets = %v, want only diagram.png (used)", assets)
}
}
func planIndex(pages []NotionPage) int {
for i, p := range pages {
if p.Title == "Plan" {
return i
}
}
return -1
}
func TestReadVaultMarkdownTitlesAndDocwellExport(t *testing.T) {
// A Docwell space export: index.md and _files/ are not pages, and
// "Guides.md" holds the pages in "Guides/".
data := zipOf(t, map[string][]byte{
"index.md": []byte("# Space\n\n- [Guides](Guides.md)"),
"Guides.md": []byte("# Guides\n\nAll guides. ![pic](_files/pic.png)"),
"Guides/Setup.md": []byte("# Setting things up\n\nBack to [Guides](../Guides.md)."),
"_files/pic.png": pngOf(t, 2, 2),
"Guides/README.md": []byte("No heading here."),
})
pages, assets, err := ReadVault(data, VaultMarkdown)
if err != nil {
t.Fatal(err)
}
if len(pages) != 3 || len(assets) != 1 {
t.Fatalf("got %d pages, %d assets", len(pages), len(assets))
}
guidesAt, _ := pageNamed(t, pages, "Guides")
_, setup := pageNamed(t, pages, "Setting things up") // The heading names it, not the file.
_, readme := pageNamed(t, pages, "README")
if setup.Parent != guidesAt || readme.Parent != guidesAt {
t.Errorf("parents: %d %d, want %d", setup.Parent, readme.Parent, guidesAt)
}
if !strings.Contains(asJSON(t, setup.Doc), `"href":"`+PageRef(guidesAt)+`"`) {
t.Errorf("relative link not resolved: %s", asJSON(t, setup.Doc))
}
}
func TestReadVaultBear(t *testing.T) {
data := zipOf(t, map[string][]byte{
"Groceries.textbundle/text.markdown": []byte("# Groceries\n\nMilk and eggs #shopping #home/kitchen #weekly plan#\n\n![](assets/list.png)\n\nSee [[Recipes]]."),
"Groceries.textbundle/info.json": []byte(`{"version":2}`),
"Groceries.textbundle/assets/list.png": pngOf(t, 2, 2),
"Recipes.textbundle/text.markdown": []byte("# Recipes\n\nPancakes."),
"Recipes.textbundle/assets/unused.jpeg": []byte("jpeg"),
})
pages, assets, err := ReadVault(data, VaultBear)
if err != nil {
t.Fatal(err)
}
if len(pages) != 2 || len(assets) != 1 {
t.Fatalf("got %d pages, %d assets", len(pages), len(assets))
}
recipesAt, _ := pageNamed(t, pages, "Recipes")
_, g := pageNamed(t, pages, "Groceries")
if want := []string{"weekly plan", "shopping", "home/kitchen"}; !reflect.DeepEqual(g.Tags, want) {
t.Errorf("tags = %v, want %v", g.Tags, want)
}
j := asJSON(t, g.Doc)
if !strings.Contains(j, `"src":"asset:0"`) || !strings.Contains(j, `"href":"`+PageRef(recipesAt)+`"`) {
t.Errorf("Groceries: %s", j)
}
}
func TestFrontMatter(t *testing.T) {
meta, rest := frontMatter("---\ntitle: Hello\ncreated: 2026-01-02T10:00:00Z\nlinks:\n - a\n - b\nnested: {a: 1}\n---\nBody")
if rest != "Body" {
t.Errorf("rest = %q", rest)
}
tags, props := splitMeta(meta)
want := map[string]any{"title": "Hello", "created": "2026-01-02", "links": []string{"a", "b"}}
if len(tags) != 0 || !reflect.DeepEqual(props, want) {
t.Errorf("tags %v props %#v", tags, props)
}
for _, text := range []string{"--- not front matter\nx", "---\n: : bad yaml [\n---\nx", "No front matter"} {
if meta, rest := frontMatter(text); meta != nil || rest != text {
t.Errorf("%q: got %v, %q", text, meta, rest)
}
}
}