summaryrefslogtreecommitdiff
path: root/ocaml/scripts/test_lmdb_eventlog.ml
blob: e7a4971b74c15b1676a8640d3ce9028c671834e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
open Nock_lib

let rec find_project_root dir =
  let pills_dir = Filename.concat dir "pills" in
  if Sys.file_exists pills_dir && Sys.is_directory pills_dir then dir
  else
    let parent = Filename.dirname dir in
    if String.equal parent dir then failwith "unable to locate project root containing pills/"
    else find_project_root parent

let project_root =
  match Sys.getenv_opt "NEOVERE_ROOT" with
  | Some root -> root
  | None ->
      let exe_dir = Filename.dirname Sys.executable_name in
      find_project_root exe_dir

let () =
  Printf.printf "\n╔═══════════════════════════════════════════════════════╗\n";
  Printf.printf "║  LMDB Event Log Test                                  ║\n";
  Printf.printf "╚═══════════════════════════════════════════════════════╝\n\n";

  let test_pier = Filename.concat project_root "test-pier-lmdb" in

  (* Clean up old test pier if it exists *)
  if Sys.file_exists test_pier then begin
    Printf.printf "Removing old test pier...\n";
    ignore (Sys.command (Printf.sprintf "rm -rf %s" test_pier))
  end;

  (* Create test pier directory *)
  Unix.mkdir test_pier 0o755;

  Printf.printf "Creating LMDB event log...\n";
  let log = Eventlog_lmdb.create test_pier in

  Printf.printf "Creating test events...\n\n";

  (* Create some test events *)
  let test_events = [
    Noun.atom (Z.of_int 42);
    Noun.cell (Noun.atom (Z.of_int 1)) (Noun.atom (Z.of_int 2));
    Noun.cell (Noun.atom (Z.of_int 3)) (Noun.cell (Noun.atom (Z.of_int 4)) (Noun.atom Z.zero));
    Noun.atom (Z.of_int 0xdeadbeef);
    Noun.cell (Noun.atom (Z.of_bits "hello")) (Noun.atom Z.zero);
  ] in

  (* Write events *)
  Printf.printf "Writing %d events to LMDB...\n" (List.length test_events);
  List.iteri (fun i event ->
    let event_num = Eventlog_lmdb.append log event in
    Printf.printf "  Event %d: wrote as event number %Ld\n" (i+1) event_num
  ) test_events;

  (* Sync to disk *)
  Printf.printf "\nSyncing to disk...\n";
  Eventlog_lmdb.sync log;

  (* Check gulf *)
  Printf.printf "\nChecking event range (gulf)...\n";
  begin match Eventlog_lmdb.gulf log with
  | None -> Printf.printf "  No events in log\n"
  | Some (first, last) ->
      Printf.printf "  First event: %Ld, Last event: %Ld\n" first last
  end;

  (* Read events back *)
  Printf.printf "\nReading events back...\n";
  for i = 1 to List.length test_events do
    let event_num = Int64.of_int i in
    let event = Eventlog_lmdb.read_event log event_num in
    Printf.printf "  Event %Ld: %s\n" event_num
      (if Noun.is_cell event then "cell" else "atom")
  done;

  (* Test replay *)
  Printf.printf "\nTesting replay...\n";
  let replay_count = ref 0 in
  Eventlog_lmdb.replay log (fun event_num _noun ->
    incr replay_count;
    Printf.printf "  Replayed event %Ld\n" event_num
  );
  Printf.printf "Replayed %d events total\n" !replay_count;

  (* Close the log *)
  Printf.printf "\nClosing event log...\n";
  Eventlog_lmdb.close log;

  (* Reopen and verify *)
  Printf.printf "Reopening event log...\n";
  let log2 = Eventlog_lmdb.create test_pier in
  let last = Eventlog_lmdb.last_event log2 in
  Printf.printf "  Last event after reopen: %Ld\n" last;

  if last = Int64.of_int (List.length test_events) then begin
    Printf.printf "\n✓ LMDB event log test PASSED!\n";
    Printf.printf "  - Wrote %d events\n" (List.length test_events);
    Printf.printf "  - Read all events back successfully\n";
    Printf.printf "  - Replay worked correctly\n";
    Printf.printf "  - Persistence verified after reopen\n"
  end else begin
    Printf.printf "\n✗ LMDB event log test FAILED!\n";
    Printf.printf "  Expected last event: %d, Got: %Ld\n"
      (List.length test_events) last;
    exit 1
  end;

  Eventlog_lmdb.close log2;

  Printf.printf "\nTest pier created at: %s\n" test_pier;
  Printf.printf "LMDB files:\n";
  ignore (Sys.command (Printf.sprintf "ls -lh %s/.urb/log/" test_pier));

  Printf.printf "\n"