t_main.ml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. (*
  2. * _ _ ____ _
  3. * _| || |_/ ___| ___ _ __ _ __ ___ | |
  4. * |_ .. _\___ \ / _ \ '_ \| '_ \ / _ \| |
  5. * |_ _|___) | __/ |_) | |_) | (_) |_|
  6. * |_||_| |____/ \___| .__/| .__/ \___/(_)
  7. * |_| |_|
  8. *
  9. * Personal Social Web.
  10. *
  11. * Copyright (C) The #Seppo contributors. All rights reserved.
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *)
  26. let tail x =
  27. Assrt.equals_string __LOC__ "ok" (if x |> Result.is_ok then "ok" else "no")
  28. open Seppo_lib
  29. open Alcotest
  30. let set_up = "setup", `Quick, (fun () ->
  31. Mirage_crypto_rng_lwt.initialize (module Mirage_crypto_rng.Fortuna);
  32. Unix.chdir "../../../test/"
  33. )
  34. let test_sift_handles () =
  35. let h = {|Some text with @
  36. linebreaks and @sepp0@example.com. And
  37. @other@example.com, even a @third@example.com!|} |> Main.find_handles in
  38. match h with
  39. | [a;b;c] ->
  40. a |> Assrt.equals_string __LOC__ "third@example.com";
  41. b |> Assrt.equals_string __LOC__ "other@example.com";
  42. c |> Assrt.equals_string __LOC__ "sepp0@example.com"
  43. | l -> l |> List.length |> Assrt.equals_int __LOC__ 30000
  44. let fn = Mapcdb.Cdb "tmp/main.cdb"
  45. let Mapcdb.Cdb fn' = fn
  46. let test_sift_tags () =
  47. Logr.debug (fun m -> m "%s.%s" "Main_test" "test_sift_tags");
  48. let open Rfc4287 in
  49. Unix.(try unlink fn' with Unix_error (ENOENT, "unlink", _) -> ());
  50. File.touch fn';
  51. let published = Rfc3339.T "2023-03-09T12:34:56+01:00" in
  52. let author = {Rfc4287.Person.empty with name = "uhu"} in
  53. let lang = Rfc4646 "en" in
  54. let uri = Uri.empty in
  55. let title = {|Just a #note|} in
  56. let content = {|th some #more #Hashtags.|} in
  57. (let* e = Entry.from_text_plain ~published ~author ~lang ~uri title content in
  58. let* e : Entry.t = Main.sift_tags fn e in
  59. let li = e.categories in
  60. li |> List.length |> Assrt.equals_int __LOC__ 3;
  61. let (Category.Label (Single la),Category.Term (Single te),sc) = li |> List.hd in
  62. la |> Assrt.equals_string __LOC__ "note";
  63. te |> Assrt.equals_string __LOC__ "note";
  64. sc |> Uri.to_string |> Assrt.equals_string __LOC__ "o/t/";
  65. let li = li |> List.tl in
  66. let (Category.Label (Single la),Category.Term (Single te),sc) = li |> List.hd in
  67. la |> Assrt.equals_string __LOC__ "more";
  68. te |> Assrt.equals_string __LOC__ "more";
  69. sc |> Uri.to_string |> Assrt.equals_string __LOC__ "o/t/";
  70. let li = li |> List.tl in
  71. let (Category.Label (Single la),Category.Term (Single te),sc) = li |> List.hd in
  72. la |> Assrt.equals_string __LOC__ "Hashtags";
  73. te |> Assrt.equals_string __LOC__ "Hashtags";
  74. sc |> Uri.to_string |> Assrt.equals_string __LOC__ "o/t/";
  75. let li = li |> List.tl in
  76. li |> List.length |> Assrt.equals_int __LOC__ 0;
  77. Ok e) |> tail;
  78. assert true
  79. let test_tmap_from_cdb () =
  80. let tm = Tag.Tmap.empty in
  81. let tm = Tag.Tmap.add "a" "A" tm in
  82. Tag.Tmap.find "a" tm |> Assrt.equals_string __LOC__ "A";
  83. assert true
  84. module Apjob = struct
  85. module Notify = struct
  86. let encode = "encode", `Quick, (fun () ->
  87. Main.Apjob.Notify.encode
  88. ("https://example.com/sender/message#3462" |> Uri.of_string)
  89. ("https://example.com/recipient/inbox" |> Uri.of_string, "unused" |> Uri.of_string)
  90. (Ezjsonm.int 17495)
  91. |> Csexp.to_string
  92. |> check string __LOC__ {|(1:239:https://example.com/sender/message#34626:notify(35:https://example.com/recipient/inbox6:unused5:17495))|}
  93. )
  94. end
  95. end
  96. let () =
  97. run
  98. "seppo_suite" [
  99. __FILE__ , [
  100. set_up;
  101. Apjob.Notify.encode;
  102. ]
  103. ];
  104. test_sift_handles ();
  105. test_sift_tags ();
  106. test_tmap_from_cdb ();
  107. assert true