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
package afdata

import (
	"reflect"
	"testing"
)

// CLI primitives pinned by the shared spec/fixtures/cli_primitives.json.

func fixtureStrings(t *testing.T, value any) []string {
	t.Helper()
	items, _ := value.([]any)
	out := make([]string, 0, len(items))
	for _, item := range items {
		out = append(out, item.(string))
	}
	return out
}

func fixtureList(t *testing.T, key string) []map[string]any {
	t.Helper()
	items, _ := loadFixtureObject("cli_primitives.json")[key].([]any)
	out := make([]map[string]any, 0, len(items))
	for _, item := range items {
		out = append(out, item.(map[string]any))
	}
	return out
}

func TestCliPrimitivesFixtureLogFilters(t *testing.T) {
	for _, tc := range fixtureList(t, "log_filters") {
		name := tc["name"].(string)
		entries := fixtureStrings(t, tc["entries"])
		want := fixtureStrings(t, tc["normalized"])
		filters := CliParseLogFilters(entries)
		if got := filters.Values(); !reflect.DeepEqual(append([]string{}, got...), append([]string{}, want...)) {
			t.Errorf("[%s] normalized = %q, want %q", name, got, want)
		}
		if got := NewLogFilters(entries).Values(); !reflect.DeepEqual(append([]string{}, got...), append([]string{}, want...)) {
			t.Errorf("[%s] NewLogFilters = %q, want %q", name, got, want)
		}
		for event, expected := range tc["enabled"].(map[string]any) {
			if got := filters.Enabled(event); got != expected.(bool) {
				t.Errorf("[%s] Enabled(%q) = %v, want %v", name, event, got, expected)
			}
		}
	}
}

func TestCliPrimitivesFixtureParseOutput(t *testing.T) {
	for _, tc := range fixtureList(t, "parse_output") {
		name := tc["name"].(string)
		got, err := CliParseOutput(tc["input"].(string))
		if want, isError := tc["error"].(string); isError {
			if err == nil || err.Error() != want {
				t.Errorf("[%s] error = %v, want %q", name, err, want)
			}
			continue
		}
		if err != nil || string(got) != tc["format"].(string) {
			t.Errorf("[%s] = %q, %v, want %q", name, got, err, tc["format"])
		}
	}
}

func TestCliPrimitivesFixtureParseOutputTo(t *testing.T) {
	for _, tc := range fixtureList(t, "parse_output_to") {
		name := tc["name"].(string)
		got, err := ParseOutputTo(tc["input"].(string))
		if want, isError := tc["error"].(string); isError {
			if err == nil || err.Error() != want {
				t.Errorf("[%s] error = %v, want %q", name, err, want)
			}
			continue
		}
		if err != nil || string(got) != tc["output_to"].(string) {
			t.Errorf("[%s] = %q, %v, want %q", name, got, err, tc["output_to"])
		}
	}
}

func optionalString(value any) string {
	text, _ := value.(string)
	return text
}

func TestCliPrimitivesFixtureBuilders(t *testing.T) {
	for _, tc := range fixtureList(t, "build_cli_error") {
		name := tc["name"].(string)
		event := BuildCliError(tc["message"].(string), optionalString(tc["hint"]))
		if !reflect.DeepEqual(event.Value(), tc["expected"]) {
			t.Errorf("[build_cli_error/%s] = %#v, want %#v", name, event.Value(), tc["expected"])
		}
	}
	for _, tc := range fixtureList(t, "build_cli_version") {
		name := tc["name"].(string)
		event := BuildCliVersion(
			tc["cli_name"].(string),
			optionalString(tc["display_name"]),
			tc["version"].(string),
			optionalString(tc["build"]),
		)
		if !reflect.DeepEqual(event.Value(), tc["expected"]) {
			t.Errorf("[build_cli_version/%s] = %#v, want %#v", name, event.Value(), tc["expected"])
		}
	}
}