lanonna/pam/test/test_pam.ml

63 lines
3.3 KiB
OCaml
Raw Normal View History

2024-04-24 09:33:01 +02:00
open Alcotest
2024-04-24 09:34:19 +02:00
open Pam.Datatypes
2024-04-23 09:35:10 +02:00
2024-04-24 09:33:01 +02:00
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 () =
2024-04-24 09:34:19 +02:00
let body = {|@alert: -12:38 10:02 +09:24 --10:00 +++23:00
2024-04-24 09:33:01 +02: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 |}
2024-04-23 09:35:10 +02:00
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
2024-04-24 09:33:01 +02:00
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-24 09:34:19 +02:00
"2024-04-18T10:00:00+02:00" ;
"2024-04-23T23:00:00+02:00" ;
2024-04-24 09:33:01 +02:00
] |> List.map (fun e -> e |> Ptime.of_rfc3339 |> Result.get_ok |> function | t, _, _ -> t) in
2024-05-01 12:03:20 +02:00
let res = Pam.Issuelib.to_datetime reminder in
2024-04-23 09:35:10 +02:00
match res with
2024-04-24 09:33:01 +02:00
| 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
2024-04-23 09:35:10 +02:00
2024-04-30 12:01:58 +02:00
let test_fire () =
2024-05-01 12:03:20 +02:00
let should_alert (lst, now) = Pam.Issuelib.should_alert now lst in
2024-04-30 12:01:58 +02:00
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
2024-05-01 12:03:20 +02:00
let alerts7 = (["2024-04-19T12:38:00+02:00"], "2024-04-19T06:38:32-04:00") |> transform in
2024-04-30 12:01:58 +02:00
(
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 ) ;
2024-05-01 12:03:20 +02:00
Alcotest.check (bool) "same time, different seconds" true ( should_alert alerts7 ) ;
2024-04-30 12:01:58 +02:00
)
2024-04-23 09:35:10 +02:00
let () =
2024-04-24 09:33:01 +02:00
Alcotest.run "test1" [
2024-04-30 12:01:58 +02:00
"alert_times", [test_case "Test Alert Times" `Quick test_parse] ;
"send_alerts", [test_case "Test sending alerts" `Quick test_fire]
2024-04-24 09:33:01 +02:00
]