name: ci

on:
  push:
    branches:
     - main
     - next
     - '*.x'
    paths-ignore:
      - 'docs/**'
      - '*.md'
  pull_request:
    paths-ignore:
      - 'docs/**'
      - '*.md'
  workflow_dispatch:
    inputs:
      nodejs-version:
        description: 'Node.js version to use (e.g., 24.0.0-rc.1)'
        required: true
        type: string

# This allows a subsequently queued workflow run to interrupt previous runs
concurrency:
  group: "${{ github.workflow }}-${{ github.event.pull_request.head.label || github.head_ref || github.ref }}"
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  quality-check:
    uses: fastify/workflows/.github/workflows/nested-quality.yml@ef591e2186785d5ab36b9fe6a79c7ce2f1d94e57 # v7.0.0
    permissions:
      contents: read
    with:
      license-check: true
      lint: true

  coverage-nix:
    needs:
      - quality-check
    permissions:
      contents: read
    uses: ./.github/workflows/coverage-nix.yml

  coverage-win:
    needs:
      - quality-check
    permissions:
      contents: read
    uses: ./.github/workflows/coverage-win.yml

  test-unit:
    needs:
      - quality-check
      - coverage-nix
      - coverage-win
    runs-on: ${{ matrix.os }}
    permissions:
      contents: read

    strategy:
      matrix:
        node-version: [24, 26]
        os: [macos-latest, ubuntu-latest, windows-latest]

    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false

      - name: Use Node.js
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: ${{ matrix.node-version }}
          cache: 'npm'
          cache-dependency-path: package.json
          check-latest: true

      - name: Install
        run: |
          npm install --ignore-scripts

      - name: Run tests
        run: |
          npm run unit

  # Useful for testing Release Candidates of Node.js
  test-unit-custom:
    if: github.event_name == 'workflow_dispatch'
    runs-on: ubuntu-latest
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false

      - name: Use Custom Node.js Version
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: ${{ github.event.inputs.nodejs-version }}
          cache: 'npm'
          cache-dependency-path: package.json
          check-latest: true

      - name: Install
        run: |
          npm install --ignore-scripts

      - name: Run tests
        run: |
          npm run unit

  test-types:
    needs:
      - quality-check
      - coverage-nix
      - coverage-win
    runs-on: 'ubuntu-latest'
    permissions:
      contents: read

    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false

      - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: '20'
          cache: 'npm'
          cache-dependency-path: package.json
          check-latest: true

      - name: Install
        run: |
          npm install --ignore-scripts

      - name: Run type tests
        run: |
          npm run test:types
        env:
          NODE_OPTIONS: no-network-family-autoselection

  test-pino-compat:
    name: Test pino compatibility
    needs:
      - quality-check
      - coverage-nix
      - coverage-win
    runs-on: ubuntu-latest
    permissions:
      contents: read
    strategy:
      matrix:
        pino-version: ['^9', '^10']

    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false

      - uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: '24'
          cache: 'npm'
          cache-dependency-path: package.json
          check-latest: true

      - name: Install dependencies
        run: |
          npm install --ignore-scripts

      - name: Install pino ${{ matrix.pino-version }}
        run: |
          npm install --ignore-scripts --no-save pino@${{ matrix.pino-version }}

      - name: Run unit tests
        run: |
          npm run unit

      - name: Run type tests
        run: |
          npm run test:types
        env:
          NODE_OPTIONS: no-network-family-autoselection

  package:
    needs:
      - test-types
      - test-unit
      - test-pino-compat
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
        with:
          persist-credentials: false
      - name: Use Node.js
        uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
        with:
          node-version: 'lts/*'
          cache: 'npm'
          cache-dependency-path: package.json
          check-latest: true
      - name: install fastify
        run: |
          npm install --ignore-scripts
      - name: install webpack stack
        run: |
          cd test/bundler/webpack && npm install --ignore-scripts
      - name: Test webpack bundle
        run: |
          cd test/bundler/webpack && npm run test
      - name: install esbuild stack
        run: |
          cd test/bundler/esbuild && npm install --ignore-scripts
      - name: Test esbuild bundle
        run: |
          cd test/bundler/esbuild && npm run test

  automerge:
    if: >
        github.event_name == 'pull_request' &&
        github.event.pull_request.head.repo.full_name == github.repository &&
        github.event.pull_request.user.login == 'dependabot[bot]'
    needs:
      - test-types
      - test-unit
      - package
    runs-on: ubuntu-latest
    permissions:
      pull-requests: write
      contents: write
    steps:
      - uses: fastify/github-action-merge-dependabot@73ec4cbb5e56df5591eae286972d5b2201ffe90f # v3.15.0
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}