# tls-xev integration workflows (#76).
#
# Enter the root devshell with `nix develop ../..`, or let
# direnv do it via .envrc. The root workspace delegates here rather than
# absorbing these details.

[private]
default:
    @just --list

[doc("Remove local build artifacts")]
[group("check")]
clean:
    rm -rf zig-out .zig-cache

[doc("Build the smoke executable")]
[group("check")]
build:
    zig build

[doc("Compile every example (no peer required)")]
[group("check")]
build-examples:
    zig build build-examples

[doc("Run unit, round-trip, and example tests")]
[group("check")]
test:
    zig build test --summary all

[doc("Format Zig and Just sources")]
[group("check")]
fmt: fmt-zig fmt-just

[doc("Lint Zig and Just sources")]
[group("check")]
lint: lint-zig lint-just

[doc("Format Zig code")]
[group("check")]
fmt-zig:
    zig fmt build.zig src/ examples/

[doc("Lint Zig code (fmt, ziglint, and the workspace std-alias rules)")]
[group("check")]
lint-zig:
    zig fmt --check build.zig src/ examples/
    ziglint build.zig src/ examples/
    rc=0; for rule in ../../rules/std-alias-*.yml; do ast-grep scan --rule "$rule" src examples --globs '*.zig' --report-style short || rc=1; done; exit $rc

[doc("Format Justfile")]
[group("check")]
fmt-just:
    just --fmt --unstable --justfile justfile

[doc("Lint Justfile")]
[group("check")]
lint-just:
    just --check --fmt --unstable --justfile justfile

[doc("Rerun a suite N times to shake out order- and timing-dependent flakes")]
[group("check")]
flake-check N="20":
    #!/usr/bin/env bash
    set -euo pipefail
    # Compilation caches but test runs do not, so each iteration really
    # re-executes. Cheap enough to be routine: these suites are milliseconds.
    for i in $(seq 1 {{ N }}); do
        if ! zig build test > /dev/null 2>&1; then
            echo "flaked on run $i of {{ N }}" >&2
            zig build test 2>&1 | tail -20 >&2
            exit 1
        fi
    done
    echo "{{ N }} consecutive green runs"

[doc("Run all CI gates for this integration")]
[group("check")]
ci: lint test build-examples
