// SPDX-License-Identifier: AGPL-3.0-or-later package convert import ( "encoding/base64" "strings" "testing" ) func TestReadMHTOneNoteSection(t *testing.T) { img := base64.StdEncoding.EncodeToString(pngOf(t, 3, 3)) var wrapped strings.Builder for i := 0; i < len(img); i += 60 { wrapped.WriteString(img[i:min(i+60, len(img))] + "\r\n") } mht := "MIME-Version: 1.0\r\n" + "Content-Type: multipart/related; boundary=\"----=_NextPart_01\"\r\n\r\n" + "------=_NextPart_01\r\nContent-Location: file:///C:/Export/Section.htm\r\nContent-Type: text/html; charset=\"utf-8\"\r\n\r\n" + "
\r\n" + "------=_NextPart_01\r\nContent-Location: file:///C:/Export/Section_files/page1.htm\r\n" + "Content-Transfer-Encoding: quoted-printable\r\nContent-Type: text/html; charset=\"windows-1252\"\r\n\r\n" + "Two
\r\n" + "------=_NextPart_01\r\nContent-Location: file:///C:/Export/Section_files/image001.png\r\n" + "Content-Transfer-Encoding: base64\r\nContent-Type: image/png\r\n\r\n" + wrapped.String() + "------=_NextPart_01--\r\n" pages, assets, err := ReadMHT([]byte(mht), "Section.mht") if err != nil { t.Fatal(err) } if len(pages) != 2 || pages[0].Title != "Café notes" || pages[1].Title != "Second" { t.Fatalf("pages = %+v", pages) } if len(assets) != 1 || assets[0].Name != "image001.png" || string(assets[0].Data[1:4]) != "PNG" { t.Fatalf("assets = %v", assets) } j := asJSON(t, pages[0].Doc) for _, want := range []string{`"src":"asset:0"`, `"href":"page:1"`, `"href":"https://example.com/"`, "First line"} { if !strings.Contains(j, want) { t.Errorf("page lacks %s: %s", want, j) } } } func TestReadMHTRejectsOtherFiles(t *testing.T) { if _, _, err := ReadMHT([]byte("just text"), "x.mht"); err == nil { t.Fatal("plain text read as MHTML") } } func TestReadReadwise(t *testing.T) { csv := "Highlight,Book Title,Book Author,Amazon Book ID,Note,Color,Tags,Location Type,Location,Highlighted at,Document tags\n" + "\"First idea,\nover two lines\",Deep Work,Cal Newport,B00X,My note,yellow,focus,location,120,2024-03-01 10:00:00+00:00,\"productivity, books\"\n" + "Second idea,Deep Work,Cal Newport,B00X,,yellow,,location,130,,\n" + "Web quote,An Article,,,,,,,,2024-04-02 08:00:00+00:00,\n" if !IsReadwiseCSV([]byte(csv)) || IsReadwiseCSV([]byte("Name,Value\na,1\n")) { t.Fatal("IsReadwiseCSV") } pages, err := ReadReadwise([]byte(csv)) if err != nil { t.Fatal(err) } if len(pages) != 3 || pages[0].Title != "Readwise" || pages[1].Title != "Deep Work" || pages[2].Parent != 0 { t.Fatalf("pages = %+v", pages) } dw := pages[1] if dw.Props["Author"] != "Cal Newport" || len(dw.Tags) != 2 || dw.Tags[1] != "books" { t.Errorf("Deep Work props %v tags %v", dw.Props, dw.Tags) } j := asJSON(t, dw.Doc) for _, want := range []string{`"type":"blockquote"`, `"First idea,"},{"type":"hardBreak"}`, "My note", "location 120 · 2024-03-01 · #focus", "Second idea"} { if !strings.Contains(j, want) { t.Errorf("Deep Work lacks %s: %s", want, j) } } } func TestFromCSV(t *testing.T) { doc, err := FromCSV([]byte("Name,Count\nApples,3\nPears,4\n")) if err != nil || len(doc.Content) != 1 || doc.Content[0].Type != "database" { t.Fatalf("doc = %+v, %v", doc, err) } }