name: Native code generation

on:
  push:
    paths:
      - ".gitea/workflows/codegen.yml"
      - "Cargo.toml"
      - "Cargo.lock"
      - "api/**"
      - "codegen/**"
      - "tools/codegen/**"
      - "tools/generate_*.py"
      - "tools/install_openjpeg_2_5_4.sh"
      - "tools/check_api_coverage.py"
      - "tools/check_codegen_gate.py"
      - "tools/check_milestone_08.py"
      - "tools/check_milestone_09_issue_59.py"
      - "tools/check_milestone_09_issue_60.py"
      - "tools/check_milestone_09_issue_61.py"
      - "tools/check_milestone_09_issue_62.py"
      - "tools/check_milestone_09_issue_63.py"
      - "tools/check_milestone_09_issue_64.py"
      - "tools/check_milestone_09_issue_65.py"
      - "tools/check_milestone_09_issue_66.py"
      - "tools/check_milestone_09.py"
      - "tools/test_milestone_09.py"
      - "crates/libremetaverse/src/agent_movement.rs"
      - "crates/libremetaverse/src/inventory.rs"
      - "crates/libremetaverse/src/inventory_manager.rs"
      - "crates/libremetaverse/src/inventory_ais.rs"
      - "crates/libremetaverse/src/inventory_ais_internal_semantics.rs"
      - "docs/agent-movement.md"
      - "docs/inventory.md"
      - "crates/libremetaverse/Cargo.toml"
      - "crates/libremetaverse/src/generated.rs"
      - "crates/libremetaverse/src/packets.rs"
      - "crates/libremetaverse/src/packet_*.rs"
      - "crates/libremetaverse/src/*_catalog.rs"
      - "tests/api-compile/**"
      - "tests/compat/Cargo.toml"
      - "tests/compat/build.rs"
      - "tests/compat/src/**"
      - "tests/compat/tests/wire_semantics.rs"
      - "tests/compat/tests/appearance_visual_semantics.rs"
      - "tests/compat/tests/inventory_store_semantics.rs"
      - "tests/compat/tests/inventory_manager_semantics.rs"
      - "tests/compat/tests/inventory_ais_semantics.rs"
      - "tests/compat/tests/task_inventory_semantics.rs"
      - "tests/compat/tests/misclassified_link_semantics.rs"
      - "tests/upstream-tests.json"
  pull_request:
    paths:
      - ".gitea/workflows/codegen.yml"
      - "Cargo.toml"
      - "Cargo.lock"
      - "api/**"
      - "codegen/**"
      - "tools/codegen/**"
      - "tools/generate_*.py"
      - "tools/install_openjpeg_2_5_4.sh"
      - "tools/check_api_coverage.py"
      - "tools/check_codegen_gate.py"
      - "tools/check_milestone_08.py"
      - "tools/check_milestone_09_issue_59.py"
      - "tools/check_milestone_09_issue_60.py"
      - "tools/check_milestone_09_issue_61.py"
      - "tools/check_milestone_09_issue_62.py"
      - "tools/check_milestone_09_issue_63.py"
      - "tools/check_milestone_09_issue_64.py"
      - "tools/check_milestone_09_issue_65.py"
      - "tools/check_milestone_09_issue_66.py"
      - "tools/check_milestone_09.py"
      - "tools/test_milestone_09.py"
      - "crates/libremetaverse/src/agent_movement.rs"
      - "crates/libremetaverse/src/inventory.rs"
      - "crates/libremetaverse/src/inventory_manager.rs"
      - "crates/libremetaverse/src/inventory_ais.rs"
      - "crates/libremetaverse/src/inventory_ais_internal_semantics.rs"
      - "docs/agent-movement.md"
      - "docs/inventory.md"
      - "crates/libremetaverse/Cargo.toml"
      - "crates/libremetaverse/src/generated.rs"
      - "crates/libremetaverse/src/packets.rs"
      - "crates/libremetaverse/src/packet_*.rs"
      - "crates/libremetaverse/src/*_catalog.rs"
      - "tests/api-compile/**"
      - "tests/compat/Cargo.toml"
      - "tests/compat/build.rs"
      - "tests/compat/src/**"
      - "tests/compat/tests/wire_semantics.rs"
      - "tests/compat/tests/appearance_visual_semantics.rs"
      - "tests/compat/tests/inventory_store_semantics.rs"
      - "tests/compat/tests/inventory_manager_semantics.rs"
      - "tests/compat/tests/inventory_ais_semantics.rs"
      - "tests/compat/tests/task_inventory_semantics.rs"
      - "tests/compat/tests/misclassified_link_semantics.rs"
      - "tests/upstream-tests.json"
  workflow_dispatch:

