2026-07-05 21:17:39 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
import unittest
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
from backend.route_registry import route_manifest
|
|
|
|
|
from database.db_helper import REPO_ROOT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FrontendApiContractTests(unittest.TestCase):
|
|
|
|
|
def test_static_frontend_endpoints_exist_in_backend_registry(self):
|
2026-07-05 22:59:44 +08:00
|
|
|
endpoint_source = _endpoint_source()
|
2026-07-05 21:17:39 +08:00
|
|
|
frontend_static = {
|
|
|
|
|
match.group("endpoint")
|
|
|
|
|
for match in re.finditer(r"\w+:\s*\"(?P<endpoint>[^\"]+)\"", endpoint_source)
|
|
|
|
|
if "${" not in match.group("endpoint")
|
|
|
|
|
}
|
|
|
|
|
backend_endpoints = {route["endpoint"] for route in route_manifest()}
|
|
|
|
|
|
|
|
|
|
self.assertTrue(frontend_static)
|
|
|
|
|
self.assertEqual(sorted(frontend_static - backend_endpoints), [])
|
|
|
|
|
|
2026-07-05 22:59:44 +08:00
|
|
|
def test_dynamic_frontend_endpoints_exist_in_backend_registry(self):
|
|
|
|
|
endpoint_source = _endpoint_source()
|
|
|
|
|
frontend_dynamic = {
|
|
|
|
|
_expand_frontend_template(match.group("template"))
|
|
|
|
|
for match in re.finditer(
|
|
|
|
|
r"\w+:\s*\((?P<params>[^)]*)\)\s*=>\s*`(?P<template>[^`]+)`",
|
|
|
|
|
endpoint_source,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
backend_expanded = {_expand_backend_endpoint(route["endpoint"]) for route in route_manifest()}
|
|
|
|
|
|
|
|
|
|
self.assertTrue(frontend_dynamic)
|
|
|
|
|
self.assertEqual(sorted(frontend_dynamic - backend_expanded), [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _endpoint_source() -> str:
|
|
|
|
|
return (REPO_ROOT / "frontend" / "shared" / "api" / "endpoints.ts").read_text(encoding="utf-8")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _expand_frontend_template(template: str) -> str:
|
|
|
|
|
return re.sub(r"\$\{(?P<param>\w+)\}", lambda match: _sample_value(match.group("param")), template)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _expand_backend_endpoint(endpoint: str) -> str:
|
|
|
|
|
return re.sub(r"\{(?P<param>[^{}]+)\}", lambda match: _sample_value(match.group("param")), endpoint)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _sample_value(param_name: str) -> str:
|
|
|
|
|
normalized = re.sub(r"(?<!^)([A-Z])", r"_\1", param_name).lower()
|
|
|
|
|
samples = {
|
|
|
|
|
"category_id": "category_sample",
|
|
|
|
|
"image_id": "image_sample",
|
|
|
|
|
"material_id": "material_sample",
|
|
|
|
|
"node_id": "node_sample",
|
|
|
|
|
"profile_id": "profile_sample",
|
|
|
|
|
"snapshot_id": "snapshot_sample",
|
|
|
|
|
"task_id": "task_sample",
|
|
|
|
|
"wardrobe_item_id": "wardrobe_item_sample",
|
|
|
|
|
"workflow_id": "workflow_sample",
|
|
|
|
|
}
|
|
|
|
|
return samples.get(normalized, f"{normalized}_sample")
|
|
|
|
|
|
2026-07-05 21:17:39 +08:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|