- frontend/: Svelte 4 + TS + Vite app — omnibar (search-and-create), ranked diacritic-insensitive filtering, CodeMirror 6 markdown editor with 400ms autosave, marked+DOMPurify preview with [[wiki-links]], tag filter, undo toast, light/dark, narrow-screen stacked layout, IndexedDB store, pull-then- push sync engine with conflict handling, versioned cache-first service worker + manifest (installable PWA) - server/: tefterd — Go stdlib HTTP + modernc.org/sqlite, /api/v1 sync API (changes/batch/health/import), SHA-256 hashed bearer token, LWW-with- conflict-copies push rules, subcommands: init, token rotate, import simplenote, compact, backup (VACUUM INTO); embeds the frontend bundle - desktop/: Wails v2 shell — single instance, hide-to-tray (fyne systray), global Ctrl+Shift+Space hotkey, quit-on-close flag - Simplenote import (CLI + web upload), idempotent via source_id dedupe; verified against a real 242-note export - Makefile (frontend/server/server-all/desktop/test), GitHub release workflow, README with systemd/Caddy/backup docs Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
125 lines
2.9 KiB
Go
125 lines
2.9 KiB
Go
package importer
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"tefter/server/store"
|
|
)
|
|
|
|
func makeZip(t *testing.T, notesJSON string) *bytes.Reader {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
w, err := zw.Create("source/notes.json")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := w.Write([]byte(notesJSON)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := zw.Close(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return bytes.NewReader(buf.Bytes())
|
|
}
|
|
|
|
const sample = `{
|
|
"activeNotes": [
|
|
{"id": "n1", "content": "Title one\r\nbody", "creationDate": "2019-03-06T04:32:52.000Z", "lastModified": "2026-07-11T19:42:01.000Z", "tags": ["work"]},
|
|
{"id": "n2", "content": "Second", "creationDate": "2020-01-01T00:00:00.000Z", "lastModified": "2020-01-02T00:00:00.000Z"}
|
|
],
|
|
"trashedNotes": [
|
|
{"id": "n3", "content": "", "creationDate": "2025-12-12T07:27:05.000Z", "lastModified": "2026-02-11T12:53:42.000Z"}
|
|
]
|
|
}`
|
|
|
|
func TestImportAndIdempotency(t *testing.T) {
|
|
st, err := store.Open(filepath.Join(t.TempDir(), "t.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer st.Close()
|
|
|
|
r := makeZip(t, sample)
|
|
sum, err := ImportZip(st, r, r.Size())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sum.Imported != 2 || sum.Trashed != 1 || sum.Skipped != 0 {
|
|
t.Fatalf("first import: %+v", sum)
|
|
}
|
|
|
|
// Re-import must not duplicate (SPEC §6).
|
|
r2 := makeZip(t, sample)
|
|
sum2, err := ImportZip(st, r2, r2.Size())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sum2.Imported != 0 || sum2.Trashed != 0 || sum2.Skipped != 3 {
|
|
t.Fatalf("re-import: %+v", sum2)
|
|
}
|
|
|
|
notes, _, _, err := st.Changes(0, 500)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(notes) != 3 {
|
|
t.Fatalf("want 3 notes, got %d", len(notes))
|
|
}
|
|
byContent := map[string]store.Note{}
|
|
for _, n := range notes {
|
|
byContent[n.Content] = n
|
|
}
|
|
one, ok := byContent["Title one\nbody"] // CRLF normalized
|
|
if !ok {
|
|
t.Fatalf("CRLF not normalized: %+v", notes)
|
|
}
|
|
if one.CreatedAt != 1551846772000 || one.ModifiedAt != 1783798921000 {
|
|
t.Fatalf("timestamps: %d %d", one.CreatedAt, one.ModifiedAt)
|
|
}
|
|
if len(one.Tags) != 1 || one.Tags[0] != "work" {
|
|
t.Fatalf("tags: %v", one.Tags)
|
|
}
|
|
}
|
|
|
|
// TestImportRealExport runs against the actual Simplenote export when present.
|
|
func TestImportRealExport(t *testing.T) {
|
|
path := os.Getenv("TEFTER_REAL_EXPORT")
|
|
if path == "" {
|
|
t.Skip("set TEFTER_REAL_EXPORT=/path/to/notes.zip to run")
|
|
}
|
|
st, err := store.Open(filepath.Join(t.TempDir(), "t.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer st.Close()
|
|
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer f.Close()
|
|
info, _ := f.Stat()
|
|
|
|
sum, err := ImportZip(st, f, info.Size())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Logf("real export: %s", sum)
|
|
if sum.Imported == 0 {
|
|
t.Fatal("expected notes from real export")
|
|
}
|
|
|
|
sum2, err := ImportZip(st, f, info.Size())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if sum2.Imported != 0 || sum2.Trashed != 0 {
|
|
t.Fatalf("real re-import not idempotent: %+v", sum2)
|
|
}
|
|
}
|