from types import SimpleNamespace

import pytest
from fastapi import HTTPException

from app.core.services.tenant import tenant_id_for_customer


def test_customer_tenant_uses_customer_tenant_when_no_override():
    customer = SimpleNamespace(tenant_id=12)

    assert tenant_id_for_customer(customer) == 12


def test_customer_tenant_allows_matching_override():
    customer = SimpleNamespace(tenant_id=12)

    assert tenant_id_for_customer(customer, requested_tenant_id=12) == 12


def test_customer_tenant_rejects_cross_tenant_override():
    customer = SimpleNamespace(tenant_id=12)

    with pytest.raises(HTTPException) as exc:
        tenant_id_for_customer(customer, requested_tenant_id=99)

    assert exc.value.status_code == 403
    assert exc.value.detail == "租户不匹配"
