#!/usr/bin/env bash
# Shared companions: scripts/ci/run-rust-formatting.pl scripts/ci/fixture-lifecycle.sh scripts/ci/test-rust-formatting-ownership.sh
set -euo pipefail
ROOT="$(CDPATH='' cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)"
# shellcheck source=scripts/ci/fixture-lifecycle.sh
source "$ROOT/scripts/ci/fixture-lifecycle.sh"
fixture="$(fixture_create "${TMPDIR:-/tmp}/rust-formatting.XXXXXX")"
fixture_complete=false
trap 'fixture_finish "$?" "$fixture" "Rust formatting fixture retained"' EXIT
consumer="$fixture/consumer with spaces"
mkdir -p "$consumer/src" "$consumer/child workspace/src" "$fixture/logs" "$fixture/bin"
for workspace in "$consumer" "$consumer/child workspace"; do
    printf '[package]\nname = "fixture"\nversion = "0.0.0"\n' > "$workspace/Cargo.toml"
    printf 'fn example() {}\n' > "$workspace/src/lib.rs"
done
cat > "$fixture/bin/cargo" <<'CARGO'
#!/usr/bin/env bash
set -euo pipefail
case "$*" in
    metadata*)
        [[ "${FORMAT_METADATA_FAIL:-0}" == 0 ]] || exit 19
        perl -MJSON::PP -MEncode=decode -MCwd=getcwd -e '$p=decode("UTF-8",getcwd()); print encode_json({workspace_root=>$p,workspace_members=>["fixture"],packages=>[{id=>"fixture",name=>"fixture",manifest_path=>"$p/Cargo.toml", targets=>[{src_path=>"$p/src/lib.rs"}]}],target_directory=>"$p/target"})'
        exit ;;
    --version|'sort --version'|'fmt --version'|'sort-derives --version') printf 'fixture %s\n' "${FORMAT_VERSION:-1}"; exit ;;
esac
printf '%s\t%s\n' "$PWD" "$*" >> "$FORMAT_EVENTS"
echo 'private formatter detail'
if [[ "$*" == "${FORMAT_FAIL:-}" ]]; then exit 23; fi
if [[ "${FORMAT_INTERRUPT:-0}" == 1 ]]; then kill -TERM "$PPID"; exit 0; fi
if [[ "${FORMAT_REWRITE:-0}" == 1 && "$*" == 'fmt --package fixture' ]]; then
    printf 'fn rewritten() {}\n' > src/lib.rs
fi
CARGO
chmod +x "$fixture/bin/cargo"
export PATH="$fixture/bin:$PATH" FORMAT_EVENTS="$fixture/events" RUNNER_TEMP="$fixture/logs"
driver="$ROOT/scripts/ci/run-rust-formatting.pl"
cd "$consumer"
run() { perl "$driver" "$@" > "$fixture/output" 2>&1; }
receipt="$consumer/target/toko-tooling/formatting.receipt"

# A successful complete selection can serve both check and write. A cache hit
# invokes no formatter, but still observes actual tools/config/source inputs.
: > "$FORMAT_EVENTS"
run --check --sort-derives --extra-workspace 'child workspace' --extra-manifest-order package,workspace
[[ "$(wc -l < "$FORMAT_EVENTS")" -eq 5 ]]
grep -E '^Manifest sorting \[\.\].*ok \([0-9]+\.[0-9]+s\)$' "$fixture/output"
grep -E '^Derive sorting \[\.\].*ok \([0-9]+\.[0-9]+s\)$' "$fixture/output"
grep -E '^Rust formatting \[child workspace\].*ok \([0-9]+\.[0-9]+s\)$' "$fixture/output"
grep -F -- 'sort --workspace --grouped --check --order package,workspace' "$FORMAT_EVENTS"
[[ -f "$receipt" ]]
: > "$FORMAT_EVENTS"
run --write --sort-derives --extra-workspace 'child workspace' --extra-manifest-order package,workspace
[[ ! -s "$FORMAT_EVENTS" ]]
grep -E '^Checking formatting inputs\.\.\. unchanged \([0-9]+\.[0-9]+s\)$' "$fixture/output"

