Files
chemenu/tools/chemenu/tests/test_log_append.py
T
torbenandClaude Opus 5.5 0fce67e6b9
CI / verify (push) Successful in 8m31s
CI / pwsh (push) Successful in 2m51s
Converters / converters (push) Successful in 9m42s
Release / converters (push) Successful in 26s
Release / release (push) Successful in 41s
Release / wiki-mirror (push) Skipped
tools, instructions: Abhängigkeiten - Tests biegen config-Pfade über set_config_path um (pytest 9.2), Teardown-Wache; Befehlsreferenz mit typer 0.29 neu erzeugt, typer>=0.29.1, kein click mehr in Tests (#243); 10.2.0-beta.2
Files changed:
- CHANGES.md
- VERSION
- docs/reference/commands/configuration.md
- docs/reference/commands/content-migrations.md
- docs/reference/commands/distribution-and-versioning.md
- docs/reference/commands/exports.md
- docs/reference/commands/finding-and-checking.md
- docs/reference/commands/git.md
- docs/reference/commands/pages.md
- docs/reference/commands/raw-material-and-uploads.md
- docs/reference/commands/types-instructions-and-docs.md
- instructions/dev/testing-conventions.md
- tools/chemenu/config.py
- tools/chemenu/tests/conftest.py
- tools/chemenu/tests/test_api.py
- tools/chemenu/tests/test_backup.py
- tools/chemenu/tests/test_cite_cmd.py
- tools/chemenu/tests/test_collect.py
- tools/chemenu/tests/test_config_shapes.py
- tools/chemenu/tests/test_conventions.py
- tools/chemenu/tests/test_corpus_cache.py
- tools/chemenu/tests/test_dist_cmd.py
- tools/chemenu/tests/test_dist_upgrade.py
- tools/chemenu/tests/test_docs_verify.py
- tools/chemenu/tests/test_doctor.py
- tools/chemenu/tests/test_export_guidelines.py
- tools/chemenu/tests/test_export_run.py
- tools/chemenu/tests/test_git_publish.py
- tools/chemenu/tests/test_hermetic_env.py
- tools/chemenu/tests/test_install_doc.py
- tools/chemenu/tests/test_instructions_cmd.py
- tools/chemenu/tests/test_kb_collections.py
- tools/chemenu/tests/test_legacy_settings.py
- tools/chemenu/tests/test_lint.py
- tools/chemenu/tests/test_log_append.py
- tools/chemenu/tests/test_migrate_cmd.py
- tools/chemenu/tests/test_new_page.py
- tools/chemenu/tests/test_page_ops.py
- tools/chemenu/tests/test_pipeline_l0.py
- tools/chemenu/tests/test_preflight.py
- tools/chemenu/tests/test_provenance.py
- tools/chemenu/tests/test_reference_doc.py
- tools/chemenu/tests/test_review.py
- tools/chemenu/tests/test_settings.py
- tools/chemenu/tests/test_task_cmd.py
- tools/chemenu/tests/test_telemetry_policy.py
- tools/chemenu/tests/test_toc.py
- tools/chemenu/tests/test_touch.py
- tools/chemenu/tests/test_upload_cmd.py
- tools/chemenu/tests/test_version_cmd.py
- tools/chemenu/tests/test_work_cmd.py
- tools/requirements.txt

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015uxGFdBAmyA23g1isCtGEY
2026-11-18 15:52:24 +01:00

140 lines
4.2 KiB
Python

import pytest
import typer
from chemenu.commands.log_append import (
format_log_entry,
ingests_since_last_lint,
log_append,
log_status,
parse_log_entries,
)
from chemenu.tests.conftest import set_config_path
def test_log_entry_format_matches_convention():
entry = format_log_entry("ingest", "raw/notes/gateway.example.net.md", "Some details.", today="2026-08-02")
assert entry == (
"## [2026-08-02] ingest | raw/notes/gateway.example.net.md\n"
"\n"
"Some details.\n"
"\n"
"---\n"
)
def test_log_entry_without_body_has_no_blank_body_block():
entry = format_log_entry("query", "What projects use MQTT?", "", today="2026-08-02")
assert entry == "## [2026-08-02] query | What projects use MQTT?\n\n---\n"
def test_parse_log_entries_reads_date_op_and_title_in_file_order():
text = (
"# Wiki Log\n\n"
"## [2026-08-01] ingest | raw/notes/a.md\n\n---\n\n"
"## [2026-08-02] lint | Lint Report 2026-08-02.md\n\n---\n"
)
assert parse_log_entries(text) == [
("2026-08-01", "ingest", "raw/notes/a.md"),
("2026-08-02", "lint", "Lint Report 2026-08-02.md"),
]
def test_ingests_since_last_lint_resets_on_a_lint_entry():
entries = [
("2026-08-01", "ingest", "a.md"),
("2026-08-02", "ingest", "b.md"),
("2026-08-03", "lint", "Lint Report.md"),
("2026-08-04", "ingest", "c.md"),
]
assert ingests_since_last_lint(entries) == 1
def test_ingests_since_last_lint_counts_from_the_start_when_never_linted():
entries = [
("2026-08-01", "ingest", "a.md"),
("2026-08-02", "query", "What is X?"),
("2026-08-03", "ingest", "b.md"),
]
assert ingests_since_last_lint(entries) == 2
def test_log_status_reports_zero_with_no_log_file(tmp_path, monkeypatch, capsys):
import chemenu.config as config
set_config_path(monkeypatch, "LOG_FILE", tmp_path / "log.md")
log_status()
assert "No kb/log.md yet" in capsys.readouterr().out
def test_log_status_warns_at_the_ten_ingest_threshold(tmp_path, monkeypatch, capsys):
import chemenu.config as config
log_file = tmp_path / "log.md"
body = "".join(
format_log_entry("ingest", f"raw/notes/{i}.md", today="2026-08-01") for i in range(10)
)
log_file.write_text(body, encoding="utf-8")
set_config_path(monkeypatch, "LOG_FILE", log_file)
log_status()
out = capsys.readouterr().out
assert "Ingests since last lint: 10" in out
assert "Threshold reached" in out
# --- log_append --body-file: unreadable is an ERROR line, never a traceback (Gitea #145) ---
def _make_missing(tmp_path):
return tmp_path / "nonexistent.md"
def _make_directory(tmp_path):
d = tmp_path / "a-directory"
d.mkdir()
return d
def _make_non_utf8(tmp_path):
p = tmp_path / "not-utf8.md"
p.write_bytes(b"\xff\xfe garbage")
return p
@pytest.mark.parametrize(
"make_bad_file",
[_make_missing, _make_directory, _make_non_utf8],
ids=["missing", "directory", "non-utf8"],
)
def test_log_append_reports_an_unreadable_body_file_as_an_error(make_bad_file, tmp_path, monkeypatch, capsys):
import chemenu.config as config
log_file = tmp_path / "log.md"
before = "# Wiki Log\n"
log_file.write_text(before, encoding="utf-8")
set_config_path(monkeypatch, "LOG_FILE", log_file)
bad_file = make_bad_file(tmp_path)
with pytest.raises(typer.Exit) as exc:
log_append(op="ingest", title="raw/notes/a.md", body="", body_file=bad_file)
assert exc.value.exit_code == 1
out = capsys.readouterr().out
assert out.startswith("ERROR ")
assert str(bad_file) in out
assert log_file.read_text(encoding="utf-8") == before
def test_log_append_reads_the_body_from_body_file(tmp_path, monkeypatch):
import chemenu.config as config
log_file = tmp_path / "log.md"
log_file.write_text("# Wiki Log\n", encoding="utf-8")
set_config_path(monkeypatch, "LOG_FILE", log_file)
body_file = tmp_path / "body.md"
body_file.write_text("Created [[Docker]].", encoding="utf-8")
log_append(op="ingest", title="raw/notes/a.md", body="", body_file=body_file)
assert "Created [[Docker]]." in log_file.read_text(encoding="utf-8")