import pytest
from pathlib import Path
from domin8.tools.diff_utils import normalize_diff
from domin8.tools.api_impact import analyze_changes
from domin8.git_ops import create_diff
def test_api_impact_js_function_export():
js = Path('tmp_api_test.js')
try:
orig = 'function internal(){return 1}\n'
new = orig + '\nexport function publicFn(){return 2}\n'
js.write_text(orig)
diff = create_diff(orig, new, str(js))
normalized = normalize_diff(diff)
# Debug: verify the hunk contains the added export line
assert any('+export function publicFn' in ''.join(h.lines) for h in normalized[0].hunks)
# Directly inspect symbol extraction helper for JS hunks
from domin8.tools.api_impact import _extract_symbols_from_hunks
symmap = _extract_symbols_from_hunks(normalized[0].hunks, normalized[0].repo_relative_path)
assert isinstance(symmap, dict)
# Expect the low-level symbol extractor to record the added export
assert 'publicFn' in symmap['added']
analysis = analyze_changes(normalized)
per = analysis['per_file'][0]
# publicFn should be detected as added (or flagged as public API impact)
assert 'publicFn' in per.get('symbols_added', []) or per['public_api_impact'] is True
finally:
try:
js.unlink()
except Exception:
pass
def test_api_impact_java_class_and_method():
java = Path('TmpApiTest.java')
try:
orig = 'public class TmpApiTest {\n private void helper(){}\n}\n'
new = 'public class TmpApiTest {\n public int newMethod(){return 1;}\n}\n'
java.write_text(orig)
diff = create_diff(orig, new, str(java))
normalized = normalize_diff(diff)
analysis = analyze_changes(normalized)
per = analysis['per_file'][0]
assert isinstance(per['symbols_added'], list)
finally:
try:
java.unlink()
except Exception:
pass