| 1 | """Deleted topics still contribute their recorded cost to store stats.""" |
| 2 | |
| 3 | from datetime import datetime, timedelta, timezone |
| 4 | |
| 5 | import store |
| 6 | |
| 7 | |
| 8 | def test_weekly_cost_includes_recent_removed_runs(tmp_path): |
| 9 | with store.scoped_db(tmp_path / "research.db"): |
| 10 | kept = store.add_topic("Current") |
| 11 | store.record_run(kept["id"], token_cost=0.25) |
| 12 | removed = store.add_topic("Expired watch") |
| 13 | store.record_run(removed["id"], token_cost=1.25) |
| 14 | old = store.record_run(removed["id"], token_cost=4.0) |
| 15 | old_date = (datetime.now(timezone.utc) - timedelta(days=10)).strftime("%Y-%m-%d %H:%M:%S") |
| 16 | conn = store._connect() |
| 17 | try: |
| 18 | with conn: |
| 19 | conn.execute("UPDATE research_runs SET run_date = ? WHERE id = ?", (old_date, old)) |
| 20 | finally: |
| 21 | conn.close() |
| 22 | assert store.remove_topic("Expired watch") |
| 23 | |
| 24 | stats = store.get_stats() |
| 25 | assert stats["runs_7d"] == 1 |
| 26 | assert stats["cost_7d"] == 1.5 |
| 27 |