Something went wrong. Try again.
fork of Kludex/zigai, ported to zig 0.17
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212#!/bin/shset -eu
project_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)coverage_dir="$project_dir/coverage"kcov_image="${ZIGAI_KCOV_IMAGE:-kcov/kcov@sha256:481289ae32e55e5b733019515acd10948a4f76dfed381765577db909664fc603}"kcov_bin="${ZIGAI_KCOV_BIN:-kcov}"coverage_target="${ZIGAI_COVERAGE_TARGET:-}"# `kcov-ignore` marks LLVM source-map artifacts whose behavior is asserted by# tests but whose exact line is not attributed when tracing static Zig binaries.coverage_exclusions="--exclude-line=kcov-ignore,pub const Agent = struct {,pub const InstructionContext = struct {,return timeoutMilliseconds,unreachable,id[length - 1] = 1,return id; // all-zero random fallback"use_docker=0if [ "${ZIGAI_FORCE_DOCKER_KCOV:-0}" = 1 ] || ! command -v kcov >/dev/null 2>&1; then use_docker=1fi
cd "$project_dir"case "$coverage_dir" in "$project_dir/coverage") ;; *) echo "refusing to clean unexpected coverage path: $coverage_dir" >&2; exit 2 ;;esacrm -rf -- "$coverage_dir"mkdir -p coverage/bincoverage_cache="$coverage_dir/zig-cache"coverage_global_cache="$coverage_dir/zig-global-cache"ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build --fetch=allyaml_root=$(find "$project_dir/zig-pkg" -path '*/yaml-*/src/lib.zig' -print -quit)if [ -z "$yaml_root" ]; then echo "could not locate the YAML test dependency" >&2 exit 2fi
compile_test() { output=$1 shift if [ -n "$coverage_target" ]; then ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig test --cache-dir "$coverage_cache" -target "$coverage_target" -fllvm --test-no-exec -femit-bin="$output" "$@" else ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig test --cache-dir "$coverage_cache" -fllvm --test-no-exec -femit-bin="$output" "$@" fi}
compile_test coverage/bin/unit-tests --dep yaml -Mroot=src/root.zig -Myaml="$yaml_root"compile_test coverage/bin/agent-tests --dep zigai -Mroot=tests/agent_loop.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root"compile_test coverage/bin/pydantic-ai-codec-tests --dep zigai -Mroot=tests/pydantic_ai_codec.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root"compile_test coverage/bin/provider-tests --dep zigai --dep yaml -Mroot=tests/provider_cassettes.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root"compile_test coverage/bin/cli-common-tests --dep zigai -Mroot=src/cli/common.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root"compile_test coverage/bin/spec-cli-tests --dep zigai -Mroot=src/cli/spec_common.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root"compile_test coverage/bin/mcp-conformance-tests --dep zigai --dep yaml -Mroot=tests/mcp_conformance.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root"if [ -n "$coverage_target" ]; then ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -target "$coverage_target" -fllvm -femit-bin=coverage/bin/openai-cli --dep zigai -Mroot=src/cli/openai.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root" ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -target "$coverage_target" -fllvm -femit-bin=coverage/bin/anthropic-cli --dep zigai -Mroot=src/cli/anthropic.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root" ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -target "$coverage_target" -fllvm -femit-bin=coverage/bin/google-cli --dep zigai -Mroot=src/cli/google.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root" ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -target "$coverage_target" -fllvm -femit-bin=coverage/bin/spec-cli --dep zigai -Mroot=src/cli/spec.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root" ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -target "$coverage_target" -fllvm -femit-bin=coverage/bin/http-smoke --dep zigai -Mroot=tests/http_transport_smoke.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root"else ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -fllvm -femit-bin=coverage/bin/openai-cli --dep zigai -Mroot=src/cli/openai.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root" ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -fllvm -femit-bin=coverage/bin/anthropic-cli --dep zigai -Mroot=src/cli/anthropic.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root" ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -fllvm -femit-bin=coverage/bin/google-cli --dep zigai -Mroot=src/cli/google.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root" ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -fllvm -femit-bin=coverage/bin/spec-cli --dep zigai -Mroot=src/cli/spec.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root" ZIG_GLOBAL_CACHE_DIR="$coverage_global_cache" zig build-exe --cache-dir "$coverage_cache" -fllvm -femit-bin=coverage/bin/http-smoke --dep zigai -Mroot=tests/http_transport_smoke.zig --dep yaml -Mzigai=src/root.zig -Myaml="$yaml_root"fi
run_kcov() { if [ "$use_docker" = 0 ]; then kcov "$coverage_exclusions" "$@" elif command -v docker >/dev/null 2>&1; then docker run --rm \ --network host \ --cap-add SYS_PTRACE \ --security-opt seccomp=unconfined \ --add-host host.docker.internal:host-gateway \ -e OPENAI_API_KEY -e OPENAI_BASE_URL \ -e ANTHROPIC_API_KEY -e ANTHROPIC_BASE_URL \ -e GEMINI_API_KEY -e GEMINI_BASE_URL \ -e ZIGAI_SPEC_MODEL -e ZIGAI_SPEC_API_KEY \ -e ZIGAI_FIXTURE_BASE_URL \ -v "$project_dir:$project_dir" -w "$project_dir" \ "$kcov_image" "$kcov_bin" "$coverage_exclusions" "$@" else echo "coverage requires kcov or Docker" >&2 exit 2 fi}
include_path="--include-path=$project_dir/src"run_kcov --clean "$include_path" coverage/unit coverage/bin/unit-testsrun_kcov --clean "$include_path" coverage/agent coverage/bin/agent-testsrun_kcov --clean "$include_path" coverage/pydantic-ai-codec coverage/bin/pydantic-ai-codec-testsrun_kcov --clean "$include_path" coverage/providers coverage/bin/provider-testsrun_kcov --clean "$include_path" coverage/cli-common coverage/bin/cli-common-testsrun_kcov --clean "$include_path" coverage/spec-cli-tests coverage/bin/spec-cli-testsrun_kcov --clean "$include_path" coverage/mcp-conformance coverage/bin/mcp-conformance-tests
fixture_bind=127.0.0.1fixture_host=127.0.0.1if [ "$use_docker" = 1 ]; then fixture_bind=0.0.0.0 fixture_host=host.docker.internalfiZIGAI_FIXTURE_HOST="$fixture_bind" python3 tests/fake_provider_server.py &server_pid=$!trap 'kill "$server_pid" 2>/dev/null || true' EXIT INT TERMsleep 1
OPENAI_API_KEY=test OPENAI_BASE_URL="http://$fixture_host:18765/openai" \ run_kcov --clean "$include_path" coverage/openai-cli coverage/bin/openai-cli testANTHROPIC_API_KEY=test ANTHROPIC_BASE_URL="http://$fixture_host:18765/anthropic" \ run_kcov --clean "$include_path" coverage/anthropic-cli coverage/bin/anthropic-cli testGEMINI_API_KEY=test GEMINI_BASE_URL="http://$fixture_host:18765/google" \ run_kcov --clean "$include_path" coverage/google-cli coverage/bin/google-cli testOPENAI_API_KEY=test OPENAI_BASE_URL="http://$fixture_host:18765/openai" \ run_kcov "$include_path" coverage/openai-cli coverage/bin/openai-cli --stream testANTHROPIC_API_KEY=test ANTHROPIC_BASE_URL="http://$fixture_host:18765/anthropic" \ run_kcov "$include_path" coverage/anthropic-cli coverage/bin/anthropic-cli --stream testGEMINI_API_KEY=test GEMINI_BASE_URL="http://$fixture_host:18765/google" \ run_kcov "$include_path" coverage/google-cli coverage/bin/google-cli --stream testOPENAI_API_KEY=test OPENAI_BASE_URL="http://$fixture_host:18765/openai" \ run_kcov "$include_path" coverage/openai-cli coverage/bin/openai-cli --tools tests/cli_tools.json testANTHROPIC_API_KEY=test ANTHROPIC_BASE_URL="http://$fixture_host:18765/anthropic" \ run_kcov "$include_path" coverage/anthropic-cli coverage/bin/anthropic-cli --tools tests/cli_tools.json testGEMINI_API_KEY=test GEMINI_BASE_URL="http://$fixture_host:18765/google" \ run_kcov "$include_path" coverage/google-cli coverage/bin/google-cli --stream --tools tests/cli_tools.json testZIGAI_FIXTURE_BASE_URL="http://$fixture_host:18765" \ run_kcov --clean "$include_path" coverage/http-smoke coverage/bin/http-smokeZIGAI_SPEC_MODEL=gpt-5-mini ZIGAI_SPEC_API_KEY=test \ run_kcov --clean "$include_path" coverage/spec-cli coverage/bin/spec-cli validate tests/agent_spec.yaml \ --interpolate --allow-env ZIGAI_SPEC_MODEL --allow-env ZIGAI_SPEC_API_KEY --capability search
# Accumulate CLI error paths in the same report.OPENAI_API_KEY=test run_kcov "$include_path" coverage/openai-cli coverage/bin/openai-cli >/dev/null 2>&1 || trueif OPENAI_API_KEY=test coverage/bin/openai-cli >/dev/null 2>&1; then echo "OpenAI CLI unexpectedly accepted a missing prompt" >&2 exit 1fiOPENAI_API_KEY= run_kcov "$include_path" coverage/openai-cli coverage/bin/openai-cli test >/dev/null 2>&1 || trueif OPENAI_API_KEY= coverage/bin/openai-cli test >/dev/null 2>&1; then echo "OpenAI CLI unexpectedly accepted an empty API key" >&2 exit 1fi(unset OPENAI_API_KEY; run_kcov "$include_path" coverage/openai-cli coverage/bin/openai-cli test >/dev/null 2>&1) || trueif (unset OPENAI_API_KEY; coverage/bin/openai-cli test >/dev/null 2>&1); then echo "OpenAI CLI unexpectedly accepted an unset API key" >&2 exit 1firun_kcov "$include_path" coverage/spec-cli coverage/bin/spec-cli >/dev/null 2>&1 || trueif coverage/bin/spec-cli >/dev/null 2>&1; then echo "agent spec CLI unexpectedly accepted missing arguments" >&2 exit 1firun_kcov "$include_path" coverage/spec-cli coverage/bin/spec-cli validate README.md >/dev/null 2>&1 || trueif coverage/bin/spec-cli validate README.md >/dev/null 2>&1; then echo "agent spec CLI unexpectedly accepted an unsupported extension" >&2 exit 1fi
run_kcov --merge coverage/merged \ coverage/unit coverage/agent coverage/pydantic-ai-codec coverage/providers coverage/cli-common coverage/spec-cli-tests coverage/mcp-conformance \ coverage/openai-cli coverage/anthropic-cli coverage/google-cli coverage/spec-cli coverage/http-smoke
report="$coverage_dir/merged/kcov-merged/coverage.json"for source in "$project_dir"/src/*.zig "$project_dir"/src/cli/*.zig "$project_dir"/src/codecs/pydantic_ai.zig "$project_dir"/src/models/*.zig "$project_dir"/src/providers/*.zig; do case "$source" in "$project_dir"/src/zanthropic.zig|\ "$project_dir"/src/zgoogle.zig|\ "$project_dir"/src/zopenai.zig|\ "$project_dir"/src/zopenai_compatible.zig|\ "$project_dir"/src/providers/cerebras.zig|\ "$project_dir"/src/providers/crusoe.zig|\ "$project_dir"/src/providers/cohere.zig|\ "$project_dir"/src/providers/deepseek.zig|\ "$project_dir"/src/providers/doubleword.zig|\ "$project_dir"/src/providers/groq.zig|\ "$project_dir"/src/providers/huggingface.zig|\ "$project_dir"/src/providers/mistral.zig|\ "$project_dir"/src/providers/openrouter.zig|\ "$project_dir"/src/providers/ovhcloud.zig|\ "$project_dir"/src/providers/pydantic_gateway.zig|\ "$project_dir"/src/providers/together.zig|\ "$project_dir"/src/providers/zai.zig) # These modules contain aliases only. Downstream consumer builds # compile them, but they have no executable lines for kcov. continue ;; esac if ! grep -Fq "\"file\": \"$source\"" "$report"; then echo "coverage report is missing $source" >&2 exit 1 fidone
percent=$(awk -F'"' '/"percent_covered"/ { value=$4 } END { print value }' "$report")covered=$(awk '/"covered_lines"/ { value=$2 } END { gsub(/,/, "", value); print value }' "$report")total=$(awk '/"total_lines"/ { value=$2 } END { gsub(/,/, "", value); print value }' "$report")echo "Coverage: $covered/$total lines ($percent%)"if [ "$percent" != "100.00" ]; then awk ' /^ ".*\.zig": \{$/ { file = $0 sub(/^ "/, "", file) sub(/": \{$/, "", file) next } /^ "[0-9]+": 0,?$/ && file != "" { line = $1 gsub(/[":]/, "", line) print "uncovered: src/" file ":" line } ' "$coverage_dir/merged/kcov-merged/codecov.json"fitest "$percent" = "100.00"