Check shipped boat consistency in the catalog validator only

Older save snapshots and user edits keep loading; the packaged data is still
held to its machine and endurance by tools/gen_contacts.py.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CW6FkwLLc6KvPHvCs5KTRo
This commit is contained in:
Claude committed 2026-10-04 08:50:27 +00:00
1 parent 18030b67eb
commit 66fc80127d
5 files changed
+50 -15

No files matched your search

-14
View File
@@ -743,8 +743,6 @@ def _collect_v2(documents, profile_keys_by_resource, runtime_weapon_keys, decoy_
raise ValueError(f"profile {profile_key!r}: magazine weapon has no compatible launcher")
sub_entries = {entry["key"]: entry for entry in documents["subs.json"]["entries"]}
sub_machine_keys = {profile["profile_key"]: profile.get("machine_key")
for profile in documents["subs.json"].get("profiles", ())}
if documents["subs.json"]["version"] == 2:
for profile_key, entry in sub_entries.items():
endurance_key = f"endurance.{profile_key}"
@@ -753,18 +751,6 @@ def _collect_v2(documents, profile_keys_by_resource, runtime_weapon_keys, decoy_
if nuclear == has_endurance:
requirement = "must not have" if nuclear else "requires"
raise ValueError(f"submarine profile {profile_key!r} {requirement} endurance")
machine = machines.get(sub_machine_keys.get(profile_key))
if machine is not None:
if nuclear != any(code.startswith("nuclear") for code in machine.propulsion_codes):
raise ValueError(f"submarine profile {profile_key!r}: propulsion codes "
"contradict the acoustic propulsion")
if float(entry["speed_kn"][1]) != float(machine.maximum_speed_kn):
raise ValueError(f"submarine profile {profile_key!r}: entry top speed "
"differs from the machine")
aip = entry["acoustic"]["propulsion"] == "elektrisch/AIP"
if has_endurance and aip != (endurances[endurance_key].aip_power_kw is not None):
raise ValueError(f"submarine profile {profile_key!r}: AIP endurance "
"contradicts the acoustic propulsion")
if any(key.removeprefix("endurance.") not in sub_entries for key in endurances):
raise ValueError("endurance component attached outside submarine catalog")
referenced["endurances"].update(endurances)
+3
View File
@@ -170,6 +170,9 @@ def test_valid_json_edits_survive_runtime_load_without_coercion_or_loss(contact_
_edit(contact_dir, "subs.json", lambda d: d["entries"][0]["acoustic"].update(
blades=[3, 7], broadband=[0.4, 20, 200], secondary_tonals=[[12, 0.2, 1]],
rpm_range=[0, 0], tonal_band_hz=[0, 0]))
# The shipped boat's machine has to keep pace with its entry top speed.
_edit(contact_dir, "subs.json", lambda d: next(
m for m in d["machines"] if m["key"] == "machine.diesel_alt").update(maximum_speed_kn=13))
_edit(contact_dir, "torpedoes.json", lambda d: d["entries"][0].update(range_nm=42))
loaded = validate(contact_dir)
sub = loaded.subs["diesel_alt"]
+18
View File
@@ -56,3 +56,21 @@ def test_validator_rejects_invalid_data(tmp_path, filename, mutate, message):
with pytest.raises(ValueError, match=message):
validate(tmp_path)
assert check(tmp_path) == 1
@pytest.mark.parametrize(("mutate", "message"), [
(lambda d: d["entries"][0].update(speed_kn=[4, 30]), "top speed differs"),
(lambda d: d["entries"][0]["acoustic"].update(propulsion="elektrisch/AIP"),
"AIP endurance contradicts"),
])
def test_validator_holds_shipped_boats_to_their_machine(tmp_path, mutate, message):
from src.data import catalog
_copy_catalog(tmp_path)
path = tmp_path / "subs.json"
data = json.loads(path.read_text(encoding="utf-8"))
mutate(data)
path.write_text(json.dumps(data), encoding="utf-8")
# The runtime loader stays lenient for save snapshots and user edits.
catalog._load_catalog_from(tmp_path)
with pytest.raises(ValueError, match=message):
validate(tmp_path)
+1 -1
View File
@@ -132,7 +132,7 @@ def test_launch_uses_the_selected_type_pattern_and_enable_point():
game.torpedoes.clear()
assert game.launch_torpedo_at(contact, 60.0) is True
assert game.torpedoes[-1].profile_key == "frigate_torp_mk2"
assert game.torpedoes[-1].speed_kn == 55.0
assert game.torpedoes[-1].speed_kn == 50.0
def test_salvo_of_two_opens_a_spread_with_rotated_datums():
+28
View File
@@ -181,12 +181,40 @@ def _validate_provenance_coverage(cat):
f"incomplete field coverage (missing={missing[:8]}, extra={extra[:8]})")
def _validate_submarine_consistency(subs):
"""Shipped boats must agree with their own machine and endurance.
Only the packaged data is held to this; the runtime loader stays lenient
so catalog snapshots in older saves and user edits still load.
"""
machines = {machine["key"]: machine for machine in subs.get("machines", ())}
endurances = {item["key"]: item for item in subs.get("endurances", ())}
machine_of = {profile["profile_key"]: profile.get("machine_key")
for profile in subs.get("profiles", ())}
for entry in subs["entries"]:
key = entry["key"]
propulsion = entry["acoustic"]["propulsion"]
nuclear = propulsion == "elektrisch/Kernantrieb"
machine = machines.get(machine_of.get(key))
if machine is not None:
codes = machine.get("propulsion_codes", ())
if nuclear != any(code.startswith("nuclear") for code in codes):
_fail("subs.json", f"{key}: propulsion codes contradict the acoustic propulsion")
if float(entry["speed_kn"][1]) != float(machine["maximum_speed_kn"]):
_fail("subs.json", f"{key}: entry top speed differs from the machine")
endurance = endurances.get(f"endurance.{key}")
aip = propulsion == "elektrisch/AIP"
if endurance is not None and aip != (endurance.get("aip_power_kw") is not None):
_fail("subs.json", f"{key}: AIP endurance contradicts the acoustic propulsion")
def validate(base_dir):
base_dir = Path(base_dir)
documents = _read_documents(base_dir)
library_keys = [entry["key"] for entry in documents["acoustics.json"]["entries"]]
loaded = catalog._load_catalog_from(base_dir)
_validate_submarine_consistency(documents["subs.json"])
runtime = _runtime_documents(loaded, library_keys)
for filename, document in documents.items():
if runtime[filename]["entries"] != document["entries"]: