fix(avocet): _write_jsonl empty-list writes empty file; add reset_last_action helper
This commit is contained in:
parent
ffd1450f62
commit
c8aea3c39f
2 changed files with 20 additions and 9 deletions
12
app/api.py
12
app/api.py
|
|
@ -20,6 +20,12 @@ def set_data_dir(path: Path) -> None:
|
||||||
_DATA_DIR = path
|
_DATA_DIR = path
|
||||||
|
|
||||||
|
|
||||||
|
def reset_last_action() -> None:
|
||||||
|
"""Reset undo state — used by tests."""
|
||||||
|
global _last_action
|
||||||
|
_last_action = None
|
||||||
|
|
||||||
|
|
||||||
def _queue_file() -> Path:
|
def _queue_file() -> Path:
|
||||||
return _DATA_DIR / "email_label_queue.jsonl"
|
return _DATA_DIR / "email_label_queue.jsonl"
|
||||||
|
|
||||||
|
|
@ -41,10 +47,8 @@ def _read_jsonl(path: Path) -> list[dict]:
|
||||||
|
|
||||||
def _write_jsonl(path: Path, records: list[dict]) -> None:
|
def _write_jsonl(path: Path, records: list[dict]) -> None:
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
path.write_text(
|
text = "\n".join(json.dumps(r, ensure_ascii=False) for r in records)
|
||||||
"\n".join(json.dumps(r, ensure_ascii=False) for r in records) + "\n",
|
path.write_text(text + "\n" if records else "", encoding="utf-8")
|
||||||
encoding="utf-8",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _append_jsonl(path: Path, record: dict) -> None:
|
def _append_jsonl(path: Path, record: dict) -> None:
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,15 @@
|
||||||
# tests/test_api.py
|
import pytest
|
||||||
import json, pytest
|
from app import api as api_module # noqa: F401
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def reset_globals(tmp_path):
|
||||||
|
from app import api
|
||||||
|
api.set_data_dir(tmp_path)
|
||||||
|
api.reset_last_action()
|
||||||
|
yield
|
||||||
|
api.reset_last_action()
|
||||||
|
|
||||||
|
|
||||||
# We'll import helpers once they exist
|
|
||||||
# For now just verify the file can be imported
|
|
||||||
def test_import():
|
def test_import():
|
||||||
from app import api # noqa: F401
|
from app import api # noqa: F401
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue