open Alcotest open Pam.Datatypes let pprint_ptime ppf x = Fmt.pf ppf "%s" (Ptime.to_rfc3339 x) let ptime_testable = Alcotest.testable pprint_ptime Ptime.equal let test_parse () = let body = {|@alert: -12:38 10:02 +09:24 --10:00 +++23:00 ## Description Specify a time format by writing the exact time of the alert, then adding zero or more '+' or '-'. Examples, due date is 1/1/1970: 1. 12:38: you get the alert 1/1/1970 at 12:38 2. -12:38: you get the alert 31/12/1969 at 12:38 3. --12:38: you get the alert 30/12/1969 at 12:38 4. +12:38: you get the alert 2/1/1970 at 12:38 |} in let m = MatrixRoom.make "test" in let reminder = {url="example.org"; title= "example"; due_date= Some "2024-04-20T23:59:59+02:00"; body=body; matrix_target=m} in let expected = [ "2024-04-19T12:38:00+02:00" ; "2024-04-20T10:02:00+02:00" ; "2024-04-21T09:24:00+02:00" ; "2024-04-18T10:00:00+02:00" ; "2024-04-23T23:00:00+02:00" ; ] |> List.map (fun e -> e |> Ptime.of_rfc3339 |> Result.get_ok |> function | t, _, _ -> t) in let res = Pam.Issue_parser.to_datetime reminder in match res with | Ok None -> Alcotest.fail "Got Ok None" | Error e -> Alcotest.fail [%string "Got Error %{e}"] | Ok (Some got) -> Alcotest.check (list ptime_testable) "Alert times" expected got let test_fire () = let should_alert (lst, now) = Pam.Issue_parser.should_alert lst now in let of_rfc x = x |> Ptime.of_rfc3339 |> Result.get_ok |> function | t, _, _ -> t in let transform (lst, clock) = (List.map of_rfc lst, of_rfc clock) in let alerts0 = (["2024-04-19T12:38:00+02:00"], "2024-04-19T06:38:00-04:00") |> transform in let alerts1 = (["2024-04-20T10:02:00+02:00"], "2024-04-20T10:08:00+00:00") |> transform in let alerts2 = (["2024-04-20T10:02:00+02:00"], "2024-04-20T10:08:00+04:00") |> transform in let alerts3 = (["2024-04-20T10:02:00+02:00"], "2024-04-22T12:00:00+00:00") |> transform in let alerts4 = (["2024-04-20T10:02:00+02:00"], "2024-04-22T12:00:00+02:00") |> transform in let alerts5 = (["2024-04-20T10:02:00+02:00" ; "2024-05-20T10:02:00+02:00"], "2024-04-22T12:00:00+02:00") |> transform in let alerts6 = (["2024-04-20T10:02:00+02:00" ; "2024-05-20T10:02:00+02:00"], "2024-04-22T12:00:00+00:00") |> transform in ( Alcotest.check (bool) "exact moment" true ( should_alert alerts0 ) ; Alcotest.check (bool) "alert is in the future" false ( should_alert alerts1 ) ; Alcotest.check (bool) "alert is in the past" false ( should_alert alerts2 ) ; Alcotest.check (bool) "alert is in the past but it is noon" true ( should_alert alerts3 ) ; Alcotest.check (bool) "alert is in the past but it is non-utc noon" false ( should_alert alerts4 ) ; Alcotest.check (bool) "one alert in the past, one in the future" false ( should_alert alerts5 ) ; Alcotest.check (bool) "one alert in the past, one in the future, it is noon" false ( should_alert alerts6 ) ; ) let () = Alcotest.run "test1" [ "alert_times", [test_case "Test Alert Times" `Quick test_parse] ; "send_alerts", [test_case "Test sending alerts" `Quick test_fire] ]