AtlatestRepositoryfolio

folio / tree / testtest-task-note-linking.sgl

1(import (sigil test)
2 (sigil struct)
3 (sigil string)
4 (sigil dict)
5 (sigil fs)
6 (sigil path)
7 (folio store)
8 (folio note)
9 (folio task)
10 (folio id)
11 (folio project)
12 (folio inbox)
13 (folio frontmatter))
15;; ============================================================
16;; note-tasks accessor
17;; ============================================================
19(test-group "note-tasks"
20 (test "note without tasks returns empty list"
21 (call-with-temp-directory
22 (lambda (dir)
23 (let* ((store (make-folio-store dir))
24 (file (note-create! store "Plain Note" '("test") "No tasks.")))
25 (let ((note (read-note file)))
26 (assert-equal '() (note-tasks note)))))))
28 (test "note with tasks returns task IDs"
29 (call-with-temp-directory
30 (lambda (dir)
31 (let* ((store (make-folio-store dir))
32 (file (note-create! store "Task Note" '("brief") "Has tasks."
33 '("t-aaaa" "t-bbbb"))))
34 (let ((note (read-note file)))
35 (assert-equal '("t-aaaa" "t-bbbb") (note-tasks note)))))))
37 (test "note with tasks preserves tags"
38 (call-with-temp-directory
39 (lambda (dir)
40 (let* ((store (make-folio-store dir))
41 (file (note-create! store "Both" '("tag1" "tag2") "Content."
42 '("t-cccc"))))
43 (let ((note (read-note file)))
44 (assert-equal 2 (length (note-tags note)))
45 (assert-equal '("t-cccc") (note-tasks note))))))))
47;; ============================================================
48;; note-edit-tasks!
49;; ============================================================
51(test-group "note-edit-tasks!"
52 (test "adds tasks to note without tasks"
53 (call-with-temp-directory
54 (lambda (dir)
55 (let* ((store (make-folio-store dir))
56 (file (note-create! store "No Tasks" '("test") "Content.")))
57 (note-edit-tasks! file '("t-1111" "t-2222"))
58 (let ((note (read-note file)))
59 (assert-equal '("t-1111" "t-2222") (note-tasks note))
60 ;; Body preserved
61 (assert-true (string-contains? (folio-note-body note) "Content.")))))))
63 (test "replaces existing tasks"
64 (call-with-temp-directory
65 (lambda (dir)
66 (let* ((store (make-folio-store dir))
67 (file (note-create! store "Has Tasks" '() "Content."
68 '("t-old1"))))
69 (note-edit-tasks! file '("t-new1" "t-new2"))
70 (let ((note (read-note file)))
71 (assert-equal '("t-new1" "t-new2") (note-tasks note))))))))
73;; ============================================================
74;; note-create! with tasks (backward compatibility)
75;; ============================================================
77(test-group "note-create! backward compatibility"
78 (test "note-create! without tasks still works"
79 (call-with-temp-directory
80 (lambda (dir)
81 (let* ((store (make-folio-store dir))
82 (file (note-create! store "Compat" '("test") "Works.")))
83 (assert-true (file-exists? file))
84 (let ((note (read-note file)))
85 (assert-equal '() (note-tasks note))
86 (assert-true (string-contains? (folio-note-body note) "Works."))))))))
88;; ============================================================
89;; has-notes flag on tasks
90;; ============================================================
92(test-group "has-notes flag"
93 (test "task with has-notes shows in metadata"
94 (let ((task (parse-task-line "- [ ] Do thing {id: t-1234, has-notes: true}")))
95 (assert-equal "true" (task-metadata-ref task 'has-notes))))
97 (test "task without has-notes returns #f"
98 (let ((task (parse-task-line "- [ ] Do thing {id: t-1234}")))
99 (assert-false (task-metadata-ref task 'has-notes))))
101 (test "has-notes roundtrips through serialize"
102 (let* ((line "- [ ] Do thing {id: t-1234, has-notes: true}")
103 (task (parse-task-line line))
104 (result (task->line task)))
105 (assert-equal line result))))
107;; ============================================================
108;; find-notes-for-task (via note-tasks scanning)
109;; ============================================================
111(test-group "find notes for task"
112 (test "finds notes referencing a task"
113 (call-with-temp-directory
114 (lambda (dir)
115 (let ((store (make-folio-store dir)))
116 (note-create! store "Brief A" '("brief") "About task A."
117 '("t-aaaa"))
118 (note-create! store "Brief B" '("brief") "About task B."
119 '("t-bbbb"))
120 (note-create! store "Brief Both" '("brief") "About both."
121 '("t-aaaa" "t-bbbb"))
122 (let* ((notes (all-notes store))
123 (matching (filter (lambda (n)
124 (member "t-aaaa" (note-tasks n)))
125 notes)))
126 (assert-equal 2 (length matching)))))))
128 (test "no notes for unknown task"
129 (call-with-temp-directory
130 (lambda (dir)
131 (let ((store (make-folio-store dir)))
132 (note-create! store "Some Note" '() "Content." '("t-aaaa"))
133 (let* ((notes (all-notes store))
134 (matching (filter (lambda (n)
135 (member "t-zzzz" (note-tasks n)))
136 notes)))
137 (assert-equal 0 (length matching))))))))