# type check the python codebase (ty)
[group('check')]
@typecheck:
    ty check

# format the entire repository
[group('check')]
@fmt:
    treefmt

# lint the python code (ruff)
[group('check')]
@lint:
    ruff check

# lint and fix the python code
[group('check')]
@lint-fix:
    ruff check --fix

# perform static code analysis (format, lint, typecheck)
[group('check')]
@check: fmt lint typecheck
    @echo "Code analysis complete"

[group('ci')]
@typecheck-ci:
    ty check

[group('ci')]
@fmt-ci:
    treefmt --ci

[group('ci')]
@lint-ci:
    ruff check --output-format=github

[group('ci')]
@ci: fmt-ci lint-ci typecheck-ci
    @echo "Code analysis complete"

# run the fast suite without Docker or an Azure namespace
[group('test')]
@test-offline *args:
    pytest -m "not connected" {{ args }}

# collect offline coverage in CI without producing a per-job report
@test-offline-ci *args:
    pytest -m "not connected" --cov --cov-report= --cov-fail-under=0 {{ args }}

# enforce the coverage gate using only the offline suite
[group('test')]
@test-offline-cov:
    pytest -m "not connected" --cov --cov-report=term-missing

# run only the emulator-backed tests
[group('test')]
@test-connected *args:
    PYTEST_ADDOPTS={{ quote("-m connected " + args) }} process-compose --no-server up --tui=false tests

# collect connected coverage in CI without producing a per-job report
@test-connected-ci *args:
    pytest -m connected -n 2 --cov --cov-report= --cov-fail-under=0 {{ args }}

@coverage-ci:
    coverage combine coverage
    coverage report

# start the Service Bus emulator and its SQL Edge backing store
[group('infra')]
@up:
    process-compose --use-uds --unix-socket "$PC_SOCKET_PATH" up --detached servicebus-emulator

# stop the emulator
[group('infra')]
@down:
    process-compose --use-uds --unix-socket "$PC_SOCKET_PATH" down

# tail the emulator logs
[group('infra')]
@logs:
    process-compose --use-uds --unix-socket "$PC_SOCKET_PATH" process start servicebus-logs
    process-compose --use-uds --unix-socket "$PC_SOCKET_PATH" attach

# regenerate the emulator's entity pool (tests/infra/servicebus-config.json)
[group('infra')]
@gen-config:
    python tests/infra/generate_config.py

# publish and consume one message using the local emulator
[group('example')]
@example:
    process-compose --no-server up --tui=false example

# send a request and receive its correlated reply using the local emulator
[group('example')]
@example-request-reply:
    process-compose --no-server up --tui=false example-request-reply

# publish and consume MessagePack using a custom codec
[group('example')]
@example-messagepack:
    process-compose --no-server up --tui=false example-messagepack

# run the test suite
# `-n 2`, not `-n auto`: the emulator caps a namespace at 10 concurrent
# connections, and each worker holds several.
[group('test')]
@test *args:
    PYTEST_ADDOPTS={{ quote(args) }} process-compose --no-server up --tui=false tests

# run the tests with a coverage report
[group('test')]
@test-cov:
    process-compose --no-server up --tui=false test-coverage

# Smoke test the built wheel in an isolated environment.
[group('build & publish')]
@smoke-wheel:
    uv run --isolated --no-project --with dist/*.whl python -c "from faststream_azure_servicebus import ServiceBusBroker; assert ServiceBusBroker"

# Smoke test the built source distribution in an isolated environment.
[group('build & publish')]
@smoke-sdist:
    uv run --isolated --no-project --with dist/*.tar.gz python -c "from faststream_azure_servicebus import ServiceBusBroker; assert ServiceBusBroker"

[group('build & publish')]
@smoke-dist: smoke-wheel smoke-sdist

# Remove built distributions so smoke tests only see fresh artifacts.
[group('build & publish')]
@clean-dist:
    mkdir -p dist
    find dist -type f \( -name "*.whl" -o -name "*.tar.gz" \) -delete

# build the distribution
[group('build & publish')]
@build: clean-dist
    uv build

# What the release workflow runs before publishing.
[group('build & publish')]
@release-ci: ci build smoke-dist
