#!/usr/bin/make -f
.DEFAULT: help

ifeq ($(shell command -v uv 2>/dev/null),)
$(error uv is required. Install it with `brew install uv`, `winget install astral-sh.uv`, or https://astral.sh/uv/install.sh)
endif

.PHONY: help
help:
	@echo "test:		run unit tests"
	@echo "test-func:	run functional tests"
	@echo "typecheck:	run mypy over vncdotool"
	@echo "servers-up:	start the docker VNC test servers"
	@echo "servers-down:	stop the docker VNC test servers"
	@echo "test-servers:	smoke test the docker VNC test server fleet"
	@echo "test-os-server:	run functional tests against this OS's VNC server"
	@echo "test-api:	run the in-process vncdotool.api lifecycle suite"
	@echo "screenshots:	screenshot each running VNC test server into a gallery"
	@echo "goldens:	capture decoder golden fixtures from the fleet"
	@echo "scenes:		regenerate the committed scene PNGs from tests/goldens/scenes.py"
	@echo "bench:		time a decoder against a committed golden fixture"
	@echo "bench-record:	time it and append the run to the tracked bench.jsonl"
	@echo "bench-report:	tabulate bench.jsonl, or diff two runs' call counts"
	@echo "coverage:	run both suites under coverage and report"
	@echo "docs:		build documentation"
	@echo "prose-lint:	check docs, README, and specs with vale"
	@echo "release:	tag and push current version to trigger PyPI release"

VERSION := $(shell uv version --bump stable --dry-run --short --no-sync 2>/dev/null)
NEXT_VERSION := $(shell uv version --bump patch --bump dev=0 --dry-run --short --no-sync 2>/dev/null)

.PHONY: release
release: test-unit
	@echo "Releasing $(VERSION)"
	uv version --bump stable --no-sync
	sd "^## $(VERSION) \(UNRELEASED\)" "## $(VERSION) ($(shell date +%Y-%m-%d))" CHANGELOG.md
	git add pyproject.toml CHANGELOG.md
	git commit -m "Release $(VERSION)"
	git tag v$(VERSION)
	git push origin main v$(VERSION)
	uv version --bump patch --bump dev=0 --no-sync
	printf '## $(NEXT_VERSION) (UNRELEASED)\n\n' | cat - CHANGELOG.md > CHANGELOG.md.tmp && mv CHANGELOG.md.tmp CHANGELOG.md
	git add pyproject.toml CHANGELOG.md
	git commit -m "Bump version to $(NEXT_VERSION)"
	git push origin main

.PHONY: docs
docs:
	uv run mkdocs build

.PHONY: test testall test-unit
test: test-unit
testall: test-unit test-func
test-unit:
	uv run python -m unittest discover tests/unit

.PHONY: bench
bench:
	uv run python -m tests.goldens.benchmark $(ARGS)

.PHONY: bench-record
bench-record:
	uv run python -m tests.goldens.benchmark --record $(ARGS)

.PHONY: bench-report
bench-report:
	uv run python -m tests.goldens.report $(ARGS)

.PHONY: test-func
test-func:
	uv run python -m unittest discover -s tests/functional -t .

.PHONY: typecheck
typecheck:
	uv run mypy vncdotool

.PHONY: prose-lint
prose-lint:
	vale sync
	uv run vale docs/*.md README.md specs/*.md

include tests/servers/servers.mk

.PHONY: coverage coverage-unit coverage-func coverage-report
coverage: coverage-unit coverage-func coverage-report

coverage-unit:
	uv run coverage run --parallel-mode -m unittest discover tests/unit

coverage-func:
	uv run coverage run --parallel-mode -m unittest discover -s tests/functional -t .

coverage-report:
	uv run coverage combine
	uv run coverage report
	uv run coverage html
	@echo "open htmlcov/index.html"
