Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion packages/ghfind-py/ghfind/_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,20 @@ def _text_matches(text: str, words: List[str]) -> bool:

def _clamp_text(text: str, limit: int) -> str:
clean = re.sub(r"\s+", " ", text).strip()
return (clean[: limit - 1] + "…") if len(clean) > limit else clean
total_units = sum(2 if ord(c) > 0xFFFF else 1 for c in clean)
if total_units <= limit:
return clean

target = limit - 1
current_units = 0
retained_chars = []
for c in clean:
c_units = 2 if ord(c) > 0xFFFF else 1
if current_units + c_units > target:
break
retained_chars.append(c)
current_units += c_units
return "".join(retained_chars) + "…"


def parse_readme_features(markdown: str) -> Dict[str, Any]:
Expand Down
40 changes: 40 additions & 0 deletions packages/ghfind-py/tests/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from datetime import datetime, timezone

import pytest

from ghfind._extract import (
compute_impact_from_contrib_map,
compute_impact_quality_signals,
Expand All @@ -10,6 +12,7 @@
is_doc_like_impact_pr,
original_repo_quality_score,
parse_github_noreply_login,
parse_readme_features,
resolve_first_commit_github_login,
)

Expand Down Expand Up @@ -196,3 +199,40 @@ def docs(repo):
assert compute_impact_quality_signals(broad, 100, "alice")["impact_quality_cap"] is None
concentrated = [docs(f"org/project-{0 if i < 10 else i}") for i in range(12)]
assert compute_impact_quality_signals(concentrated, 12, "alice")["impact_quality_cap"] is None


_INTRO_PREFIX = "文" * 348
_SECTION_PREFIX = "a" * 218
_TITLE_PREFIX = "a" * 1491
_PREFIX_WITH_EMOJI = f"{'文' * 347}🚀"


@pytest.mark.parametrize(
"name,markdown,expected",
[
("an intro", f"# Project\n\n{_INTRO_PREFIX}🚀end", f"Intro: {_INTRO_PREFIX}… Sections:"),
(
"a section",
f"# Project\n\nOverview.\n\n## Usage\n\n{_SECTION_PREFIX}🚀end",
f"Usage: {_SECTION_PREFIX}… Signals: usage",
),
("the full summary", f"# {_TITLE_PREFIX}🚀end", f"Title: {_TITLE_PREFIX}…"),
(
"an intro where the emoji fits before the ellipsis",
f"# Project\n\n{_PREFIX_WITH_EMOJI}end",
f"Intro: {_PREFIX_WITH_EMOJI}… Sections:",
),
(
"an intro at the length limit",
f"# Project\n\n{_INTRO_PREFIX}🚀",
f"Intro: {_INTRO_PREFIX}🚀 Sections:",
),
],
)
def test_parse_readme_features_preserves_unicode(name, markdown, expected):
features = parse_readme_features(markdown)
summary = features["prompt_summary"]
assert expected in summary
utf16_len = sum(2 if ord(c) > 0xFFFF else 1 for c in summary)
assert utf16_len <= 1500