# Real invalidation boundaries, including untracked source, child input, tool
# identity, configuration, workspace/policy selection and the supplying driver.
for change in source same-time child manifest config tool selection; do
    case "$change" in
        source) printf 'fn added() {}\n' > src/untracked.rs ;;
        same-time)
            cp -p src/lib.rs "$fixture/source-timestamp"
            printf 'fn changed() {}\n' > src/lib.rs
            touch -r "$fixture/source-timestamp" src/lib.rs ;;
        child) printf 'fn child() {}\n' > 'child workspace/src/lib.rs' ;;
        manifest) printf '\n# changed\n' >> Cargo.toml ;;
        config) printf 'max_width = 90\n' > rustfmt.toml ;;
        tool) printf '\n# changed executable\n' >> "$fixture/bin/cargo" ;;
        selection) : ;;
    esac
    : > "$FORMAT_EVENTS"
    arguments=(--check --sort-derives --extra-workspace 'child workspace' --extra-manifest-order 'package,workspace')
    [[ "$change" != selection ]] || arguments+=(--manifest-order 'package,workspace')
    run "${arguments[@]}"
    [[ "$(wc -l < "$FORMAT_EVENTS")" -eq 5 ]]
done

# An unsupported/incomplete inventory falls back to actual passes, never a
# cached success. Corrupt or unreadable bookkeeping is just a miss.
printf 'corrupt\n' > "$receipt"
: > "$FORMAT_EVENTS"
run --check
[[ "$(wc -l < "$FORMAT_EVENTS")" -eq 2 ]]
: > "$FORMAT_EVENTS"
if FORMAT_METADATA_FAIL=1 run --check; then exit 1; fi
[[ ! -s "$FORMAT_EVENTS" ]]

# Ordinary cfg_attr metadata does not bind unrelated later words or literals.
# The first argument is a cfg predicate, even when its key is named path.
for source in \
    $'#[cfg_attr(feature = "host", derive(Clone))]\nstruct Value;\n/// Local path supplying the catalog snapshot.\nfn catalog() {}' \
    $'#[cfg_attr(\n any(feature = "host", test),\n cfg_attr(not(test), derive(Clone)),\n)]\nstruct Value;\n// path = "elsewhere"' \
    '#[cfg_attr(path = "host", derive(Clone))] struct Value;' \
    '#[cfg_attr(test, doc = r##"fake ] #[path = "elsewhere"]"##)] struct Value;' \
    $'// #[path = "elsewhere"]\n/* outer /* #[cfg_attr(test, path = "elsewhere")] */ comment */\n#[r#cfg_attr(test, derive(Clone))] struct Value;'; do
    printf '%s\n' "$source" > src/attributes.rs
    : > "$FORMAT_EVENTS"
    run --check
    [[ "$(wc -l < "$FORMAT_EVENTS")" == 2 ]]
    : > "$FORMAT_EVENTS"
    run --write
    [[ ! -s "$FORMAT_EVENTS" ]]
    grep -E '^Checking formatting inputs\.\.\. unchanged ' "$fixture/output" > /dev/null
done
rm src/attributes.rs

# Real module-path attributes, including nested/multiline/raw-identifier forms,
# and ambiguous syntax cannot authorize reuse. Formatting still executes.
for attribute in \
    '#[path = "../../external.rs"]' \
    '#[cfg_attr(test, path = "../../external.rs")]' \
    $'#[cfg_attr(\n all(feature = "host", not(test)),\n cfg_attr(any(test, feature = "other"), path = r##"../../external.rs"##),\n)]' \
    '#[ /* outer /* nested */ */ path /* comment */ = "../../external.rs"]' \
    '#[r#cfg_attr(test, r#path = "../../external.rs")]' \
    '#![cfg_attr(test, path = "../../external.rs")]' \
    '#[cfg_attr(test, derive(Clone))' \
    '#[cfg_attr(test, derive(Clone)]' \
    '#[cfg_attr(test)]' \
    '#[cfg_attr(test, doc = r##"unfinished'; do
    printf '%s\nmod external;\n' "$attribute" > src/redirect.rs
    : > "$FORMAT_EVENTS"
    run --check
    [[ "$(wc -l < "$FORMAT_EVENTS")" -eq 2 ]]
    grep -F 'running without shortcut' "$fixture/output" > /dev/null
