Spaces:
Sleeping
Sleeping
Download tests/test_event_log.py from DerXanity/farient-deckcheck: direct link, hf CLI and curl.
- Browser
- Download file 4.52 kB
-
https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/tests/test_event_log.py
- Command line
-
hf download hf://spaces/DerXanity/farient-deckcheck/tests/test_event_log.py
-
curl -L -o test_event_log.py https://huggingface.co/spaces/DerXanity/farient-deckcheck/resolve/main/tests/test_event_log.py
4.52 kB
| """Tests for ``services.event_log`` β the per-job JSONL stream backing SSE.""" | |
| from __future__ import annotations | |
| from pathlib import Path | |
| import pytest | |
| from services.event_log import ( | |
| EVENTS_FILE_NAME, | |
| append_event, | |
| events_path, | |
| file_size, | |
| read_events_since, | |
| ) | |
| def test_append_creates_file_and_appends_line(tmp_path: Path): | |
| append_event(tmp_path, "slide-reviewed", {"slide_number": 1, "ok": True}) | |
| p = events_path(tmp_path) | |
| assert p.exists() | |
| assert p.name == EVENTS_FILE_NAME | |
| content = p.read_text(encoding="utf-8") | |
| assert content.count("\n") == 1 | |
| assert "slide-reviewed" in content | |
| assert "\"slide_number\": 1" in content | |
| def test_read_events_since_drains_in_order(tmp_path: Path): | |
| append_event(tmp_path, "slide-reviewed", {"slide_number": 1}) | |
| append_event(tmp_path, "slide-reviewed", {"slide_number": 2}) | |
| append_event(tmp_path, "slide-reviewed", {"slide_number": 3}) | |
| events, offset = read_events_since(tmp_path, 0) | |
| assert [e["data"]["slide_number"] for e in events] == [1, 2, 3] | |
| # Offset should equal the file size. | |
| assert offset == file_size(tmp_path) > 0 | |
| def test_read_events_since_resumes_from_offset(tmp_path: Path): | |
| append_event(tmp_path, "slide-reviewed", {"slide_number": 1}) | |
| first_events, first_offset = read_events_since(tmp_path, 0) | |
| assert len(first_events) == 1 | |
| append_event(tmp_path, "slide-reviewed", {"slide_number": 2}) | |
| new_events, new_offset = read_events_since(tmp_path, first_offset) | |
| assert [e["data"]["slide_number"] for e in new_events] == [2] | |
| assert new_offset > first_offset | |
| def test_read_events_handles_missing_file(tmp_path: Path): | |
| """No file yet β empty list, offset 0.""" | |
| events, offset = read_events_since(tmp_path, 0) | |
| assert events == [] | |
| assert offset == 0 | |
| def test_read_events_skips_blank_lines(tmp_path: Path): | |
| """Blank lines are ignored without breaking the stream.""" | |
| p = events_path(tmp_path) | |
| p.parent.mkdir(parents=True, exist_ok=True) | |
| p.write_text("\n\n", encoding="utf-8") | |
| events, offset = read_events_since(tmp_path, 0) | |
| assert events == [] | |
| # We DID consume the blank lines (offset advances past them). | |
| assert offset == 2 | |
| def test_read_events_tolerates_malformed_line(tmp_path: Path): | |
| """A bad JSON line is skipped β surrounding good lines still come through.""" | |
| p = events_path(tmp_path) | |
| p.parent.mkdir(parents=True, exist_ok=True) | |
| good = "{\"event\":\"x\",\"data\":{\"k\":1}}" | |
| bad = "{not-json" | |
| p.write_text(good + "\n" + bad + "\n" + good + "\n", encoding="utf-8") | |
| events, offset = read_events_since(tmp_path, 0) | |
| # Two good events; one bad skipped. | |
| assert len(events) == 2 | |
| assert all(e["data"]["k"] == 1 for e in events) | |
| assert offset == p.stat().st_size | |
| def test_read_events_does_not_consume_partial_trailing_line(tmp_path: Path): | |
| """A line without a trailing \\n must NOT be returned β keep it for next read.""" | |
| p = events_path(tmp_path) | |
| p.parent.mkdir(parents=True, exist_ok=True) | |
| p.write_text("{\"event\":\"a\",\"data\":{}}\n{\"event\":\"b\"", encoding="utf-8") | |
| events, offset = read_events_since(tmp_path, 0) | |
| # Only the first (newline-terminated) line is read. | |
| assert len(events) == 1 | |
| assert events[0]["event"] == "a" | |
| # Offset stops *before* the partial second line. | |
| assert offset < p.stat().st_size | |
| # Now finish writing the partial line + newline. | |
| with p.open("a", encoding="utf-8") as f: | |
| f.write(",\"data\":{}}\n") | |
| events_2, offset_2 = read_events_since(tmp_path, offset) | |
| assert len(events_2) == 1 | |
| assert events_2[0]["event"] == "b" | |
| assert offset_2 == p.stat().st_size | |
| def test_append_does_not_raise_on_write_failure(tmp_path: Path, monkeypatch): | |
| """Best-effort: a write failure must not propagate (audit must keep going).""" | |
| import services.event_log as event_log_module | |
| real_mkdir = Path.mkdir | |
| def boom(self, *_a, **_kw): | |
| # Only fail when called from within the event_log module's own | |
| # mkdir invocation; let other tests' mkdir calls work normally. | |
| raise OSError("disk full") | |
| # Patch via the module's Path attribute so we don't poison sibling tests. | |
| monkeypatch.setattr(event_log_module.Path, "mkdir", boom) | |
| target = tmp_path / "nope" | |
| # MUST NOT raise. | |
| append_event(target, "slide-reviewed", {"slide_number": 1}) | |
| # Sanity: the real mkdir is restored after the test by monkeypatch. | |
| _ = real_mkdir | |