diff --git a/app/src/components/FalloutPanel.js b/app/src/components/FalloutPanel.js index c2cbb904..1b1ae649 100644 --- a/app/src/components/FalloutPanel.js +++ b/app/src/components/FalloutPanel.js @@ -1000,19 +1000,25 @@ export function Timeline({after, onAfter, playing, onPlaying, startMs}) { ; } -// Whether the forecast's winds run out before the fallout of bursts at `hour` is down: a warning -// saying so, or null. +// A warning about the forecast for bursts at its hour `hour` (an index), or null: whether it is out of +// date, and whether its winds run out before their fallout is down. The winds follow the hour chosen, +// so the last hour is held for the whole simulation only when it is the hour chosen. export function forecastShortText(forecast, hour) { if (!forecast) return null; + const last = forecast.hours.length - 1; + let held = null; + if (hour >= last) { + held = "Its last hour's winds are held for the whole simulation."; + } else if (forecastRunsOut(forecast, hour) !== null) { + held = `It ends at ${hourLabel(forecast.hours[last], false)}, before all the fallout is down: ` + + "its last hour's winds are held from then on."; + } if (forecastExpired(forecast)) { - return "This forecast has run out: its last hour's winds are held for the whole simulation. " - + "Download a new one on the map's Manage tab, or use one wind at every height."; + return ["This forecast is out of date: its winds are past ones, not today's.", held, + "Download a new one on the map's Manage tab, or use one wind at every height."] + .filter(Boolean).join(" "); } - if (forecastRunsOut(forecast, hour) !== null) { - return `This forecast ends at ${hourLabel(forecast.hours[forecast.hours.length - 1], false)}, before ` - + "all the fallout is down: its last hour's winds are held from then on."; - } - return null; + return held && `This forecast is short. ${held}`; } // The forecast winds aloft: which hour, how old, where they reach, and whether they last until the diff --git a/app/src/components/FalloutPanel.test.js b/app/src/components/FalloutPanel.test.js index 012e7925..5f0e8eb6 100644 --- a/app/src/components/FalloutPanel.test.js +++ b/app/src/components/FalloutPanel.test.js @@ -328,12 +328,22 @@ describe('the forecast\'s warning', () => { const hourMs = Math.floor(Date.now() / 3600000) * 3600000; const of = (from, count) => ({hours: Array.from({length: count}, (_, h) => new Date(hourMs + (from + h) * 3600000).toISOString())}); - test('says when it has run out, or ends before the fallout is down, and else nothing', () => { + test('says when it is out of date, or ends before the fallout is down, and else nothing', () => { expect(forecastShortText(null, 0)).toBeNull(); - expect(forecastShortText(of(-30, 10), 9)).toMatch(/has run out/); + expect(forecastShortText(of(-30, 10), 9)).toMatch(/out of date.*last hour's winds are held for the whole simulation/); expect(forecastShortText(of(-1, 30), 1)).toMatch(/ends at .*, before all the fallout is down/); expect(forecastShortText(of(-1, 96), 1)).toBeNull(); }); + + test('says the last hour is held only from when it is, for the hour chosen', () => { + // Out of date, but its first hour still has 48 hours of winds after it: none is held. + const old = of(-200, 96); + expect(forecastShortText(old, 0)).toMatch(/out of date/); + expect(forecastShortText(old, 0)).not.toMatch(/held/); + expect(forecastShortText(old, 60)).toMatch(/held from then on/); + expect(forecastShortText(old, 60)).not.toMatch(/whole simulation/); + expect(forecastShortText(old, 95)).toMatch(/held for the whole simulation/); + }); }); describe('the targets simulated', () => { @@ -543,9 +553,9 @@ describe('the fallout panel\'s steps', () => { await act(async () => new Promise((r) => setTimeout(r, 50))); fireEvent.click(screen.getByRole('textbox', {name: 'Wind'})); fireEvent.click([...document.querySelectorAll('[role=option]')].find((o) => o.textContent === 'Forecast winds aloft')); - expect(screen.getByText(/This forecast has run out: its last hour's winds are held/)).toBeTruthy(); + expect(screen.getByText(/This forecast is out of date: .*last hour's winds are held/)).toBeTruthy(); fireEvent.click(screen.getByRole('button', {name: 'Next: the fallout'})); - expect(screen.getByText(/This forecast has run out/)).toBeTruthy(); + expect(screen.getByText(/This forecast is out of date/)).toBeTruthy(); } finally { mockLatestWinds = null; } diff --git a/modules/map/climatology.py b/modules/map/climatology.py index 72143acc..bb5317a5 100644 --- a/modules/map/climatology.py +++ b/modules/map/climatology.py @@ -56,12 +56,31 @@ def month_name(month: int) -> str: return f'month-{month:02d}.bin.gz' +# The kept files found to be what is expected: path -> (size, mtime) when hashed. +_checked = dict() + + def month_path(month: int) -> Optional[Path]: - """The kept file of `month`, or None.""" - if month not in MONTHS: + """The kept file of `month`, or None if there is none, or it is not the file expected (it is then + fetched again). A file is hashed again only after it changes.""" + expected = MONTHS.get(month) + if not expected: return None path = climatology_directory() / month_name(month) - return path if path.is_file() else None + try: + stat = path.stat() + except FileNotFoundError: + return None + if _checked.get(path) == (stat.st_size, stat.st_mtime_ns): + return path + with path.open('rb') as fh: + actual = hashlib.file_digest(fh, 'sha256').hexdigest() + if actual != expected: + _checked.pop(path, None) + logger.warning(f'{path} is not the climatology expected (sha256 {actual}); it will be fetched again') + return None + _checked[path] = (stat.st_size, stat.st_mtime_ns) + return path def months_kept() -> List[int]: diff --git a/modules/map/test/test_climatology.py b/modules/map/test/test_climatology.py index d33b9dd8..29070b9d 100644 --- a/modules/map/test/test_climatology.py +++ b/modules/map/test/test_climatology.py @@ -116,3 +116,21 @@ async def test_worker_waits_for_internet_and_drive(test_directory, published, fl flags.media_mounted.set() await asyncio.wait_for(worker, timeout=5) fetch.assert_awaited_once() + + +@pytest.mark.asyncio +async def test_month_kept_must_be_what_is_expected(test_directory, published): + """A kept month that is changed, or replaced, is not listed or served, and is fetched again.""" + data, bodies = published + get, asked = fake_get(bodies) + with mock.patch('modules.map.climatology.aiohttp_get', get), mock.patch('modules.map.climatology.asyncio.sleep'): + await climatology.fetch_missing() + path = climatology.month_path(1) + assert path + + path.write_bytes(gzip.compress(b'WFCL another month')) + assert climatology.month_path(1) is None + assert climatology.months_kept() == [2] + + assert await climatology.fetch_missing() == [1] + assert climatology.month_path(1).read_bytes() == data[1]