generated from coulomb/repo-seed
120 lines
3.6 KiB
Python
120 lines
3.6 KiB
Python
from pathlib import Path
|
|
|
|
from click.testing import CliRunner
|
|
|
|
from markitect_tool.backend import (
|
|
BackendManifest,
|
|
DependencyEdge,
|
|
DocumentSnapshot,
|
|
ProvenanceEnvelope,
|
|
capability_check,
|
|
load_backend_manifest,
|
|
load_backend_registry,
|
|
snapshot_identity_for_file,
|
|
)
|
|
from markitect_tool.cli import main
|
|
|
|
|
|
def test_load_markdown_backend_manifest():
|
|
manifest = load_backend_manifest("examples/backends/local-sqlite-backend.md")
|
|
|
|
assert manifest.id == "local-sqlite-cache"
|
|
assert "snapshots" in manifest.capabilities
|
|
assert manifest.storage["engine"] == "sqlite"
|
|
|
|
|
|
def test_load_yaml_backend_registry_and_filter_by_capability():
|
|
registry = load_backend_registry(["examples/backends"])
|
|
|
|
assert [manifest.id for manifest in registry.list()] == [
|
|
"local-context-packages",
|
|
"local-sqlite-cache",
|
|
]
|
|
assert [manifest.id for manifest in registry.find_by_capability("context_packages")] == [
|
|
"local-context-packages"
|
|
]
|
|
|
|
|
|
def test_backend_capability_check_reports_missing_capabilities():
|
|
manifest = BackendManifest(id="memory", capabilities=["snapshots", "provenance"])
|
|
|
|
check = capability_check(manifest, ["snapshots", "fts", "provenance"])
|
|
|
|
assert not check.compatible
|
|
assert check.missing == ["fts"]
|
|
|
|
|
|
def test_snapshot_identity_includes_content_and_parse_options(tmp_path: Path):
|
|
source = tmp_path / "doc.md"
|
|
source.write_text("# Doc\n", encoding="utf-8")
|
|
|
|
first = snapshot_identity_for_file(source, parse_options={"tables": True})
|
|
second = snapshot_identity_for_file(source, parse_options={"tables": False})
|
|
|
|
assert first.content_hash == second.content_hash
|
|
assert first.parse_options_hash != second.parse_options_hash
|
|
assert first.snapshot_id != second.snapshot_id
|
|
|
|
|
|
def test_document_snapshot_and_provenance_to_dict(tmp_path: Path):
|
|
source = tmp_path / "doc.md"
|
|
source.write_text("# Doc\n", encoding="utf-8")
|
|
identity = snapshot_identity_for_file(source)
|
|
edge = DependencyEdge(source_id="section:doc", target="std:terms.md", kind="reference")
|
|
provenance = ProvenanceEnvelope(
|
|
operation="snapshot.put",
|
|
snapshot_id=identity.snapshot_id,
|
|
source_path=str(source),
|
|
content_hash=identity.content_hash,
|
|
dependencies=[edge],
|
|
backend_id="local",
|
|
)
|
|
snapshot = DocumentSnapshot(
|
|
identity=identity,
|
|
document={"headings": [{"text": "Doc"}]},
|
|
dependencies=[edge],
|
|
provenance=provenance,
|
|
)
|
|
|
|
data = snapshot.to_dict()
|
|
|
|
assert data["snapshot_id"] == identity.snapshot_id
|
|
assert data["dependencies"][0]["kind"] == "reference"
|
|
assert data["provenance"]["backend_id"] == "local"
|
|
|
|
|
|
def test_mkt_backend_list_and_inspect():
|
|
runner = CliRunner()
|
|
|
|
listed = runner.invoke(main, ["backend", "list", "--path", "examples/backends"])
|
|
inspected = runner.invoke(
|
|
main,
|
|
[
|
|
"backend",
|
|
"inspect",
|
|
"local-sqlite-cache",
|
|
"--path",
|
|
"examples/backends",
|
|
"--require",
|
|
"snapshots",
|
|
"--require",
|
|
"provenance",
|
|
],
|
|
)
|
|
|
|
assert listed.exit_code == 0
|
|
assert "local-sqlite-cache" in listed.output
|
|
assert inspected.exit_code == 0
|
|
assert "compatible" in inspected.output
|
|
|
|
|
|
def test_mkt_backend_snapshot_id(tmp_path: Path):
|
|
source = tmp_path / "doc.md"
|
|
source.write_text("# Doc\n", encoding="utf-8")
|
|
|
|
result = CliRunner().invoke(main, ["backend", "snapshot-id", str(source)])
|
|
|
|
assert result.exit_code == 0
|
|
assert result.output.startswith("snapshot:")
|
|
assert "content_hash: sha256:" in result.output
|