|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import posixpath |
| 4 | +import unittest |
| 5 | + |
| 6 | + |
| 7 | +def export_okf_link_path(source_file: str, target_id: str) -> str: |
| 8 | + """Model wiki-export Step 3.5's required target-file relpath behavior.""" |
| 9 | + source_dir = posixpath.dirname(source_file) |
| 10 | + target_file = f"{target_id}.md" |
| 11 | + return posixpath.relpath(target_file, source_dir) |
| 12 | + |
| 13 | + |
| 14 | +def import_okf_link_target(source_file: str, markdown_target: str) -> str: |
| 15 | + """Model wiki-import Step 4-OKF's .md path -> page id reversal.""" |
| 16 | + source_dir = posixpath.dirname(source_file) |
| 17 | + resolved = posixpath.normpath(posixpath.join(source_dir, markdown_target)) |
| 18 | + if not resolved.endswith(".md"): |
| 19 | + raise ValueError(f"expected markdown file path, got: {markdown_target}") |
| 20 | + return resolved[:-3] |
| 21 | + |
| 22 | + |
| 23 | +class OkfSameNameLinkRoundTripTest(unittest.TestCase): |
| 24 | + def test_child_to_parent_uses_target_file_path(self) -> None: |
| 25 | + source = "projects/social-twitter/concepts/mem0-memory-analysis.md" |
| 26 | + target_id = "projects/social-twitter" |
| 27 | + |
| 28 | + exported = export_okf_link_path(source, target_id) |
| 29 | + |
| 30 | + self.assertEqual(exported, "../../social-twitter.md") |
| 31 | + |
| 32 | + def test_child_to_parent_round_trips_back_to_parent_id(self) -> None: |
| 33 | + source = "projects/social-twitter/concepts/mem0-memory-analysis.md" |
| 34 | + markdown_target = "../../social-twitter.md" |
| 35 | + |
| 36 | + restored = import_okf_link_target(source, markdown_target) |
| 37 | + |
| 38 | + self.assertEqual(restored, "projects/social-twitter") |
| 39 | + |
| 40 | + def test_naive_id_relpath_is_the_buggy_case(self) -> None: |
| 41 | + source_dir = "projects/social-twitter/concepts" |
| 42 | + target_id = "projects/social-twitter" |
| 43 | + |
| 44 | + buggy = posixpath.relpath(target_id, source_dir) + ".md" |
| 45 | + |
| 46 | + self.assertEqual(buggy, "...md") |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == "__main__": |
| 50 | + unittest.main() |
0 commit comments