Repository navigation
Expand file tree
/
Copy pathtest_cli.py
More file actions
614 lines (518 loc) · 20.5 KB
/
Copy pathtest_cli.py
File metadata and controls
614 lines (518 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
import json
from pathlib import Path
from typer.testing import CliRunner
from sentiment_benchmark.cli import app
from sentiment_benchmark.metrics import evaluate_responses
from sentiment_benchmark.models import DatasetRow, LLMResponseRecord, RunConfig
from sentiment_benchmark.news_source import NewsArticleRecord, NewsFetchResult, article_record_id, make_news_fetch_config, normalize_url
from sentiment_benchmark.newsapi_source import NewsApiConfigurationError
from sentiment_benchmark.prompts import make_prompt
from sentiment_benchmark.storage import BenchmarkStore
runner = CliRunner()
def test_cli_sweep_rejects_invalid_parameter_lists_before_reading_inputs() -> None:
cases = [
("--thresholds", "not-a-number", "comma-separated numeric list"),
("--thresholds", "nan", "finite numbers"),
("--thresholds", "", "cannot be empty"),
("--horizons", "0,1", "positive integers"),
("--horizons", "", "cannot be empty"),
]
for option, value, expected in cases:
result = runner.invoke(app, ["sweep-trading-strategy", "--run-dir", "missing", option, value])
assert result.exit_code != 0
assert expected in result.output
assert "Traceback" not in result.output
def test_cli_sweep_rejects_malformed_max_news_date_before_reading_inputs() -> None:
result = runner.invoke(
app,
["sweep-trading-strategy", "--run-dir", "missing", "--max-news-date", "20260907"],
)
assert result.exit_code != 0
assert "YYYY-MM-DD" in result.output
assert "Traceback" not in result.output
def test_cli_tui_delegates_to_logged_entrypoint(monkeypatch) -> None:
calls = []
monkeypatch.setattr("sentiment_benchmark.tui.main", lambda: calls.append("main"))
result = runner.invoke(app, ["tui"])
assert result.exit_code == 0
assert calls == ["main"]
def test_cli_concurrency_default_is_provider_aware() -> None:
from sentiment_benchmark.cli import _resolve_concurrency
from sentiment_benchmark.constants import DEFAULT_CEREBRAS_CONCURRENCY
assert _resolve_concurrency("cerebras", None) == DEFAULT_CEREBRAS_CONCURRENCY
assert _resolve_concurrency("openrouter", None) == 1
assert _resolve_concurrency("ollama", None) == 1
# An explicit flag always wins over the provider default.
assert _resolve_concurrency("cerebras", 8) == 8
def test_cli_run_shows_progress_and_metrics_summary(tmp_path: Path, monkeypatch) -> None:
from contextlib import asynccontextmanager
dataset = tmp_path / "data.csv"
dataset.write_text(
"Sentence,Sentiment\npositive example,positive\nnegative example,negative\nneutral example,neutral\n",
encoding="utf-8",
)
db_path = tmp_path / "run.sqlite"
class StubClient:
async def classify(self, model_id, prompt, example, temperature=0.0, max_completion_tokens=8, retries=3):
label = example.sentence.split()[0]
return LLMResponseRecord(
row_number=example.row_number,
model_id=model_id,
prompt_hash=prompt.prompt_hash,
raw_content=label,
normalized_label=label,
parse_status="valid",
status="success",
latency_ms=10,
)
async def get_generation_metadata(self, generation_id, retries=3):
return None
@asynccontextmanager
async def stub_make_llm_client(*args, **kwargs):
yield StubClient()
monkeypatch.setattr("sentiment_benchmark.cli.make_llm_client", stub_make_llm_client)
result = runner.invoke(
app,
[
"run",
"--models",
"stub/model",
"--mode",
"pilot",
"--sample-per-class",
"1",
"--dataset-path",
str(dataset),
"--db-path",
str(db_path),
],
)
assert result.exit_code == 0, result.output
# Per-row success lines are replaced by the progress display.
assert "Saved response" not in result.output
# The closing summary shows status, throughput, and the metrics table.
assert "completed" in result.output
assert "rows/min" in result.output
assert "Metrics" in result.output
assert "stub/model" in result.output
assert "results --run-id" in result.output
def test_cli_run_surfaces_row_errors_and_resume_hint(tmp_path: Path, monkeypatch) -> None:
from contextlib import asynccontextmanager
dataset = tmp_path / "data.csv"
dataset.write_text(
"Sentence,Sentiment\npositive example,positive\nnegative example,negative\nneutral example,neutral\n",
encoding="utf-8",
)
db_path = tmp_path / "run.sqlite"
class FailingStubClient:
async def classify(self, model_id, prompt, example, temperature=0.0, max_completion_tokens=8, retries=3):
return LLMResponseRecord(
row_number=example.row_number,
model_id=model_id,
prompt_hash=prompt.prompt_hash,
raw_content=None,
normalized_label=None,
parse_status="error",
status="api_error",
error="HTTP 404: model_not_found",
latency_ms=5,
)
async def get_generation_metadata(self, generation_id, retries=3):
return None
@asynccontextmanager
async def stub_make_llm_client(*args, **kwargs):
yield FailingStubClient()
monkeypatch.setattr("sentiment_benchmark.cli.make_llm_client", stub_make_llm_client)
result = runner.invoke(
app,
[
"run",
"--models",
"stub/model",
"--mode",
"pilot",
"--sample-per-class",
"1",
"--dataset-path",
str(dataset),
"--db-path",
str(db_path),
],
)
assert result.exit_code == 0, result.output
# Rich may soft-wrap the long line; assert on the unbreakable error token.
assert "model_not_found" in result.output
assert "3 row(s) failed" in result.output
assert "--resume-run-id" in result.output
def test_cli_sweep_accepts_prices_override_and_news_date_cutoff(tmp_path: Path) -> None:
import csv as _csv
run_dir = tmp_path / "analysis"
run_dir.mkdir()
dates = ["2026-06-08", "2026-06-09", "2026-06-10", "2026-06-11", "2026-06-12", "2026-06-15", "2026-06-16", "2026-06-17"]
with (run_dir / "daily_signals.csv").open("w", newline="", encoding="utf-8") as handle:
writer = _csv.DictWriter(
handle,
fieldnames=[
"symbol", "news_date", "scorer_id", "article_count", "valid_count",
"mean_score", "signal", "signal_value", "availability_timestamp",
],
)
writer.writeheader()
for day in dates:
writer.writerow({
"symbol": "AAPL", "news_date": day, "scorer_id": "llm/test", "article_count": "3",
"valid_count": "3", "mean_score": "0.5", "signal": "positive", "signal_value": "1",
"availability_timestamp": "",
})
# A signal past the price history: --max-news-date must drop it.
writer.writerow({
"symbol": "AAPL", "news_date": "2026-06-30", "scorer_id": "llm/test", "article_count": "1",
"valid_count": "1", "mean_score": "0.5", "signal": "positive", "signal_value": "1",
"availability_timestamp": "",
})
prices_csv = tmp_path / "external_prices.csv"
sessions = dates + ["2026-06-18", "2026-06-19", "2026-06-22"]
with prices_csv.open("w", newline="", encoding="utf-8") as handle:
writer = _csv.DictWriter(
handle,
fieldnames=["symbol", "session_date", "open", "high", "low", "close", "volume", "repaired"],
)
writer.writeheader()
for day in sessions:
writer.writerow({
"symbol": "AAPL", "session_date": day, "open": "100.0", "high": "105.0",
"low": "95.0", "close": "101.0", "volume": "1000.0", "repaired": "False",
})
result = runner.invoke(
app,
[
"sweep-trading-strategy",
"--run-dir", str(run_dir),
"--scorer", "llm/test",
"--prices", str(prices_csv),
"--max-news-date", "2026-06-17",
"--thresholds", "0.0",
"--horizons", "1",
],
)
assert result.exit_code == 0, result.output
assert (run_dir / "sweep.csv").exists()
with (run_dir / "daily_signals.csv").open("a", encoding="utf-8") as handle:
handle.write("AAPL,20260617,llm/test,1,1,0.5,positive,1,\n")
malformed_result = runner.invoke(
app,
[
"sweep-trading-strategy",
"--run-dir", str(run_dir),
"--scorer", "llm/test",
"--prices", str(prices_csv),
"--max-news-date", "2026-06-17",
"--thresholds", "0.0",
"--horizons", "1",
],
)
assert malformed_result.exit_code != 0
assert "invalid news_date" in malformed_result.output
assert "YYYY-MM-DD" in malformed_result.output
assert "Traceback" not in malformed_result.output
def test_cli_trading_strategy_dry_run_has_no_credential_dependency() -> None:
result = runner.invoke(app, ["run-trading-strategy", "--dry-run"])
assert result.exit_code == 0
assert "Trading Strategy Dry Run" in result.output
assert "AAPL, AMZN, TSLA" in result.output
assert "Entry Rule" in result.output
def test_cli_lists_funded_cross_sectional_evaluation_frame() -> None:
result = runner.invoke(app, ["list-strategies"])
assert result.exit_code == 0
assert "cross_sectional" in result.output
assert "funded daily portfolios" in result.output
assert "planned fast-follow" not in result.output
def test_cli_newsapi_check_reports_missing_key(monkeypatch) -> None:
def missing():
raise NewsApiConfigurationError("NEWSAPI_API_KEY is required for NewsAPI sourcing")
monkeypatch.setattr("sentiment_benchmark.cli._make_newsapi_client", missing)
result = runner.invoke(app, ["newsapi-check"])
assert result.exit_code != 0
assert "NEWSAPI_API_KEY is required" in result.output
def test_cli_lseg_init_config_writes_preset(tmp_path: Path) -> None:
output = tmp_path / "lseg.toml"
result = runner.invoke(
app,
[
"lseg-init-config",
"--collection-id",
"cli_lseg",
"--start",
"2025-06-26T00:00:00Z",
"--end",
"2026-06-26T00:00:00Z",
"--output",
str(output),
],
)
assert result.exit_code == 0
assert "LSEG Config Created" in result.output
text = output.read_text()
assert 'symbol = "GOOGL"' in text
assert 'ric = "JPM.N"' in text
assert "window_days = 1" in text
def test_cli_lseg_catalog_writes_metadata(tmp_path: Path) -> None:
corpus = tmp_path / "lseg" / "corpus"
corpus.mkdir(parents=True)
(corpus / "manifest.json").write_text(
json.dumps(
{
"status": "completed",
"config": {
"collection": {"id": "corpus", "start": "2025-06-26T00:00:00Z", "end": "2026-06-26T00:00:00Z"},
"companies": [{"symbol": "AAPL"}],
},
"counts": {"articles": 3, "eligible": 2, "ineligible": 1, "text_quality": {"ok": 2}},
"sharing": {"redistribute": False, "licensed_full_text": True},
}
)
)
result = runner.invoke(app, ["lseg-catalog", "--derived-root", str(tmp_path / "lseg")])
assert result.exit_code == 0
assert "LSEG Corpus Catalog" in result.output
assert (tmp_path / "lseg" / "catalog.json").exists()
assert (tmp_path / "lseg" / "catalog.csv").exists()
def _record(prompt_hash: str, row_number: int, model_id: str, label: str) -> LLMResponseRecord:
return LLMResponseRecord(
row_number=row_number,
model_id=model_id,
prompt_hash=prompt_hash,
raw_content=label,
normalized_label=label,
parse_status="valid",
status="success",
latency_ms=10,
total_tokens=2,
)
def _seed(tmp_path: Path) -> tuple[Path, int]:
db_path = tmp_path / "cli.sqlite"
store = BenchmarkStore(db_path)
store.initialize()
rows = [
DatasetRow(1, "a", "positive", False, False, 1),
DatasetRow(2, "b", "negative", False, False, 1),
DatasetRow(3, "c", "neutral", False, False, 1),
]
store.upsert_dataset(rows, str(tmp_path / "data.csv"))
prompt = make_prompt("t", "sys", "{sentence}", "label_only")
store.save_prompt(prompt)
config = RunConfig(
models=["model-a", "model-b"],
prompt=prompt,
mode="pilot",
dataset_path=str(tmp_path / "data.csv"),
db_path=str(db_path),
base_url="x",
sample_per_class=1,
)
run_id = store.create_run(config, [1, 2, 3])
for row_number, label in {1: "positive", 2: "negative", 3: "neutral"}.items():
store.save_response(run_id, _record(prompt.prompt_hash, row_number, "model-a", label))
for row_number, label in {1: "positive", 2: "positive", 3: "neutral"}.items():
store.save_response(run_id, _record(prompt.prompt_hash, row_number, "model-b", label))
store.mark_run_complete(run_id)
for model_id in ("model-a", "model-b"):
store.save_metrics(run_id, evaluate_responses(rows, store.fetch_responses(run_id, model_id), model_id, scope="primary"))
return db_path, run_id
def test_cli_runs_lists_seeded_run(tmp_path: Path) -> None:
db_path, run_id = _seed(tmp_path)
result = runner.invoke(app, ["runs", "--db-path", str(db_path)])
assert result.exit_code == 0
assert "Benchmark Runs" in result.output
assert "model-a, model-b" in result.output
def test_cli_runs_handles_empty_db(tmp_path: Path) -> None:
db_path = tmp_path / "empty.sqlite"
BenchmarkStore(db_path).initialize()
result = runner.invoke(app, ["runs", "--db-path", str(db_path)])
assert result.exit_code == 0
assert "No runs found" in result.output
def test_cli_results_shows_metrics_and_confusion(tmp_path: Path) -> None:
db_path, run_id = _seed(tmp_path)
result = runner.invoke(app, ["results", "--run-id", str(run_id), "--db-path", str(db_path), "--confusion"])
assert result.exit_code == 0
assert f"Run {run_id} Metrics" in result.output
assert "Confusion matrix" in result.output
def test_cli_results_missing_run_exits_nonzero(tmp_path: Path) -> None:
db_path, _ = _seed(tmp_path)
result = runner.invoke(app, ["results", "--run-id", "999", "--db-path", str(db_path)])
assert result.exit_code == 1
assert "No metrics found" in result.output
def test_cli_compare_reports_mcnemar_and_cis(tmp_path: Path) -> None:
db_path, run_id = _seed(tmp_path)
result = runner.invoke(
app,
[
"compare",
"--run-a",
str(run_id),
"--model-a",
"model-a",
"--model-b",
"model-b",
"--db-path",
str(db_path),
"--n-resamples",
"200",
],
)
assert result.exit_code == 0
assert "Comparison" in result.output
assert "McNemar p =" in result.output
def test_cli_compare_no_overlap_exits_nonzero(tmp_path: Path) -> None:
db_path, run_id = _seed(tmp_path)
result = runner.invoke(
app,
["compare", "--run-a", str(run_id), "--model-a", "model-a", "--model-b", "ghost", "--db-path", str(db_path)],
)
assert result.exit_code == 1
assert "No overlapping" in result.output
def test_cli_compare_rejects_bad_metric(tmp_path: Path) -> None:
db_path, run_id = _seed(tmp_path)
result = runner.invoke(
app,
["compare", "--run-a", str(run_id), "--model-a", "model-a", "--model-b", "model-b", "--metric", "bogus", "--db-path", str(db_path)],
)
assert result.exit_code != 0
def _news_result(query: str = "market news", config=None) -> NewsFetchResult:
normalized = normalize_url("https://example.com/article")
return NewsFetchResult(
config=config or make_news_fetch_config(query=query, max_results=1),
fetched_at="2026-06-07T12:00:00+00:00",
records=[
NewsArticleRecord(
record_id=article_record_id(normalized),
url="https://example.com/article",
normalized_url=normalized,
title="Market article",
snippet="Snippet",
article_text="Full article text",
extraction_status="success",
)
],
search_request_id="search-1",
search_usage={"credits": 1},
)
def _write_package_source(path: Path, *, query: str = "bank earnings sentiment", url: str = "https://example.com/article") -> Path:
path.mkdir(parents=True)
normalized = normalize_url(url)
record = {
"record_id": article_record_id(normalized),
"url": url,
"normalized_url": normalized,
"title": "Market article",
"snippet": "Snippet",
"article_text": "Full article text",
"extraction_status": "success",
}
(path / "articles.jsonl").write_text(json.dumps(record, sort_keys=True) + "\n", encoding="utf-8")
(path / "manifest.json").write_text(
json.dumps(
{
"schema_version": 1,
"source": "tavily",
"fetched_at": "2026-06-07T12:00:00+00:00",
"query": query,
"record_count": 1,
"search_request_id": "search-1",
"extract_request_id": "extract-1",
},
sort_keys=True,
),
encoding="utf-8",
)
return path
def _write_batch_matrix(path: Path) -> Path:
path.write_text(
"""
version = 1
description = "Reusable Tavily query matrix for CLI tests."
[[queries]]
id = "bank_earnings"
family = "Bank earnings"
query = "bank earnings sentiment"
topic = "news"
time_range = "month"
max_results = 1
search_depth = "basic"
coverage_target = "Company performance and earnings commentary."
include_domains = []
exclude_domains = []
notes = "Use for source collection; labels are intentionally absent."
[[queries]]
id = "market_volatility"
family = "Market volatility"
query = "market volatility stocks bonds currency"
topic = "news"
time_range = "month"
max_results = 1
search_depth = "basic"
coverage_target = "Market movement and event-driven reporting."
include_domains = []
exclude_domains = []
notes = "Use for source collection; labels are intentionally absent."
""".strip()
+ "\n",
encoding="utf-8",
)
return path
class FakeNewsClient:
def __init__(self) -> None:
self.configs = []
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc, traceback) -> None:
return None
async def fetch(self, config):
self.configs.append(config)
return _news_result(config.query, config=config)
def test_cli_news_check_uses_tavily_client(monkeypatch) -> None:
fake = FakeNewsClient()
monkeypatch.setattr("sentiment_benchmark.cli._make_tavily_news_client", lambda: fake)
result = runner.invoke(app, ["news-check", "--query", "financial markets", "--max-results", "1"])
assert result.exit_code == 0
assert "Tavily News Check" in result.output
assert "search-1" in result.output
assert fake.configs[0].extract is False
def test_cli_fetch_news_writes_outputs(tmp_path: Path, monkeypatch) -> None:
fake = FakeNewsClient()
monkeypatch.setattr("sentiment_benchmark.cli._make_tavily_news_client", lambda: fake)
output_dir = tmp_path / "news"
result = runner.invoke(
app,
[
"fetch-news",
"--query",
"bank earnings sentiment",
"--max-results",
"1",
"--output-dir",
str(output_dir),
],
)
assert result.exit_code == 0
assert "Tavily News Fetch" in result.output
exported = list(output_dir.glob("tavily_news_*"))
assert len(exported) == 1
assert (exported[0] / "articles.jsonl").exists()
assert (exported[0] / "articles.csv").exists()
assert (exported[0] / "manifest.json").exists()
def test_cli_fetch_news_rejects_invalid_topic(tmp_path: Path) -> None:
result = runner.invoke(
app,
[
"fetch-news",
"--query",
"bank earnings sentiment",
"--topic",
"bogus",
"--output-dir",
str(tmp_path / "news"),
],
)
assert result.exit_code != 0
assert "topic must be one of" in result.output