- 新增模块级 _request_calculation_modes(body):常量从 ondemand_calc.contract 取, 缺省 mean / apparent 并标 product_default_explicit;非法值 400; position_mode=mean 400 且消息含 position_mode_unsupported - 5 处隐式 body.get(..., 'mean') 改为显式口径(缓存键、高严谨 birth payload、 主题报告、_compute_chart_sync 三处);主题报告不再把非法值静默改成 mean - 缓存键加 calculation.position_mode,cache_schema_version 8 → 9(三栏注释) - execute_consultation_workflow 的两条返回路径都带 calculation_profile 未动:declared_window_chart 与其余不在本单范围的入口(保持原隐式默认); domain_calculation_service.py 等冻结文件未改。 Tests: tests/test_explicit_calculation_modes_bug1828.py(11 条);连同 api_server_growth_contract 与 ondemand contract 共 27 条全过。 前端部分(schema / toolInput / 预热 / 回执)下一轮继续。 Co-Authored-By: Grok (agent loop)
78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
"""本命计算显式传入交点口径与位置口径(#185,BUG-1828)。
|
||
|
||
口径只认 `scripts/ondemand_calc/contract.py` 的一份定义(产品 2026-11-09 #173 拍板:mean / apparent);
|
||
缺省用默认值并标 `product_default_explicit`,`position_mode = mean` 返回 400。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
import pytest
|
||
|
||
ROOT = Path(__file__).resolve().parents[1]
|
||
for path in (str(ROOT), str(ROOT / 'scripts')):
|
||
if path not in sys.path:
|
||
sys.path.insert(0, path)
|
||
|
||
import jyotish_api_server as server # noqa: E402
|
||
|
||
BIRTH = {'year': 1990, 'month': 6, 'day': 15, 'hour': 10, 'minute': 20, 'lat': 31.2, 'lon': 121.5, 'tz': 8}
|
||
|
||
|
||
def test_defaults_come_from_the_shared_contract_and_are_marked() -> None:
|
||
modes = server._request_calculation_modes(dict(BIRTH))
|
||
assert modes['node_mode'] == 'mean'
|
||
assert modes['position_mode'] == 'apparent'
|
||
assert modes['profile_source'] == 'product_default_explicit'
|
||
|
||
|
||
def test_explicit_values_are_marked_as_caller_supplied() -> None:
|
||
modes = server._request_calculation_modes({**BIRTH, 'node_mode': 'true', 'position_mode': 'apparent'})
|
||
assert modes['node_mode'] == 'true'
|
||
assert modes['position_mode'] == 'apparent'
|
||
assert modes['profile_source'] == 'caller_supplied'
|
||
|
||
|
||
# 空串按「没传」处理(沿用 _request_node_mode 的旧语义),所以列在无效值里的只有真非法值。
|
||
@pytest.mark.parametrize('value', ['vn', 'fixed', 1])
|
||
def test_invalid_node_mode_is_a_bad_request(value) -> None:
|
||
with pytest.raises(server.BadRequest) as excinfo:
|
||
server._request_calculation_modes({**BIRTH, 'node_mode': value})
|
||
assert 'node_mode' in str(excinfo.value)
|
||
|
||
|
||
def test_mean_position_mode_is_refused_with_the_named_reason() -> None:
|
||
with pytest.raises(server.BadRequest) as excinfo:
|
||
server._request_calculation_modes({**BIRTH, 'position_mode': 'mean'})
|
||
assert 'position_mode_unsupported' in str(excinfo.value)
|
||
|
||
|
||
def test_unknown_position_mode_is_a_bad_request() -> None:
|
||
with pytest.raises(server.BadRequest) as excinfo:
|
||
server._request_calculation_modes({**BIRTH, 'position_mode': 'topocentric'})
|
||
assert 'position_mode' in str(excinfo.value)
|
||
|
||
|
||
def test_cache_payload_records_both_modes_and_bumps_the_schema() -> None:
|
||
payload = server._build_api_chart_cache_payload(dict(BIRTH))
|
||
assert payload['cache_schema_version'] == 9
|
||
assert payload['calculation']['node_mode'] == 'mean'
|
||
assert payload['calculation']['position_mode'] == 'apparent'
|
||
|
||
|
||
def test_cache_payload_changes_when_the_profile_changes() -> None:
|
||
default = server._build_api_chart_cache_payload(dict(BIRTH))
|
||
node_true = server._build_api_chart_cache_payload({**BIRTH, 'node_mode': 'true'})
|
||
assert node_true['calculation'] != default['calculation']
|
||
assert node_true['calculation']['node_mode'] == 'true'
|
||
|
||
|
||
def test_defaults_are_the_single_definition_from_ondemand_calc() -> None:
|
||
from scripts.ondemand_calc import contract
|
||
|
||
modes = server._request_calculation_modes(dict(BIRTH))
|
||
assert modes['node_mode'] == contract.PRODUCT_DEFAULT_NODE_MODE
|
||
assert modes['position_mode'] == contract.PRODUCT_DEFAULT_POSITION_MODE
|