- test_render_install_returns_string: basic smoke test - test_render_install_contains_pnputil: SCRPT-01 pnputil + Add-PrinterDriver - test_render_install_print_config: SCRPT-01 duplex mode translation - test_render_install_wow64_guard: SCRPT-05 WOW64 relaunch guard - test_render_install_uac_guard: SCRPT-04 SYSTEM vs user + UAC elevation - test_render_install_idempotency: SCRPT-01 idempotency checks - test_render_install_booleans: SCRPT-01 boolean rendering
95 lines
3.3 KiB
Python
95 lines
3.3 KiB
Python
"""Unit tests for script_generator.py — SCRPT-01, SCRPT-04, SCRPT-05."""
|
|
import pytest
|
|
from imptune.generators.script_generator import render_install
|
|
|
|
|
|
SAMPLE_ARGS = dict(
|
|
printer_name="HP LaserJet 4000",
|
|
ip_address="192.168.1.10",
|
|
port_name="IP_192.168.1.10",
|
|
driver_name="HP Universal Printing PCL 6",
|
|
inf_filename="hpcu270u.inf",
|
|
duplex_mode="LongEdge",
|
|
color_mode=True,
|
|
paper_size="A4",
|
|
collate=True,
|
|
)
|
|
|
|
|
|
def test_render_install_returns_string():
|
|
"""render_install returns a non-empty string."""
|
|
result = render_install(**SAMPLE_ARGS)
|
|
assert isinstance(result, str)
|
|
assert len(result) > 0
|
|
|
|
|
|
def test_render_install_contains_pnputil():
|
|
"""SCRPT-01: Output contains pnputil /add-driver and Add-PrinterDriver."""
|
|
result = render_install(**SAMPLE_ARGS)
|
|
assert "pnputil.exe /add-driver" in result
|
|
assert "Add-PrinterDriver" in result
|
|
assert "hpcu270u.inf" in result
|
|
assert "HP Universal Printing PCL 6" in result
|
|
|
|
|
|
def test_render_install_print_config():
|
|
"""SCRPT-01: Set-PrintConfiguration with translated duplex mode."""
|
|
# LongEdge -> TwoSidedLongEdge
|
|
result = render_install(**SAMPLE_ARGS)
|
|
assert "Set-PrintConfiguration" in result
|
|
assert "TwoSidedLongEdge" in result
|
|
assert "A4" in result
|
|
|
|
# ShortEdge -> TwoSidedShortEdge
|
|
result_short = render_install(**{**SAMPLE_ARGS, "duplex_mode": "ShortEdge"})
|
|
assert "TwoSidedShortEdge" in result_short
|
|
|
|
# OneSided -> OneSided (no change)
|
|
result_one = render_install(**{**SAMPLE_ARGS, "duplex_mode": "OneSided"})
|
|
assert "OneSided" in result_one
|
|
# Must NOT contain the two-sided variants when OneSided
|
|
assert "TwoSided" not in result_one
|
|
|
|
|
|
def test_render_install_wow64_guard():
|
|
"""SCRPT-05: WOW64 relaunch guard is present in the output."""
|
|
result = render_install(**SAMPLE_ARGS)
|
|
assert "PROCESSOR_ARCHITECTURE" in result
|
|
assert "SysNative" in result
|
|
# Guard must appear near the top (before pnputil)
|
|
wow64_pos = result.find("PROCESSOR_ARCHITECTURE")
|
|
pnputil_pos = result.find("pnputil.exe")
|
|
assert wow64_pos < pnputil_pos, "WOW64 guard must appear before pnputil"
|
|
|
|
|
|
def test_render_install_uac_guard():
|
|
"""SCRPT-04: SYSTEM vs user detection with UAC self-elevation."""
|
|
result = render_install(**SAMPLE_ARGS)
|
|
assert "IsSystem" in result
|
|
assert "Start-Process" in result
|
|
assert "-Verb Runas" in result or "Verb Runas" in result
|
|
|
|
|
|
def test_render_install_idempotency():
|
|
"""SCRPT-01: All add operations wrapped in idempotency checks."""
|
|
result = render_install(**SAMPLE_ARGS)
|
|
# Port idempotency
|
|
assert "Get-PrinterPort" in result
|
|
assert "Add-PrinterPort" in result
|
|
# Printer idempotency
|
|
assert "Get-Printer" in result
|
|
assert "Add-Printer" in result
|
|
# Port check must come before Add-PrinterPort
|
|
get_port_pos = result.find("Get-PrinterPort")
|
|
add_port_pos = result.find("Add-PrinterPort")
|
|
assert get_port_pos < add_port_pos, "Get-PrinterPort check must precede Add-PrinterPort"
|
|
|
|
|
|
def test_render_install_booleans():
|
|
"""Color and collate are rendered as $true / $false in the script."""
|
|
result_true = render_install(**SAMPLE_ARGS) # color_mode=True, collate=True
|
|
assert "$true" in result_true
|
|
|
|
result_false = render_install(**{**SAMPLE_ARGS, "color_mode": False, "collate": False})
|
|
assert "$false" in result_false
|