name: CI

on:
  workflow_dispatch:
  pull_request:
  push:
    branches:
      - main
  schedule:
    - cron: '0 3 * * 0' # every Sunday at 3am

env:
  CI: true

jobs:
  lint_js:
    name: Lint JS
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: pnpm
      - name: Install Dependencies
        run: pnpm install --frozen-lockfile
      - name: ESLint
        run: pnpm lint

  test_type_checking:
    name: 'Tests: Type Check'
    timeout-minutes: 5
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: pnpm
      - name: Install Dependencies
        run: pnpm install --frozen-lockfile
      - run: pnpm tsc --noEmit
        working-directory: tests

  tests:
    name: Tests
    timeout-minutes: 5
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        # how to say "not these" so we don't miss anything?
        # waiting on a an api from vitest for querying
        # the list of tests ahead of time before running them.
        #
        # https://github.com/vitest-dev/vitest/issues/2901
        #
        # It would be great if vitest had a flag to give us the JSON of all the tests,
        # so we could be sure we don't miss anything
        # and then generate this list from a previous C.I. job
        slow-test:
          # flags
          - addon-location
          - test-app-location
          - addon-only

          # existing monorepo
          - monorepo with npm
          - monorepo with pnpm

          # build-only tests for testing if the rollup config works at all
          - rollup-build
          - declarations-configuration

    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: pnpm
      - name: Install Dependencies
        run: pnpm install --frozen-lockfile
      - run: pnpm vitest --testNamePattern "${{ matrix.slow-test }}"
        working-directory: tests


  defaults_tests:
    name: "Slow tests: ${{ matrix.slow-test }}"
    timeout-minutes: 5
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        slow-test:
          - defaults with npm
          - defaults with pnpm
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: pnpm
      - name: Install Dependencies
        run: pnpm install --frozen-lockfile
      - run: pnpm vitest --testNamePattern "${{ matrix.slow-test }}"
        working-directory: tests

  typescript_tests:
    name: "Slow tests: ${{ matrix.slow-test }}"
    timeout-minutes: 5
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        slow-test:
          - typescript with npm
          - typescript with pnpm
    steps:
      - uses: actions/checkout@v4
      - uses: pnpm/action-setup@v4
        with:
          version: 8
      - uses: actions/setup-node@v4
        with:
          node-version: 18
          cache: pnpm
      - name: Install Dependencies
        run: pnpm install --frozen-lockfile
      - run: pnpm vitest --testNamePattern "${{ matrix.slow-test }}"
        working-directory: tests
