"""PLAN 349-0u sub7 — 세션 관 · 구조물 초안에 좌표가 없어도 노선 따라 측점이 밀림(349-0d 되살림). · 세션 초안 = 정본 PUT 꼴(좌표 뺌) — 앞쪽 꺾임점을 크게 끌면 그 뒤 관 · 구조물 측점이 길이 는 만큼 뒤로 · 되돌리기 = 초안 값 그대로 · 다시하기 = 다시 밂 · 조작 하나 = 묶음 한 번 · 입력 쓰기 막음 0 짝: `helper_349_0u_follow_draft_xy.cjs`(실제 TS 를 Node 로 · 서버 창구만 가짜 · 검증_sub7). """ from __future__ import annotations import csv import json import math import subprocess import sys import tempfile from pathlib import Path import pytest HERE = Path(__file__).resolve().parent ROOT = HERE.parents[1] if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from B05_Profile.B05_Profile_Structures_Schema import structure_type_map # noqa: E402 from common_util.common_util_drainage_pipes import ( # noqa: E402 PipePoint, project_pipe_points, ) from common_util.common_util_route_geometry import RouteVertex # noqa: E402 from common_util.common_util_surface_sampler import dtm_grid_bytes # noqa: E402 from config.config_system_design import STANDARD_CROSS_SECTION # noqa: E402 pytestmark = pytest.mark.skipif( not (ROOT / "config" / "node_modules" / "typescript").is_dir(), reason="프론트엔드 의존성 미설치", ) PROJECT = ROOT / "storage/1/3/6d3b2812-6d5a-4168-a932-40e4ae303d5a" # 검증_sub7 ROUTE = PROJECT / "B05_Profile/route" SECTION = PROJECT / "B06_Section" GRID = PROJECT / "B04_PreProcess/models/dtm_classification_smooth.npz" ONES = {g: {"loose": 1.0, "compacted": 1.0} for g in ("soil", "ripping_rock", "blasting_rock")} def _csv(path: Path) -> list[list[float]]: with path.open(encoding="utf-8") as handle: return [[float(r["x"]), float(r["y"])] for r in csv.DictReader(handle)] def _json(path: Path): return json.loads(path.read_text(encoding="utf-8")) def _structure(sid: str, line: list[list[float]], ratio: float, off: float) -> dict: """노선 위(off 0) · 옆(off m) 점 구조물 — 저장 노선 누가거리 · 좌표.""" lengths = [math.dist(a, b) for a, b in zip(line, line[1:])] at = sum(lengths) * ratio walked = 0.0 for (a, b), length in zip(zip(line, line[1:]), lengths): if walked + length >= at: t = (at - walked) / length dx, dy = (b[0] - a[0]) / length, (b[1] - a[1]) / length x, y = a[0] + dx * length * t - dy * off, a[1] + dy * length * t + dx * off break walked += length return { "structure_id": sid, "type_id": "erosion_check", "placement": "point", "chainage_m": round(at, 2), "start_m": None, "end_m": None, "x": x, "y": y, "options": {}, } @pytest.fixture(scope="module") def r() -> dict: if not (GRID.is_file() and (SECTION / "longitudinal/longitudinal.json").is_file()): pytest.skip("검증_sub7 없음") curves = _json(ROUTE / "planned_route_curves.json") sections = sorted( (_json(p) for p in (SECTION / "cross_sections").glob("cross_*.json")), key=lambda s: s["chainage_m"], ) pipes = [ # 서버 GET 처럼 id 붙음 {**pipe, "id": f"p{i}"} for i, pipe in enumerate( _json(PROJECT / "B04_PreProcess/drainage/edits/pipe_points.json")["points"] ) ] line = _csv(ROUTE / "planned_route.csv") structures = [_structure("s-near", line, 0.5, 0.0), _structure("s-far", line, 0.7, 30.0)] job = { "line": line, "structures": structures, "nodes": _csv(ROUTE / "planned_route_nodes.csv"), "curves": [ {k: c[k] for k in ("start", "apex", "end", "radius_m")} for c in curves["curves"] ], "stationSnap": bool(curves.get("station_snap")), "minRadius": curves["min_radius_m"], "interval": 20.0, "types": [ {"type_id": t.type_id, "group": t.group, "placement": t.placement} for t in structure_type_map().values() ], "pipes": pipes, "dx": -60.0, "dy": 0.0, "standard": STANDARD_CROSS_SECTION, "conversion": ONES, "detail": { "longitudinal": _json(SECTION / "longitudinal/longitudinal.json"), "cross_sections": sections, }, } with tempfile.TemporaryDirectory() as folder: path = Path(folder) / "job.json" path.write_text(json.dumps(job), encoding="utf-8") grid = Path(folder) / "grid.bin" grid.write_bytes(dtm_grid_bytes(GRID)) proc = subprocess.run( # noqa: S603 — 고정 실행 파일 ["node", str(HERE / "helper_349_0u_follow_draft_xy.cjs"), str(path), str(grid)], capture_output=True, text=True, encoding="utf-8", timeout=300, ) assert proc.returncode == 0, proc.stderr return json.loads(proc.stdout) def _moved(before: list, after: list) -> list[float]: return [b[1] - a[1] for a, b in zip(before, after)] def test_앞쪽_꺾임점_끌기_측점_밀림(r): d = r["drag"] assert d["runs"] == [["route"]], d["runs"] assert d["blocked"] == 0 grow = d["length"] - r["savedLength"] assert grow > 5, grow # 앞쪽 길이 늚 pipes = _moved(r["draftPipes"], d["pipes"]) assert len(d["pipes"]) == len(r["draftPipes"]) # 끈 꼭짓점 뒤 관 = 길이 는 만큼(정수 m 맞춤 · 1 m 안) — 대부분 그 뒤 tail = [m for (_, c), m in zip(r["draftPipes"], pipes) if c > r["draftPipes"][2][1]] assert tail and all(abs(m - grow) <= 1.0 for m in tail), (grow, pipes) s = _moved(r["draftStructures"], d["structures"]) assert all(abs(m - grow) <= 1.0 for m in s), (grow, s) assert len(d["followed"]) == 1 def test_되돌리기_초안_값(r): for u in (r["undo"], r["undo2"]): assert u["runs"] == [["route"]], u["runs"] assert u["pipes"] == r["draftPipes"] assert u["structures"] == r["draftStructures"] assert abs(u["length"] - r["savedLength"]) < 1e-6 assert u["blocked"] == 0 def test_다시하기_다시_밂(r): d, again = r["drag"], r["redo"] assert again["runs"] == [["route"]], again["runs"] assert again["pipes"] == d["pipes"] and again["structures"] == d["structures"] assert again["blocked"] == 0 def test_열기만_가게_그대로(r): o = r["open"] assert o["pipes"] == r["draftPipes"] and o["structures"] == r["draftStructures"] assert o["followed"] == [] def test_따라감_서버와_같음(r): """좌표 없는 초안도 안 옮긴 관 = 정본 좌표 — 서버 노선 저장과 같은 측점.""" line, walked, vertices = r["drag"]["line"], 0.0, [] for i, (x, y) in enumerate(line): walked += math.dist(line[i - 1], (x, y)) if i else 0.0 vertices.append(RouteVertex(x=x, y=y, z=0.0, chainage_m=walked)) pipes = _json(PROJECT / "B04_PreProcess/drainage/edits/pipe_points.json")["points"] server = project_pipe_points( [PipePoint(chainage_m=p["chainage_m"], x=p["x"], y=p["y"]) for p in pipes], vertices ) assert [c for _, c in r["drag"]["pipes"]] == [p.chainage_m for p in server]