nomadro/scripts/smoke-portal-upgrade.py
Eric 308974bf5e Harden platform upgrade: ops admin, live config, shared service proxy, and smoke deploys.
Ship Operations console polish, PocketBase social fixtures, and portal join-leads without local DBs or probe scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 01:20:55 +08:00

53 lines
3.2 KiB
Python

"""Read-only HTTP sweep of built portal pages plus targeted negative checks."""
import asyncio
import json
import sys
import time
from pathlib import Path
import httpx
ROOT = Path(__file__).resolve().parents[1]
OUT = ROOT / 'artifacts/platform-upgrade'
BASE = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:3008'
async def main():
manifest = json.loads((ROOT / '.next/server/app-paths-manifest.json').read_text())
routes = set()
for key in manifest:
if not key.endswith('/page') or '[' in key or key.startswith('/_'):
continue
segments = [part for part in key.removesuffix('/page').split('/') if part and not part.startswith('(')]
routes.add('/' + '/'.join(segments))
prerender = json.loads((ROOT / '.next/prerender-manifest.json').read_text())
routes.update(route for route in prerender['routes'] if not route.startswith(('/api/', '/_')) and '[' not in route and '.' not in route)
semaphore = asyncio.Semaphore(4)
async with httpx.AsyncClient(base_url=BASE, timeout=30, follow_redirects=True, trust_env=False) as client:
async def probe(route):
async with semaphore:
started = time.monotonic()
try:
response = await client.get(route)
return {'route': route, 'status': response.status_code, 'finalPath': response.url.path,
'latencyMs': round((time.monotonic() - started) * 1000),
'serverError': response.status_code >= 500}
except httpx.HTTPError as error:
return {'route': route, 'status': None, 'error': type(error).__name__, 'serverError': True}
pages = await asyncio.gather(*(probe(route) for route in sorted(routes)))
checks = []
for route in ['/api/admin/overview', '/api/admin/users', '/api/admin/orders', '/api/admin/config', '/api/portal/join-leads', '/api/admin/social/ugc/join-leads']:
response = await client.get(route)
checks.append({'route': route, 'expected': 401, 'status': response.status_code})
response = await client.post('/api/pay/confirm', json={'order_id': 'another_vip_order_123_a', 'user_id': 'wrong'})
checks.append({'route': '/api/pay/confirm', 'expected': 403, 'status': response.status_code})
response = await client.post('/api/pay/confirm', json={})
checks.append({'route': '/api/pay/confirm (missing fields)', 'expected': 400, 'status': response.status_code})
for order_id, user_id in [('user123_book_order_123_abc', 'user123'), ('user123_other_vip_order_123_abc', 'user123')]:
response = await client.post('/api/pay/confirm', json={'order_id': order_id, 'user_id': user_id})
checks.append({'route': '/api/pay/confirm (wrong product or exact owner)', 'expected': 403, 'status': response.status_code})
result = {'base': BASE, 'pages': pages, 'checks': checks}
(OUT / 'portal-http.json').write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8')
print(json.dumps({'pages': len(pages), 'serverErrors': [p for p in pages if p['serverError']], 'notFound': [p['route'] for p in pages if p['status'] == 404], 'checks': checks}, ensure_ascii=False))
asyncio.run(main())