# Temperlux CI pipeline — build, test, clippy, coverage, hw-test, auto-tag

when:
  - event: [push, manual]
    branch: [master]
  - event: manual

steps:
  - name: build-and-test
    image: rust:1.96-slim
    commands:
      - apt-get update -qq && apt-get install -qq -y libddcutil-dev pkg-config > /dev/null 2>&1
      - rustc --version
      - cargo build --release 2>&1
      - cargo test 2>&1

  - name: clippy
    image: rust:1.96-slim
    commands:
      - apt-get update -qq && apt-get install -qq -y libddcutil-dev libgtk-4-dev libadwaita-1-dev pkg-config > /dev/null 2>&1
      - rustup component add clippy
      - cargo clippy -- -D warnings 2>&1
      - cargo clippy --features gui -- -D warnings 2>&1

  # Coverage: overall tollgate (70% on testable code) + diff coverage (80% on new code)
  - name: coverage
    image: rust:1.96-slim
    commands:
      - |
        apt-get update -qq && apt-get install -qq -y llvm-dev libddcutil-dev pkg-config git > /dev/null 2>&1
        cargo install cargo-llvm-cov@0.6.16 2>&1 | tail -1

        echo "=== Overall coverage tollgate (70% LINE on testable code) ==="
        cargo llvm-cov --fail-under-lines 70 --ignore-filename-regex "(src/main\\.rs|src/bin/ddc-diag\\.rs|src/display/ddc_ffi\\.rs|src/display/backlight\\.rs)" 2>&1

        echo "=== Diff coverage tollgate (80% LINE on new code) ==="
        cargo install covguard@0.1.0 2>&1 | tail -1
        BASE_SHA=$(git merge-base origin/master HEAD 2>/dev/null || echo HEAD~1)
        cargo llvm-cov --lcov --output-path lcov.info --ignore-filename-regex "(src/main\\.rs|src/bin/ddc-diag\\.rs|src/display/ddc_ffi\\.rs|src/display/backlight\\.rs)" 2>&1
        covguard check --base "$BASE_SHA" --head HEAD --lcov lcov.info --threshold 80 --out covguard-report.json --md covguard-comment.md
    when:
      event: manual

  # Hardware-in-the-loop tests — READ ONLY
  # Auto-runs on master push. On PRs, trigger manually from Woodpecker UI
  # (requires admin access — only RhoMancer can trigger).
  # Requires: Temperlux repo is TRUSTED in Woodpecker (volumes only work for trusted repos)
  - name: hw-test
    image: rust:1.96-slim
    commands:
      - |
        apt-get update -qq && apt-get install -qq -y libddcutil-dev pkg-config > /dev/null 2>&1
        cargo test --features hw-test --test test_ddc_hardware -- --test-threads=1 2>&1
    volumes:
      - /dev/i2c-8:/dev/i2c-8
      - /dev/i2c-9:/dev/i2c-9
      - /dev/i2c-10:/dev/i2c-10
      - /dev/i2c-11:/dev/i2c-11
      - /dev/i2c-12:/dev/i2c-12
    when:
      - event: push
        branch: master
      - event: manual

  # Auto-tag: bump version + tag on feat/fix commits (push to master only)
  - name: auto-tag
    image: rust:1.96-slim
    commands:
      - |
        apt-get update -qq && apt-get install -qq -y git > /dev/null 2>&1
        git fetch --tags
        sh scripts/auto-tag.sh
    when:
      event: push
      branch: master
