name: test
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
on:
  pull_request: { types: [opened, reopened, synchronize, ready_for_review] }
  push: { branches: [ main ] }
env:
  LOG_LEVEL: info
  SWIFT_DETERMINISTIC_HASHING: 1

jobs:
  unit-tests:
    uses: vapor/ci/.github/workflows/run-unit-tests.yml@main
    with:
      with_release_mode_testing: true
      with_api_check: false
      with_linting: true
      with_musl: true
      with_android: true
      android_copy_files: Tests/VaporToolboxTests/Manifests/
    secrets:
      CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
  
  test-makefile:
    runs-on: ubuntu-latest
    container: swift:6.0-noble
    steps:
      - name: Check out toolbox
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          fetch-tags: true
      - name: Install make
        run: |
          apt-get update
          apt-get install -y make
      - name: Config Git
        run: |
          git config --global --add safe.directory /__w/toolbox/toolbox
          git checkout -b temp-branch
      - name: Test Makefile
        run: |
          make build
          make install DEST=/tmp/vapor
          /tmp/vapor --version
          make uninstall DEST=/tmp/vapor
          make clean

  integration-linux:
    if: ${{ github.event_name == 'pull_request' && !github.event.pull_request.draft }}
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}-${{ toJSON(matrix) }}
      cancel-in-progress: true
    strategy:
      fail-fast: false
      matrix:
        fluentflags:
          - --no-fluent
          # - --fluent.db mysql
          - --fluent.db postgres
          - --fluent.db sqlite
        leafflags: [--leaf, --no-leaf]
        include:
          # - fluentflags: '--fluent.db mysql'
          #   dbhostname: mysql
          - fluentflags: '--fluent.db postgres'
            dbhostname: psql
    runs-on: ubuntu-latest
    container: swift:6.0-noble
    services:
      # mysql:
      #   image: mysql:latest
      #   env: { MYSQL_ALLOW_EMPTY_PASSWORD: 'true', MYSQL_USER: vapor_username, MYSQL_PASSWORD: vapor_password, MYSQL_DATABASE: vapor_database }
      psql:
        image: postgres:latest
        env: { POSTGRES_USER: vapor_username, POSTGRES_DB: vapor_database, POSTGRES_PASSWORD: vapor_password,
               POSTGRES_HOST_AUTH_METHOD: 'scram-sha-256', POSTGRES_INITDB_ARGS: '--auth-host=scram-sha-256' }
    steps:
      - name: Check out toolbox
        uses: actions/checkout@v4
      - name: Build toolbox
        run: swift build
      - name: Execute new project command
        env:
          FLUENTFLAGS: ${{ matrix.fluentflags }}
          LEAFFLAGS: ${{ matrix.leafflags }}
        run: |
          swift run \
            vapor new toolbox-test \
                --no-commit -o /tmp/toolbox-test \
                ${FLUENTFLAGS} ${LEAFFLAGS}
      - name: Test new project
        run: swift test --package-path /tmp/toolbox-test
        env:
          DATABASE_HOST: ${{ matrix.dbhostname }}
          DATABASE_URL: ${{ matrix.dbhosturl }}
  
  integration-macos:
    strategy:
      fail-fast: false
      matrix:
        fluentflags:
          - --no-fluent
          - --fluent.db mysql
          - --fluent.db postgres
          - --fluent.db sqlite
        leafflags: [--leaf, --no-leaf]
    runs-on: macos-15
    steps:
      - name: Select toolchain
        uses: maxim-lobanov/setup-xcode@v1
        with: { xcode-version: latest-stable }
      - name: Check out toolbox
        uses: actions/checkout@v4
      - name: Build toolbox
        run: swift build
      - name: Execute new project command
        env:
          FLUENTFLAGS: ${{ matrix.fluentflags }}
          LEAFFLAGS: ${{ matrix.leafflags }}
        run: |
          swift run \
            vapor new toolbox-test \
                --no-commit -o /tmp/toolbox-test \
                ${FLUENTFLAGS} ${LEAFFLAGS}
      - name: Test new project
        run: swift build --package-path /tmp/toolbox-test