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@v1
        with:
          bun-version: '1.1.4'
      - name: 'Build tests'
        run: pnpm exec vite build -c .config/vite.bun.ts
      - name: 'Run tests'
        run: cd dist/tests && bun test
  
  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.0'
      - name: 'Build tests'
        run: pnpm exec vite build -c .config/vite.deno.ts
      - name: 'Run tests'
        run: cd dist/tests && deno test -A --unstable-ffi --node-modules-dir=false
  
  test-web:
    runs-on: docker
    container: mcr.microsoft.com/playwright:v1.42.1

    strategy:
      matrix:
        browser: [chromium, firefox]
    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 --browser.name=${{ matrix.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
        env:
          API_ID: ${{ secrets.TELEGRAM_API_ID }}
          API_HASH: ${{ secrets.TELEGRAM_API_HASH }}
          SESSION_DC1: ${{ secrets.SESSION_DC1 }}
          SESSION_DC2: ${{ secrets.SESSION_DC2 }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: 'cd e2e && pnpm run test:all'
  e2e-deno:
    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
      - uses: https://github.com/denoland/setup-deno@v1
        with:
          deno-version: '2.0'
      - name: Run end-to-end tests under Deno
        env:
          API_ID: ${{ secrets.TELEGRAM_API_ID }}
          API_HASH: ${{ secrets.TELEGRAM_API_HASH }}
          SESSION_DC1: ${{ secrets.SESSION_DC1 }}
          SESSION_DC2: ${{ secrets.SESSION_DC2 }}
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: 'cd e2e && deno task test:all'