name: locked

on:
  push:
    paths-ignore:
      - 'docs/**'
  pull_request:
    paths-ignore:
      - 'docs/**'

  workflow_dispatch:

  schedule:
    # nightly
    - cron: '31 1 * * *' # 01:31 UTC, 02:31/03:31 Munich, 03:31/04:31 Tartu
    # GitHub Actions load is high at minute 0, so avoid that

jobs:
  regression:
    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
          - macos-latest
        ocaml-compiler:
          - ocaml-variants.4.14.2+options,ocaml-option-flambda # matches opam lock file
          # don't add any other because they won't be used

    runs-on: ${{ matrix.os }}

    env:
      OCAMLRUNPARAM: b

    steps:
      - name: Checkout code
        uses: actions/checkout@v7

      - name: Set up OCaml ${{ matrix.ocaml-compiler }}
        env:
          # otherwise setup-ocaml pins non-locked dependencies
          # https://github.com/ocaml/setup-ocaml/issues/166
          OPAMLOCKED: locked
        uses: ocaml/setup-ocaml@v3
        with:
          ocaml-compiler: ${{ matrix.ocaml-compiler }}

      - name: Install graph-easy # TODO: remove if depext --with-test works (https://github.com/ocaml/opam/issues/5836)
        if: ${{ startsWith(matrix.os, 'ubuntu') }}
        run: sudo apt install -y libgraph-easy-perl

      - name: Install dependencies
        run: opam install . --deps-only --locked --with-test

      - name: Install os gem for operating system detection
        run: sudo gem install os

      - name: Build
        run: ./make.sh nat

      - name: Download Linux headers
        run: ./make.sh headers

      - name: Test
        run: opam exec -- dune runtest

      - uses: actions/upload-artifact@v7
        if: always()
        with:
          name: suite_result-${{ matrix.os }}
          path: _build/default/tests/suite_result/

  extraction:
    if: ${{ github.event_name == 'workflow_dispatch' || github.event_name == 'schedule' }}

    strategy:
      fail-fast: false
      matrix:
        os:
          - ubuntu-latest
        ocaml-compiler:
          - ocaml-variants.4.14.2+options,ocaml-option-flambda # matches opam lock file
          # don't add any other because they won't be used

    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout code
        uses: actions/checkout@v7

      - name: Set up OCaml ${{ matrix.ocaml-compiler }}
        env:
          # otherwise setup-ocaml pins non-locked dependencies
          # https://github.com/ocaml/setup-ocaml/issues/166
          OPAMLOCKED: locked
        uses: ocaml/setup-ocaml@v3
        with:
          ocaml-compiler: ${{ matrix.ocaml-compiler }}

      - name: Install graph-easy # TODO: remove if depext --with-test works (https://github.com/ocaml/opam/issues/5836)
        if: ${{ startsWith(matrix.os, 'ubuntu') }}
        run: sudo apt install -y libgraph-easy-perl

      - name: Install spin
        run: sudo apt-get -y install spin

      - name: Install dependencies
        run: opam install . --deps-only --locked --with-test

      - name: Install os gem for operating system detection
        run: sudo gem install os

      - name: Build
        run: ./make.sh nat

      - name: Test extraction
        run: opam exec -- dune runtest tests/extraction
