storage_test.ml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 equals_string = Assrt.equals_string
  27. let equals_int = Assrt.equals_int
  28. open Seppo_lib
  29. open Storage
  30. let test_dir_of_ix () =
  31. Logr.info (fun m -> m "%s.%s" "storage_test" "test_dir_of_ix");
  32. let a,b = "app/var/db/o/p/23.s" |> Page.of_fn |> Option.get in
  33. a |> equals_string __LOC__ "o/p";
  34. b |> equals_int __LOC__ 23;
  35. let a,_ = "app/var/db/o/t/foo/23.s" |> Page.of_fn |> Option.get in
  36. a |> equals_string __LOC__ "o/t/foo";
  37. ()
  38. let test_tuple () =
  39. Logr.info (fun m -> m "storage_test.test_tuple");
  40. Logr.info (fun m -> m "%s.%s" "storage_test" "test_tuple");
  41. (23,42) |> TwoPad10.to_string |> equals_string __LOC__ "(10:0x0000001710:0x0000002a)";
  42. (0x3fff_ffff,42) |> TwoPad10.to_string |> equals_string __LOC__ "(10:0x3fffffff10:0x0000002a)";
  43. let (a,b) = "(10:000000002310:0000000042)"
  44. |> Csexp.parse_string_many
  45. |> Result.value ~default:[]
  46. |> TwoPad10.decode_many
  47. |> List.hd in
  48. a |> equals_int __LOC__ 23;
  49. b |> equals_int __LOC__ 42;
  50. assert true
  51. (*
  52. let test_json () =
  53. let minify = false in
  54. let base = Uri.of_string "https://example.com/su/" in
  55. let item = Rfc4287_test.mk_sample () in
  56. item |> As2.Note.mk_note_json ~base
  57. |> As2.Note.mk_create_json ~base item
  58. |> Ezjsonm.to_string ~minify
  59. |> eq_s __LOC__ {|{
  60. "type": "Create",
  61. "id": "https://example.com/su/o/p-12/#23/Create",
  62. "actor": "https://example.com/su/activitypub/",
  63. "published": "2023-02-11T11:07:23+01:00",
  64. "to": [
  65. "https://www.w3.org/ns/activitystreams#Public"
  66. ],
  67. "cc": [
  68. "https://example.com/su/activitypub/followers/"
  69. ],
  70. "object": {
  71. "type": "Note",
  72. "id": "o/p-12/#23",
  73. "actor": "activitypub/",
  74. "to": [
  75. "https://www.w3.org/ns/activitystreams#Public"
  76. ],
  77. "cc": [
  78. "activitypub/followers/"
  79. ],
  80. "mediaType": "text/plain; charset=utf8",
  81. "content": "I am happy to announce the premiere release of #Seppo!, Personal #Social #Media under funding of NLnet.nl.\n\nFind it at https://Seppo.Social/downloads/\n\nIt has no notable user facing #ActivityPub features so far, but\n\n- easy setup of instance & account,\n- #webfinger discoverability (from e.g. mastodon search),\n- a welcoming, long-term reliable website.\n\nI made this embarrassingly limited release to build awareness for low-barrier-entry internet services in general and especially in the field of personal communication as well as letting the #fediverse and #permacomputing communities know.\n\nYour comments are very much appreciated.",
  82. "sensitive": false,
  83. "summary": "#Announce Seppo.Social v0.1 and Request for Comments.",
  84. "published": "2023-02-11T10:07:23Z",
  85. "tags": [
  86. {
  87. "type": "Hashtag",
  88. "href": "o/t/webfinger/",
  89. "name": "#webfinger"
  90. },
  91. {
  92. "type": "Hashtag",
  93. "href": "o/t/Social/",
  94. "name": "#Social"
  95. },
  96. {
  97. "type": "Hashtag",
  98. "href": "o/t/Seppo/",
  99. "name": "#Seppo"
  100. },
  101. {
  102. "type": "Hashtag",
  103. "href": "o/t/permacomputing/",
  104. "name": "#permacomputing"
  105. },
  106. {
  107. "type": "Hashtag",
  108. "href": "o/t/Media/",
  109. "name": "#Media"
  110. },
  111. {
  112. "type": "Hashtag",
  113. "href": "o/t/Fediverse/",
  114. "name": "#Fediverse"
  115. },
  116. {
  117. "type": "Hashtag",
  118. "href": "o/t/Announce/",
  119. "name": "#Announce"
  120. },
  121. {
  122. "type": "Hashtag",
  123. "href": "o/t/ActivityPub/",
  124. "name": "#ActivityPub"
  125. }
  126. ]
  127. }
  128. }|}
  129. *)
  130. let test_strut () =
  131. Logr.info (fun m -> m "%s -> %s" __FILE__ "strut");
  132. let strut' (p0,p1 as s) =
  133. let r = s |> TwoPad10.strut |> Csexp.to_string in
  134. Logr.info (fun m -> m "%s.%s %d %s" "" "" (p1-p0) r);
  135. r
  136. in
  137. (0,6) |> strut' |> equals_string __LOC__ "(0:0:)";
  138. (0,7) |> strut' |> equals_string __LOC__ "(0:1:x)";
  139. (0,8) |> strut' |> equals_string __LOC__ "(0:2:xx)";
  140. (0,9) |> strut' |> equals_string __LOC__ "(0:3:xxx)";
  141. (0,14) |> strut' |> equals_string __LOC__ "(0:8:xxxxxxxx)";
  142. (0,15) |> strut' |> equals_string __LOC__ "(0:9:xxxxxxxxx)";
  143. (0,16) |> strut' |> equals_string __LOC__ "(1:x9:xxxxxxxxx)";
  144. (0,17) |> strut' |> equals_string __LOC__ "(0:10:xxxxxxxxxx)";
  145. (0,18) |> strut' |> equals_string __LOC__ "(0:11:xxxxxxxxxxx)";
  146. (0,106) |> strut' |> equals_string __LOC__ "(0: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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)";
  152. assert true
  153. module Page = struct
  154. let test_jig () =
  155. let j = "%-%/index.xml" |> Make.Jig.make in
  156. let v = "o/p-42/index.xml" |> Make.Jig.cut j |> Option.value ~default:[] in
  157. (match v with
  158. | [dir;idx] ->
  159. dir |> equals_string __LOC__ "o/p";
  160. idx |> equals_string __LOC__ "42"
  161. | _ -> failwith __LOC__);
  162. let dir,idx = "app/var/db/o/p/42.s" |> Storage.Page.of_fn |> Option.get in
  163. dir |> equals_string __LOC__ "o/p";
  164. idx |> equals_int __LOC__ 42
  165. let test_pred_succ () =
  166. let v = "app/var/db/o/p/42.s" |> Storage.Page.of_fn |> Option.get in
  167. let dir,idx = v |> Storage.Page.pred in
  168. dir |> equals_string __LOC__ "o/p";
  169. idx |> equals_int __LOC__ 41;
  170. let dir,idx = v |> Storage.Page.succ in
  171. dir |> equals_string __LOC__ "o/p";
  172. idx |> equals_int __LOC__ 43
  173. let test_other_feeds () =
  174. let _e = match
  175. Rfc4287.Entry.from_text_plain
  176. ~published:(Rfc3339.T "1970-01-01T00:00:00Z")
  177. ~author:Uri.empty
  178. ~lang:(Rfc4287.Rfc4646 "nl")
  179. ~uri:Uri.empty
  180. "title" "content" with
  181. | Ok o -> o
  182. | Error e -> failwith e
  183. in
  184. let s,i = match _e |> Storage.Page.other_feeds with
  185. | [x] -> x
  186. | _ -> failwith "ouch" in
  187. s |> Assrt.equals_string __LOC__ "o/d/1970-01-01";
  188. i |> Assrt.equals_int __LOC__ (-3)
  189. end
  190. module TwoPad10 = struct
  191. let _test_id_to_page_i () =
  192. Logr.info (fun m -> m "%s" __LOC__);
  193. let id = "o/p-12/#35" |> Uri.of_string in
  194. match id |> Storage.TwoPad10.id_to_page_i with
  195. | Ok ((f,j),i) ->
  196. f |> Assrt.equals_string __LOC__ "o/p";
  197. j |> Assrt.equals_int __LOC__ 12;
  198. i |> Assrt.equals_int __LOC__ 35;
  199. | _ -> failwith __LOC__
  200. let _test_from_id' () =
  201. Logr.info (fun m -> m "%s" __LOC__);
  202. let _a,_b = "o/p-12/#35"
  203. |> Uri.of_string
  204. |> Storage.TwoPad10.from_id ~prefix:"data/"
  205. |> Result.get_ok in
  206. ()
  207. end
  208. let () =
  209. (* Unix.chdir "../../../test/"; *)
  210. test_dir_of_ix ();
  211. test_tuple ();
  212. test_strut ();
  213. (* test_json (); *)
  214. Page.test_jig ();
  215. Page.test_pred_succ ();
  216. Page.test_other_feeds ();
  217. (*
  218. TwoPad10.test_id_to_page_i ();
  219. TwoPad10.test_from_id' ();
  220. *)
  221. assert true