Files
adaptive-pricing/projects/coulomb-pricing/tests/test_publication.py

125 lines
3.6 KiB
Python

from __future__ import annotations
import json
from pathlib import Path
from observatory.load import load_pricing_models, load_product
from observatory.publication import (
build_stripe_publication_preview,
publish_to_stripe_shadow,
rollback_stripe_shadow,
)
DATA_DIR = Path(__file__).resolve().parent.parent / "data"
def _catalog():
return load_product(DATA_DIR), load_pricing_models(DATA_DIR)
def test_overage_preview_includes_meter_and_approximate_mapping(tmp_path: Path) -> None:
product, models = _catalog()
preview = build_stripe_publication_preview(
product,
models,
DATA_DIR,
model_id="membership-plus-overage",
state_path=tmp_path / "stripe-state.json",
)
assert preview["artifact_counts"]["exact"] >= 3
assert preview["artifact_counts"]["approximate"] >= 1
assert preview["artifact_counts"]["unsupported"] == 0
assert any(
operation["provider_object_type"] == "billing_meter"
for operation in preview["plan"]["operations"]
)
def test_credit_allowance_preview_marks_unsupported_usage_mapping(tmp_path: Path) -> None:
product, models = _catalog()
preview = build_stripe_publication_preview(
product,
models,
DATA_DIR,
model_id="membership-plus-credits",
state_path=tmp_path / "stripe-state.json",
)
unsupported = {
artifact["source_key"]
for artifact in preview["plan"]["unsupported_artifacts"]
}
assert "price:membership-plus-credits:ai-credit-allowance" in unsupported
assert preview["artifact_counts"]["unsupported"] >= 1
def test_publication_is_idempotent_and_detects_drift(tmp_path: Path) -> None:
product, models = _catalog()
state_path = tmp_path / "stripe-state.json"
publish_to_stripe_shadow(
product,
models,
DATA_DIR,
model_id="flat-899-eur-monthly",
state_path=state_path,
)
preview = build_stripe_publication_preview(
product,
models,
DATA_DIR,
model_id="flat-899-eur-monthly",
state_path=state_path,
)
assert {operation["kind"] for operation in preview["plan"]["operations"]} == {"noop"}
raw_state = json.loads(state_path.read_text(encoding="utf-8"))
price_artifact = next(
artifact
for artifact in raw_state["artifacts"]
if artifact["provider_id"] == "price--flat-899-eur-monthly--membership-access"
)
price_artifact["payload"]["unit_amount_decimal"] = "9.49"
state_path.write_text(json.dumps(raw_state, indent=2), encoding="utf-8")
drifted = build_stripe_publication_preview(
product,
models,
DATA_DIR,
model_id="flat-899-eur-monthly",
state_path=state_path,
)
assert drifted["plan"]["drift"]
assert any(operation["kind"] == "update" for operation in drifted["plan"]["operations"])
def test_publication_rollback_restores_prior_revision(tmp_path: Path) -> None:
product, models = _catalog()
state_path = tmp_path / "stripe-state.json"
first = publish_to_stripe_shadow(
product,
models,
DATA_DIR,
model_id="flat-899-eur-monthly",
state_path=state_path,
)
publish_to_stripe_shadow(
product,
models,
DATA_DIR,
model_id="membership-plus-overage",
state_path=state_path,
)
rollback = rollback_stripe_shadow(
DATA_DIR,
first["result"]["revision"]["revision_id"],
state_path=state_path,
)
assert rollback["result"]["state"]["active_model_id"] == "flat-899-eur-monthly"
assert rollback["result"]["revision"]["summary"].startswith("Rolled back")