agent-first-data 0.41.0

Self-describing data and structured CLI contracts for AI agents — field-name suffixes for units and secrets, the afdata CLI for rendering, validation, path guards, and reading or editing structured documents (JSON, TOML, YAML, dotenv, INI, Markdown), plus libraries for Rust, Go, Python, TypeScript, and Bash.
Documentation
"""CLI primitives pinned by the shared spec/fixtures/cli_primitives.json."""
import json
import os
from io import StringIO

import pytest

from agent_first_data import (
    CliEmitter,
    Event,
    LogFilters,
    OutputFormat,
    OutputTo,
    build_cli_error,
    build_cli_version,
    cli_parse_log_filters,
    cli_parse_output,
    json_result,
    render,
)


def _fixture():
    path = os.path.join(
        os.path.dirname(__file__), "..", "..", "spec", "fixtures", "cli_primitives.json"
    )
    with open(path, encoding="utf-8") as f:
        return json.load(f)


def test_log_filters():
    for case in _fixture()["log_filters"]:
        filters = cli_parse_log_filters(case["entries"])
        assert list(filters) == case["normalized"], f"[{case['name']}]"
        assert filters == LogFilters(case["entries"]), f"[{case['name']}] constructor"
        for event, expected in case["enabled"].items():
            assert filters.enabled(event) is expected, f"[{case['name']}] enabled({event!r})"


def test_log_filters_are_immutable():
    assert not hasattr(LogFilters(["a"]), "append")


def test_parse_output():
    for case in _fixture()["parse_output"]:
        if "error" in case:
            with pytest.raises(ValueError) as info:
                cli_parse_output(case["input"])
            assert str(info.value) == case["error"], f"[{case['name']}]"
        else:
            assert cli_parse_output(case["input"]).value == case["format"], f"[{case['name']}]"


def test_parse_output_to():
    for case in _fixture()["parse_output_to"]:
        if "error" in case:
            with pytest.raises(ValueError) as info:
                OutputTo.parse(case["input"])
            assert str(info.value) == case["error"], f"[{case['name']}]"
        else:
            assert OutputTo.parse(case["input"]).value == case["output_to"], f"[{case['name']}]"


def test_builders_return_events():
    fixture = _fixture()
    for case in fixture["build_cli_error"]:
        event = build_cli_error(case["message"], case["hint"])
        assert isinstance(event, Event), f"[{case['name']}]"
        assert event.to_dict() == case["expected"], f"[{case['name']}]"
    for case in fixture["build_cli_version"]:
        event = build_cli_version(
            case["cli_name"], case["display_name"], case["version"], case["build"]
        )
        assert isinstance(event, Event), f"[{case['name']}]"
        assert event.to_dict() == case["expected"], f"[{case['name']}]"


def test_render_unwraps_event():
    event = json_result({"ok": True}).build()
    assert render(event, OutputFormat.JSON) == render(event.to_dict(), OutputFormat.JSON)


def test_emit_takes_events_only():
    out = StringIO()
    emitter = CliEmitter.stream(out, OutputFormat.JSON)
    with pytest.raises(AttributeError):
        emitter.emit({"kind": "result", "result": {}, "trace": {}})
    assert out.getvalue() == ""