1414
1515def write_jsonl (path : Path , rows : list [object ]) -> Path :
1616 path .write_text (
17- "" .join (json .dumps (row ) + "\n " if isinstance (row , dict ) else f"{ row } \n " for row in rows ),
17+ "" .join (
18+ json .dumps (row ) + "\n " if isinstance (row , dict ) else f"{ row } \n "
19+ for row in rows
20+ ),
1821 encoding = "utf-8" ,
1922 )
2023 return path
2124
2225
23- def result (url : str , source : str , query : str , title : str = "" , snippet : str = "" , metadata : dict | None = None ) -> dict :
26+ def result (
27+ url : str ,
28+ source : str ,
29+ query : str ,
30+ title : str = "" ,
31+ snippet : str = "" ,
32+ metadata : dict | None = None ,
33+ ) -> dict :
2434 return {
2535 "url" : url ,
2636 "title" : title ,
@@ -33,7 +43,9 @@ def result(url: str, source: str, query: str, title: str = "", snippet: str = ""
3343
3444
3545def score_file (path : Path , target : int = 30 ) -> dict :
36- return judge .score_results (judge .load_results (path ), str (path ), target = target , now = NOW )
46+ return judge .score_results (
47+ judge .load_results (path ), str (path ), target = target , now = NOW
48+ )
3749
3850
3951def test_empty_input (tmp_path : Path ) -> None :
@@ -43,50 +55,91 @@ def test_empty_input(tmp_path: Path) -> None:
4355
4456
4557def test_single_result (tmp_path : Path ) -> None :
46- row = result ("https://example.com/1" , "github" , "python judge" , title = "Python judge" )
58+ row = result (
59+ "https://example.com/1" , "github" , "python judge" , title = "Python judge"
60+ )
4761 score = score_file (write_jsonl (tmp_path / "single.jsonl" , [row ]))
4862 assert score ["dimensions" ]["quantity" ] == pytest .approx (1 / 30 )
4963 assert score ["dimensions" ]["diversity" ] == 0.0
5064 assert score ["dimensions" ]["relevance" ] == 1.0
5165
5266
5367def test_multi_platform_diversity (tmp_path : Path ) -> None :
54- rows = [result (f"https://example.com/{ idx } " , source , "topic" , title = "topic" ) for idx , source in enumerate (
55- ["github" ] * 4 + ["reddit" ] * 3 + ["hackernews" ] * 3 ,
56- start = 1 ,
57- )]
68+ rows = [
69+ result (f"https://example.com/{ idx } " , source , "topic" , title = "topic" )
70+ for idx , source in enumerate (
71+ ["github" ] * 4 + ["reddit" ] * 3 + ["hackernews" ] * 3 ,
72+ start = 1 ,
73+ )
74+ ]
5875 score = score_file (write_jsonl (tmp_path / "diverse.jsonl" , rows ))
5976 assert score ["dimensions" ]["diversity" ] > 0.5
6077
6178
6279def test_single_platform_low_diversity (tmp_path : Path ) -> None :
63- rows = [result (f"https://example.com/{ idx } " , "github" , "topic" , title = "topic" ) for idx in range (10 )]
80+ rows = [
81+ result (f"https://example.com/{ idx } " , "github" , "topic" , title = "topic" )
82+ for idx in range (10 )
83+ ]
6484 score = score_file (write_jsonl (tmp_path / "single-platform.jsonl" , rows ))
6585 assert score ["dimensions" ]["diversity" ] == 0.0
6686
6787
6888def test_relevance_all_match (tmp_path : Path ) -> None :
69- rows = [result (f"https://example.com/{ idx } " , "github" , "agent search" , title = "Agent search guide" ) for idx in range (3 )]
89+ rows = [
90+ result (
91+ f"https://example.com/{ idx } " ,
92+ "github" ,
93+ "agent search" ,
94+ title = "Agent search guide" ,
95+ )
96+ for idx in range (3 )
97+ ]
7098 score = score_file (write_jsonl (tmp_path / "all-match.jsonl" , rows ))
7199 assert score ["dimensions" ]["relevance" ] == 1.0
72100
73101
74102def test_relevance_none_match (tmp_path : Path ) -> None :
75- rows = [result (f"https://example.com/{ idx } " , "github" , "agent search" , title = "database tuning" , snippet = "sql indexes" ) for idx in range (3 )]
103+ rows = [
104+ result (
105+ f"https://example.com/{ idx } " ,
106+ "github" ,
107+ "agent search" ,
108+ title = "database tuning" ,
109+ snippet = "sql indexes" ,
110+ )
111+ for idx in range (3 )
112+ ]
76113 score = score_file (write_jsonl (tmp_path / "none-match.jsonl" , rows ))
77114 assert score ["dimensions" ]["relevance" ] == 0.0
78115
79116
80117def test_freshness_all_recent (tmp_path : Path ) -> None :
81118 recent = (NOW - dt .timedelta (days = 30 )).isoformat ().replace ("+00:00" , "Z" )
82- rows = [result (f"https://example.com/{ idx } " , "reddit" , "topic" , metadata = {"published_at" : recent }) for idx in range (3 )]
119+ rows = [
120+ result (
121+ f"https://example.com/{ idx } " ,
122+ "reddit" ,
123+ "topic" ,
124+ metadata = {"published_at" : recent },
125+ )
126+ for idx in range (3 )
127+ ]
83128 score = score_file (write_jsonl (tmp_path / "fresh.jsonl" , rows ))
84129 assert score ["dimensions" ]["freshness" ] == 1.0
85130
86131
87132def test_freshness_all_old (tmp_path : Path ) -> None :
88133 old = (NOW - dt .timedelta (days = 300 )).isoformat ().replace ("+00:00" , "Z" )
89- rows = [result (f"https://example.com/{ idx } " , "reddit" , "topic" , metadata = {"updated_at" : old }) for idx in range (3 )]
134+ rows = [
135+ result (
136+ f"https://example.com/{ idx } " ,
137+ "reddit" ,
138+ "topic" ,
139+ metadata = {"updated_at" : old },
140+ )
141+ for idx in range (3 )
142+ ]
90143 score = score_file (write_jsonl (tmp_path / "old.jsonl" , rows ))
91144 assert score ["dimensions" ]["freshness" ] == 0.0
92145
@@ -95,15 +148,25 @@ def test_freshness_unix_timestamp(tmp_path: Path) -> None:
95148 recent_ts = int ((NOW - dt .timedelta (days = 30 )).timestamp ())
96149 old_ts = int ((NOW - dt .timedelta (days = 300 )).timestamp ())
97150 rows = [
98- result ("https://example.com/1" , "reddit" , "topic" , metadata = {"created_utc" : recent_ts }),
99- result ("https://example.com/2" , "reddit" , "topic" , metadata = {"created_utc" : old_ts }),
151+ result (
152+ "https://example.com/1" ,
153+ "reddit" ,
154+ "topic" ,
155+ metadata = {"created_utc" : recent_ts },
156+ ),
157+ result (
158+ "https://example.com/2" , "reddit" , "topic" , metadata = {"created_utc" : old_ts }
159+ ),
100160 ]
101161 score = score_file (write_jsonl (tmp_path / "unix.jsonl" , rows ))
102162 assert score ["dimensions" ]["freshness" ] == 0.5
103163
104164
105165def test_custom_target (tmp_path : Path ) -> None :
106- rows = [result (f"https://example.com/{ idx } " , "github" , f"query { idx } " , title = "query" ) for idx in range (10 )]
166+ rows = [
167+ result (f"https://example.com/{ idx } " , "github" , f"query { idx } " , title = "query" )
168+ for idx in range (10 )
169+ ]
107170 score = score_file (write_jsonl (tmp_path / "target.jsonl" , rows ), target = 10 )
108171 assert score ["dimensions" ]["quantity" ] == 1.0
109172
@@ -120,10 +183,19 @@ def test_invalid_json_lines(tmp_path: Path) -> None:
120183
121184
122185def test_cli_exit_codes (tmp_path : Path ) -> None :
123- evidence = write_jsonl (tmp_path / "cli.jsonl" , [result ("https://example.com/1" , "github" , "judge" , title = "judge" )])
186+ evidence = write_jsonl (
187+ tmp_path / "cli.jsonl" ,
188+ [result ("https://example.com/1" , "github" , "judge" , title = "judge" )],
189+ )
124190 script = Path (judge .__file__ )
125- success = subprocess .run ([sys .executable , str (script ), str (evidence )], capture_output = True , text = True )
126- missing = subprocess .run ([sys .executable , str (script ), str (tmp_path / "missing.jsonl" )], capture_output = True , text = True )
191+ success = subprocess .run (
192+ [sys .executable , str (script ), str (evidence )], capture_output = True , text = True
193+ )
194+ missing = subprocess .run (
195+ [sys .executable , str (script ), str (tmp_path / "missing.jsonl" )],
196+ capture_output = True ,
197+ text = True ,
198+ )
127199 assert success .returncode == 0
128200 assert json .loads (success .stdout )["meta" ]["total_results" ] == 1
129201 assert missing .returncode == 1
0 commit comments