test(openspec): map cloud integration scenarios

This commit is contained in:
2026-07-13 08:21:44 +08:00
parent ffc3df6c5b
commit 7b717a05a9
8 changed files with 200 additions and 6 deletions
@@ -72,7 +72,11 @@ def create_internal_router(
request: Request,
) -> HeartbeatResponse:
authorize_host(request, host_id)
_validate_snapshot(pool, host_id=host_id, payload=payload)
allow_device_takeover = _validate_snapshot(
pool,
host_id=host_id,
payload=payload,
)
devices = [
Device(
id=device.device_id,
@@ -82,7 +86,12 @@ def create_internal_router(
)
for device in payload.devices
]
pool.sync_host_devices(host_id, devices, address=payload.address)
pool.sync_host_devices(
host_id,
devices,
address=payload.address,
allow_device_takeover=allow_device_takeover,
)
return HeartbeatResponse(
host_id=host_id,
accepted_devices=len(devices),
@@ -228,7 +237,7 @@ def _validate_snapshot(
*,
host_id: str,
payload: HeartbeatRequest,
) -> None:
) -> bool:
if payload.host_id != host_id:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
@@ -244,6 +253,7 @@ def _validate_snapshot(
now = utc_now()
hosts = {host.host_id: host for host in pool.store.list_hosts()}
conflicts: list[str] = []
stale_owner_found = False
requested_ids = set(device_ids)
for device in pool.store.list_devices():
if device.device_id not in requested_ids or device.host_id == host_id:
@@ -254,8 +264,11 @@ def _validate_snapshot(
age_seconds = (now - owner.last_seen_at).total_seconds()
if age_seconds <= pool.config.stale_after_seconds:
conflicts.append(device.device_id)
else:
stale_owner_found = True
if conflicts:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"device ownership conflict: {sorted(set(conflicts))}",
)
return stale_owner_found