|
| 1 | +"""CashPilot-45k and -tb5: two affirmatives nobody had earned. |
| 2 | +
|
| 3 | +**45k** — the Active Services card showed "0" when the count could not be taken. |
| 4 | +``_get_all_worker_containers`` opens SQLite, so a locked or busy database, or a |
| 5 | +JSON-decode failure on a worker row, lands in the ``except`` while containers |
| 6 | +are in fact running. The fallback was ``0``, which reads as "nothing is |
| 7 | +running", and the only other signal was a ``logger.debug`` — and DEBUG is off in |
| 8 | +production, so both places that could have said something said nothing. |
| 9 | +
|
| 10 | +**tb5** — the bell rendered an empty alert list as "All collectors healthy". On |
| 11 | +a fresh install, or after a restart before the first hourly collection, nothing |
| 12 | +has been checked. The bell's own FAILURE path is written correctly ("Alerts |
| 13 | +unavailable" rather than healthy), which makes the never-ran case the outlier |
| 14 | +rather than a matter of style. |
| 15 | +
|
| 16 | +Both are the same shape: absent presented as an affirmative. |
| 17 | +""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +import re |
| 22 | +from pathlib import Path |
| 23 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 24 | + |
| 25 | +import pytest |
| 26 | + |
| 27 | +ROOT = Path(__file__).resolve().parents[1] |
| 28 | +APP_JS = ROOT / "app" / "static" / "js" / "app.js" |
| 29 | + |
| 30 | + |
| 31 | +def js() -> str: |
| 32 | + text = APP_JS.read_text(encoding="utf-8") |
| 33 | + text = re.sub(r"/\*.*?\*/", "", text, flags=re.S) |
| 34 | + return "\n".join(re.sub(r"(^|\s)//.*$", "", line) for line in text.splitlines()) |
| 35 | + |
| 36 | + |
| 37 | +async def _summary(*, worker_read_fails): |
| 38 | + from app import main |
| 39 | + |
| 40 | + containers = ( |
| 41 | + AsyncMock(side_effect=Exception("database is locked")) if worker_read_fails else AsyncMock(return_value=[]) |
| 42 | + ) |
| 43 | + with ( |
| 44 | + patch.object(main, "_require_auth_api", lambda r: None), |
| 45 | + patch.object(main.database, "get_earnings_dashboard_summary", AsyncMock(return_value={"total": 0})), |
| 46 | + patch.object(main.database, "get_config", AsyncMock(return_value={})), |
| 47 | + patch.object(main.database, "get_earnings_summary", AsyncMock(return_value=[])), |
| 48 | + patch.object(main, "_get_all_worker_containers", containers), |
| 49 | + ): |
| 50 | + return await main.api_earnings_summary(MagicMock()) |
| 51 | + |
| 52 | + |
| 53 | +class TestAnUncountableFleetIsUnknown: |
| 54 | + @pytest.mark.asyncio |
| 55 | + async def test_a_failed_read_reports_none(self): |
| 56 | + assert (await _summary(worker_read_fails=True))["active_services"] is None |
| 57 | + |
| 58 | + @pytest.mark.asyncio |
| 59 | + async def test_a_successful_read_of_nothing_still_reports_zero(self): |
| 60 | + """The control. A fleet with no running containers really is zero.""" |
| 61 | + assert (await _summary(worker_read_fails=False))["active_services"] == 0 |
| 62 | + |
| 63 | + @pytest.mark.asyncio |
| 64 | + async def test_the_failure_is_logged_where_it_can_be_seen(self, caplog): |
| 65 | + """DEBUG is off in production, so a debug log is the same as silence.""" |
| 66 | + import logging |
| 67 | + |
| 68 | + with caplog.at_level(logging.WARNING, logger="app.main"): |
| 69 | + caplog.clear() |
| 70 | + await _summary(worker_read_fails=True) |
| 71 | + assert any("Could not count active services" in r.getMessage() for r in caplog.records) |
| 72 | + |
| 73 | + def test_the_card_renders_a_dash(self): |
| 74 | + source = js() |
| 75 | + assert "data.active_services || 0" not in source, "unknown still renders as 0" |
| 76 | + assert "data.active_services == null" in source |
| 77 | + |
| 78 | + |
| 79 | +class TestTheBellDoesNotClaimUncheckedHealth: |
| 80 | + async def _alerts(self, *, alerts, has_run): |
| 81 | + from app import main |
| 82 | + |
| 83 | + with ( |
| 84 | + patch.object(main, "_require_auth_api", lambda r: None), |
| 85 | + patch.object(main, "_collector_alerts", alerts), |
| 86 | + patch.object(main, "_collection_has_run", has_run), |
| 87 | + ): |
| 88 | + return await main.api_collector_alerts(MagicMock()) |
| 89 | + |
| 90 | + @pytest.mark.asyncio |
| 91 | + async def test_a_fresh_install_reports_that_nothing_has_run(self): |
| 92 | + out = await self._alerts(alerts=[], has_run=False) |
| 93 | + assert out["collected"] is False |
| 94 | + assert out["alerts"] == [] |
| 95 | + |
| 96 | + @pytest.mark.asyncio |
| 97 | + async def test_after_a_run_with_no_problems_it_says_so(self): |
| 98 | + """The control: a genuine all-clear must remain expressible.""" |
| 99 | + out = await self._alerts(alerts=[], has_run=True) |
| 100 | + assert out["collected"] is True |
| 101 | + |
| 102 | + @pytest.mark.asyncio |
| 103 | + async def test_the_alerts_still_come_through_tagged(self): |
| 104 | + out = await self._alerts(alerts=[{"platform": "grass", "error": "boom"}], has_run=True) |
| 105 | + assert out["alerts"][0]["kind"] == "collector" |
| 106 | + |
| 107 | + def test_the_ui_distinguishes_the_two(self): |
| 108 | + source = js() |
| 109 | + assert "payload.collected" in source |
| 110 | + assert "No collection has run yet" in source |
| 111 | + |
| 112 | + def test_the_all_clear_is_still_possible(self): |
| 113 | + """The control: the affirmative must survive for the case that earns it.""" |
| 114 | + assert "All collectors healthy" in js() |
| 115 | + |
| 116 | + def test_the_ui_reads_the_new_shape(self): |
| 117 | + source = js() |
| 118 | + assert "payload.alerts" in source |
| 119 | + |
| 120 | + |
| 121 | +class TestTheFlagSurvivesARestart: |
| 122 | + """A restart must not make the bell claim nothing has ever been checked.""" |
| 123 | + |
| 124 | + async def _warm(self, *, stored_alerts, earnings): |
| 125 | + from app import main |
| 126 | + |
| 127 | + with ( |
| 128 | + patch.object(main, "_collection_has_run", False), |
| 129 | + patch.object(main.database, "list_alerts", AsyncMock(return_value=stored_alerts)), |
| 130 | + patch.object(main.database, "get_earnings_summary", AsyncMock(return_value=earnings)), |
| 131 | + ): |
| 132 | + await main._warm_collector_alerts() |
| 133 | + return main._collection_has_run |
| 134 | + |
| 135 | + @pytest.mark.asyncio |
| 136 | + async def test_stored_alerts_prove_a_run_happened(self): |
| 137 | + stored = [{"kind": "collector", "subject": "grass", "message": "boom"}] |
| 138 | + assert await self._warm(stored_alerts=stored, earnings=[]) is True |
| 139 | + |
| 140 | + @pytest.mark.asyncio |
| 141 | + async def test_earnings_rows_prove_it_too(self): |
| 142 | + rows = [{"platform": "honeygain", "balance": 1.0, "currency": "USD"}] |
| 143 | + assert await self._warm(stored_alerts=[], earnings=rows) is True |
| 144 | + |
| 145 | + @pytest.mark.asyncio |
| 146 | + async def test_a_genuinely_fresh_install_stays_false(self): |
| 147 | + """The control: without it this passes by always claiming a run.""" |
| 148 | + assert await self._warm(stored_alerts=[], earnings=[]) is False |
| 149 | + |
| 150 | + @pytest.mark.asyncio |
| 151 | + async def test_a_failed_lookup_does_not_claim_a_run(self): |
| 152 | + from app import main |
| 153 | + |
| 154 | + with ( |
| 155 | + patch.object(main, "_collection_has_run", False), |
| 156 | + patch.object(main.database, "list_alerts", AsyncMock(return_value=[])), |
| 157 | + patch.object(main.database, "get_earnings_summary", AsyncMock(side_effect=Exception("locked"))), |
| 158 | + ): |
| 159 | + await main._warm_collector_alerts() |
| 160 | + assert main._collection_has_run is False |
0 commit comments