95 lines
3.5 KiB
Go
95 lines
3.5 KiB
Go
package notes
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestFileStoreUpsertListDeleteAndPersist(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "notes.json")
|
|
reminderAt := int64(2000)
|
|
|
|
store, err := OpenFileStore(path)
|
|
if err != nil {
|
|
t.Fatalf("OpenFileStore() error = %v", err)
|
|
}
|
|
|
|
if _, err := store.Upsert(Note{ID: "old", Title: "Old", Color: "#FFFFFF", UpdatedAt: 100}); err != nil {
|
|
t.Fatalf("Upsert(old) error = %v", err)
|
|
}
|
|
if _, err := store.Upsert(Note{
|
|
ID: "new",
|
|
Title: "New",
|
|
Body: "Formatted body",
|
|
BodyFormats: []BodyFormat{{Start: 0, End: 9, Style: "bold"}},
|
|
Owner: "owner-1",
|
|
Collaborators: []string{"friend@example.org", "friend@example.org", " "},
|
|
Color: "#FFF475",
|
|
UpdatedAt: 200,
|
|
SortOrder: 300,
|
|
Pinned: true,
|
|
Archived: true,
|
|
Deleted: true,
|
|
Checklist: true,
|
|
Items: []Item{{ID: "item-1", Text: "Buy milk", Checked: true}},
|
|
Labels: []string{"Home", "Errands"},
|
|
ReminderAt: &reminderAt,
|
|
Images: []Image{{ID: "image-1", MimeType: "image/jpeg", Base64: "abc123"}},
|
|
Audios: []Audio{{ID: "audio-1", MimeType: "audio/mp4", Base64: "def456", DurationMillis: 1200}},
|
|
}); err != nil {
|
|
t.Fatalf("Upsert(new) error = %v", err)
|
|
}
|
|
|
|
notes := store.List()
|
|
if len(notes) != 2 {
|
|
t.Fatalf("List() length = %d, want 2", len(notes))
|
|
}
|
|
if notes[0].ID != "new" {
|
|
t.Fatalf("List()[0].ID = %q, want %q", notes[0].ID, "new")
|
|
}
|
|
if notes[0].SortOrder != 300 {
|
|
t.Fatalf("List()[0].SortOrder = %d, want 300", notes[0].SortOrder)
|
|
}
|
|
if notes[0].Body != "Formatted body" || len(notes[0].BodyFormats) != 1 || notes[0].BodyFormats[0].Style != "bold" {
|
|
t.Fatalf("List()[0] body formatting = body:%q formats:%+v, want one bold range", notes[0].Body, notes[0].BodyFormats)
|
|
}
|
|
if notes[0].Owner != "owner-1" || len(notes[0].Collaborators) != 1 || notes[0].Collaborators[0] != "friend@example.org" {
|
|
t.Fatalf("List()[0] sharing = owner:%q collaborators:%+v, want one collaborator", notes[0].Owner, notes[0].Collaborators)
|
|
}
|
|
if !notes[0].Pinned || !notes[0].Archived || !notes[0].Deleted {
|
|
t.Fatalf("List()[0] state = pinned:%v archived:%v deleted:%v, want all true", notes[0].Pinned, notes[0].Archived, notes[0].Deleted)
|
|
}
|
|
if !notes[0].Checklist || len(notes[0].Items) != 1 || notes[0].Items[0].Text != "Buy milk" || !notes[0].Items[0].Checked {
|
|
t.Fatalf("List()[0] checklist = %+v, want one checked item", notes[0].Items)
|
|
}
|
|
if len(notes[0].Labels) != 2 || notes[0].Labels[0] != "Home" || notes[0].Labels[1] != "Errands" {
|
|
t.Fatalf("List()[0] labels = %+v, want Home and Errands", notes[0].Labels)
|
|
}
|
|
if notes[0].ReminderAt == nil || *notes[0].ReminderAt != reminderAt {
|
|
t.Fatalf("List()[0] ReminderAt = %v, want %d", notes[0].ReminderAt, reminderAt)
|
|
}
|
|
if len(notes[0].Images) != 1 || notes[0].Images[0].ID != "image-1" || notes[0].Images[0].Base64 != "abc123" {
|
|
t.Fatalf("List()[0] images = %+v, want image-1", notes[0].Images)
|
|
}
|
|
if len(notes[0].Audios) != 1 || notes[0].Audios[0].ID != "audio-1" || notes[0].Audios[0].Base64 != "def456" {
|
|
t.Fatalf("List()[0] audios = %+v, want audio-1", notes[0].Audios)
|
|
}
|
|
|
|
if err := store.Delete("new"); err != nil {
|
|
t.Fatalf("Delete(new) error = %v", err)
|
|
}
|
|
|
|
reopened, err := OpenFileStore(path)
|
|
if err != nil {
|
|
t.Fatalf("OpenFileStore(reopen) error = %v", err)
|
|
}
|
|
|
|
notes = reopened.List()
|
|
if len(notes) != 1 {
|
|
t.Fatalf("reopened List() length = %d, want 1", len(notes))
|
|
}
|
|
if notes[0].ID != "old" {
|
|
t.Fatalf("reopened List()[0].ID = %q, want %q", notes[0].ID, "old")
|
|
}
|
|
}
|