mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-11-03 22:32:27 +08:00
288 lines
9.6 KiB
YAML
288 lines
9.6 KiB
YAML
name: Unit Tests
|
|
|
|
on:
|
|
pull_request:
|
|
types: [opened, ready_for_review, synchronize]
|
|
paths:
|
|
- 'src/langbot/**'
|
|
- 'tests/**'
|
|
- '.github/workflows/run-tests.yml'
|
|
- 'pyproject.toml'
|
|
- 'uv.lock'
|
|
- 'run_tests.sh'
|
|
- 'scripts/test-*.sh'
|
|
- 'scripts/test-*.py'
|
|
push:
|
|
branches:
|
|
- master
|
|
- develop
|
|
- 'feat/**'
|
|
# No path filter on push: every push to the branches above runs the
|
|
# full unit-test suite. feat/** branches in particular must be tested
|
|
# on every push (they accumulate large changes before a PR exists).
|
|
|
|
jobs:
|
|
test:
|
|
name: Unit Tests
|
|
runs-on: ubuntu-latest
|
|
strategy:
|
|
matrix:
|
|
python-version: ['3.11', '3.12', '3.13']
|
|
fail-fast: false
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Python ${{ matrix.python-version }}
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: ${{ matrix.python-version }}
|
|
|
|
- name: Install uv
|
|
uses: astral-sh/setup-uv@v4
|
|
|
|
- name: Install dependencies
|
|
run: uv sync --dev --frozen
|
|
|
|
- name: Run unit + smoke tests
|
|
if: matrix.python-version != '3.12'
|
|
run: uv run pytest tests/unit_tests/ tests/smoke/ -q --tb=short -rs --durations=10
|
|
|
|
- name: Run unit + smoke tests with coverage
|
|
if: matrix.python-version == '3.12'
|
|
run: |
|
|
uv run pytest tests/unit_tests/ tests/smoke/ \
|
|
--cov=langbot --cov-branch --cov-report=xml --cov-report=json --cov-report=term-missing \
|
|
--cov-fail-under=0 -q --tb=short -rs --durations=10
|
|
# Preserve the existing line-coverage gate. With --cov-branch,
|
|
# pytest-cov's aggregate total combines statements and branches.
|
|
python - <<'PY'
|
|
import json
|
|
totals = json.load(open('coverage.json'))['totals']
|
|
line_coverage = 100 * totals['covered_lines'] / totals['num_statements']
|
|
assert line_coverage >= 18, f'Line coverage {line_coverage:.2f}% is below 18%'
|
|
PY
|
|
|
|
- name: Report source coverage and complexity
|
|
if: always() && matrix.python-version == '3.12' && hashFiles('coverage.json') != ''
|
|
env:
|
|
BASE_SHA: ${{ github.event.pull_request.base.sha || github.event.before }}
|
|
run: |
|
|
args=()
|
|
if [[ "$BASE_SHA" =~ ^[0-9a-f]{40}$ && "$BASE_SHA" != 0000000000000000000000000000000000000000 ]]; then
|
|
git fetch --no-tags --depth=1 origin "$BASE_SHA"
|
|
args+=(--base-ref "$BASE_SHA")
|
|
fi
|
|
uv run python scripts/test-complexity.py \
|
|
--json complexity.json --summary complexity.md --coverage-json coverage.json "${args[@]}"
|
|
cat complexity.md >> "$GITHUB_STEP_SUMMARY"
|
|
|
|
- name: Upload source quality reports
|
|
if: always() && matrix.python-version == '3.12'
|
|
uses: actions/upload-artifact@v4
|
|
with:
|
|
name: source-quality-reports
|
|
path: |
|
|
coverage.json
|
|
coverage.xml
|
|
complexity.json
|
|
complexity.md
|
|
if-no-files-found: ignore
|
|
|
|
- name: Upload coverage to Codecov
|
|
if: matrix.python-version == '3.12'
|
|
uses: codecov/codecov-action@v5
|
|
with:
|
|
files: ./coverage.xml
|
|
flags: unit-tests
|
|
name: coverage-report
|
|
fail_ci_if_error: false
|
|
env:
|
|
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
|
|
|
|
- name: Test Summary
|
|
if: always()
|
|
run: |
|
|
echo "## Unit Tests Results" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "Python Version: ${{ matrix.python-version }}" >> $GITHUB_STEP_SUMMARY
|
|
echo "Test Status: ${{ job.status }}" >> $GITHUB_STEP_SUMMARY
|
|
|
|
integration:
|
|
name: Fast Integration Tests
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Python
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: '3.12'
|
|
|
|
- name: Install uv
|
|
uses: astral-sh/setup-uv@v4
|
|
|
|
- name: Install dependencies
|
|
run: uv sync --dev --frozen
|
|
|
|
- name: Run fast integration tests
|
|
run: uv run pytest tests/integration/ -m "not slow" -q --tb=short -rs --durations=10
|
|
|
|
- name: Integration Test Summary
|
|
if: always()
|
|
run: |
|
|
echo "## Integration Tests Results" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "Test Status: ${{ job.status }}" >> $GITHUB_STEP_SUMMARY
|
|
|
|
e2e:
|
|
name: E2E Startup Tests
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Python
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: '3.12'
|
|
|
|
- name: Install uv
|
|
uses: astral-sh/setup-uv@v4
|
|
|
|
- name: Install dependencies
|
|
run: uv sync --dev --frozen
|
|
|
|
- name: Run E2E startup tests
|
|
run: uv run pytest tests/e2e --ignore=tests/e2e/test_local_runner_fake_provider.py -q --tb=short -rs --durations=10
|
|
|
|
- name: E2E Test Summary
|
|
if: always()
|
|
run: |
|
|
echo "## E2E Startup Test Results" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "Test Status: ${{ job.status }}" >> $GITHUB_STEP_SUMMARY
|
|
|
|
box-integration:
|
|
name: Box Integration Tests
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Python
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: '3.12'
|
|
|
|
- name: Install uv
|
|
uses: astral-sh/setup-uv@v4
|
|
|
|
- name: Install dependencies
|
|
run: uv sync --dev --frozen
|
|
|
|
- name: Check Docker runtime
|
|
run: docker info
|
|
|
|
- name: Run Box integration tests
|
|
run: uv run pytest tests/integration_tests -q --tb=short -rs --durations=10
|
|
|
|
- name: Box Integration Test Summary
|
|
if: always()
|
|
run: |
|
|
echo "## Box Integration Test Results" >> $GITHUB_STEP_SUMMARY
|
|
echo "" >> $GITHUB_STEP_SUMMARY
|
|
echo "Test Status: ${{ job.status }}" >> $GITHUB_STEP_SUMMARY
|
|
|
|
local-agent:
|
|
name: Local Agent Contract E2E
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
env:
|
|
LANGBOT_E2E_REQUIRE_LOCAL_AGENT: '1'
|
|
LANGBOT_E2E_LOCAL_AGENT_PACKAGE: ${{ github.workspace }}/.pytest_cache/local-agent/LocalAgent.lbpkg
|
|
|
|
steps:
|
|
- name: Checkout code
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set up Python
|
|
uses: actions/setup-python@v5
|
|
with:
|
|
python-version: '3.12'
|
|
|
|
- name: Install uv
|
|
uses: astral-sh/setup-uv@v4
|
|
|
|
- name: Install dependencies
|
|
run: uv sync --dev --frozen
|
|
|
|
# Resolve every run, before consulting the archive cache. The latest
|
|
# pointer is never cached or replaced with a stale successful version.
|
|
- name: Resolve latest LocalAgent from Space
|
|
id: local-agent
|
|
run: uv run python scripts/test-local-agent-fixture.py resolve local-agent-resolution.json --github-output "$GITHUB_OUTPUT"
|
|
|
|
- name: Cache resolved Space artifact
|
|
uses: actions/cache@v4
|
|
with:
|
|
path: ${{ github.workspace }}/.pytest_cache/local-agent
|
|
key: local-agent-space-${{ steps.local-agent.outputs.cache_key }}
|
|
|
|
# Revalidate every cache hit against this run's metadata. Provisioning
|
|
# failures are reported separately from Core/runtime contract failures.
|
|
- name: Provision and verify resolved LocalAgent
|
|
run: uv run python scripts/test-local-agent-fixture.py fetch local-agent-resolution.json "$LANGBOT_E2E_LOCAL_AGENT_PACKAGE"
|
|
|
|
- name: Check Docker runtime for LocalAgent Box probe
|
|
run: docker info
|
|
|
|
- name: Run required LocalAgent contract tests
|
|
run: |
|
|
uv run pytest tests/e2e/test_local_runner_fake_provider.py \
|
|
-q --tb=short -rs --durations=10 --junitxml=local-agent-results.xml
|
|
python - <<'PY'
|
|
import xml.etree.ElementTree as ET
|
|
suites = ET.parse('local-agent-results.xml').getroot().iter('testsuite')
|
|
counts = [suite.attrib for suite in suites]
|
|
assert sum(int(suite['tests']) for suite in counts) > 0, 'No LocalAgent tests ran'
|
|
assert sum(int(suite.get('skipped', 0)) for suite in counts) == 0, 'LocalAgent tests must not skip in CI'
|
|
PY
|
|
|
|
- name: Upload LocalAgent test results
|
|
if: always()
|
|
uses: actions/upload-artifact@v4
|
|
with:
|
|
name: local-agent-test-results
|
|
path: |
|
|
local-agent-results.xml
|
|
local-agent-resolution.json
|
|
if-no-files-found: ignore
|
|
|
|
- name: LocalAgent Test Summary
|
|
if: always()
|
|
run: |
|
|
echo "## Local Agent Contract E2E" >> $GITHUB_STEP_SUMMARY
|
|
echo "Latest Space artifact: langbot-team/LocalAgent (version/checksum in resolution artifact)" >> $GITHUB_STEP_SUMMARY
|
|
echo "Missing fixtures fail this job; optional local source checkouts are not used." >> $GITHUB_STEP_SUMMARY
|
|
echo "Test Status: ${{ job.status }}" >> $GITHUB_STEP_SUMMARY
|
|
|
|
# Preserve the existing check name for branch protection without running the
|
|
# unit suite a fourth time. Python 3.12 above owns the unchanged 18% gate.
|
|
coverage:
|
|
name: Coverage Gate
|
|
runs-on: ubuntu-latest
|
|
needs: [test, integration]
|
|
if: always()
|
|
steps:
|
|
- name: Verify test and coverage results
|
|
run: |
|
|
test "${{ needs.test.result }}" = success
|
|
test "${{ needs.integration.result }}" = success
|
|
echo "Coverage threshold: 18%, enforced in Unit Tests (3.12)." >> $GITHUB_STEP_SUMMARY
|