build-small-hackathon/hackathon-advisor
16
1from pathlib import Path2 3from tests.helpers import load_test_index4import json5 6from hackathon_advisor.data import (7 Project,8 ProjectIndex,9 public_project_summary,10 public_project_title,11)12 13 14def test_project_index_searches_snapshot() -> None:15 index = load_test_index()16 17 hits = index.search("lullaby children audio", limit=3)18 19 assert hits20 assert hits[0].project.id.startswith("build-small-hackathon/")21 assert hits[0].page_number >= 122 assert index.index_algorithm == "llama-cpp-embedding-v1"23 24 25def test_project_index_whitespace() -> None:26 index = load_test_index()27 28 items = index.find_whitespace(limit=3)29 30 assert len(items) == 331 assert all(item.label for item in items)32 33 34def test_public_project_cards_hide_generic_submission_copy() -> None:35 assert public_project_title("My Build Small Hackathon") == "Untitled project"36 assert public_project_summary("This is my submission for the build-small-hackathon") == ""37 assert public_project_summary("Todo") == ""38 assert public_project_summary("Local-first personal knowledge agent") == "Local-first personal knowledge agent"39 40 project = Project(41 id="build-small-hackathon/my-build-small-hackathon",42 title="My Build Small Hackathon",43 summary="This is my submission for the build-small-hackathon",44 tags=(),45 models=(),46 datasets=(),47 likes=0,48 sdk="gradio",49 license="",50 created_at="",51 last_modified="",52 host="",53 url="https://example.test",54 )55 56 public = project.to_public_dict()57 58 assert public["title"] == "Untitled project"59 assert public["summary"] == ""60 61 62def test_searchable_text_includes_main_app_file_signals() -> None:63 project = Project(64 id="build-small-hackathon/idea-canvas",65 title="Idea Canvas",66 summary="",67 tags=("gradio",),68 models=(),69 datasets=(),70 likes=0,71 sdk="gradio",72 license="",73 created_at="",74 last_modified="",75 host="",76 url="https://example.test",77 app_file="app.py",78 app_file_embedding_text="score_idea\ngr.Textbox\nProject idea",79 )80 81 searchable = project.searchable_text82 83 assert "main app file: app.py" in searchable84 assert "score_idea" in searchable85 assert "Project idea" in searchable86 87 88def test_public_project_tags_exclude_hosting_metadata() -> None:89 project = Project.from_dict(90 {91 "id": "build-small-hackathon/idea-canvas",92 "title": "Idea Canvas",93 "summary": "",94 "tags": ["gradio", "region:us", "local-first", "region:eu", "gradio"],95 "models": [],96 "datasets": [],97 "url": "https://example.test",98 }99 )100 101 assert project.tags == ("gradio", "region:us", "local-first", "region:eu", "gradio")102 assert project.to_public_dict()["tags"] == ["gradio", "local-first"]103 104 105def test_searchable_text_excludes_refresh_readme_body_for_stable_reuse() -> None:106 project = Project(107 id="build-small-hackathon/long-readme",108 title="Long README",109 summary="",110 tags=(),111 models=(),112 datasets=(),113 likes=0,114 sdk="gradio",115 license="",116 created_at="",117 last_modified="",118 host="",119 url="https://example.test",120 readme_body="a" * 2500 + "middle should not be embedded" + "b" * 2500,121 )122 123 searchable = project.searchable_text124 125 assert "readme:" not in searchable126 assert "middle should not be embedded" not in searchable127 128 129def test_project_index_rejects_mismatched_snapshot(tmp_path: Path) -> None:130 payload = json.loads(Path("data/project_index.json").read_text(encoding="utf-8"))131 payload["snapshot_generated_at"] = "2000-01-01T00:00:00+00:00"132 bad_index = tmp_path / "project_index.json"133 bad_index.write_text(json.dumps(payload), encoding="utf-8")134 135 try:136 ProjectIndex.from_files(Path("data/projects.json"), bad_index)137 except ValueError as error:138 assert "different snapshot timestamp" in str(error)139 else:140 raise AssertionError("mismatched index should be rejected")141 142 143def test_project_index_retains_validated_payload() -> None:144 payload = json.loads(Path("data/project_index.json").read_text(encoding="utf-8"))145 index = ProjectIndex.from_files(Path("data/projects.json"), Path("data/project_index.json"))146 147 assert index.index_payload["snapshot_digest"] == payload["snapshot_digest"]148 assert len(index.index_payload["documents"]) == len(index.projects)149 