todo_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package caldav
  2. import (
  3. "strings"
  4. "testing"
  5. "time"
  6. )
  7. func TestReminderToICS_BasicFields(t *testing.T) {
  8. rm := ReminderItem{
  9. ID: "rm_test1",
  10. Title: "Buy milk",
  11. Notes: "Two cartons",
  12. Priority: 3, // High
  13. DueDate: "2024-03-15",
  14. DueTime: "14:30",
  15. URL: "https://example.com",
  16. }
  17. ics := reminderToICS(rm)
  18. checks := []string{
  19. "BEGIN:VCALENDAR",
  20. "BEGIN:VTODO",
  21. "UID:rm_test1@arozos",
  22. "SUMMARY:Buy milk",
  23. "DESCRIPTION:Two cartons",
  24. "DUE:20240315T143000",
  25. "PRIORITY:1", // High -> 1
  26. "STATUS:NEEDS-ACTION",
  27. "URL:https://example.com",
  28. "END:VTODO",
  29. "END:VCALENDAR",
  30. }
  31. for _, want := range checks {
  32. if !strings.Contains(ics, want) {
  33. t.Errorf("reminderToICS: missing %q in output:\n%s", want, ics)
  34. }
  35. }
  36. }
  37. func TestReminderToICS_AllDayDue(t *testing.T) {
  38. rm := ReminderItem{ID: "rm_ad", Title: "Pay rent", DueDate: "2024-04-01"}
  39. ics := reminderToICS(rm)
  40. if !strings.Contains(ics, "DUE;VALUE=DATE:20240401") {
  41. t.Errorf("expected date-only DUE, got:\n%s", ics)
  42. }
  43. }
  44. func TestReminderToICS_Completed(t *testing.T) {
  45. completedAt := time.Date(2024, 3, 16, 9, 0, 0, 0, time.UTC).UnixMilli()
  46. rm := ReminderItem{ID: "rm_done", Title: "Done", Completed: true, CompletedAt: completedAt}
  47. ics := reminderToICS(rm)
  48. for _, want := range []string{"STATUS:COMPLETED", "PERCENT-COMPLETE:100", "COMPLETED:20240316T090000Z"} {
  49. if !strings.Contains(ics, want) {
  50. t.Errorf("reminderToICS completed: missing %q in:\n%s", want, ics)
  51. }
  52. }
  53. }
  54. func TestReminderToICS_Recurring(t *testing.T) {
  55. rm := ReminderItem{ID: "rm_rec", Title: "Standup", DueDate: "2024-03-15", RRule: "FREQ=WEEKLY"}
  56. ics := reminderToICS(rm)
  57. if !strings.Contains(ics, "RRULE:FREQ=WEEKLY") {
  58. t.Errorf("expected RRULE in recurring reminder, got:\n%s", ics)
  59. }
  60. }
  61. func TestICSToReminder_RoundTrip(t *testing.T) {
  62. original := ReminderItem{
  63. ID: "rm_rt",
  64. Title: "Round trip",
  65. Notes: "some notes",
  66. Priority: 2, // Medium
  67. DueDate: "2024-06-01",
  68. DueTime: "08:15",
  69. URL: "https://example.org",
  70. RRule: "FREQ=DAILY;INTERVAL=2",
  71. }
  72. ics := reminderToICS(original)
  73. parsed, err := icsToReminder(ics, "rm_rt")
  74. if err != nil {
  75. t.Fatalf("icsToReminder error: %v", err)
  76. }
  77. if parsed.Title != original.Title {
  78. t.Errorf("Title: got %q want %q", parsed.Title, original.Title)
  79. }
  80. if parsed.Notes != original.Notes {
  81. t.Errorf("Notes: got %q want %q", parsed.Notes, original.Notes)
  82. }
  83. if parsed.Priority != original.Priority {
  84. t.Errorf("Priority: got %d want %d", parsed.Priority, original.Priority)
  85. }
  86. if parsed.DueDate != original.DueDate {
  87. t.Errorf("DueDate: got %q want %q", parsed.DueDate, original.DueDate)
  88. }
  89. if parsed.DueTime != original.DueTime {
  90. t.Errorf("DueTime: got %q want %q", parsed.DueTime, original.DueTime)
  91. }
  92. if parsed.URL != original.URL {
  93. t.Errorf("URL: got %q want %q", parsed.URL, original.URL)
  94. }
  95. if parsed.RRule != original.RRule {
  96. t.Errorf("RRule: got %q want %q", parsed.RRule, original.RRule)
  97. }
  98. }
  99. func TestICSToReminder_iOSCompleted(t *testing.T) {
  100. ics := "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VTODO\r\n" +
  101. "UID:ios-task@icloud.com\r\n" +
  102. "SUMMARY:Finish report\r\n" +
  103. "STATUS:COMPLETED\r\n" +
  104. "PERCENT-COMPLETE:100\r\n" +
  105. "COMPLETED:20240320T120000Z\r\n" +
  106. "END:VTODO\r\nEND:VCALENDAR\r\n"
  107. rm, err := icsToReminder(ics, "rm_url")
  108. if err != nil {
  109. t.Fatalf("icsToReminder error: %v", err)
  110. }
  111. if !rm.Completed {
  112. t.Error("expected reminder to be completed")
  113. }
  114. if rm.CompletedAt == 0 {
  115. t.Error("expected CompletedAt to be set")
  116. }
  117. if rm.Title != "Finish report" {
  118. t.Errorf("Title: got %q", rm.Title)
  119. }
  120. }
  121. func TestParseICSDue(t *testing.T) {
  122. cases := []struct {
  123. key, val string
  124. wantDate string
  125. wantTime string
  126. description string
  127. }{
  128. {"DUE", "20240315T143000", "2024-03-15", "14:30", "floating datetime"},
  129. {"DUE", "20240315T143000Z", "2024-03-15", "14:30", "utc datetime treated as wall-clock"},
  130. {"DUE;VALUE=DATE", "20240401", "2024-04-01", "", "all-day"},
  131. }
  132. for _, tc := range cases {
  133. d, tm := parseICSDue(tc.key, tc.val)
  134. if d != tc.wantDate || tm != tc.wantTime {
  135. t.Errorf("parseICSDue(%s) [%s]: got (%q,%q) want (%q,%q)", tc.val, tc.description, d, tm, tc.wantDate, tc.wantTime)
  136. }
  137. }
  138. }
  139. func TestPriorityMapping(t *testing.T) {
  140. // ArozOS -> ICS -> ArozOS should be stable for the four canonical levels.
  141. for _, p := range []int{0, 1, 2, 3} {
  142. ics := arozPriorityToICS(p)
  143. back := icsPriorityToAroz(ics)
  144. if back != p {
  145. t.Errorf("priority round trip: %d -> ICS %d -> %d", p, ics, back)
  146. }
  147. }
  148. // Spot-check the iOS-facing values.
  149. if arozPriorityToICS(3) != 1 {
  150. t.Errorf("High should map to ICS PRIORITY 1, got %d", arozPriorityToICS(3))
  151. }
  152. if icsPriorityToAroz(5) != 2 {
  153. t.Errorf("ICS PRIORITY 5 should map to Medium, got %d", icsPriorityToAroz(5))
  154. }
  155. }
  156. func TestRemindersCTag_ChangesWithData(t *testing.T) {
  157. a := []ReminderItem{{ID: "a", Title: "A"}}
  158. b := []ReminderItem{{ID: "a", Title: "A"}, {ID: "b", Title: "B"}}
  159. if remindersCTag(a) == remindersCTag(b) {
  160. t.Error("remindersCTag should differ when reminders differ")
  161. }
  162. }
  163. func TestParseResourcePath(t *testing.T) {
  164. cases := []struct {
  165. path string
  166. wantColl string
  167. wantID string
  168. }{
  169. {"/alice/calendar/ev1.ics", "calendar", "ev1"},
  170. {"/alice/reminders/rm1.ics", "reminders", "rm1"},
  171. {"/alice/reminders/", "", ""},
  172. {"/alice/", "", ""},
  173. {"/alice/unknown/x.ics", "", ""},
  174. }
  175. for _, tc := range cases {
  176. coll, id := parseResourcePath(tc.path)
  177. if coll != tc.wantColl || id != tc.wantID {
  178. t.Errorf("parseResourcePath(%q): got (%q,%q) want (%q,%q)", tc.path, coll, id, tc.wantColl, tc.wantID)
  179. }
  180. }
  181. }
  182. func TestNormalizeRRule(t *testing.T) {
  183. cases := []struct{ in, want string }{
  184. {"FREQ=DAILY", "FREQ=DAILY"},
  185. {"RRULE:FREQ=WEEKLY", "FREQ=WEEKLY"},
  186. {" FREQ=MONTHLY\r\n", "FREQ=MONTHLY"},
  187. {"", ""},
  188. }
  189. for _, tc := range cases {
  190. if got := normalizeRRule(tc.in); got != tc.want {
  191. t.Errorf("normalizeRRule(%q): got %q want %q", tc.in, got, tc.want)
  192. }
  193. }
  194. }
  195. func TestEventToICS_Recurring(t *testing.T) {
  196. ev := CalendarEvent{
  197. ID: "ev_rec",
  198. Title: "Weekly sync",
  199. Start: time.Date(2024, 3, 15, 10, 0, 0, 0, time.UTC).UnixMilli(),
  200. End: time.Date(2024, 3, 15, 11, 0, 0, 0, time.UTC).UnixMilli(),
  201. RRule: "FREQ=WEEKLY;BYDAY=MO",
  202. }
  203. ics := eventToICS(ev)
  204. if !strings.Contains(ics, "RRULE:FREQ=WEEKLY;BYDAY=MO") {
  205. t.Errorf("expected RRULE in recurring event, got:\n%s", ics)
  206. }
  207. parsed, err := icsToEvent(ics, "ev_rec")
  208. if err != nil {
  209. t.Fatalf("icsToEvent error: %v", err)
  210. }
  211. if parsed.RRule != "FREQ=WEEKLY;BYDAY=MO" {
  212. t.Errorf("event RRule round trip: got %q", parsed.RRule)
  213. }
  214. }