done
attribute='derive(Clone)'
for ((depth = 0; depth < 70; depth++)); do attribute="cfg_attr(test, $attribute)"; done
printf '#[%s]\nstruct Deep;\n' "$attribute" > src/redirect.rs
: > "$FORMAT_EVENTS"
run --check
[[ "$(wc -l < "$FORMAT_EVENTS")" == 2 ]]
grep -F 'running without shortcut' "$fixture/output" > /dev/null
if grep -F 'Deep recursion' "$fixture/output"; then exit 1; fi
rm src/redirect.rs
: > "$FORMAT_EVENTS"
RUSTFMT=custom-rustfmt run --check
[[ "$(wc -l < "$FORMAT_EVENTS")" -eq 2 ]]
grep -F 'running without shortcut' "$fixture/output" > /dev/null

# Failure preserves the exact status/log and prevents the later pass or receipt.
printf 'fn failure_input() {}\n' > src/lib.rs
cp "$receipt" "$fixture/previous-receipt"
: > "$FORMAT_EVENTS"
status=0
FORMAT_FAIL='sort --workspace --grouped --check' run --check || status=$?
[[ "$status" == 23 && "$(wc -l < "$FORMAT_EVENTS")" -eq 1 ]]
cmp "$receipt" "$fixture/previous-receipt"
grep -E '^Manifest sorting .*FAILED \(exit 23, [0-9]+\.[0-9]+s\)$' "$fixture/output"
grep -F 'Details:' "$fixture/output"
grep -l 'private formatter detail' "$fixture/logs"/formatting.* > /dev/null

# A changed write is not a proof about the new inputs; check them once before
# allowing reuse. The write path never runs a duplicate check-before-write pass.
FORMAT_REWRITE=1 run --write
cmp "$receipt" "$fixture/previous-receipt"
: > "$FORMAT_EVENTS"
run --check
[[ "$(wc -l < "$FORMAT_EVENTS")" -eq 2 ]]
: > "$FORMAT_EVENTS"
run --write
[[ ! -s "$FORMAT_EVENTS" ]]

# Explicit qualification and isolated hook inputs do not reuse the developer
# receipt. Source/config changes after a hit must still invalidate it.
for bypass in explicit hook; do
    : > "$FORMAT_EVENTS"
    if [[ "$bypass" == explicit ]]; then run --check --no-cache
    else TOKO_TOOLING_FORMAT_WORKTREE="$consumer" run --check; fi
    [[ "$(wc -l < "$FORMAT_EVENTS")" -eq 2 ]]
done
printf 'fn interrupted() {}\n' > src/lib.rs
cp "$receipt" "$fixture/previous-receipt"
status=0
FORMAT_INTERRUPT=1 run --write || status=$?
[[ "$status" == 143 ]]
cmp "$receipt" "$fixture/previous-receipt"
: > "$FORMAT_EVENTS"
run --check
[[ "$(wc -l < "$FORMAT_EVENTS")" -eq 2 ]]

# Reject an ambiguous roster before any write, including aliases of the root.
: > "$FORMAT_EVENTS"
if run --write --extra-workspace .; then exit 1; fi
[[ ! -s "$FORMAT_EVENTS" ]]
if run --write --extra-workspace ..; then exit 1; fi
[[ ! -s "$FORMAT_EVENTS" ]]

# Untrusted workspace labels remain visible without terminal control effects.
control_workspace=$'control-\033-\342\200\256'
mkdir "$control_workspace"
cp Cargo.toml "$control_workspace/"
run --check --no-cache --extra-workspace "$control_workspace"
if LC_ALL=C grep -q $'\e' "$fixture/output"; then exit 1; fi
if grep -q $'\342\200\256' "$fixture/output"; then exit 1; fi
grep -F '\x{202e}' "$fixture/output" > /dev/null

# Standalone files have an explicit edition/tool. Their module/configuration
# closure is uncached, so no receipt can skip their actual formatter pass.
printf 'fn standalone() {}\n' > 'standalone source.rs'
cat > "$fixture/bin/rustfmt" <<'RUSTFMT'
#!/usr/bin/env bash
set -euo pipefail
if [[ "$*" == --version ]]; then
    [[ "${FORMAT_RUSTFMT_VERSION_FAIL:-0}" == 0 ]] || exit 29
    echo 'fixture Rustfmt'; exit
