name: Test Unit

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

jobs:
  generate-packages:
    name: List Go Packages
    runs-on: ubuntu-latest
    timeout-minutes: 15
    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
        uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
        with:
          go-version-file: '.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
    timeout-minutes: 15
    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
        uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
        with:
          go-version-file: '.go-version'
          check-latest: true

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

  test-ui-unit:
    runs-on: ubuntu-latest
    timeout-minutes: 15

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

      - name: Enable corepack
        run: corepack enable

      - 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
        working-directory: ./webui
        env:
          VITE_APP_BASE_API_URL: "/api"
        run: |
          yarn install
          yarn test:unit:ci