jobs:
  deterministic:
    runs-on: ubuntu-latest
    env:
      OPENJPEG_PREFIX: /tmp/metacrate-openjpeg-${{ github.run_id }}
      PKG_CONFIG_PATH: /tmp/metacrate-openjpeg-${{ github.run_id }}/lib/pkgconfig
      LD_LIBRARY_PATH: /tmp/metacrate-openjpeg-${{ github.run_id }}/lib
      CARGO_BUILD_JOBS: 1
      CARGO_INCREMENTAL: 0
      CARGO_PROFILE_DEV_DEBUG: 0
      CARGO_PROFILE_TEST_DEBUG: 0
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy,rustfmt
      - name: Install audited OpenJPEG 2.5.4
        run: |
          sudo apt-get update
          sudo apt-get install --yes build-essential cmake pkg-config
          tools/install_openjpeg_2_5_4.sh "$OPENJPEG_PREFIX"
      - name: Test diagnostics and byte-identical generation
        run: cargo test -p libremetaverse-codegen
      - name: Verify vendored hashes and checked-in output
        run: cargo run -p libremetaverse-codegen -- check
      - name: Verify authoritative API mappings and consumer fixture
        run: |
          python3 tools/generate_rust_mapping.py --check
          python3 tools/generate_api_shims.py --check
          python3 tools/check_api_coverage.py
          python3 tools/check_milestone_08.py
          python3 tools/check_milestone_09_issue_59.py
          python3 tools/check_milestone_09_issue_60.py
          python3 tools/check_milestone_09_issue_61.py
          python3 tools/check_milestone_09_issue_62.py
          python3 tools/check_milestone_09_issue_63.py
          python3 tools/check_milestone_09_issue_64.py
          python3 tools/check_milestone_09_issue_65.py
          python3 tools/check_milestone_09_issue_66.py
          python3 tools/check_milestone_09.py
          cargo check --manifest-path tests/api-compile/Cargo.toml --locked
      - name: Test generated wire codecs and translated parity cases
        run: |
          cargo test -p libremetaverse --lib generated_codec_tests
          cargo test -p libremetaverse --test packet_wire
          cargo test -p libremetaverse --test packet_osd
          cargo test -p libremetaverse-compat-tests --test wire_semantics
          cargo test -p libremetaverse-compat-tests --test inventory_store_semantics
          cargo test -p libremetaverse-compat-tests --test inventory_manager_semantics
          cargo test -p libremetaverse-compat-tests --test inventory_ais_semantics
          cargo test -p libremetaverse --lib inventory_ais_internal_semantics
          cargo test -p libremetaverse-compat-tests --test task_inventory_semantics
          cargo test -p libremetaverse-compat-tests --test misclassified_link_semantics
      - name: Test generated visual and foliage catalogs
        run: |
          cargo test -p libremetaverse --test generated_catalogs
          cargo test -p libremetaverse-compat-tests --test appearance_visual_semantics
          cargo test -p libremetaverse-compat-tests --test appearance_semantics
          cargo test -p libremetaverse --lib appearance_
          cargo test -p libremetaverse --lib initial_outfit::tests
      - name: Test generated skeleton, attention, and genepool catalogs
        run: |
          cargo test -p libremetaverse --test generated_avatar_catalogs
          cargo test -p libremetaverse --lib linden_skeleton
          cargo test -p libremetaverse --lib joint_base_support_category
          cargo test -p libremetaverse --lib joint_get_aliases_list
      - name: Build and audit clean Rust-only release artifacts
        run: |
          cargo build -p libremetaverse-codegen --release --locked -j 1
          python3 tools/check_codegen_gate.py --release-dir target/release
      - name: Lint native generator
        run: |
          cargo clippy -p libremetaverse-codegen --all-targets -- -D warnings
          cargo clippy -p libremetaverse --lib --no-deps -- -D warnings
      - name: Prove normal consumer build does not execute generation
        run: cargo build -p libremetaverse --locked
