|
| 1 | +# Tests for the substrate-indexed code completion engine. |
| 2 | +# |
| 3 | +# Each test runs the predictor against examples/lib/prometheus.omc as |
| 4 | +# a real corpus and asserts ranking properties. |
| 5 | + |
| 6 | +fn assert_true(cond, msg) { if !cond { test_record_failure(msg); } } |
| 7 | +fn assert_eq(actual, expected, msg) { |
| 8 | + if actual != expected { |
| 9 | + test_record_failure(msg + ": expected " + to_string(expected) |
| 10 | + + " got " + to_string(actual)); |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +fn _corpus() { |
| 15 | + return ["examples/lib/prometheus.omc"]; |
| 16 | +} |
| 17 | + |
| 18 | +# ---- Corpus ingestion ---- |
| 19 | + |
| 20 | +fn test_corpus_size_matches_real_corpus() { |
| 21 | + h size = omc_corpus_size(_corpus()); |
| 22 | + # Prometheus has ~70 top-level fns currently. Lower bound is the |
| 23 | + # only stable assertion (the corpus grows over time). |
| 24 | + assert_true(size > 30, "corpus has > 30 fns (got " + to_string(size) + ")"); |
| 25 | +} |
| 26 | + |
| 27 | +# ---- Predict shape ---- |
| 28 | + |
| 29 | +fn test_predict_returns_array_of_dicts() { |
| 30 | + h hits = omc_predict_files(_corpus(), "fn prom_linear_", 3); |
| 31 | + assert_eq(type_of(hits), "array", "result is array"); |
| 32 | + assert_true(arr_len(hits) > 0, "got at least one hit"); |
| 33 | + h first = arr_get(hits, 0); |
| 34 | + assert_eq(type_of(first), "dict", "elements are dicts"); |
| 35 | +} |
| 36 | + |
| 37 | +fn test_predict_top_k_caps_results() { |
| 38 | + h hits = omc_predict_files(_corpus(), "fn prom_", 2); |
| 39 | + assert_true(arr_len(hits) <= 2, "respects top_k=2"); |
| 40 | +} |
| 41 | + |
| 42 | +# ---- Entry fields ---- |
| 43 | + |
| 44 | +fn test_each_hit_has_required_fields() { |
| 45 | + h hits = omc_predict_files(_corpus(), "fn prom_linear_", 1); |
| 46 | + h h0 = arr_get(hits, 0); |
| 47 | + assert_true(dict_has(h0, "fn_name"), "fn_name"); |
| 48 | + assert_true(dict_has(h0, "source"), "source"); |
| 49 | + assert_true(dict_has(h0, "file"), "file"); |
| 50 | + assert_true(dict_has(h0, "canonical_hash"), "canonical_hash"); |
| 51 | + assert_true(dict_has(h0, "attractor"), "attractor"); |
| 52 | + assert_true(dict_has(h0, "prefix_match_len"), "prefix_match_len"); |
| 53 | + assert_true(dict_has(h0, "substrate_distance"), "substrate_distance"); |
| 54 | +} |
| 55 | + |
| 56 | +# ---- Ranking ---- |
| 57 | + |
| 58 | +fn test_prom_linear_prefix_returns_the_three_prom_linear_fns() { |
| 59 | + # The prefix `fn prom_linear_` should surface exactly the three |
| 60 | + # prom_linear_* defs in Prometheus (new, forward, params). |
| 61 | + h hits = omc_predict_files(_corpus(), "fn prom_linear_", 10); |
| 62 | + assert_true(arr_len(hits) >= 3, |
| 63 | + "at least 3 results for fn prom_linear_ prefix"); |
| 64 | + # Collect the names. |
| 65 | + h names = []; |
| 66 | + for entry in hits { |
| 67 | + arr_push(names, dict_get(entry, "fn_name")); |
| 68 | + } |
| 69 | + assert_true(arr_contains(names, "prom_linear_new"), |
| 70 | + "prom_linear_new present in " + to_string(names)); |
| 71 | + assert_true(arr_contains(names, "prom_linear_forward"), |
| 72 | + "prom_linear_forward present in " + to_string(names)); |
| 73 | + assert_true(arr_contains(names, "prom_linear_params"), |
| 74 | + "prom_linear_params present in " + to_string(names)); |
| 75 | +} |
| 76 | + |
| 77 | +fn test_prefix_match_len_nonzero_when_prefix_exists_in_corpus() { |
| 78 | + h hits = omc_predict_files(_corpus(), "fn prom_linear_", 3); |
| 79 | + h h0 = arr_get(hits, 0); |
| 80 | + assert_true(dict_get(h0, "prefix_match_len") > 0, |
| 81 | + "prefix_match_len > 0 when prefix tokens are in corpus"); |
| 82 | +} |
| 83 | + |
| 84 | +fn test_substrate_distance_is_nonneg() { |
| 85 | + h hits = omc_predict_files(_corpus(), "fn prom_linear_", 5); |
| 86 | + for entry in hits { |
| 87 | + assert_true(dict_get(entry, "substrate_distance") >= 0, |
| 88 | + "substrate_distance is non-negative"); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +# ---- Provenance ---- |
| 93 | + |
| 94 | +fn test_file_provenance_points_at_source() { |
| 95 | + h hits = omc_predict_files(_corpus(), "fn prom_linear_", 1); |
| 96 | + h h0 = arr_get(hits, 0); |
| 97 | + assert_eq(dict_get(h0, "file"), "examples/lib/prometheus.omc", |
| 98 | + "file is the source path we passed in"); |
| 99 | +} |
| 100 | + |
| 101 | +fn test_source_contains_fn_keyword() { |
| 102 | + h hits = omc_predict_files(_corpus(), "fn prom_linear_", 1); |
| 103 | + h source = dict_get(arr_get(hits, 0), "source"); |
| 104 | + assert_true(str_contains(source, "fn "), "source has fn keyword"); |
| 105 | +} |
| 106 | + |
| 107 | +# ---- Empty inputs ---- |
| 108 | + |
| 109 | +fn test_empty_paths_returns_empty() { |
| 110 | + h hits = omc_predict_files([], "fn anything", 5); |
| 111 | + assert_eq(arr_len(hits), 0, "empty paths → empty hits"); |
| 112 | +} |
| 113 | + |
| 114 | +fn test_top_k_zero_returns_empty() { |
| 115 | + h hits = omc_predict_files(_corpus(), "fn prom_", 0); |
| 116 | + assert_eq(arr_len(hits), 0, "top_k=0 → empty hits"); |
| 117 | +} |
0 commit comments