"""HTTP layer: parameters, responses, errors and the reverse proxy prefix.""" from __future__ import annotations import pytest from fastapi.testclient import TestClient from app.main import app @pytest.fixture(scope="module") def client(): with TestClient(app) as test_client: yield test_client def test_health(client): response = client.get("/health") assert response.status_code == 200 body = response.json() assert body["status"] == "ok" assert body["default_country"] == "CZ" def test_docs(client): assert client.get("/docs").status_code == 200 def test_openapi_servers_use_proxy_prefix(): with TestClient(app, root_path="/apps/workdays") as proxied: schema = proxied.get("/openapi.json").json() assert schema["servers"][0]["url"] == "/apps/workdays" assert "/workdays/add" in schema["paths"] def test_add_defaults_to_cz(client): body = client.get("/workdays/add", params={"date": "26.11.2026", "days": 3}).json() assert body["result"] == "2026-12-01" assert body["calendar"]["code"] == "CZ" assert body["calendar"]["language"] == "cs" assert body["calendar"]["weekend"] == ["sat", "sun"] def test_add_lists_skipped_holidays(client): body = client.get("/workdays/add", params={"date": "2026-12-23", "days": 1}).json() assert body["result"] == "2026-12-28" assert [item["date"] for item in body["skipped_holidays"]] == ["2026-12-24", "2026-12-25"] def test_add_with_extra_days(client): body = client.get( "/workdays/add", params={"date": "2026-11-26", "days": 1, "extra_holidays": "2026-11-27", "extra_workdays": "28.11.2026"}, ).json() assert body["result"] == "2026-11-28" def test_is_workday(client): body = client.get("/workdays/is-workday", params={"date": "2026-12-24"}).json() assert body["is_workday"] is False assert body["holidays"] == ["Štědrý den"] def test_language_falls_back_to_country_default(client): body = client.get("/workdays/is-workday", params={"date": "2026-10-03", "country": "DE"}).json() assert body["calendar"]["language"] == "de" assert body["holidays"] == ["Tag der Deutschen Einheit"] def test_language_variant_match(client): body = client.get("/workdays/is-workday", params={"date": "2026-12-24", "language": "en"}).json() assert body["calendar"]["language"] == "en_US" def test_region_and_market(client): body = client.get("/workdays/is-workday", params={"date": "2026-06-04", "country": "de", "region": "by"}).json() assert body["is_workday"] is False assert body["calendar"]["region"] == "BY" body = client.get("/workdays/next", params={"date": "2026-04-02", "market": "XECB"}).json() assert body["result"] == "2026-04-07" assert body["calendar"]["type"] == "market" def test_between(client): body = client.get("/workdays/between", params={"start": "2026-12-01", "end": "2026-12-31"}).json() assert body["workdays"] == 21 assert body["holidays_off"] == 2 def test_holidays_list(client): body = client.get("/holidays", params={"year": 2026}).json() assert len(body["holidays"]) == 13 christmas = next(item for item in body["holidays"] if item["date"] == "2026-12-26") assert christmas["is_weekend"] is True def test_countries_eu_filter(client): items = client.get("/countries", params={"eu": True}).json()["items"] assert len(items) == 27 germany = next(item for item in items if item["code"] == "DE") assert {"code": "BY", "name": "Bayern"} in germany["regions"] def test_markets(client): items = client.get("/markets").json()["items"] assert any(item["code"] == "XECB" for item in items) @pytest.mark.parametrize( ("params", "error_code"), [ ({"date": "2026-13-01", "days": 1}, "invalid_date"), ({"date": "zitra", "days": 1}, "invalid_date"), ({"date": "2026-11-26", "days": 1, "country": "XX"}, "unknown_country"), ({"date": "2026-11-26", "days": 1, "country": "DE", "region": "ZZ"}, "unknown_region"), ({"date": "2026-11-26", "days": 1, "categories": "foo"}, "unknown_category"), ({"date": "2026-11-26", "days": 1, "country": "CZ", "market": "XECB"}, "invalid_request"), ({"date": "2026-11-26", "days": 1, "weekend": "mon,tue,wed,thu,fri,sat,sun"}, "invalid_request"), ({"date": "1900-01-01", "days": 1}, "out_of_range"), ({"date": "2100-12-30", "days": 5}, "out_of_range"), ], ) def test_errors(client, params, error_code): response = client.get("/workdays/add", params=params) assert response.status_code == 400 assert response.json()["error_code"] == error_code