|
| 1 | +"""Tests for Gleam import extraction and specifier resolution.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from jcodemunch_mcp.parser.imports import ( |
| 6 | + _LANGUAGE_EXTRACTORS, |
| 7 | + _gleam_source_roots, |
| 8 | + extract_imports, |
| 9 | + resolve_specifier, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +GLEAM_SOURCE = """\ |
| 14 | +import gleam/io |
| 15 | +import gleam/option.{type Option, None, Some} |
| 16 | +import holmes_msg as msg |
| 17 | +import webse/config_types.{type Config, Config} |
| 18 | +import cx/commands/vetf/aggregate |
| 19 | +
|
| 20 | +pub fn main() { |
| 21 | + io.println("import ignored inside a string") |
| 22 | +} |
| 23 | +""" |
| 24 | + |
| 25 | + |
| 26 | +class TestGleamImportExtraction: |
| 27 | + |
| 28 | + def test_gleam_in_language_extractors(self): |
| 29 | + """Gleam must be registered in _LANGUAGE_EXTRACTORS.""" |
| 30 | + assert "gleam" in _LANGUAGE_EXTRACTORS |
| 31 | + |
| 32 | + def test_extracts_plain_import(self): |
| 33 | + edges = extract_imports(GLEAM_SOURCE, "src/app.gleam", "gleam") |
| 34 | + specifiers = [e["specifier"] for e in edges] |
| 35 | + assert "gleam/io" in specifiers |
| 36 | + |
| 37 | + def test_extracts_unqualified_names_stripping_type_prefix(self): |
| 38 | + edges = extract_imports(GLEAM_SOURCE, "src/app.gleam", "gleam") |
| 39 | + by_spec = {e["specifier"]: e["names"] for e in edges} |
| 40 | + assert by_spec["gleam/option"] == ["Option", "None", "Some"] |
| 41 | + # `{type Config, Config}` imports the type and the constructor — |
| 42 | + # both surface as the mention name "Config". |
| 43 | + assert by_spec["webse/config_types"] == ["Config", "Config"] |
| 44 | + |
| 45 | + def test_module_alias_keeps_specifier(self): |
| 46 | + edges = extract_imports(GLEAM_SOURCE, "src/app.gleam", "gleam") |
| 47 | + by_spec = {e["specifier"]: e["names"] for e in edges} |
| 48 | + assert "holmes_msg" in by_spec |
| 49 | + assert by_spec["holmes_msg"] == [] |
| 50 | + |
| 51 | + def test_extracts_deep_module_path(self): |
| 52 | + edges = extract_imports(GLEAM_SOURCE, "src/app.gleam", "gleam") |
| 53 | + specifiers = [e["specifier"] for e in edges] |
| 54 | + assert "cx/commands/vetf/aggregate" in specifiers |
| 55 | + |
| 56 | + def test_edge_count(self): |
| 57 | + edges = extract_imports(GLEAM_SOURCE, "src/app.gleam", "gleam") |
| 58 | + assert len(edges) == 5 |
| 59 | + |
| 60 | + def test_multiline_unqualified_list(self): |
| 61 | + source = ( |
| 62 | + "import holmes_msg.{\n" |
| 63 | + " type HolmesMsg, type Envelope,\n" |
| 64 | + " decode_msg,\n" |
| 65 | + "}\n" |
| 66 | + ) |
| 67 | + edges = extract_imports(source, "src/app.gleam", "gleam") |
| 68 | + assert len(edges) == 1 |
| 69 | + assert edges[0]["specifier"] == "holmes_msg" |
| 70 | + assert edges[0]["names"] == ["HolmesMsg", "Envelope", "decode_msg"] |
| 71 | + |
| 72 | + def test_unqualified_name_alias(self): |
| 73 | + source = "import gleam/option.{Some as S}\n" |
| 74 | + edges = extract_imports(source, "src/app.gleam", "gleam") |
| 75 | + assert edges[0]["names"] == ["Some"] |
| 76 | + |
| 77 | + def test_no_false_positives_from_non_import_lines(self): |
| 78 | + source = 'pub fn main() {\n io.println("import gleam/io")\n}\n' |
| 79 | + edges = extract_imports(source, "src/app.gleam", "gleam") |
| 80 | + assert edges == [] |
| 81 | + |
| 82 | + |
| 83 | +WEVE_STYLE_FILES = { |
| 84 | + "packages/yamlutils/src/yamlutils.gleam", |
| 85 | + "packages/ion/src/ion/ion_yaml.gleam", |
| 86 | + "cells/webse/src/webse/config_yaml.gleam", |
| 87 | + "cells/webse/src/webse/config_types.gleam", |
| 88 | + "cells/webse/test/webse_test.gleam", |
| 89 | + "cells/webse/test/support/helper.gleam", |
| 90 | + "cells/webse/dev/seed_data.gleam", |
| 91 | + "soma/src/soma/config_yaml.gleam", |
| 92 | + "cx/src/cx/cluster_config_yaml.gleam", |
| 93 | + "cells/webse/gleam.toml", |
| 94 | + "packages/yamlutils/gleam.toml", |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +class TestGleamSourceRoots: |
| 99 | + |
| 100 | + def test_structural_roots_from_gleam_files(self): |
| 101 | + roots = _gleam_source_roots(frozenset(WEVE_STYLE_FILES)) |
| 102 | + assert "packages/yamlutils/src" in roots |
| 103 | + assert "cells/webse/src" in roots |
| 104 | + assert "cells/webse/test" in roots |
| 105 | + assert "cells/webse/dev" in roots |
| 106 | + assert "soma/src" in roots |
| 107 | + assert "cx/src" in roots |
| 108 | + |
| 109 | + def test_gleam_toml_derived_roots(self): |
| 110 | + files = frozenset({"cells/empty/gleam.toml"}) |
| 111 | + roots = _gleam_source_roots(files) |
| 112 | + assert "cells/empty/src" in roots |
| 113 | + assert "cells/empty/test" in roots |
| 114 | + assert "cells/empty/dev" in roots |
| 115 | + |
| 116 | + def test_root_level_package(self): |
| 117 | + files = frozenset({"src/app.gleam", "test/app_test.gleam", "gleam.toml"}) |
| 118 | + roots = _gleam_source_roots(files) |
| 119 | + assert "src" in roots |
| 120 | + assert "test" in roots |
| 121 | + |
| 122 | + |
| 123 | +class TestGleamSpecifierResolution: |
| 124 | + |
| 125 | + @pytest.mark.parametrize("specifier,importer,expected", [ |
| 126 | + # Cross-package import into another package's src root |
| 127 | + ("yamlutils", "packages/ion/src/ion/ion_yaml.gleam", |
| 128 | + "packages/yamlutils/src/yamlutils.gleam"), |
| 129 | + # Same-package import |
| 130 | + ("webse/config_types", "cells/webse/src/webse/config_yaml.gleam", |
| 131 | + "cells/webse/src/webse/config_types.gleam"), |
| 132 | + # Test module importing its package's src module |
| 133 | + ("webse/config_types", "cells/webse/test/webse_test.gleam", |
| 134 | + "cells/webse/src/webse/config_types.gleam"), |
| 135 | + # Test module importing a test-only helper module |
| 136 | + ("support/helper", "cells/webse/test/webse_test.gleam", |
| 137 | + "cells/webse/test/support/helper.gleam"), |
| 138 | + # Dev module importing its package's src module |
| 139 | + ("webse/config_types", "cells/webse/dev/seed_data.gleam", |
| 140 | + "cells/webse/src/webse/config_types.gleam"), |
| 141 | + # Stdlib / hex dependency: unresolvable, no edge |
| 142 | + ("gleam/io", "cells/webse/src/webse/config_yaml.gleam", None), |
| 143 | + ("gleam/list", "soma/src/soma/config_yaml.gleam", None), |
| 144 | + ], ids=[ |
| 145 | + "cross_package", "same_package", "test_to_src", "test_helper", |
| 146 | + "dev_to_src", "stdlib_io", "stdlib_list", |
| 147 | + ]) |
| 148 | + def test_resolution(self, specifier, importer, expected): |
| 149 | + result = resolve_specifier(specifier, importer, set(WEVE_STYLE_FILES)) |
| 150 | + assert result == expected |
| 151 | + |
| 152 | + def test_non_gleam_importer_unaffected(self): |
| 153 | + """A slash specifier from a non-.gleam importer must not hit gleam roots.""" |
| 154 | + result = resolve_specifier( |
| 155 | + "webse/config_types", "scripts/tool.py", set(WEVE_STYLE_FILES) |
| 156 | + ) |
| 157 | + assert result is None |
| 158 | + |
| 159 | + def test_end_to_end_extract_and_resolve(self): |
| 160 | + source = "import webse/config_types.{type Config}\n" |
| 161 | + edges = extract_imports(source, "cells/webse/src/webse/config_yaml.gleam", "gleam") |
| 162 | + assert len(edges) == 1 |
| 163 | + resolved = resolve_specifier( |
| 164 | + edges[0]["specifier"], |
| 165 | + "cells/webse/src/webse/config_yaml.gleam", |
| 166 | + set(WEVE_STYLE_FILES), |
| 167 | + ) |
| 168 | + assert resolved == "cells/webse/src/webse/config_types.gleam" |
| 169 | + |
| 170 | + def test_gleam_not_in_missing_extractors(self, tmp_path): |
| 171 | + """After the extractor lands, index_folder must not flag gleam.""" |
| 172 | + from jcodemunch_mcp.tools.index_folder import index_folder |
| 173 | + |
| 174 | + src = tmp_path / "src" |
| 175 | + store = tmp_path / "store" |
| 176 | + src.mkdir() |
| 177 | + store.mkdir() |
| 178 | + |
| 179 | + (src / "app.gleam").write_text( |
| 180 | + "import gleam/io\n\npub fn main() {\n io.println(\"hello\")\n}\n" |
| 181 | + ) |
| 182 | + |
| 183 | + r = index_folder(str(src), use_ai_summaries=False, storage_path=str(store)) |
| 184 | + assert r["success"] is True |
| 185 | + |
| 186 | + missing = r.get("missing_extractors", []) |
| 187 | + assert "gleam" not in missing, ( |
| 188 | + f"gleam should no longer be in missing_extractors: {missing}" |
| 189 | + ) |
0 commit comments