name: flatpak-test
on:
  pull_request

jobs:
  run:
    runs-on: ubuntu-latest
    permissions: {}
    timeout-minutes: 20
    steps:
      - name: Install required build and test dependencies
        run: |
          sudo apt update
          sudo apt install -y --no-install-recommends autoconf automake elfutils libglib2.0-dev libsystemd-dev xsltproc xmlto gettext flatpak xvfb cockpit-system appstream appstream-util

      - name: Configure flathub remote
        run: flatpak remote-add --user --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

      - name: Install flatpak-builder
        run: flatpak install --assumeyes --user org.flatpak.Builder

      - name: Clone repository
        uses: actions/checkout@v4
        with:
          # need this to also fetch tags
          fetch-depth: 0

      - name: Build and install flatpak
        run: ELEMENT_TREE_NO_INDENT=1 dbus-run-session sh -x containers/flatpak/install --user --install-deps-from=flathub

      - name: Smoke-test the installed flatpak
        run: |
          export COCKPIT_TEST_SSH_PASS=foobar
          sudo useradd -c User -s /bin/bash user
          echo user:$COCKPIT_TEST_SSH_PASS | sudo chpasswd
          export COCKPIT_TEST_SSH_HOST=user@127.0.0.1

          . /etc/profile.d/flatpak.sh
          xvfb-run sh -ec '
            dbus-run-session containers/flatpak/test/test-ssh
            dbus-run-session containers/flatpak/test/test-browser
          '
