name: Tests

on:
  push:
    branches: [ master ]
    paths-ignore:
      - '*.md' # ignore changes to readmes
  pull_request:
    branches: [ master ]

jobs:
  lint: 
    runs-on: node22
    if: github.actor != 'desu-bot' # do not run after release
    steps:
      - uses: actions/checkout@v4
      - uses: ./.forgejo/actions/init
      - uses: https://github.com/denoland/setup-deno@v1
        with:
          deno-version: '2.0'
      - name: 'TypeScript'
        run: pnpm run lint:tsc:ci
      - name: 'ESLint'
        run: pnpm run lint:ci
      - name: 'Circular dependencies'
        run: pnpm run lint:dpdm
      - name: 'deno publish --dry-run'
        run: pnpm exec fuman-build jsr gen-deno-workspace --with-dry-run

  test-node:
    strategy:
      matrix:
        node-version: [node18, node20, node22]
    runs-on: ${{ matrix.node-version }}
    steps:
      - uses: actions/checkout@v4
      - uses: ./.forgejo/actions/init
      - name: 'Run tests'
        run: pnpm run test:ci
  
  test-bun:
    runs-on: node22
    steps:
      - uses: actions/checkout@v4
      - uses: ./.forgejo/actions/init
      - uses: oven-sh/setup-bun@v2
        with:
          bun-version: '1.2.4'
      - name: 'Run tests'
        run: pnpm run test:bun
  
  test-deno:
    runs-on: node22
    steps:
      - uses: actions/checkout@v4
      - uses: ./.forgejo/actions/init
      - uses: https://github.com/denoland/setup-deno@v1
        with:
          deno-version: '2.2.2'
      - name: 'Run tests'
        run: pnpm run test:deno
  
  test-web:
    runs-on: docker
    container: mcr.microsoft.com/playwright:v1.42.1

    steps:
      - uses: actions/checkout@v4
      - run: apt-get update && apt-get install -y make gcc g++
      - uses: ./.forgejo/actions/init
      - name: 'Run tests'
        # i wish we didn't have to do this, but vitest in browser is very flaky
        # see: https://github.com/vitest-dev/vitest/issues/4173
        uses: https://github.com/nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 30
          command: pnpm run test:browser

  e2e:
    runs-on: node22
    needs: [lint, test-node, test-web, test-bun, test-deno]
    permissions:
      contents: read
      actions: write
    steps:
      - uses: actions/checkout@v4
      - uses: ./.forgejo/actions/init
      - name: Run end-to-end tests
        uses: https://github.com/nick-fields/retry@v2
        with:
          max_attempts: 3
          timeout_minutes: 10
          command: |
            set -eauo pipefail
            export API_ID=${{ secrets.TELEGRAM_API_ID }}
            export API_HASH=${{ secrets.TELEGRAM_API_HASH }}
            export SESSION_DC1=${{ secrets.SESSION_DC1 }}
            export SESSION_DC2=${{ secrets.SESSION_DC2 }}
            export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
            cd e2e
            pnpm run test:all
  # for some reason it hangs indefinitely in ci lol
  # e2e-deno:
  #   runs-on: node22
  #   needs: [e2e]
  #   permissions:
  #     contents: read
  #     actions: write
  #   steps:
  #     - uses: actions/checkout@v4
  #     - uses: ./.forgejo/actions/init
  #     - uses: https://github.com/denoland/setup-deno@v1
  #       with:
  #         deno-version: '2'
  #     - name: Run end-to-end tests under Deno
  #       uses: https://github.com/nick-fields/retry@v2
  #       with:
  #         max_attempts: 3
  #         timeout_minutes: 10
  #         command: |
  #           set -eauo pipefail
  #           export API_ID=${{ secrets.TELEGRAM_API_ID }}
  #           export API_HASH=${{ secrets.TELEGRAM_API_HASH }}
  #           export SESSION_DC1=${{ secrets.SESSION_DC1 }}
  #           export SESSION_DC2=${{ secrets.SESSION_DC2 }}
  #           export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
  #           cd e2e
  #           deno task test:all