this repo has no description
1(* Tests for preloaded package detection and CRC verification.
2
3 Uses Impl.check_preload_status directly (pure function, no JS deps)
4 with mock lookup functions to simulate different scenarios. *)
5
6open Js_top_worker.Impl
7
8let make_dcs ?(crcs = []) modules =
9 { dcs_url = "test://";
10 dcs_toplevel_modules = modules;
11 dcs_file_prefixes = [];
12 dcs_module_crcs = crcs }
13
14let print_status = function
15 | Preloaded -> print_string "Preloaded"
16 | Not_loaded -> print_string "Not_loaded"
17 | Partially_loaded { loaded; missing } ->
18 Printf.printf "Partially_loaded(loaded=[%s], missing=[%s])"
19 (String.concat "," loaded) (String.concat "," missing)
20 | Crc_mismatch ms ->
21 Printf.printf "Crc_mismatch([%s])" (String.concat "; " ms)
22
23(* Mock helpers *)
24let available_set = ref []
25let binary_crcs = ref []
26let server_crcs = ref []
27
28let is_available m = List.mem m !available_set
29let get_binary_crc m = List.assoc_opt m !binary_crcs
30let get_universe_crc m = List.assoc_opt m !server_crcs
31
32let setup ~available ~binary ~server =
33 available_set := available;
34 binary_crcs := binary;
35 server_crcs := server
36
37let check dcs =
38 check_preload_status ~is_available ~get_binary_crc ~get_universe_crc dcs
39
40let%expect_test "all modules linked, CRCs match — Preloaded" =
41 setup ~available:["Foo"; "Bar"]
42 ~binary:[("Foo", "abc123"); ("Bar", "def456")]
43 ~server:[("Foo", "abc123"); ("Bar", "def456")];
44 let dcs = make_dcs ~crcs:[("Foo", "abc123"); ("Bar", "def456")]
45 ["Foo"; "Bar"] in
46 print_status (check dcs);
47 [%expect {| Preloaded |}]
48
49let%expect_test "no modules linked — Not_loaded" =
50 setup ~available:[] ~binary:[] ~server:[];
51 let dcs = make_dcs ~crcs:[("Foo", "abc123")] ["Foo"] in
52 print_status (check dcs);
53 [%expect {| Not_loaded |}]
54
55let%expect_test "CRC mismatch" =
56 setup ~available:["Foo"]
57 ~binary:[("Foo", "binary_crc")]
58 ~server:[("Foo", "universe_crc")];
59 let dcs = make_dcs ~crcs:[("Foo", "universe_crc")] ["Foo"] in
60 print_status (check dcs);
61 [%expect {| Crc_mismatch([Foo (universe=universe_crc binary=binary_crc)]) |}]
62
63let%expect_test "partial load" =
64 setup ~available:["Foo"]
65 ~binary:[("Foo", "abc123")]
66 ~server:[];
67 let dcs = make_dcs ~crcs:[("Foo", "abc123"); ("Bar", "def456")]
68 ["Foo"; "Bar"] in
69 print_status (check dcs);
70 [%expect {| Partially_loaded(loaded=[Foo], missing=[Bar]) |}]
71
72let%expect_test "no CRCs in universe — still Preloaded" =
73 setup ~available:["Foo"]
74 ~binary:[("Foo", "abc123")]
75 ~server:[];
76 let dcs = make_dcs ["Foo"] in
77 print_status (check dcs);
78 [%expect {| Preloaded |}]
79
80let%expect_test "empty module list — Not_loaded" =
81 setup ~available:[] ~binary:[] ~server:[];
82 let dcs = make_dcs [] in
83 print_status (check dcs);
84 [%expect {| Not_loaded |}]
85
86let%expect_test "server CRC overrides dcs_module_crcs" =
87 (* get_universe_crc returns server_crcs, ignoring dcs.dcs_module_crcs *)
88 setup ~available:["Foo"]
89 ~binary:[("Foo", "binary_crc")]
90 ~server:[("Foo", "real_server_crc")];
91 let dcs = make_dcs ~crcs:[("Foo", "stale_json_crc")] ["Foo"] in
92 print_status (check dcs);
93 [%expect {| Crc_mismatch([Foo (universe=real_server_crc binary=binary_crc)]) |}]
94
95let%expect_test "binary CRC missing — no mismatch" =
96 setup ~available:["Foo"]
97 ~binary:[]
98 ~server:[("Foo", "server_crc")];
99 let dcs = make_dcs ~crcs:[("Foo", "server_crc")] ["Foo"] in
100 print_status (check dcs);
101 [%expect {| Preloaded |}]
102
103let%expect_test "multiple modules, one mismatch" =
104 setup ~available:["Foo"; "Bar"]
105 ~binary:[("Foo", "abc123"); ("Bar", "wrong")]
106 ~server:[("Foo", "abc123"); ("Bar", "def456")];
107 let dcs = make_dcs ~crcs:[("Foo", "abc123"); ("Bar", "def456")]
108 ["Foo"; "Bar"] in
109 print_status (check dcs);
110 [%expect {| Crc_mismatch([Bar (universe=def456 binary=wrong)]) |}]