name: "Test @notesnook/core"

on:
  workflow_dispatch:
  push:
    branches:
      - "master"
    paths:
      - "packages/core/**"
      # re-run workflow if workflow file changes
      - ".github/workflows/core.tests.yml"
  pull_request_target:
    branches:
      - "master"
    paths:
      - "packages/core/**"
      # re-run workflow if workflow file changes
      - ".github/workflows/core.tests.yml"
    types:
      - "ready_for_review"
      - "opened"
      - "synchronize"
      - "reopened"

jobs:
  authorize:
    environment: ${{ github.event_name == 'pull_request_target' &&
      github.event.pull_request.head.repo.full_name != github.repository &&
      'external' || 'internal' }}
    runs-on: ubuntu-latest
    steps:
      - run: echo true

  test:
    needs: authorize
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ github.event.pull_request.head.sha || github.ref }}

      - name: Setup Node
        uses: ./.github/actions/setup-node-with-cache

      - name: Prepare environment
        run: |
          npm ci --ignore-scripts --prefer-offline --no-audit
          npm run bootstrap -- --scope=core
      - name: Add environment variables
        run: |
          echo "USER_EMAIL=${{ secrets.USER_EMAIL }}" >> $GITHUB_ENV
          echo "USER_PASSWORD=${{ secrets.USER_PASSWORD }}" >> $GITHUB_ENV
          echo "USER_TOTP_SECRET=${{ secrets.USER_TOTP_SECRET }}" >> $GITHUB_ENV
          echo "USER_HASHED_PASSWORD=${{ secrets.USER_HASHED_PASSWORD }}" >> $GITHUB_ENV

      - name: Run all @notesnook/core tests
        run: npm run tx @notesnook/core:test:e2e
