import pytest from cbb_refresh_verify.pcb_bridge import AutomationLicenseError, validate_document class FakeDocument: def __init__(self): self.validate_calls = [] def Validate(self, value): self.validate_calls.append(value) return 1234 if value == 0 else True class FakeLicenseApp: def __init__(self): self.keys = [] def GetToken(self, key): self.keys.append(key) return 5678 def test_validate_uses_official_automation_token_without_logging_token(): document = FakeDocument() license_app = FakeLicenseApp() events = [] validate_document(document, lambda: license_app, emit=lambda **event: events.append(event)) assert document.validate_calls == [0, 5678] assert license_app.keys == [1234] assert [event['stage'] for event in events] == [ 'license_key_requested', 'license_token_received', 'document_validated' ] assert '5678' not in repr(events) def test_unregistered_licensing_server_allows_ui_fallback(): class ComFailure(Exception): hresult = -2147221164 # REGDB_E_CLASSNOTREG with pytest.raises(AutomationLicenseError) as raised: validate_document(FakeDocument(), lambda: (_ for _ in ()).throw(ComFailure('missing'))) assert raised.value.fallback_allowed is True assert raised.value.error_code == 'AUTOMATION_LICENSE_UNAVAILABLE' def test_rejected_license_token_is_fatal_not_ui_fallback(): class RejectingDocument(FakeDocument): def Validate(self, value): if value: raise RuntimeError('token rejected') return super().Validate(value) with pytest.raises(AutomationLicenseError) as raised: validate_document(RejectingDocument(), lambda: FakeLicenseApp()) assert raised.value.fallback_allowed is False assert raised.value.error_code == 'AUTOMATION_LICENSE_REJECTED'