Skip to content

test(snapshot): cover the Engine.snapshot() contract - #203

Open
harsh-thakkar7 wants to merge 1 commit into
LeyckerS:mainfrom
harsh-thakkar7:test/engine-snapshot-contract
Open

harsh-thakkar7 wants to merge 1 commit into
LeyckerS:mainfrom
harsh-thakkar7:test/engine-snapshot-contract

Conversation

@harsh-thakkar7

Copy link
Copy Markdown
Contributor

Closes #57

Problem

Engine.snapshot() is the single interface between the engine and every front-end — the GUI polls it ~12 times a second, moon_bridge.py serves it over the loopback API, and render_gui.py/integration_http.py depend on its shape. It had no unit tests: everything it produced was verified only by running a real download and looking at the window. #58 (a clock that never stopped) lived in this method and nothing caught it, because catching it needed nothing more than calling the method twice and comparing two numbers.

Change

One new file, tests/test_snapshot.py — 11 tests, no production code changed. The mid-run states are produced by setting the counters snapshot() reads directly rather than by driving a download, so what is under test is the snapshot contract and not the downloader. The five behaviours from the issue, each pinned by tests named for it:

  1. Idle before anything runs — test_fresh_engine_reports_idle_with_zeroed_counters: stage == "idle", elapsed_s == 0, all counters zero, empty log, cursor == 0, and it does not raise.
  2. Stage progression — test_stage_is_extracting_while_links_remain_unextracted, test_stage_is_downloading_once_every_link_is_extracted, test_stage_is_done_once_every_download_finished.
  3. The log cursor — test_log_cursor_returns_every_line_when_starting_from_zero, test_a_second_read_with_the_same_cursor_returns_nothing (the no-gaps contract), test_lines_logged_after_the_cursor_come_back_on_the_next_read.
  4. ETA and speed are safe when empty — test_speed_and_eta_are_zero_before_any_run, test_eta_stays_zero_once_downloads_are_in_flight (files downloading, no byte samples yet: no division by zero, no clamped ceiling).
  5. Counters reset between runs — test_start_clears_the_previous_runs_totals: a real run through the browser_calls fixture, started over the remains of a finished seven-link run; asserts the totals are the new run's and the old ok == 7 did not leak in.

The clock test

test_a_finished_run_keeps_stage_done_and_stops_the_clock is the test that would have caught #58: a finished run must report stage == "done" and the same elapsed_s on every later snapshot. #58 is closed, so it asserts the shipped behaviour rather than failing on it.

Verification

  • python -m pytest tests/ -q → 181 passed (170 before this file), no browser and no network; the whole file runs in ~1.1 s
  • ruff check . → clean (ruff 0.16.4; CI pins 0.16.1)
  • No production code changed — the diff is the one new test file
  • snapshot() did not need restructuring to be tested

Not covered automatically: nothing in the GUI itself. These tests pin the numbers snapshot() returns; whether a given widget renders them correctly still needs the eye check only render_gui.py can give (per #179).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

test: Engine.snapshot() has no unit tests, and it is the contract every front-end depends on

1 participant