name: Test Unit

on:
  pull_request:
    branches:
      - '*'
    paths-ignore:
      - 'docs/**'
      - '**.md'
      - 'script/gcg/**'

env:
  GO_VERSION: '1.25'

jobs:

  generate-packages:
    name: List Go Packages
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - name: Check out code
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          fetch-depth: 0

      - name: Set up Go ${{ env.GO_VERSION }}
        uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
        with:
          go-version: ${{ env.GO_VERSION }}
          check-latest: true

      - name: Generate matrix
        id: set-matrix
        run: |
          matrix_output=$(go run ./internal/testsci/genmatrix.go)
          echo "$matrix_output"
          echo "$matrix_output" >> $GITHUB_OUTPUT

  test-unit:
    runs-on: ubuntu-latest
    needs: generate-packages
    strategy:
      matrix:
        package: ${{ fromJson(needs.generate-packages.outputs.matrix) }}

    steps:
      - name: Check out code
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          fetch-depth: 0

      - name: Set up Go ${{ env.GO_VERSION }}
        uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
        with:
          go-version: ${{ env.GO_VERSION }}
          check-latest: true

      - name: Avoid generating webui
        run: touch webui/static/index.html

      - name: Tests
        run: |
          go test -v -parallel 8 ${{ matrix.package.group }}

  test-ui-unit:
    runs-on: ubuntu-latest

    steps:
      - name: Check out code
        uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
        with:
          fetch-depth: 0

      - name: Set up Node.js ${{ env.NODE_VERSION }}
        uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6.2.0
        with:
          node-version-file: webui/.nvmrc
          cache: 'yarn'
          cache-dependency-path: webui/yarn.lock

      - name: Setup safe-chain
        run: |
          npm i -g @aikidosec/safe-chain
          safe-chain setup-ci

      - name: UI unit tests
        run: |
          yarn --cwd webui install --ignore-scripts
          yarn --cwd webui test:unit:ci
