aboutsummaryrefslogtreecommitdiff
path: root/pocketbook/tests/test_store.py
blob: fab5bd64b781b433d9c9c098a31c6ddede832de6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pytest
from pocketbook.store import NoteStore
from pocketbook.note import Note


class TestNoteStoreCreate:
    def test_create_note(self, notes_dir):
        store = NoteStore(notes_dir)
        filename = store.create("My Title", "My Body")
        assert (notes_dir / filename).exists()
        content = (notes_dir / filename).read_text()
        assert content == "My Title\n\nMy Body"

    def test_create_assigns_incrementing_order(self, notes_dir):
        store = NoteStore(notes_dir)
        f1 = store.create("First", "")
        f2 = store.create("Second", "")
        assert Note.parse_order(f1) == 1
        assert Note.parse_order(f2) == 2

    def test_create_auto_creates_directory(self, tmp_path):
        d = tmp_path / "nonexistent" / "pocketbook"
        store = NoteStore(d)
        filename = store.create("Test", "body")
        assert d.exists()
        assert (d / filename).exists()


class TestNoteStoreList:
    def test_list_empty(self, notes_dir):
        # Remove the sample file if any fixture created one
        for f in notes_dir.iterdir():
            f.unlink()
        store = NoteStore(notes_dir)
        assert store.list_notes() == []

    def test_list_returns_sorted_by_order(self, notes_dir):
        (notes_dir / "0002-20260101-120000-abc12.txt").write_text("B\n\nbody b")
        (notes_dir / "0001-20260101-120000-def34.txt").write_text("A\n\nbody a")
        (notes_dir / "0003-20260101-120000-ghi56.txt").write_text("C\n\nbody c")
        store = NoteStore(notes_dir)
        notes = store.list_notes()
        assert len(notes) == 3
        assert notes[0][1].title == "A"
        assert notes[1][1].title == "B"
        assert notes[2][1].title == "C"

    def test_list_skips_non_txt_files(self, notes_dir):
        (notes_dir / "0001-20260101-120000-abc12.txt").write_text("Note\n\nbody")
        (notes_dir / "README.md").write_text("not a note")
        store = NoteStore(notes_dir)
        assert len(store.list_notes()) == 1

    def test_list_skips_corrupted_filenames(self, notes_dir):
        (notes_dir / "0001-20260101-120000-abc12.txt").write_text("Good\n\nbody")
        (notes_dir / "bad-name.txt").write_text("Bad\n\nbody")
        store = NoteStore(notes_dir)
        notes = store.list_notes()
        assert len(notes) == 1
        assert notes[0][1].title == "Good"


class TestNoteStoreUpdate:
    def test_update_note(self, notes_dir):
        fname = "0001-20260101-120000-abc12.txt"
        (notes_dir / fname).write_text("Old Title\n\nOld body")
        store = NoteStore(notes_dir)
        store.update(fname, "New Title", "New body")
        content = (notes_dir / fname).read_text()
        assert content == "New Title\n\nNew body"

    def test_update_nonexistent_raises(self, notes_dir):
        store = NoteStore(notes_dir)
        with pytest.raises(FileNotFoundError):
            store.update("0099-20260101-120000-nope0.txt", "T", "B")


class TestNoteStoreDelete:
    def test_delete_note(self, notes_dir):
        fname = "0001-20260101-120000-abc12.txt"
        (notes_dir / fname).write_text("Delete me\n\nbody")
        store = NoteStore(notes_dir)
        store.delete(fname)
        assert not (notes_dir / fname).exists()

    def test_delete_nonexistent_raises(self, notes_dir):
        store = NoteStore(notes_dir)
        with pytest.raises(FileNotFoundError):
            store.delete("0099-20260101-120000-nope0.txt")


class TestNoteStoreReorder:
    def test_reorder_renumbers_files(self, notes_dir):
        # Create files with gaps in ordering
        (notes_dir / "0001-20260101-120000-aaa11.txt").write_text("A\n\na")
        (notes_dir / "0005-20260101-120000-bbb22.txt").write_text("B\n\nb")
        (notes_dir / "0010-20260101-120000-ccc33.txt").write_text("C\n\nc")
        store = NoteStore(notes_dir)
        store.reorder()
        notes = store.list_notes()
        assert Note.parse_order(notes[0][0]) == 1
        assert Note.parse_order(notes[1][0]) == 2
        assert Note.parse_order(notes[2][0]) == 3