name: Test on Raspberry Pi OS

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  test-on-rpi-os:
    name: Run Tests on Raspberry Pi OS
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
      - name: Build Raspberry Pi OS
        uses: pguyot/arm-runner-action@v2
        id: build_image
        env:
          INKY_INITAL_PASSWORD: ${{ secrets.INKY_INITAL_PASSWORD }}
          OPENWEATHERMAP_API_KEY: ${{ secrets.OPENWEATHERMAP_API_KEY }}
          SAMPLE_ICAL_URL: ${{ secrets.SAMPLE_ICAL_URL }}
          TEST_ICAL_URL: ${{ secrets.TEST_ICAL_URL }}
          TODOIST_API_KEY: ${{ secrets.TODOIST_API_KEY }}
          TINDIE_API_KEY: ${{ secrets.TINDIE_API_KEY }}
          TINDIE_USERNAME: ${{ secrets.TINDIE_USERNAME }}
        with:
          # Set the base_image to the desired Raspberry Pi OS version
          base_image: https://downloads.raspberrypi.com/raspios_lite_armhf/images/raspios_lite_armhf-2024-03-15/2024-03-15-raspios-bookworm-armhf-lite.img.xz
          image_additional_mb: 3072 # enlarge free space to 3GB
          optimize_image: true
          # user: inky --> not supported?
          commands: |
            cd /home
            sudo useradd -m -p "$(openssl passwd -1 $INKY_INITAL_PASSWORD)" inky
            sudo usermod -a -G adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,gpio,i2c,spi inky
            sudo su inky
            echo $HOME
            whoami
            cd /home/inky
            sudo apt-get update -y
            sudo apt-get install git zlib1g libjpeg-dev libatlas-base-dev rustc libopenjp2-7 python3-dev scons libssl-dev python3-venv python3-pip git libfreetype6-dev wkhtmltopdf libopenblas-base libopenblas-dev -y
            echo $PWD && ls
            git clone https://github.com/aceinnolab/Inkycal
            cd Inkycal
            python3.11 -m venv venv
            . venv/bin/activate
            python -m pip install --upgrade pip
            pip install wheel
            pip install -e ./
            pip install RPi.GPIO==0.7.1 spidev==3.5 gpiozero==2.0
            wget https://raw.githubusercontent.com/aceinnolab/Inkycal/assets/tests/settings.json
            pip install pytest
            python -m pytest