-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_serialization.py
More file actions
408 lines (345 loc) · 15.2 KB
/
Copy pathtest_serialization.py
File metadata and controls
408 lines (345 loc) · 15.2 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
"""Tests for Task serialization methods."""
from taskmark._types import Task
class TestToDict:
"""Tests for Task.to_dict()."""
def test_minimal_task(self) -> None:
"""Minimal task serializes to state and title only."""
task = Task(state="open", title="Simple task")
result = task.to_dict()
assert result == {"state": "open", "title": "Simple task"}
def test_field_ordering(self) -> None:
"""Fields are ordered by reading priority."""
task = Task(
state="open",
title="Task with fields",
priority="A",
due_date="2024-12-15",
estimate_minutes=120,
tags=["urgent"],
project_path="backend",
assignees=["alice"],
)
result = task.to_dict()
keys = list(result.keys())
# Verify priority ordering: state, title, priority come first
assert keys.index("state") < keys.index("title")
assert keys.index("title") < keys.index("priority")
# Time fields before people fields
assert keys.index("due_date") < keys.index("assignees")
assert keys.index("estimate_minutes") < keys.index("project_path")
def test_excludes_empty_by_default(self) -> None:
"""Empty lists, dicts, and None values are excluded by default."""
task = Task(
state="open",
title="Task",
priority=None,
tags=[],
custom_fields={},
)
result = task.to_dict()
assert "priority" not in result
assert "tags" not in result
assert "custom_fields" not in result
def test_include_empty(self) -> None:
"""Can include empty values with exclude_empty=False."""
task = Task(state="open", title="Task", tags=[])
result = task.to_dict(exclude_empty=False)
assert "tags" in result
assert result["tags"] == []
def test_excludes_location_by_default(self) -> None:
"""Location fields excluded by default."""
task = Task(state="open", title="Task", file=None, line=42, indent=2)
result = task.to_dict()
assert "file" not in result
assert "line" not in result
assert "indent" not in result
def test_include_location(self) -> None:
"""Can include location with include_location=True."""
task = Task(state="open", title="Task", line=42, indent=2)
result = task.to_dict(include_location=True)
assert result["line"] == 42
assert result["indent"] == 2
def test_excludes_provenance_by_default(self) -> None:
"""Provenance fields excluded by default."""
task = Task(
state="open",
title="Task",
tags=["urgent"],
inherited_tags=["q4"],
explicit_tags=["urgent"],
)
result = task.to_dict()
assert "inherited_tags" not in result
assert "explicit_tags" not in result
def test_include_provenance(self) -> None:
"""Can include provenance with include_provenance=True."""
task = Task(
state="open",
title="Task",
tags=["urgent"],
inherited_tags=["q4"],
explicit_tags=["urgent"],
)
result = task.to_dict(include_provenance=True)
assert result["inherited_tags"] == ["q4"]
assert result["explicit_tags"] == ["urgent"]
def test_subtasks_recursive(self) -> None:
"""Subtasks are serialized recursively."""
subtask = Task(state="open", title="Subtask", priority="B")
task = Task(state="open", title="Parent", subtasks=[subtask])
result = task.to_dict()
assert len(result["subtasks"]) == 1
assert result["subtasks"][0] == {"state": "open", "title": "Subtask", "priority": "B"}
class TestToMarkdown:
"""Tests for Task.to_markdown()."""
def test_minimal_task(self) -> None:
"""Minimal task produces checkbox and title."""
task = Task(state="open", title="Simple task")
assert task.to_markdown() == "[ ] Simple task"
def test_done_state(self) -> None:
"""Done tasks use [x] checkbox."""
task = Task(state="done", title="Completed")
assert task.to_markdown() == "[x] Completed"
def test_cancelled_state(self) -> None:
"""Cancelled tasks use [-] checkbox."""
task = Task(state="cancelled", title="Cancelled")
assert task.to_markdown() == "[-] Cancelled"
def test_blocked_state(self) -> None:
"""Blocked tasks use [!] checkbox."""
task = Task(state="blocked", title="Blocked")
assert task.to_markdown() == "[!] Blocked"
def test_done_with_dates(self) -> None:
"""Done tasks include created and done dates after checkbox."""
task = Task(
state="done",
title="Completed task",
created_date="2024-03-01",
done_date="2024-03-05",
)
assert task.to_markdown() == "[x] 2024-03-01 2024-03-05 Completed task"
def test_priority(self) -> None:
"""Priority appears in parentheses after checkbox/dates."""
task = Task(state="open", title="Important", priority="A")
assert task.to_markdown() == "[ ] (A) Important"
def test_estimate_hours(self) -> None:
"""Estimate in hours formats as ~Xh."""
task = Task(state="open", title="Task", estimate_minutes=120)
assert task.to_markdown() == "[ ] Task ~2h"
def test_estimate_minutes(self) -> None:
"""Estimate in minutes formats as ~Xm."""
task = Task(state="open", title="Task", estimate_minutes=30)
assert task.to_markdown() == "[ ] Task ~30m"
def test_estimate_mixed(self) -> None:
"""Mixed estimate formats as ~XhYm."""
task = Task(state="open", title="Task", estimate_minutes=90)
assert task.to_markdown() == "[ ] Task ~1h30m"
def test_assignees_explicit_only(self) -> None:
"""By default, only explicit assignees are included."""
task = Task(
state="open",
title="Task",
assignees=["alice", "bob"],
inherited_assignees=["team"],
explicit_assignees=["alice"],
)
assert task.to_markdown() == "[ ] Task @alice"
def test_assignees_all(self) -> None:
"""With use_explicit_only=False, all assignees included."""
task = Task(
state="open",
title="Task",
assignees=["alice", "bob"],
inherited_assignees=["team"],
explicit_assignees=["alice"],
)
assert task.to_markdown(use_explicit_only=False) == "[ ] Task @alice @bob"
def test_projects_explicit_only(self) -> None:
"""By default, only explicit project_path is included."""
task = Task(
state="open",
title="Task",
project_path="a/b/c",
inherited_project_path="a",
explicit_project_path="b/c",
)
assert task.to_markdown() == "[ ] Task +b/c"
def test_tags_explicit_only(self) -> None:
"""By default, only explicit tags are included."""
task = Task(
state="open",
title="Task",
tags=["q4", "urgent"],
inherited_tags=["q4"],
explicit_tags=["urgent"],
)
assert task.to_markdown() == "[ ] Task #urgent"
def test_due_date(self) -> None:
"""Due date formatted as due:YYYY-MM-DD."""
task = Task(state="open", title="Task", due_date="2024-12-15")
assert task.to_markdown() == "[ ] Task due:2024-12-15"
def test_planned_date(self) -> None:
"""Planned date formatted as planned:YYYY-MM-DD."""
task = Task(state="open", title="Task", planned_date="2024-12-20")
assert task.to_markdown() == "[ ] Task planned:2024-12-20"
def test_recurrence(self) -> None:
"""Recurrence formatted as repeat:pattern."""
task = Task(state="open", title="Standup", recurrence="daily")
assert task.to_markdown() == "[ ] Standup repeat:daily"
def test_custom_fields(self) -> None:
"""Custom fields formatted as key:value."""
task = Task(
state="open",
title="Task",
custom_fields={"type": "bug"},
explicit_custom_fields={"type": "bug"},
)
assert task.to_markdown() == "[ ] Task type:bug"
def test_custom_fields_quoted(self) -> None:
"""Custom fields with spaces are quoted."""
task = Task(
state="open",
title="Task",
custom_fields={"desc": "fix the bug"},
explicit_custom_fields={"desc": "fix the bug"},
)
assert task.to_markdown() == '[ ] Task desc:"fix the bug"'
def test_full_task(self) -> None:
"""Full task with all fields in correct order."""
task = Task(
state="open",
title="Implement feature",
priority="A",
estimate_minutes=240,
due_date="2024-12-15",
recurrence=None,
assignees=["alice"],
project_path="backend/api",
tags=["urgent"],
explicit_assignees=["alice"],
explicit_project_path="backend/api",
explicit_tags=["urgent"],
)
# Order: checkbox priority title estimate assignees project tags due:
expected = "[ ] (A) Implement feature ~4h @alice +backend/api #urgent due:2024-12-15"
assert task.to_markdown() == expected
class TestToMarkdownLine:
"""Tests for Task.to_markdown_line()."""
def test_no_indent(self) -> None:
"""Root task has no indent."""
task = Task(state="open", title="Task", indent=0)
assert task.to_markdown_line() == "- [ ] Task"
def test_with_indent(self) -> None:
"""Subtask has proper indent (2 spaces per level)."""
task = Task(state="open", title="Subtask", indent=1)
assert task.to_markdown_line() == " - [ ] Subtask"
def test_nested_indent(self) -> None:
"""Deeply nested task has multiple indents."""
task = Task(state="open", title="Deep subtask", indent=3)
assert task.to_markdown_line() == " - [ ] Deep subtask"
class TestToMarkdownBlock:
"""Tests for Task.to_markdown_block()."""
def test_single_task(self) -> None:
"""Single task without subtasks."""
task = Task(state="open", title="Task", indent=0)
assert task.to_markdown_block() == "- [ ] Task"
def test_with_subtasks(self) -> None:
"""Task with subtasks renders as block."""
subtask1 = Task(state="open", title="Step 1", indent=1)
subtask2 = Task(state="done", title="Step 2", indent=1)
task = Task(state="open", title="Parent", indent=0, subtasks=[subtask1, subtask2])
expected = """- [ ] Parent
- [ ] Step 1
- [x] Step 2"""
assert task.to_markdown_block() == expected
def test_nested_subtasks(self) -> None:
"""Nested subtasks render correctly."""
grandchild = Task(state="open", title="Grandchild", indent=2)
child = Task(state="open", title="Child", indent=1, subtasks=[grandchild])
parent = Task(state="open", title="Parent", indent=0, subtasks=[child])
expected = """- [ ] Parent
- [ ] Child
- [ ] Grandchild"""
assert parent.to_markdown_block() == expected
class TestEquality:
"""Tests for Task equality methods."""
def test_content_eq_same_content(self) -> None:
"""Tasks with same content are content-equal."""
task1 = Task(state="open", title="Task", tags=["urgent"])
task2 = Task(state="open", title="Task", tags=["urgent"])
assert task1.content_eq(task2)
def test_content_eq_different_provenance(self) -> None:
"""Tasks with same content but different provenance are content-equal."""
task1 = Task(
state="open",
title="Task",
tags=["urgent"],
inherited_tags=[],
explicit_tags=["urgent"],
)
task2 = Task(
state="open",
title="Task",
tags=["urgent"],
inherited_tags=["urgent"],
explicit_tags=[],
)
assert task1.content_eq(task2)
def test_content_eq_different_location(self) -> None:
"""Tasks with same content but different location are content-equal."""
task1 = Task(state="open", title="Task", line=10)
task2 = Task(state="open", title="Task", line=20)
assert task1.content_eq(task2)
def test_content_eq_different_content(self) -> None:
"""Tasks with different content are not content-equal."""
task1 = Task(state="open", title="Task A")
task2 = Task(state="open", title="Task B")
assert not task1.content_eq(task2)
def test_content_eq_subtasks(self) -> None:
"""Content equality includes subtasks."""
sub1 = Task(state="open", title="Sub")
sub2 = Task(state="open", title="Sub")
task1 = Task(state="open", title="Parent", subtasks=[sub1])
task2 = Task(state="open", title="Parent", subtasks=[sub2])
assert task1.content_eq(task2)
def test_content_hash_same_content(self) -> None:
"""Same content produces same hash."""
task1 = Task(state="open", title="Task", tags=["urgent"])
task2 = Task(state="open", title="Task", tags=["urgent"])
assert task1.content_hash() == task2.content_hash()
def test_content_hash_different_provenance(self) -> None:
"""Different provenance produces same hash."""
task1 = Task(state="open", title="Task", inherited_tags=["a"])
task2 = Task(state="open", title="Task", inherited_tags=["b"])
assert task1.content_hash() == task2.content_hash()
def test_identical_to_same(self) -> None:
"""Identical tasks are identical."""
task1 = Task(state="open", title="Task", line=10, inherited_tags=["a"])
task2 = Task(state="open", title="Task", line=10, inherited_tags=["a"])
assert task1.identical_to(task2)
def test_identical_to_different_provenance(self) -> None:
"""Different provenance means not identical."""
task1 = Task(state="open", title="Task", inherited_tags=["a"])
task2 = Task(state="open", title="Task", inherited_tags=["b"])
assert not task1.identical_to(task2)
def test_identical_to_different_location(self) -> None:
"""Different location means not identical."""
task1 = Task(state="open", title="Task", line=10)
task2 = Task(state="open", title="Task", line=20)
assert not task1.identical_to(task2)
class TestFormatDuration:
"""Tests for Task._format_duration()."""
def test_minutes_only(self) -> None:
"""Under 60 minutes formats as ~Xm."""
assert Task._format_duration(30) == "~30m"
assert Task._format_duration(1) == "~1m"
assert Task._format_duration(59) == "~59m"
def test_hours_only(self) -> None:
"""Even hours format as ~Xh."""
assert Task._format_duration(60) == "~1h"
assert Task._format_duration(120) == "~2h"
assert Task._format_duration(480) == "~8h"
def test_hours_and_minutes(self) -> None:
"""Mixed formats as ~XhYm."""
assert Task._format_duration(90) == "~1h30m"
assert Task._format_duration(150) == "~2h30m"
assert Task._format_duration(61) == "~1h1m"