name: Test

# Mirror the local pre-push hook's test and coverage gates in CI, so they are
# enforced on every PR even when a contributor (or a fork) hasn't installed the
# git hooks. Previously CI ran only fmt + clippy (see lint.yml); `cargo test`
# and the 100% line-coverage floor lived solely in the pre-push hook, so a PR
# that skipped hook setup could break tests or drop coverage and still go
# green.
#
# This relies on the committed `prebuilt.html` (build.rs falls back to it when
# trunk is absent), so no trunk or wasm target is needed to build and test the
# server crate. Both commands mirror CONTRIBUTING.md's documented pre-push
# gate — keep the hook, CONTRIBUTING.md, and this workflow in lockstep.

# Restrict the default GITHUB_TOKEN to read-only; this workflow only needs to
# read repository contents to check out and run the test suite.
permissions:
  contents: read

on:
  pull_request:
  push:
    branches: [main]
  workflow_call:
    inputs:
      ref:
        description: >
          Git ref to check out. Empty (the default) means "use the ref that
          triggered this run" — set only by changeset-release.yml, which
          calls this workflow directly to test a branch (changeset-release/main)
          other than the one that triggered it.
        required: false
        type: string

# Cancel a stale run when a PR gets a new push, matching lint.yml's behavior
# so superseded test runs don't pile up and burn CI minutes. Literal "test"
# prefix, not ${{ github.workflow }} — see lint.yml's comment: workflow_call
# callers (cut-release.yml) would otherwise collide lint and test onto the
# same concurrency group and race-cancel each other.
concurrency:
  group: test-${{ inputs.ref || github.ref }}
  cancel-in-progress: true

jobs:
  test:
    name: cargo test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          ref: ${{ inputs.ref }}

      - name: Install Rust
        uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 # v1
        with:
          toolchain: stable

      - name: Cache cargo registry and target/
        uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          # Scoped so the `test` and `clippy` jobs' target/ artifacts (built
          # with different flags: plain build vs --all-targets clippy) don't
          # clobber each other's cache entry.
          shared-key: test

      - name: Run tests
        # Match the local pre-push hook and the `clippy` job in lint.yml:
        # this is a non-virtual workspace (the root Cargo.toml declares both
        # a [package] and a [workspace]), so a bare `cargo test` only runs
        # the root `moadim` package's ~985 tests and silently skips the `ui`
        # member crate's ~274 tests. Without `--workspace`, a PR could break
        # every test in `ui/` and this job would still go green.
        run: cargo test --workspace

  coverage:
    name: cargo llvm-cov (100% line floor)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0

      - name: Install Rust (with llvm-tools-preview)
        uses: dtolnay/rust-toolchain@e97e2d8cc328f1b50210efc529dca0028893a2d9 # v1
        with:
          toolchain: stable
          components: llvm-tools-preview

      - name: Cache cargo registry (registry only, not target)
        uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-

      - name: Install cargo-llvm-cov
        run: cargo install cargo-llvm-cov --locked

      - name: Enforce 100% line coverage (excluding main.rs)
        run: cargo llvm-cov --fail-under-lines 100 --ignore-filename-regex 'src/main\.rs'
