name: Test Suite

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main, develop]

jobs:
  frontend-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v2
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'pnpm'
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
      - name: Run tests
        run: pnpm test
      - name: Generate coverage
        run: pnpm test:coverage
      - name: Upload coverage
        uses: codecov/codecov-action@v3
        with:
          files: ./coverage/coverage-final.json

  api-tests:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres:14
        env:
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: sankofa_test
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
        ports:
          - 5432:5432
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v2
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'pnpm'
      - name: Install dependencies
        working-directory: ./api
        run: pnpm install --frozen-lockfile
      - name: Run migrations
        working-directory: ./api
        run: pnpm run db:migrate
        env:
          DB_HOST: localhost
          DB_PORT: 5432
          DB_NAME: sankofa_test
          DB_USER: postgres
          DB_PASSWORD: postgres
      - name: Run tests
        working-directory: ./api
        run: pnpm test
        env:
          DB_HOST: localhost
          DB_PORT: 5432
          DB_NAME: sankofa_test
          DB_USER: postgres
          DB_PASSWORD: postgres

  blockchain-tests:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v2
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'pnpm'
      - name: Install dependencies
        working-directory: ./blockchain
        run: pnpm install --frozen-lockfile
      - name: Run tests
        working-directory: ./blockchain
        run: pnpm test

