name: Unit Tests

on:
  # 指向 v2 的 PR 与推送都跑全量单测，作为合并门禁
  pull_request:
    branches:
      - v2
  push:
    branches:
      - v2
  # 允许手动触发
  workflow_dispatch:

permissions:
  contents: read

concurrency:
  group: unit-tests-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  pytest:
    runs-on: ubuntu-latest
    name: Unit Tests

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'
          cache: 'pip'

      - name: Cache pip dependencies
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.in', '**/requirements.txt') }}
          restore-keys: |
            ${{ runner.os }}-pip-

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip setuptools wheel
          # 用 requirements.in 还原 CI / 全新环境（含 pytest~=8.4 与 moviepilot-rust 等可选扩展），
          # 与本地"干净 venv 复现"一致；测试运行器 pytest 已在 requirements.in 中声明。
          pip install -r requirements.in

      - name: Run tests
        run: |
          # tests/run.py 以 pytest 跑 tests 全量；tests/conftest.py 在收集前把 CONFIG_DIR
          # 指向临时库并建表，测试杜绝真实网络/外部服务（详见 docs/testing.md）。
          python tests/run.py
