Thicket/tests/test_vault_writer.py

59 lines
2.2 KiB
Python

"""ObsidianVaultWriter — frontmatter, escaping, collision handling."""
from __future__ import annotations
import re
from thicket.vault_writer import ObsidianVaultWriter
def test_note_written_with_frontmatter_and_header(vault, sample_docs):
writer = ObsidianVaultWriter(vault)
source = sample_docs / "reading-list.txt"
note = writer.write("Reading List", "body text", source)
assert note.parent == vault / "Ingested_Brain"
text = note.read_text(encoding="utf-8")
assert text.startswith("---\n")
assert re.search(r'^title: "Reading List"$', text, re.M)
assert re.search(r'^source_file: "reading-list\.txt"$', text, re.M)
assert "tags:" in text and "brain/ingested" in text and "source/txt" in text
assert "# Reading List" in text
assert "*Source document: `reading-list.txt`*" in text
assert text.endswith("body text")
def test_title_with_quotes_survives_frontmatter(vault, sample_docs):
writer = ObsidianVaultWriter(vault)
source = sample_docs / "reading-list.txt"
note = writer.write('The "Real" Deal', "x", source)
text = note.read_text(encoding="utf-8")
fm = text.split("---")[1]
assert 'title: "The \\"Real\\" Deal"' in fm
def test_reingest_same_source_overwrites(vault, sample_docs):
writer = ObsidianVaultWriter(vault)
source = sample_docs / "reading-list.txt"
first = writer.write("Same Title", "v1", source)
second = writer.write("Same Title", "v2", source)
assert first == second
assert "v2" in first.read_text(encoding="utf-8")
def test_same_title_different_source_gets_suffix(vault, sample_docs):
writer = ObsidianVaultWriter(vault)
a = writer.write("Same Title", "from A", sample_docs / "reading-list.txt")
b = writer.write("Same Title", "from B", sample_docs / "deep-learning-notes.md")
assert a != b
assert a.exists() and b.exists()
# suffix is a 6-hex digest of the source name
assert re.search(r"-[0-9a-f]{6}\.md$", b.name)
def test_symbol_only_title_falls_back_to_untitled(vault, sample_docs):
writer = ObsidianVaultWriter(vault)
source = sample_docs / "reading-list.txt"
note = writer.write("###", "x", source)
assert note.stem.startswith("untitled")