# SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
#
# SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only

# Builds the flutter frontend and runs tests

name: Flutter-dev

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

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        preset:
          - name: dev-flutter
            isAOT: false
            args: []
          # - name: dev-flutter-asan
          #   isAOT: false
          #   args: ["--asan"]
          # - name: dev-flutter-aot
          #   isAOT: true
          #   args: ["--aot"]
          # - name: dev-flutter-aot-asan
          #   isAOT: true
          #   args: ["--aot", "--asan"]

    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          submodules: true
          token: ${{ secrets.PAT_KDDW_REFERENCE_SCREENSHOTS }}

      - name: Install dependencies on Ubuntu
        run: |
          sudo apt update -qq
          sudo apt install libgtk-3-dev libglib2.0-dev ninja-build libspdlog-dev lld xvfb -y

      - uses: subosito/flutter-action@v2
        with:
          flutter-version: "3.x"
          channel: "stable"

      - name: Configure project ${{ matrix.preset.name }}
        run: cmake -S . -B ./build-${{ matrix.preset.name }} --preset ${{ matrix.preset.name }}

      - name: Build Project ${{ matrix.preset.name }}
        run: cmake --build ./build-${{ matrix.preset.name }}

      - name: Build the example
        run: |
          cd examples/flutter
          flutter build linux

      - name: Run tests
        run: |
          cd ${{ github.workspace }}/src/flutter/dart
          flutter pub get
          flutter test
        env:
          KDDW_BINDINGS_LIB: ${{ github.workspace }}/build-dev-flutter/lib/libkddockwidgets.so

      - name: Setup git author name
        run: |
          git config --global user.email "kddw@actions"
          git config --global user.name "KDDW Actions"

      # the reference is always latest main, as submodule sha1 might be lagging
      # as it's updated nightly
      - name: checkout latest main of ref images
        run: |
          cd tests/reference-images
          git checkout main

      - name: Run integration tests
        run: |
          cd tests/flutter/
          flutter pub get
          xvfb-run flutter test -d linux integration_test/ui_test.dart
        env:
          KDDW_BINDINGS_LIB: ${{ github.workspace }}/build-dev-flutter/lib/libkddockwidgets.so

      - name: Compare against reference images
        shell: bash
        if: github.event_name == 'pull_request'
        run: |
          ./tests/reference-images/compare_images.sh
        env:
          PAT: ${{ secrets.PAT_KDDW_REFERENCE_SCREENSHOTS }}
          GITHUB_EVENT_PATH: ${{ github.event_path }}
          GH_TOKEN: ${{ github.token }}
