|
| 1 | +"""Tests for the cross-reference checker script.""" |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from scripts.check_cross_refs import check_cross_refs |
| 7 | + |
| 8 | + |
| 9 | +def _write(tmp: Path, rel_path: str, content: str) -> None: |
| 10 | + p = tmp / rel_path |
| 11 | + p.parent.mkdir(parents=True, exist_ok=True) |
| 12 | + p.write_text(content, encoding="utf-8") |
| 13 | + |
| 14 | + |
| 15 | +def test_valid_python_ref() -> None: |
| 16 | + """Known Python-scope ref resolves.""" |
| 17 | + with tempfile.TemporaryDirectory() as tmp: |
| 18 | + src = Path(tmp) |
| 19 | + _write(src, "langsmith/page.mdx", "Use @[StateGraph] here.") |
| 20 | + errors = check_cross_refs(src) |
| 21 | + assert errors == [] |
| 22 | + |
| 23 | + |
| 24 | +def test_unresolved_ref() -> None: |
| 25 | + """Unknown ref is reported.""" |
| 26 | + with tempfile.TemporaryDirectory() as tmp: |
| 27 | + src = Path(tmp) |
| 28 | + _write(src, "langsmith/page.mdx", "Use @[NonExistentWidget] here.") |
| 29 | + errors = check_cross_refs(src) |
| 30 | + assert len(errors) == 1 |
| 31 | + assert errors[0][2] == "NonExistentWidget" |
| 32 | + |
| 33 | + |
| 34 | +def test_js_scope_fence() -> None: |
| 35 | + """Ref inside :::js fence is checked against js scope.""" |
| 36 | + with tempfile.TemporaryDirectory() as tmp: |
| 37 | + src = Path(tmp) |
| 38 | + content = ":::js\n@[StateGraph]\n:::\n" |
| 39 | + _write(src, "oss/page.mdx", content) |
| 40 | + errors = check_cross_refs(src) |
| 41 | + assert errors == [] |
| 42 | + |
| 43 | + |
| 44 | +def test_js_only_ref_in_python_scope_fails() -> None: |
| 45 | + """JS-only ref is unresolved when file defaults to Python scope.""" |
| 46 | + with tempfile.TemporaryDirectory() as tmp: |
| 47 | + src = Path(tmp) |
| 48 | + _write(src, "oss/python/page.mdx", "@[wrapVitest]") |
| 49 | + errors = check_cross_refs(src) |
| 50 | + assert len(errors) == 1 |
| 51 | + assert errors[0][2] == "wrapVitest" |
| 52 | + |
| 53 | + |
| 54 | +def test_oss_shared_file_checks_both_scopes() -> None: |
| 55 | + """Shared oss/ file (not python/ or javascript/) checks both scopes.""" |
| 56 | + with tempfile.TemporaryDirectory() as tmp: |
| 57 | + src = Path(tmp) |
| 58 | + _write(src, "oss/page.mdx", "@[StateGraph]") |
| 59 | + errors = check_cross_refs(src) |
| 60 | + assert errors == [] |
| 61 | + |
| 62 | + |
| 63 | +def test_ref_inside_code_block_ignored() -> None: |
| 64 | + """Refs inside fenced code blocks are not checked.""" |
| 65 | + with tempfile.TemporaryDirectory() as tmp: |
| 66 | + src = Path(tmp) |
| 67 | + content = "```python\n@[NonExistentWidget]\n```\n" |
| 68 | + _write(src, "langsmith/page.mdx", content) |
| 69 | + errors = check_cross_refs(src) |
| 70 | + assert errors == [] |
| 71 | + |
| 72 | + |
| 73 | +def test_escaped_ref_ignored() -> None: |
| 74 | + r"""Escaped refs (\@[...]) are not checked.""" |
| 75 | + with tempfile.TemporaryDirectory() as tmp: |
| 76 | + src = Path(tmp) |
| 77 | + _write(src, "langsmith/page.mdx", "\\@[NonExistentWidget]") |
| 78 | + errors = check_cross_refs(src) |
| 79 | + assert errors == [] |
| 80 | + |
| 81 | + |
| 82 | +def test_titled_ref_format() -> None: |
| 83 | + """@[title][ref] format extracts the ref name correctly.""" |
| 84 | + with tempfile.TemporaryDirectory() as tmp: |
| 85 | + src = Path(tmp) |
| 86 | + _write(src, "langsmith/page.mdx", "@[my title][StateGraph]") |
| 87 | + errors = check_cross_refs(src) |
| 88 | + assert errors == [] |
| 89 | + |
| 90 | + |
| 91 | +def test_titled_ref_unresolved() -> None: |
| 92 | + """@[title][unknown] format reports the ref name.""" |
| 93 | + with tempfile.TemporaryDirectory() as tmp: |
| 94 | + src = Path(tmp) |
| 95 | + _write(src, "langsmith/page.mdx", "@[my title][UnknownRef]") |
| 96 | + errors = check_cross_refs(src) |
| 97 | + assert len(errors) == 1 |
| 98 | + assert errors[0][2] == "UnknownRef" |
| 99 | + |
| 100 | + |
| 101 | +def test_code_samples_snippets_skipped() -> None: |
| 102 | + """Files under snippets/code-samples/ are skipped.""" |
| 103 | + with tempfile.TemporaryDirectory() as tmp: |
| 104 | + src = Path(tmp) |
| 105 | + _write(src, "snippets/code-samples/example.mdx", "@[NonExistentWidget]") |
| 106 | + errors = check_cross_refs(src) |
| 107 | + assert errors == [] |
| 108 | + |
| 109 | + |
| 110 | +def test_backtick_ref() -> None: |
| 111 | + """@[`ClassName`] format resolves correctly.""" |
| 112 | + with tempfile.TemporaryDirectory() as tmp: |
| 113 | + src = Path(tmp) |
| 114 | + _write(src, "langsmith/page.mdx", "@[`StateGraph`]") |
| 115 | + errors = check_cross_refs(src) |
| 116 | + assert errors == [] |
| 117 | + |
| 118 | + |
| 119 | +def test_multiple_refs_on_same_line() -> None: |
| 120 | + """Multiple refs on the same line are all checked.""" |
| 121 | + with tempfile.TemporaryDirectory() as tmp: |
| 122 | + src = Path(tmp) |
| 123 | + _write( |
| 124 | + src, |
| 125 | + "langsmith/page.mdx", |
| 126 | + "Use @[StateGraph] and @[NonExistentWidget] together.", |
| 127 | + ) |
| 128 | + errors = check_cross_refs(src) |
| 129 | + assert len(errors) == 1 |
| 130 | + assert errors[0][2] == "NonExistentWidget" |
0 commit comments