# Development verbs for the zarr-indexing package. Recipes run with this
# directory as the working directory regardless of where `just` is invoked.

# Quoted arguments must survive delegation from the root Justfile.
set positional-arguments

# In the repository, recipes overlay the in-repo zarr (editable) so they run
# against the code beside them. pyproject.toml stays standalone, so from an
# unpacked sdist, where ../.. is not the repository, there is no overlay and
# the suites that need zarr skip.
repo_zarr := if path_exists("../../src/zarr/__init__.py") == "true" { "--with-editable ../.." } else { "" }

# List available recipes
default:
    @just --list

# zarr is not a dependency: repo_zarr supplies it, so the suites that read
# through zarr run here instead of skipping.
# Run the test suite; extra args are passed to pytest
test *args:
    uv run {{ repo_zarr }} --group test python -m pytest tests src/zarr_indexing "$@"

# TensorStore is the oracle for the parity suites, which skip without it. It
# ships binary wheels only, so it rides in as a run-time overlay rather than
# joining a dependency group; if a future Python lacks a tensorstore wheel,
# gate the CI job that calls this on the matrix version.
# Run the tensorstore parity suites; extra args are passed to pytest
test-tensorstore *args:
    uv run {{ repo_zarr }} --group test --with 'tensorstore>=0.1.84' python -m pytest tests/test_ndsel_tensorstore.py tests/test_tensorstore_parity.py "$@"

# Lint with the same invocation CI uses. Ruff is pinned to the repo-wide
# version in the root .pre-commit-config.yaml; bump together.
lint:
    uvx ruff@0.16.0 check .

# Type-check the package sources, documentation Python, and their contract tests
typecheck:
    uv run --group test --with pyright pyright

# Run these checks with the locally selected interpreter and dependencies;
# CI also exercises its configured Python-version matrix
check: lint typecheck test test-tensorstore docs-check

# Preview the changelog that the next release would generate
changelog-draft:
    uvx towncrier build --draft --version Unreleased

# Build this package's documentation site, warnings as errors
docs-check:
    env DISABLE_MKDOCS_2_WARNING=true uv run --group docs mkdocs build --strict

# With no argument, uses port 8000 if free, otherwise an ephemeral free port;
# an explicitly requested port is used as-is so a conflict fails loudly.
# Serve this package's documentation site
docs-serve port="":
    #!/usr/bin/env bash
    set -euo pipefail
    port="{{ port }}"
    if [ -z "$port" ]; then
        port=$(uv run --group docs python -c '
    import socket
    s = socket.socket()
    try:
        s.bind(("127.0.0.1", 8000))
    except OSError:
        s.close()
        s = socket.socket()
        s.bind(("127.0.0.1", 0))
    print(s.getsockname()[1])
    s.close()
    ')
    fi
    exec env DISABLE_MKDOCS_2_WARNING=true uv run --group docs mkdocs serve -a "localhost:$port"
