aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_todo.py
blob: f648a36cbff7917f95de11c77f16e7080d13a27d (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# deck-it - tests for the Vikunja todo module
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import tempfile
import unittest
from datetime import datetime, timezone

from deckit.module import ModuleError
from deckit.modules import todo

NOW = datetime(2026, 11, 30, 12, 0, tzinfo=timezone.utc)
NONE = "0001-01-01T00:00:00Z"


def task(id, due=NONE, reminders=None, title=None):
    return {"id": id, "title": title or f"t{id}", "due_date": due,
            "reminders": [{"reminder": r} for r in reminders] if reminders else None}


class Pick(unittest.TestCase):
    def test_future_reminder_hides_a_task(self):
        got = todo.pick([task(1, due="2026-11-29T09:00:00Z", reminders=["2026-11-30T12:30:00Z"]),
                         task(2, reminders=["2026-11-30T11:00:00Z"])], NOW)
        self.assertEqual([t["id"] for t in got], [2])

    def test_overdue_first_then_by_due_then_reminder_only(self):
        got = todo.pick([task(1, reminders=["2026-11-30T08:00:00Z"]),
                         task(2, due="2026-11-30T18:00:00Z"),
                         task(3, due="2026-11-28T09:00:00Z")], NOW)
        self.assertEqual([t["id"] for t in got], [3, 2, 1])

    def test_year_one_reads_as_no_due_date(self):
        self.assertIsNone(todo.pick([task(1, reminders=["2026-11-30T08:00:00Z"])], NOW)[0]["due"])
        self.assertEqual(todo.pick([task(2, due="2026-11-30T18:00:00Z")], NOW)[0]["due"],
                         "2026-11-30T18:00:00Z")


class Fake:
    def __init__(self, tasks):
        self.tasks, self.calls = tasks, []

    def __call__(self, method, url, token, data=None):
        self.calls.append((method, url, data))
        if url.endswith("/api/v1/info"):
            return {"frontend_url": "http://vikunja.example.org/"}
        if method == "GET" and "/api/v2/tasks?" in url:
            return {"items": self.tasks}
        if method == "GET":
            return next(t for t in self.tasks if url.endswith(f"/tasks/{t['id']}"))
        return {}


class Module(unittest.TestCase):
    def setUp(self):
        self.dir = tempfile.TemporaryDirectory()
        tok = Path(self.dir.name) / "token"
        tok.write_text("secret\n")
        self.http = Fake([task(7, due="2026-11-29T09:00:00Z", reminders=["2026-11-29T08:00:00Z"])])
        self.opened = []
        self.m = todo.Todo({"todo": {"url": "http://vikunja.example.org:3456/"}, "http": self.http,
                            "token": tok, "popen": self.opened.append, "now": lambda: NOW})
        self.m.refresh()

    def tearDown(self):
        self.dir.cleanup()

    def test_state_lists_tasks(self):
        st = self.m.state()
        self.assertEqual([t["id"] for t in st["tasks"]], [7])
        self.assertIsNone(st["error"])

    def test_unlisted_id_is_refused(self):
        for act in ("open", "done", "snooze"):
            with self.assertRaises(ModuleError):
                self.m.actions[act]({"id": 8})

    def test_open_runs_xdg_open_on_the_task(self):
        self.m.actions["open"]({"id": 7})
        self.assertEqual(self.opened, [["xdg-open", "http://vikunja.example.org/tasks/7"]])

    def test_done_patches_only_done(self):
        self.m.actions["done"]({"id": 7})
        self.assertIn(("PATCH", "http://vikunja.example.org:3456/api/v2/tasks/7", {"done": True}),
                      self.http.calls)

    def test_snooze_keeps_old_reminders_and_adds_one(self):
        self.m.actions["snooze"]({"id": 7})
        patch = [c for c in self.http.calls if c[0] == "PATCH"][-1]
        self.assertEqual(patch[2], {"reminders": [{"reminder": "2026-11-29T08:00:00Z"},
                                                  {"reminder": "2026-11-30T12:30:00Z"}]})

    def test_error_is_reported(self):
        def down(*a, **k):
            raise OSError("connection refused")
        self.m.http = down
        self.m.refresh()
        self.assertEqual(self.m.state(), {"tasks": [], "error": "connection refused"})

    def test_empty_url_is_quiet(self):
        m = todo.make({"todo": {"url": ""}})
        self.assertEqual(m.state(), {"tasks": [], "error": None})
        self.assertEqual(m.actions, {})


if __name__ == "__main__":
    unittest.main()