name: ci

on:
  pull_request:
  push:
    branches:
      - main

jobs:
  lint:
    name: Lint Code
    runs-on: ubuntu-24.04
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7

      - name: Install dependencies
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get install -y bash codespell podman
          uv run -- make install-requirements

      - name: Run format check
        run: |
          uv run -- make check-format

      - name: Run lint
        run: |
          uv run -- make lint

      - name: Run man check
        run: |
          uv run -- make man-check

      - name: Run type checking
        run: |
          uv run -- make type-check

  build-image:
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7

      - name: Install dependencies
        shell: bash
        run: |
          df -h
          sudo apt-get update
          sudo apt-get install -y bash codespell pipx podman
          uv run -- make install-requirements

      - name: Upgrade to podman 5
        run: |
           set -e
           # /mnt has ~ 65 GB free disk space. / is too small.
           sudo mkdir -m a=rwx -p /mnt/tmp /mnt/runner
           sudo mkdir -m o=rwx -p /home/runner/.local
           sudo chown runner:runner /mnt/runner /home/runner/.local
           sudo mount --bind /mnt/runner /home/runner/.local
           # Enable universe repository which contains podman
           sudo add-apt-repository "deb http://old-releases.ubuntu.com/ubuntu oracular universe"
           # Update package lists
           sudo apt-get update
           sudo apt-get purge firefox
           # Install specific podman version
           sudo apt-get upgrade -y podman crun

      - name: Build a container for CPU inferencing
        shell: bash
        run: |
          ./container_build.sh build -s ramalama

  unit-test:
    name: Unit Tests
    runs-on: ubuntu-24.04
    timeout-minutes: 20
    steps:
      - uses: actions/checkout@v6
      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7

      - name: Install dependencies
        shell: bash
        run: |
          df -h
          sudo apt-get update
          sudo apt-get install -y bash codespell pipx podman
          uv run -- make install-requirements

      - name: Upgrade to podman 5
        run: |
           set -e
           # Enable universe repository which contains podman
           sudo add-apt-repository "deb http://old-releases.ubuntu.com/ubuntu oracular universe"
           # Update package lists
           sudo apt-get update
           sudo apt-get purge firefox
           # Install specific podman version
           sudo apt-get upgrade -y podman crun

      - name: Run unit tests
        run: |
          uv run -- make unit-tests

  bats:
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7

      - name: install bats
        shell: bash
        run: |
           df -h
           # /mnt has ~ 65 GB free disk space. / is too small.
           sudo mkdir -m a=rwx -p /mnt/tmp /mnt/runner
           sudo mkdir -m o=rwx -p /home/runner/.local
           sudo chown runner:runner /mnt/runner /home/runner/.local
           sudo mount --bind /mnt/runner /home/runner/.local
           sudo apt-get update
           sudo apt-get install podman bats bash codespell
           uv run -- make install-requirements

      - name: install ollama
        shell: bash
        run: ./.github/scripts/install-ollama.sh

      - name: Upgrade to podman 5
        run: |
           set -e
           # Enable universe repository which contains podman
           sudo add-apt-repository "deb http://old-releases.ubuntu.com/ubuntu oracular universe"
           # Update package lists
           sudo apt-get update
           sudo apt-get purge firefox
           # Install specific podman version
           sudo apt-get upgrade -y podman crun

      - name: run bats
        run: |
           TEMPDIR=/mnt/tmp
           uv run -- make bats

  bats-nocontainer:
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7

      - name: install bats
        shell: bash
        run: |
           df -h
           sudo apt-get update
           sudo apt-get install podman bats bash codespell git cmake libcurl4-openssl-dev
           sudo ./container-images/scripts/build_llama_and_whisper.sh
           uv run -- make install-requirements

      - name: install ollama
        shell: bash
        run: ./.github/scripts/install-ollama.sh

      - name: Upgrade to podman 5
        run: |
           set -e
           # Enable universe repository which contains podman
           sudo add-apt-repository "deb http://old-releases.ubuntu.com/ubuntu oracular universe"
           # Update package lists
           sudo apt-get update
           sudo apt-get purge firefox
           # Install specific podman version
           sudo apt-get upgrade -y podman crun

      - name: run bats-nocontainer
        run: |
           uv run -- make bats-nocontainer

  docker:
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7

      - name: install bats
        shell: bash
        run: |
           sudo apt-get update
           sudo apt-get install bats bash codespell
           uv run -- make install-requirements

      - name: install ollama
        shell: bash
        run: ./.github/scripts/install-ollama.sh

      - name: Upgrade to podman 5
        run: |
           set -e
           # Enable universe repository which contains podman
           sudo add-apt-repository "deb http://old-releases.ubuntu.com/ubuntu oracular universe"
           # Update package lists
           sudo apt-get update
           sudo apt-get purge firefox
           # Install specific podman version
           sudo apt-get upgrade -y podman crun

      - name: Free Disk Space Linux
        shell: bash
        run: |
           sudo docker rmi "$(docker image ls -aq)" >/dev/null 2>&1 || true
           sudo rm -rf \
              /usr/share/dotnet /usr/local/lib/android /opt/ghc \
              /usr/local/share/powershell /usr/share/swift /usr/local/.ghcup \
              /usr/lib/jvm || true

      # /mnt has ~ 65 GB free disk space. / is too small.
      - name: Reconfigure Docker data-root
        run: |
           sudo mkdir -p /mnt/docker /etc/docker
           echo '{"data-root": "/mnt/docker"}' > /tmp/daemon.json
           sudo mv /tmp/daemon.json /etc/docker/daemon.json
           cat /etc/docker/daemon.json
           sudo systemctl restart docker.service
           sudo mkdir -m a=rwx -p /mnt/tmp /mnt/runner
           sudo mkdir -m o=rwx -p /home/runner/.local
           sudo chown runner:runner /mnt/runner /home/runner/.local
           sudo mount --bind /mnt/runner /home/runner/.local
           df -h

      - name: run bats-docker
        run: |
           docker info
           uv run -- make bats-docker

  macos:
    runs-on: macos-15
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7
        with:
          python-version: '<3.14'

      - name: install mlx-lm
        shell: bash
        run: |
          uv tool install mlx-lm

      - name: install golang
        shell: bash
        run: |
           brew install go bats bash jq llama.cpp shellcheck
           uv run -- make install-requirements

      - name: install ollama
        shell: bash
        run: ./.github/scripts/install-ollama.sh

      - name: run bats
        shell: bash
        run: |
           uv run -- make bats-nocontainer

  e2e-tests:
    name: E2E Tests
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7
        with:
          activate-environment: true

      - name: install requirements
        shell: bash
        run: |
           df -h
           # /mnt has ~ 65 GB free disk space. / is too small.
           sudo mkdir -m a=rwx -p /mnt/runner
           sudo mkdir -m o=rwx -p /home/runner/.local
           sudo chown runner:runner /mnt/runner /home/runner/.local
           sudo mount --bind /mnt/runner /home/runner/.local
           sudo apt-get update
           sudo apt-get install podman bash codespell
           uv tool install tox --with tox-uv
           uv pip install ".[dev]"

      - name: install ollama
        shell: bash
        run: ./.github/scripts/install-ollama.sh

      - name: Upgrade to podman 5
        run: |
           set -e
           # Enable universe repository which contains podman
           sudo add-apt-repository "deb http://old-releases.ubuntu.com/ubuntu oracular universe"
           # Update package lists
           sudo apt-get update
           sudo apt-get purge firefox
           # Install specific podman version
           sudo apt-get upgrade -y podman crun

      - name: run e2e-tests
        run: |
           make e2e-tests

  e2e-tests-nocontainer:
    name: E2E Tests (--no-container)
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7
        with:
          activate-environment: true

      - name: install requirements
        shell: bash
        run: |
          df -h
          sudo apt-get update
          sudo apt-get install podman bash codespell git cmake libcurl4-openssl-dev
          sudo ./container-images/scripts/build_llama_and_whisper.sh
          uv tool install tox --with tox-uv
          uv pip install ".[dev]"

      - name: install ollama
        shell: bash
        run: ./.github/scripts/install-ollama.sh

      - name: Upgrade to podman 5
        run: |
           set -e
           # Enable universe repository which contains podman
           sudo add-apt-repository "deb http://old-releases.ubuntu.com/ubuntu oracular universe"
           # Update package lists
           sudo apt-get update
           sudo apt-get purge firefox
           # Install specific podman version
           sudo apt-get upgrade -y podman crun

      - name: run e2e-tests-nocontainer
        run: |
           make e2e-tests-nocontainer

  e2e-tests-docker:
    name: E2E Tests (docker)
    runs-on: ubuntu-24.04
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7
        with:
          activate-environment: true

      - name: install requirements
        shell: bash
        run: |
           sudo apt-get update
           sudo apt-get install bash codespell
           uv tool install tox --with tox-uv
           uv pip install ".[dev]"

      - name: install ollama
        shell: bash
        run: ./.github/scripts/install-ollama.sh

      - name: Upgrade to podman 5
        run: |
           set -e
           # Enable universe repository which contains podman
           sudo add-apt-repository "deb http://old-releases.ubuntu.com/ubuntu oracular universe"
           # Update package lists
           sudo apt-get update
           sudo apt-get purge firefox
           # Install specific podman version
           sudo apt-get upgrade -y podman crun

      - name: Free Disk Space Linux
        shell: bash
        run: |
           sudo docker rmi "$(docker image ls -aq)" >/dev/null 2>&1 || true
           sudo rm -rf \
              /usr/share/dotnet /usr/local/lib/android /opt/ghc \
              /usr/local/share/powershell /usr/share/swift /usr/local/.ghcup \
              /usr/lib/jvm || true

      # /mnt has ~ 65 GB free disk space. / is too small.
      - name: Reconfigure Docker data-root
        run: |
           sudo mkdir -p /mnt/docker /etc/docker
           echo '{"data-root": "/mnt/docker"}' > /tmp/daemon.json
           sudo mv /tmp/daemon.json /etc/docker/daemon.json
           cat /etc/docker/daemon.json
           sudo systemctl restart docker.service
           sudo mkdir -m a=rwx -p /mnt/tmp /mnt/runner
           sudo mkdir -m o=rwx -p /home/runner/.local
           sudo chown runner:runner /mnt/runner /home/runner/.local
           sudo mount --bind /mnt/runner /home/runner/.local
           df -h

      - name: run e2e-tests-docker
        run: |
           docker info
           make e2e-tests-docker

  e2e-tests-macos:
    name: E2E Tests (macos)
    runs-on: macos-15
    timeout-minutes: 60
    steps:
      - uses: actions/checkout@v6

      - name: Install the latest version of uv
        uses: astral-sh/setup-uv@v7
        with:
          activate-environment: true
          python-version: '<3.14'

      - name: install mlx-lm
        shell: bash
        run: |
          uv pip install mlx-lm

      - name: install requirements
        shell: bash
        run: |
          brew install go bash jq llama.cpp shellcheck podman
          uv tool install tox --with tox-uv
          uv run -- make install-requirements

      - name: install ollama
        shell: bash
        run: ./.github/scripts/install-ollama.sh

      - name: run e2e-tests-nocontainer
        shell: bash
        run: |
          make e2e-tests-nocontainer

# FIXME: ci script should be able to run on MAC.
#      - name: Run ci
#        shell: bash
#        run: make ci
