Local preservation snapshot, not a new deployment or simulation acceptance.
158 lines
6.2 KiB
Python
158 lines
6.2 KiB
Python
"""Library editing never rewrites a route or erases evidence/worker ownership."""
|
|
|
|
import json
|
|
|
|
import pytest
|
|
from test_ai_polygon_realtime import realtime as _realtime
|
|
|
|
from k1link.simulation.ai_polygon.contracts import RunCreate
|
|
from k1link.simulation.ai_polygon.library import LibraryMetadata
|
|
|
|
realtime = _realtime
|
|
|
|
|
|
def make_plan(store, row):
|
|
return store.plans.create(
|
|
row,
|
|
"Trail",
|
|
[
|
|
{"id": "a" * 16, "position": [0, 0, 0]},
|
|
{"id": "b" * 16, "position": [2, 0, 0]},
|
|
],
|
|
0,
|
|
)
|
|
|
|
|
|
def test_rename_remove_restore_preserves_immutable_plan(realtime):
|
|
store, _, row = realtime
|
|
plan = make_plan(store, row)
|
|
identity = plan["plan_id"]
|
|
path = store.plans.root / (identity + ".json")
|
|
before = path.read_bytes()
|
|
store.update_library("plans", identity, {"display_name": "New trail"})
|
|
assert store.plans.visible_list()[0]["display_name"] == "New trail"
|
|
store.update_library("plans", identity, {"removed": True})
|
|
assert store.plans.visible_list() == []
|
|
# Metadata survives a fresh reader, while evidence remains byte-identical.
|
|
assert LibraryMetadata(store.plans.library.root).get(identity)["removed"]
|
|
assert path.read_bytes() == before
|
|
assert store.plans.get(identity) == plan
|
|
store.update_library("plans", identity, {"removed": False})
|
|
assert store.plans.visible_list()[0]["display_name"] == "New trail"
|
|
|
|
|
|
def test_removal_cannot_hide_worker_owner_and_keeps_terminal_evidence(realtime):
|
|
store, _, row = realtime
|
|
identity = row["run_id"]
|
|
with pytest.raises(RuntimeError, match="завершите"):
|
|
store.update_library("runs", identity, {"removed": True})
|
|
row["state"] = "stopped"
|
|
store._save(row)
|
|
with pytest.raises(RuntimeError, match="освобождения"):
|
|
store.update_library("runs", identity, {"removed": True})
|
|
store.active = None
|
|
before = (store.directory(identity) / "run.json").read_bytes()
|
|
store.update_library("runs", identity, {"removed": True})
|
|
assert store.library.visible(store.list(), "run_id") == []
|
|
assert (store.directory(identity) / "run.json").read_bytes() == before
|
|
store.update_library("runs", identity, {"removed": False})
|
|
assert store.library.visible(store.list(), "run_id")[0]["run_id"] == identity
|
|
|
|
|
|
def test_removed_plan_is_not_launchable_or_deduplicated(realtime):
|
|
store, _, row = realtime
|
|
plan = make_plan(store, row)
|
|
store.update_library("plans", plan["plan_id"], {"removed": True})
|
|
replacement = make_plan(store, row)
|
|
assert replacement["plan_id"] != plan["plan_id"]
|
|
row["state"] = "stopped"
|
|
store._save(row)
|
|
store.active = None
|
|
store.queue.release_simulation(row["run_id"])
|
|
with pytest.raises(ValueError, match="удалено"):
|
|
store.start(
|
|
RunCreate(world_id=row["world"]["world_id"], clock="realtime", plan_id=plan["plan_id"]),
|
|
"removed-plan-check",
|
|
)
|
|
|
|
|
|
def test_library_rejects_paths_and_unknown_records(realtime, tmp_path):
|
|
store, _, _ = realtime
|
|
with pytest.raises(FileNotFoundError):
|
|
store.update_library("plans", "aiplan-" + "f" * 32, {"removed": True})
|
|
with pytest.raises(FileNotFoundError):
|
|
store.library.update("../run", {"removed": True})
|
|
target = tmp_path / "outside.json"
|
|
target.write_text(json.dumps({"private": True}))
|
|
identity = "airun-" + "f" * 32
|
|
(store.library.root / (identity + ".json")).symlink_to(target)
|
|
with pytest.raises(ValueError, match="regular"):
|
|
store.library.update(identity, {"removed": True})
|
|
assert json.loads(target.read_text()) == {"private": True}
|
|
|
|
|
|
def test_library_api_validation_catalog_and_restart(realtime, tmp_path):
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
|
|
from k1link.web.ai_polygon_api import build_ai_polygon_router
|
|
|
|
store, _, row = realtime
|
|
plan = make_plan(store, row)
|
|
row["state"] = "stopped"
|
|
store._save(row)
|
|
api = "/api/v1/ai-polygon"
|
|
for iteration in range(2):
|
|
app = FastAPI()
|
|
app.include_router(build_ai_polygon_router(tmp_path))
|
|
with TestClient(app) as client:
|
|
if iteration == 0:
|
|
for invalid in (
|
|
{"display_name": " "},
|
|
{"display_name": "x" * 121},
|
|
{"removed": "yes"},
|
|
{"sha256": "tampered"},
|
|
):
|
|
assert (
|
|
client.patch(f"{api}/plans/{plan['plan_id']}", json=invalid).status_code
|
|
== 422
|
|
)
|
|
assert (
|
|
client.patch(
|
|
f"{api}/plans/{plan['plan_id']}",
|
|
json={"display_name": "Renamed", "removed": True},
|
|
).status_code
|
|
== 200
|
|
)
|
|
assert (
|
|
client.patch(f"{api}/runs/{row['run_id']}", json={"removed": True}).status_code
|
|
== 200
|
|
)
|
|
catalog = client.get(f"{api}/catalog").json()
|
|
assert not catalog["plans"] and not catalog["runs"]
|
|
assert client.get(f"{api}/runs/{row['run_id']}").json()["run_id"] == row["run_id"]
|
|
if iteration == 1:
|
|
client.patch(f"{api}/plans/{plan['plan_id']}", json={"removed": False})
|
|
restored = client.get(f"{api}/catalog").json()["plans"][0]
|
|
assert (
|
|
restored["display_name"] == "Renamed" and restored["sha256"] == plan["sha256"]
|
|
)
|
|
|
|
|
|
def test_run_snapshots_current_task_label_without_changing_route(realtime):
|
|
store, _, row = realtime
|
|
plan = make_plan(store, row)
|
|
store.update_library("plans", plan["plan_id"], {"display_name": "Renamed trail"})
|
|
row["state"] = "stopped"
|
|
store._save(row)
|
|
store.active = None
|
|
store.queue.release_simulation(row["run_id"])
|
|
run = store.start(
|
|
RunCreate(world_id=row["world"]["world_id"], clock="realtime", plan_id=plan["plan_id"]),
|
|
"renamed-plan-check",
|
|
)
|
|
assert run["plan_display_name"] == "Renamed trail"
|
|
assert run["plan"] == plan
|
|
store.update_library("plans", plan["plan_id"], {"display_name": "Another name"})
|
|
assert store.get(run["run_id"])["plan_display_name"] == "Renamed trail"
|