name: Test
on:
  pull_request:
  push:
    branches:
      - main
      - "v*.*"
jobs:
  main:
    runs-on: ubuntu-latest
    env:
      MIX_ENV: test
    steps:
      - name: Checkout git repo
        uses: actions/checkout@v3
      - name: Read ./versions
        run: |
          . versions
          echo "elixir=$elixir" >> $GITHUB_ENV
          echo "otp=$otp" >> $GITHUB_ENV
          echo "openssl=$openssl" >> $GITHUB_ENV
      - name: Install Erlang & Elixir
        uses: erlef/setup-beam@v1
        with:
          otp-version: ${{ env.otp }}
          elixir-version: ${{ env.elixir }}
      - name: Cache Mix
        uses: actions/cache@v3
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-${{ hashFiles('**/mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-
      - name: Install mix dependencies
        run: mix deps.get
      - name: Check formatting
        run: mix format --check-formatted
      - name: Check warnings
        run: mix compile --warnings-as-errors
      - name: Run tests
        run: mix test
      - name: Install Node
        uses: actions/setup-node@v3
        with:
          node-version: "18.x"
      - name: Cache npm dependencies
        uses: actions/cache@v3
        with:
          path: ~/.npm
          key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-
      - name: Install npm dependencies
        run: npm ci --prefix assets
      - name: Check assets formatting
        run: npm run format-check --prefix assets
      - name: Run assets tests
        run: npm test --prefix assets

  epmdless:
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    env:
      MIX_ENV: test
      LIVEBOOK_EPMDLESS: true
      ELIXIR_ERL_OPTIONS: "-epmd_module Elixir.Livebook.EPMD -start_epmd false -erl_epmd_port 0"
    steps:
      - name: Checkout git repo
        uses: actions/checkout@v3
      - name: Read ./versions
        run: |
          . versions
          echo "elixir=$elixir" >> $GITHUB_ENV
          echo "otp=$otp" >> $GITHUB_ENV
          echo "openssl=$openssl" >> $GITHUB_ENV
      - name: Install Erlang & Elixir
        uses: erlef/setup-beam@v1
        with:
          otp-version: ${{ env.otp }}
          elixir-version: ${{ env.elixir }}
      - name: Cache Mix
        uses: actions/cache@v3
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-${{ hashFiles('**/mix.lock') }}
          restore-keys: |
            ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-
      - name: Install mix dependencies
        run: mix deps.get
      - name: Run tests
        run: mix test

  windows:
    runs-on: windows-latest
    if: github.event_name == 'push'
    env:
      MIX_ENV: test
    steps:
      - name: Configure Git
        run: git config --global core.autocrlf input
      - name: Checkout git repo
        uses: actions/checkout@v3
      - name: Read ./versions
        shell: bash
        run: |
          . versions
          echo "elixir=$elixir" >> $GITHUB_ENV
          echo "otp=$otp" >> $GITHUB_ENV
          echo "openssl=$openssl" >> $GITHUB_ENV
      - name: Install Erlang & Elixir
        uses: erlef/setup-beam@v1
        with:
          version-type: strict
          otp-version: ${{ env.otp }}
          elixir-version: ${{ env.elixir }}
      - name: Start epmd
        run: $(elixir -e 'IO.puts "#{:code.root_dir()}/erts-#{:erlang.system_info(:version)}"')/bin/epmd.exe -daemon
        shell: bash
      # Add tar that supports symlinks, see https://github.com/actions/virtual-environments/issues/4679
      - name: Add tar.exe
        run: |
          "C:\Program Files\Git\usr\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8
      - name: Cache Mix
        uses: actions/cache@v3
        with:
          path: |
            deps
            _build
          key: ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-${{ hashFiles('**/mix.lock') }}
          # Reinstalling deps fails on Windows in rare cases, so we always use a new cache
          # restore-keys: |
          #   ${{ runner.os }}-mix-${{ env.elixir }}-${{ env.otp }}-
      - name: Install mix dependencies
        run: mix deps.get
      - name: Run tests
        run: mix test
      - name: Build the app
        run: bash .github/scripts/app/build_windows.sh

  macos:
    runs-on: macos-latest
    if: github.event_name == 'push'
    steps:
      - name: Checkout git repo
        uses: actions/checkout@v3
      - name: Read ./versions
        run: |
          . versions
          echo "elixir=$elixir" >> $GITHUB_ENV
          echo "otp=$otp" >> $GITHUB_ENV
          echo "openssl=$openssl" >> $GITHUB_ENV
      - name: Cache Mix Deps
        uses: actions/cache@v3
        with:
          path: deps
          key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
      - name: Cache Bootstrap OTP
        uses: actions/cache@v3
        with:
          path: elixirkit/otp_bootstrap/_build/otp-rel-${{ env.otp }}-openssl-${{ env.openssl }}-macos-universal
          key: ${{ runner.os }}-bootstrap-${{ env.otp }}-${{ hashFiles('elixirkit/otp_bootstrap/build.sh') }}
      - name: Cache Elixir
        uses: actions/cache@v3
        with:
          path: _build/elixir-${{ env.elixir }}
          key: ${{ runner.os }}-elixir-${{ env.elixir }}
      - name: Build the app
        run: .github/scripts/app/build_macos.sh
