File size: 3,320 Bytes
2379054
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""Pre-production checks: VBA, Form A/B, drawing review, database indexes."""

from pathlib import Path


from core.database import get_connection, insert_chainages, insert_provenance, query_geometry
from core.drawing_reviewer import DrawingReviewer
from core.form_ab_assistant import FormABAssistant
from agent.code_tools import generate_all_vba_scripts, generate_vba_from_template

ROOT = Path(__file__).resolve().parents[1]
TEMPLATES = ROOT / "templates" / "vba"


def test_vba_templates_have_required_placeholders():
    required = ("{{CHAINAGE_LIST}}", "{{ALIGNMENT_NAME}}", "{{PROJECT_NAME}}")
    for path in TEMPLATES.glob("*.bas.template"):
        text = path.read_text(encoding="utf-8")
        for token in required:
            assert token in text, f"{path.name} missing {token}"
    long_section = (TEMPLATES / "long_section.bas.template").read_text(encoding="utf-8")
    assert "{{LEVEL_LIST}}" in long_section


def test_generate_vba_from_template_fills_placeholders():
    script = generate_vba_from_template(
        "survey_check",
        {
            "CHAINAGE_LIST": "1000, 1010",
            "ALIGNMENT_NAME": "CL_Existing",
            "PROJECT_NAME": "AuditProject",
        },
    )
    assert "1000, 1010" in script
    assert "CL_Existing" in script
    assert "AuditProject" in script
    assert "{{CHAINAGE_LIST}}" not in script


def test_generate_all_vba_scripts_from_sqlite():
    import pandas as pd

    df = pd.DataFrame(
        {
            "datum_chainage": [1000.0, 1010.0],
            "level_m": [12.1, 12.2],
            "offset_alignment": ["Main_Line", "Main_Line"],
        }
    )
    insert_chainages(df, "proposed", source_file="vba_test.xml")
    scripts = generate_all_vba_scripts([], project_name="SQLiteVBA", alignment="Main_Line")
    assert "survey_check" in scripts
    assert "long_section" in scripts
    assert "1000" in scripts["survey_check"] or "1010" in scripts["survey_check"]


def test_form_ab_draft_sections():
    draft = FormABAssistant().generate_draft({"project_name": "FormABTest", "stage": "GRIP 4"})
    assert "project_background" in draft
    assert "FormABTest" in draft["project_background"]
    assert "assumptions" in draft


def test_drawing_reviewer_handles_invalid_pdf():
    issues = DrawingReviewer().review_pdf(b"not-a-pdf", {"number": "NR-001", "revision": "P01"})
    assert isinstance(issues, list)
    assert issues


def test_lift_slue_export_columns():
    from core.orchestrator import build_lift_slue_df
    df = build_lift_slue_df(
        {1: {"lift": {1000: 10}, "slue": {1000: 5}, "pr_cant": {1000: 20}, "pr_radius": {1000: 200}}},
        [1000],
        {1: "Main"},
    )
    for col in ["Track", "Chainage", "Lift (mm)", "Slue (mm)", "Cant (mm)", "Radius (m)", "Source", "Standard clause"]:
        assert col in df.columns
    conn = get_connection()
    cursor = conn.cursor()
    cursor.execute("SELECT name FROM sqlite_master WHERE type='index'")
    names = {row[0] for row in cursor.fetchall()}
    conn.close()
    assert "idx_chainages_alignment" in names
    assert "idx_chainages_type" in names
    assert "idx_chainages_chainage" in names or "idx_chainages_ch" in names
    insert_provenance("test", "1", source_file="audit.xml", chainage=1000.0)
    geom = query_geometry()
    assert geom is not None