fi
{ printf 'rustfmt'; printf ' <%s>' "$@"; printf '\n'; } >> "$FORMAT_EVENTS"
[[ "${FORMAT_RUSTFMT_FAIL:-0}" == 0 ]] || exit 37
RUSTFMT
chmod +x "$fixture/bin/rustfmt"
standalone_args=(--rustfmt-source 'standalone source.rs' --rustfmt-edition 2024)
cp "$receipt" "$fixture/standalone-receipt"
for mode in --check --write --check; do
    : > "$FORMAT_EVENTS"
    run "$mode" "${standalone_args[@]}"
    [[ "$(wc -l < "$FORMAT_EVENTS")" == 3 ]]
    grep -F 'Standalone Rust sources selected; running without shortcut' "$fixture/output" > /dev/null
    grep -E '^Standalone Rust formatting.*ok \([0-9]+\.[0-9]+s\)$' "$fixture/output" > /dev/null
    if [[ "$mode" == --check ]]; then
        grep -F "rustfmt <--edition> <2024> <--check> <$consumer/standalone source.rs>" "$FORMAT_EVENTS" > /dev/null
    else
        grep -Fx "rustfmt <--edition> <2024> <$consumer/standalone source.rs>" "$FORMAT_EVENTS" > /dev/null
    fi
    cmp "$receipt" "$fixture/standalone-receipt"
done
: > "$FORMAT_EVENTS"
status=0
FORMAT_RUSTFMT_VERSION_FAIL=1 run --write "${standalone_args[@]}" || status=$?
[[ "$status" == 29 && ! -s "$FORMAT_EVENTS" ]]
status=0
FORMAT_RUSTFMT_FAIL=1 run --check "${standalone_args[@]}" || status=$?
[[ "$status" == 37 && "$(wc -l < "$FORMAT_EVENTS")" == 3 ]]
grep -E '^Standalone Rust formatting.*FAILED \(exit 37,' "$fixture/output" > /dev/null
cmp "$receipt" "$fixture/standalone-receipt"
printf 'fn outside() {}\n' > "$fixture/outside.rs"
for invalid in missing outside duplicate edition no-sources; do
    : > "$FORMAT_EVENTS"
    case "$invalid" in
        missing) arguments=(--rustfmt-source absent.rs --rustfmt-edition 2024) ;;
        outside) arguments=(--rustfmt-source "$fixture/outside.rs" --rustfmt-edition 2024) ;;
        duplicate) arguments=("${standalone_args[@]}" --rustfmt-source 'standalone source.rs') ;;
        edition) arguments=(--rustfmt-source 'standalone source.rs' --rustfmt-edition 2025) ;;
        no-sources) arguments=(--rustfmt-edition 2024) ;;
    esac
    if run --write "${arguments[@]}"; then exit 1; fi
    [[ ! -s "$FORMAT_EVENTS" ]]
done
: > "$FORMAT_EVENTS"
if run --write --rustfmt-source 'standalone source.rs'; then exit 1; fi
[[ ! -s "$FORMAT_EVENTS" ]]

# Shared lexer edits are formatting-input changes too, even if the driver and
# consumer files are unchanged. Keep this mutation inside a complete local copy.
mkdir "$fixture/driver"
cp "$driver" "$ROOT/scripts/ci/RustSource.pm" "$fixture/driver/"
driver="$fixture/driver/run-rust-formatting.pl"
run --check
: > "$FORMAT_EVENTS"
run --check
[[ ! -s "$FORMAT_EVENTS" ]]
printf '\n# changed lexer input\n' >> "$fixture/driver/RustSource.pm"
run --check
[[ "$(wc -l < "$FORMAT_EVENTS")" == 2 ]]

fixture_complete=true
echo 'Rust formatting reuse, invalidation, per-pass timing and failure evidence passed'
PATH="${PATH#"$fixture/bin:"}" bash "$ROOT/scripts/ci/test-rust-formatting-ownership.sh"
