"""표 쪽 나눔 — `ui_template_sheet_ops.ts` `pagePlan` 을 Node 로 바로 돌려 봄. 격자(`_render`)가 쪽을 이 계획대로 그림. 줄 120 문서 → project 쪽 3(50 · 50 · 20) · 쪽마다 머리(격자가 쪽마다 thead) · 합계는 마지막 쪽만 · 「전구간」 줄은 맨 위 · master 는 한 쪽. """ from __future__ import annotations import json import shutil import subprocess from pathlib import Path import pytest ROOT = Path(__file__).resolve().parents[2] OPS = ROOT / "ui_template" / "sheet" / "ui_template_sheet_ops.ts" pytestmark = pytest.mark.skipif(shutil.which("node") is None, reason="node 가 없음") SCRIPT = """ const { pagePlan } = await import(process.argv[1]); const doc = JSON.parse(process.argv[2]); const out = {}; for (const mode of ["project", "master"]) { out[mode] = pagePlan(doc, mode).map((p) => ({ ids: p.rows.map((r) => r.id), first: p.first, totals: p.totals, })); } console.log(JSON.stringify(out)); """ def _plan(doc: dict) -> dict: result = subprocess.run( [ "node", "--experimental-strip-types", "--no-warnings", "--input-type=module", "-e", SCRIPT, OPS.as_uri(), json.dumps(doc, ensure_ascii=False), ], capture_output=True, text=True, encoding="utf-8", timeout=60, ) assert result.returncode == 0, result.stderr return json.loads(result.stdout) def _doc(n: int, **head) -> dict: rows = [{"id": f"p{i}", "값": {}} for i in range(1, n)] rows.insert(7, {"id": "all", "값": {}, "고정": "전구간"}) # 문서 가운데 있어도 맨 위로 return {"양식": "시험", "종류": "표", "판": 1, "열": [], "줄": rows, **head} def test_120_rows_make_three_pages_totals_last(): plan = _plan(_doc(120))["project"] assert [len(p["ids"]) for p in plan] == [50, 50, 20] assert [p["first"] for p in plan] == [1, 51, 101] assert [p["totals"] for p in plan] == [False, False, True] assert plan[0]["ids"][0] == "all" assert sum((p["ids"] for p in plan), []).count("all") == 1 def test_page_rows_from_doc_and_master_single_page(): got = _plan(_doc(55, 쪽줄=20)) assert [len(p["ids"]) for p in got["project"]] == [20, 20, 15] assert [len(p["ids"]) for p in got["master"]] == [55] assert got["master"][0]["totals"] is True def test_empty_doc_still_one_page_with_totals(): plan = _plan({"양식": "시험", "종류": "표", "판": 1, "열": [], "줄": []})["project"] assert plan == [{"ids": [], "first": 1, "totals": True}]