# CI for CERNAL on GitHub — iGEM 2026, Tel Aviv University.
#
# Why this file exists alongside .gitlab-ci.yml:
#   .gitlab-ci.yml is the iGEM Project Software deliverable (the competition asks for a
#   CI/CD pipeline configuration on iGEM GitLab) and it stays exactly where it is. GitHub
#   ignores it completely. Since the team actually collaborates on
#   github.com/zivbental/cernal_software, without this file every push and every pull
#   request lands unchecked — which is how seven people working in parallel silently fork
#   the shared scoring and folding code without anyone noticing.
#
# This file is therefore the real gate. It runs the same commands as .gitlab-ci.yml, which
# are the same commands `./do lint` and `./do test` run locally, so "green here" and "green
# on my laptop" mean the same thing. If you change one of the two CI files, change the other.
#
# Turn it into an actual gate: Settings -> Branches -> add a rule for `main` requiring the
# `backend` and `frontend` checks to pass before merging.

name: CI

on:
  pull_request:
  push:
    branches: [main]

# A new push to the same branch cancels the previous, still-running check.
concurrency:
  group: ci-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  # Backend: lint, format, Django system checks and the full pytest suite.
  # Runs against LocalEngine, the only engine — so the suite needs the scientific
  # stack, which `uv sync` provides (viennarna ships a prebuilt wheel) and the bundled
  # reference transcriptomes, which are committed. Real folding makes this slower than
  # the removed MockEngine was; that is the cost of the suite testing real science.
  backend:
    name: backend (ruff + django check + pytest)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # setup-uv reads .python-version (3.13) and provisions that interpreter itself.
      # Do NOT add a setup-python step: pyproject requires >=3.13,<3.14 and a runner
      # default of 3.12 or 3.14 would fail the install rather than the tests.
      - uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true

      # `--locked` fails the job if uv.lock has drifted from pyproject.toml instead of
      # silently re-resolving. That drift is caught here, by one person, rather than by
      # seven simultaneous merge conflicts on a 30k-line generated file. If this step
      # fails, run `uv lock` locally and commit the result — do not downgrade the flag.
      - run: uv sync --locked --extra dev

      - run: uv run ruff check .
      - run: uv run ruff format --check .
      - run: uv run python manage.py check
      - run: uv run pytest -q

  # Python client conformance (docs/public-api.md §11.4): submits
  # clients/fixtures/design_request.json against a real, running LocalEngine server
  # (pytest-django's live_server) through the published cernal package's own HTTP code
  # path, and checks the result columns against clients/fixtures/expected_columns.json.
  # `--with` installs the client and its one runtime dependency (requests) into an
  # ephemeral overlay — the main project never depends on requests.
  client-python:
    name: client-python (conformance)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/setup-uv@v5
        with:
          enable-cache: true
      - run: uv sync --locked --extra dev
      - run: >
          uv run --with requests --with 'cernal @ ./clients/python'
          pytest clients/python/tests -c clients/python/tests/pytest.ini -q

  # Frontend: type-check, lint, the server-render regression test, and a real build.
  # `npm run check` is "tsc --noEmit && eslint . && node e2e/render-logic.mjs" — the last
  # step renders the results views headlessly via esbuild + react-dom/server, so no
  # browser download is required and the job stays fast.
  frontend:
    name: frontend (typecheck + eslint + build)
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: frontend
    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: npm
          # This path is resolved from the repository root, not from working-directory.
          cache-dependency-path: frontend/package-lock.json

      # `npm ci` (not `npm install`) so the lockfile is honoured exactly and the run
      # cannot quietly pick up a newer transitive dependency than anyone tested against.
      - run: npm ci

      # src/routeTree.gen.ts is gitignored — TanStack Router's Vite plugin writes it as a
      # side effect of a real `vite build`, not of `tsc` alone. Both `npm run check` and
      # `npm run build` start with a bare `tsc --noEmit`, so on a fresh checkout (nothing
      # has ever run `vite build` here) that first `tsc` fails before the file exists:
      # "Cannot find module './routeTree.gen'". `build:fast` is `vite build` with no
      # leading tsc, so it generates the file without caring that it's missing yet.
      - run: npm run build:fast
      - run: npm run check
      - run: npm run build
