Files
cbb-refresh-verify-795/tests/test_native_add_failures.py
T

110 lines
6.4 KiB
Python

"""File-only native failure and legacy path-boundary checks."""
from pathlib import Path
import pytest
ERROR = b'REUSE BLOCK: INFORMATION: Block\tError! Cannot copy iCDB base of Reusable Block project.\r\n'
def test_copy_failure_log_must_be_new_and_match_current_block(tmp_path):
from cbb_refresh_verify.native_add_failures import snapshot_add_log, fresh_add_failure
library = tmp_path/'lib.lmc'; library.touch()
log = tmp_path/'LogFiles/ReuseBlockLogLO.txt'; log.parent.mkdir(); log.write_bytes(ERROR)
baseline = snapshot_add_log(library)
assert fresh_add_failure(baseline, 'Block') is None
with log.open('ab') as stream: stream.write(ERROR.replace(b'Block\t', b'Other\t'))
assert fresh_add_failure(baseline, 'Block') is None
with log.open('ab') as stream: stream.write(ERROR)
assert 'Cannot copy iCDB base' in fresh_add_failure(baseline, 'Block')
# A replaced/rotated prefix is not evidence that an old error is new.
log.write_bytes(b'replaced\n'+ERROR)
assert fresh_add_failure(baseline, 'Block') is None
def test_newly_created_failure_log_is_current_round_evidence(tmp_path):
from cbb_refresh_verify.native_add_failures import snapshot_add_log, fresh_add_failure
library = tmp_path/'lib.lmc'
baseline = snapshot_add_log(library)
log = tmp_path/'LogFiles/ReuseBlockLogLO.txt'; log.parent.mkdir(); log.write_bytes(ERROR)
assert fresh_add_failure(baseline, 'Block')
@pytest.mark.parametrize('change', ['none', 'title', 'question', 'button_id', 'button_text', 'disabled', 'extra_button'])
def test_only_observed_path_error_dialog_can_be_acknowledged(change):
from cbb_refresh_verify.native_add_failures import failure_ack_button
title, text = 'Library Manager', '\n系统找不到指定的路径。\r\n'
buttons = [{'hwnd': 123, 'id': 2, 'class': 'Button', 'text': '确定', 'enabled': True}]
if change == 'title': title = 'Unrelated'
if change == 'question': text += '是否继续?'
if change == 'button_id': buttons[0]['id'] = 1
if change == 'button_text': buttons[0]['text'] = '是'
if change == 'disabled': buttons[0]['enabled'] = False
if change == 'extra_button': buttons.append({**buttons[0], 'hwnd': 456})
assert failure_ack_button(title, text, buttons) == (123 if change == 'none' else None)
def test_native_path_guard_includes_runtime_files_and_projected_target(tmp_path):
from cbb_refresh_verify.native_add_failures import validate_native_copy_paths
source = tmp_path/'source/Block.prj'; source.parent.mkdir(); source.touch()
validate_native_copy_paths(source, tmp_path/'library/lib.lmc', 'Block')
runtime = source.parent/'CES/LogFiles'/('x'*120)/('y'*90+'.log')
runtime.parent.mkdir(parents=True); runtime.touch()
with pytest.raises(ValueError, match='NATIVE_PATH_TOO_LONG'):
validate_native_copy_paths(source, tmp_path/'library/lib.lmc', 'Block')
assert runtime.exists()
runtime.unlink()
# Even a short staged tree can exceed the legacy limit at its target.
target = tmp_path/('library-'+'x'*150)/'lib.lmc'
with pytest.raises(ValueError, match='目标'):
validate_native_copy_paths(source, target, 'Block')
@pytest.mark.parametrize('kind', ['known', 'unknown', 'foreign'])
def test_failure_observer_only_acknowledges_owned_known_dialog(tmp_path, monkeypatch, kind):
import json
from types import SimpleNamespace
import win32gui, win32process
from cbb_refresh_verify.native_add_failures import observe_add_failure, snapshot_add_log
state = {'open': True, 'clicks': []}
monkeypatch.setattr(win32process, 'GetWindowThreadProcessId', lambda _: (1, 999 if kind == 'foreign' else 123))
monkeypatch.setattr(win32gui, 'IsWindowVisible', lambda _: True)
monkeypatch.setattr(win32gui, 'GetClassName', lambda h: {1: '#32770', 10: 'Button', 11: 'Static'}[h])
monkeypatch.setattr(win32gui, 'GetWindowText', lambda h: {1: 'Library Manager', 10: '确定',
11: '系统找不到指定的路径。' if kind == 'known' else '未确认的原生提示'}[h])
monkeypatch.setattr(win32gui, 'EnumChildWindows', lambda h, callback, p: [callback(c, p) for c in [10, 11]])
monkeypatch.setattr(win32gui, 'GetDlgCtrlID', lambda h: 2 if h == 10 else 0)
monkeypatch.setattr(win32gui, 'IsWindowEnabled', lambda _: True)
monkeypatch.setattr(win32gui, 'IsWindow', lambda _: state['open'])
def post(hwnd, message, *_):
state['clicks'].append(hwnd); state['open'] = False
monkeypatch.setattr(win32gui, 'PostMessage', post)
monkeypatch.setattr(win32gui, 'SetForegroundWindow', lambda *_: pytest.fail('foreground input is forbidden'))
args = (SimpleNamespace(EnumDesktopWindows=lambda: [1]), {123}, tmp_path,
snapshot_add_log(tmp_path/'lib.lmc'), 'Block', {})
if kind == 'known':
with pytest.raises(RuntimeError, match='NATIVE_ADD_FAILED'): observe_add_failure(*args)
result = json.loads((tmp_path/'native-add-output.json').read_text(encoding='utf-8'))
assert result['success_message'] is None and result['failure']['acknowledged']
assert state['clicks'] == [10]
else:
observe_add_failure(*args)
assert not state['clicks'] and not (tmp_path/'native-add-output.json').exists()
assert (tmp_path/'native-add-dialogs.jsonl').exists() is (kind == 'unknown')
def test_path_rejection_returns_safe_before_any_native_identity_or_alignment(tmp_path, monkeypatch):
from cbb_refresh_verify.management import Capability, ManagementRequest
from cbb_refresh_verify.native_management import NativeManagementAdapter
import cbb_refresh_verify.management_transaction as authority
source = tmp_path/'source/Block.prj'; source.parent.mkdir(); source.touch()
runtime = source.parent/('x'*150)/('y'*100+'.log')
runtime.parent.mkdir(); runtime.write_bytes(b'preserved history')
adapter = NativeManagementAdapter(mentor_home=tmp_path)
monkeypatch.setattr(adapter, 'capabilities', lambda: {'add': Capability(True, 'test boundary')})
monkeypatch.setattr(authority, 'current_lease', lambda _: pytest.fail('long path reached native transaction setup'))
request = ManagementRequest('update', tmp_path/'lib.lmc', 'Block', source=source)
staged, result = adapter.prepare_add_transaction(request, tmp_path/'evidence', original_source=source)
assert staged == request and result.identity is None and result.safe_to_continue
assert 'NATIVE_PATH_TOO_LONG' in result.detail and runtime.read_bytes() == b'preserved history'