|
| 1 | +# Additional code_intel coverage including diff + metrics. |
| 2 | + |
| 3 | +fn assert_eq(actual, expected, msg) { |
| 4 | + if actual != expected { |
| 5 | + test_record_failure(msg + ": expected " + to_string(expected) + " got " + to_string(actual)); |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +fn assert_true(cond, msg) { if !cond { test_record_failure(msg); } } |
| 10 | + |
| 11 | +# omc_code_diff |
| 12 | +fn test_diff_identical() { |
| 13 | + h d = omc_code_diff( |
| 14 | + "fn f(x) { return x; }", |
| 15 | + "fn f(x) { return x; }" |
| 16 | + ); |
| 17 | + assert_eq(arr_len(dict_get(d, "added")), 0, "no additions"); |
| 18 | + assert_eq(arr_len(dict_get(d, "removed")), 0, "no removals"); |
| 19 | + assert_eq(arr_len(dict_get(d, "modified")), 0, "no mods"); |
| 20 | + assert_eq(arr_len(dict_get(d, "unchanged")), 1, "one unchanged"); |
| 21 | +} |
| 22 | + |
| 23 | +fn test_diff_added() { |
| 24 | + h d = omc_code_diff( |
| 25 | + "fn f(x) { return x; }", |
| 26 | + "fn f(x) { return x; } fn g(x) { return x; }" |
| 27 | + ); |
| 28 | + assert_eq(arr_len(dict_get(d, "added")), 1, "g added"); |
| 29 | +} |
| 30 | + |
| 31 | +fn test_diff_removed() { |
| 32 | + h d = omc_code_diff( |
| 33 | + "fn f(x) { return x; } fn g(x) { return x; }", |
| 34 | + "fn f(x) { return x; }" |
| 35 | + ); |
| 36 | + assert_eq(arr_len(dict_get(d, "removed")), 1, "g removed"); |
| 37 | +} |
| 38 | + |
| 39 | +fn test_diff_modified() { |
| 40 | + h d = omc_code_diff( |
| 41 | + "fn f(x) { return x; }", |
| 42 | + "fn f(x) { return x + 1; }" |
| 43 | + ); |
| 44 | + assert_eq(arr_len(dict_get(d, "modified")), 1, "f modified"); |
| 45 | +} |
| 46 | + |
| 47 | +fn test_diff_alpha_rename_unchanged() { |
| 48 | + h d = omc_code_diff( |
| 49 | + "fn f(x) { return x; }", |
| 50 | + "fn f(a) { return a; }" |
| 51 | + ); |
| 52 | + assert_eq(arr_len(dict_get(d, "unchanged")), 1, "alpha-rename is unchanged"); |
| 53 | + assert_eq(arr_len(dict_get(d, "modified")), 0, "no actual mods"); |
| 54 | +} |
| 55 | + |
| 56 | +# omc_code_metrics |
| 57 | +fn test_metrics_has_all_fields() { |
| 58 | + h m = omc_code_metrics("fn f(x) { return x; }"); |
| 59 | + assert_true(dict_has(m, "complexity"), "has complexity"); |
| 60 | + assert_true(dict_has(m, "ast_size"), "has ast_size"); |
| 61 | + assert_true(dict_has(m, "ast_depth"), "has ast_depth"); |
| 62 | + assert_true(dict_has(m, "source_bytes"), "has source_bytes"); |
| 63 | + assert_true(dict_has(m, "token_count"), "has token_count"); |
| 64 | + assert_true(dict_has(m, "compression_ratio"), "has compression_ratio"); |
| 65 | +} |
| 66 | + |
| 67 | +fn test_metrics_complexity_grows() { |
| 68 | + h simple = omc_code_metrics("fn f(x) { return x; }"); |
| 69 | + h branchy = omc_code_metrics("fn f(x) { if x > 0 { if x > 1 { return 1; } return 0; } return 0; }"); |
| 70 | + assert_true(dict_get(branchy, "complexity") > dict_get(simple, "complexity"), |
| 71 | + "complexity orders"); |
| 72 | +} |
| 73 | + |
| 74 | +fn test_metrics_token_count_positive() { |
| 75 | + h m = omc_code_metrics("fn f(x) { return x; }"); |
| 76 | + assert_true(dict_get(m, "token_count") > 0, "non-zero tokens"); |
| 77 | +} |
| 78 | + |
| 79 | +fn test_metrics_compression_ratio_positive() { |
| 80 | + h m = omc_code_metrics("fn f(x) { return x; }"); |
| 81 | + assert_true(dict_get(m, "compression_ratio") > 0.0, "non-zero ratio"); |
| 82 | +} |
| 83 | + |
| 84 | +# Composition tests |
| 85 | +fn test_diff_then_metrics() { |
| 86 | + # The basic LLM workflow: diff to see what changed, then metrics |
| 87 | + # on the new version. |
| 88 | + h old = "fn f(x) { return x; }"; |
| 89 | + h new = "fn f(x) { return x + 1; } fn g(x) { return x * 2; }"; |
| 90 | + h d = omc_code_diff(old, new); |
| 91 | + assert_eq(arr_len(dict_get(d, "added")), 1, "added g"); |
| 92 | + h m = omc_code_metrics(new); |
| 93 | + assert_true(dict_get(m, "complexity") >= 1.0, "metrics on new"); |
| 94 | +} |
| 95 | + |
| 96 | +# Larger summary integration test |
| 97 | +fn test_summary_has_classes() { |
| 98 | + h s = omc_code_summary("class Foo { x; y; } fn f() {}"); |
| 99 | + assert_eq(arr_len(dict_get(s, "classes")), 1, "one class"); |
| 100 | + assert_eq(arr_get(dict_get(s, "classes"), 0), "Foo", "class name"); |
| 101 | +} |
| 102 | + |
| 103 | +fn test_summary_imports() { |
| 104 | + h s = omc_code_summary("import \"foo\"; fn main() {}"); |
| 105 | + assert_eq(arr_len(dict_get(s, "imports")), 1, "one import"); |
| 106 | +} |
| 107 | + |
| 108 | +fn test_summary_stmt_count() { |
| 109 | + h s = omc_code_summary("fn a() {} fn b() {} fn c() {}"); |
| 110 | + assert_eq(dict_get(s, "stmt_count"), 3, "three stmts"); |
| 111 | +} |
| 112 | + |
| 113 | +# canonical hash invariance through rounds of canonical |
| 114 | +fn test_canonical_idempotent() { |
| 115 | + h c1 = omc_code_canonical("fn f(x) { return x; }"); |
| 116 | + h c2 = omc_code_canonical(c1); |
| 117 | + assert_eq(c1, c2, "canonical is idempotent"); |
| 118 | +} |
0 commit comments