name: Server Test Suite

on:
  pull_request:
    branches:
      - main
    paths:
      - apps/api/**
  # schedule:
  #   - cron: '0 */4 * * *'

env:
  BULL_AUTH_KEY: ${{ secrets.BULL_AUTH_KEY }}
  HOST: ${{ secrets.HOST }}
  POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
  POSTHOG_HOST: ${{ secrets.POSTHOG_HOST }}
  OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
  PORT: ${{ secrets.PORT }}
  REDIS_URL: ${{ secrets.REDIS_URL }}
  SCRAPING_BEE_API_KEY: ${{ secrets.SCRAPING_BEE_API_KEY }}
  SUPABASE_ANON_TOKEN: ${{ secrets.SUPABASE_ANON_TOKEN }}
  SUPABASE_SERVICE_TOKEN: ${{ secrets.SUPABASE_SERVICE_TOKEN }}
  SUPABASE_URL: ${{ secrets.SUPABASE_URL }}
  SUPABASE_REPLICA_URL: ${{ secrets.SUPABASE_REPLICA_URL }}
  TEST_API_KEY: ${{ secrets.TEST_API_KEY }}
  FIRE_ENGINE_BETA_URL: ${{ secrets.FIRE_ENGINE_BETA_URL }}
  USE_DB_AUTHENTICATION: true
  SERPER_API_KEY: ${{ secrets.SERPER_API_KEY }}
  ENV: ${{ secrets.ENV }}
  RUNPOD_MU_POD_ID: ${{ secrets.RUNPOD_MU_POD_ID }}
  RUNPOD_MU_API_KEY: ${{ secrets.RUNPOD_MU_API_KEY }}
  USE_GO_MARKDOWN_PARSER: true

jobs:
  test:
    name: Run tests
    runs-on: ubuntu-latest
    services:
      redis:
        image: redis
        ports:
          - 6379:6379
    steps:
      - uses: actions/checkout@v3
      - name: Tailscale
        uses: tailscale/github-action@v3
        with:
          oauth-client-id: ${{ secrets.TS_OAUTH_CLIENT_ID }}
          oauth-secret: ${{ secrets.TS_OAUTH_SECRET }}
          tags: tag:ci
      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 10
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: "20"
          cache: "pnpm"
          cache-dependency-path: './apps/api/pnpm-lock.yaml'
      - name: Install dependencies
        run: pnpm install
        working-directory: ./apps/api
      - name: Set up Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.19'
          cache-dependency-path: ./apps/api/sharedLibs/go-html-to-md/go.sum
      - name: Build go-html-to-md
        run: |
          go mod tidy
          go build -o html-to-markdown.so -buildmode=c-shared html-to-markdown.go
          chmod +x html-to-markdown.so
        working-directory: ./apps/api/sharedLibs/go-html-to-md
      - name: Start the application
        run: npm start > api.log 2>&1 &
        working-directory: ./apps/api
        id: start_app
      - name: Start worker
        run: npm run workers > worker.log 2>&1 &
        working-directory: ./apps/api
        id: start_workers
      - name: Start index worker
        run: npm run index-worker &
        working-directory: ./apps/api
        id: start_index_worker
      - name: Wait for API
        run: pnpx wait-on tcp:3002 -t 15s
      - name: Run snippet tests
        run: |
          npm run test:snips
        working-directory: ./apps/api