name: JS

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

jobs:
  format:
    name: ESLint & Prettier
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: 'npm'
      - run: npm ci
      - run: npm run check-format
      - run: npm run lint

  type-check:
    name: Check Types
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: 'npm'
      - run: npm ci
      - run: npm run check-types

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: 'npm'
      - run: npm ci
      - run: npm run build

  test:
    name: Test
    runs-on: ubuntu-latest
    container: node
    services:
      postgres:
        image: postgres
        env:
          POSTGRES_PASSWORD: postgres
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: 18.x
          cache: 'npm'
      - run: npm ci
      - name: Run tests
        # Github will have started the db in the container for us.
        run: npm run test-nostartdb
        env:
          DATABASE_URL: 'postgresql://postgres:postgres@postgres:5432/test'
