322 lines
23 KiB
Python
322 lines
23 KiB
Python
"""Actual Windows EXE: batch import, native run, report, restart, rerun, report.
|
|
|
|
Use saved test copies. Expectations must be supplied before launch. This tool
|
|
never creates replacement receipts or changes task state directly in SQLite.
|
|
"""
|
|
import argparse
|
|
from contextlib import contextmanager
|
|
import json
|
|
import os
|
|
import shutil
|
|
from pathlib import Path
|
|
import sqlite3
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]/'src'))
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
from dx_productivity.trial_task_center.store import TaskStore
|
|
from dx_productivity.trial_task_center.verification.service import VerificationService
|
|
from dx_productivity.trial_task_center.native_netlist import _hash
|
|
from verification_closed_loop import freeze_inputs, verify_closed_loop, require
|
|
|
|
|
|
def main(argv=None):
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
for name in ('exe', 'before-root', 'after-root', 'output'):
|
|
parser.add_argument('--'+name, type=Path, required=True)
|
|
parser.add_argument('--expected-items', nargs='+', required=True, help='Exact relative paths, e.g. A/input.prj B/input.prj')
|
|
parser.add_argument('--expect-verdict', choices=('CONSISTENT', 'WARNING', 'DIFFERENT'), required=True)
|
|
parser.add_argument('--fault-batch', action='store_true', help='Use private copies and prepend one deliberately invalid database fixture')
|
|
parser.add_argument('--control-scenario', choices=('stop','owner-exit'), help='Separate C08 scenario; no fault switches in the product')
|
|
parser.add_argument('--handoff-data',type=Path,help='Existing disposable data root containing real saved execution receipts; never import a pair')
|
|
args = parser.parse_args(argv)
|
|
output = args.output.resolve(); exe = args.exe.resolve()
|
|
before, after = args.before_root.resolve(), args.after_root.resolve()
|
|
if output.exists(): parser.error('output must be a new directory')
|
|
if output.is_relative_to(before) or output.is_relative_to(after): parser.error('output must be outside input roots')
|
|
if len(set(args.expected_items))!=len(args.expected_items): parser.error('expected-items must be unique')
|
|
output.mkdir(parents=True)
|
|
# Import UI automation only for the EXE path; service acceptance remains portable.
|
|
import win32con
|
|
import win32gui
|
|
from packaged_window_driver import WindowDriver, window_image
|
|
from accept_packaged_ui import wait
|
|
data = args.handoff_data.resolve(strict=True) if args.handoff_data else output/'data'
|
|
if args.handoff_data and (args.fault_batch or args.control_scenario):parser.error('handoff-data is a separate scenario')
|
|
report = {'standard': 'verification-closed-loop-v1', 'validation_scope': 'independent-native:C01-C07',
|
|
'evidence_kind': 'native-packaged-ui', 'executable': str(exe), 'executable_sha256': _hash(exe),
|
|
'expected_items': args.expected_items, 'expected_verdict': args.expect_verdict, 'checks': [],
|
|
'not_run': ['C08:packaged-fault-cases', 'actual-successful-replacement-handoff', 'continuous-full-regression']}
|
|
if args.handoff_data:report['not_run'].remove('actual-successful-replacement-handoff')
|
|
if args.control_scenario:report['not_run'][0]='C08:'+('owner-exit' if args.control_scenario=='stop' else 'stop')
|
|
original = {str(p): _hash(p) for root in (before, after) for p in root.rglob('*') if p.is_file()}
|
|
failed_name = '00-invalid/input.prj' if args.fault_batch else None
|
|
if args.fault_batch:
|
|
for phase, source in (('before', before), ('after', after)):
|
|
destination = output/'inputs'/phase
|
|
shutil.copytree(source, destination)
|
|
fixture = destination/'00-invalid'; (fixture/'database').mkdir(parents=True)
|
|
(fixture/'input.prj').write_text('KEY RootBlock "board"\nKEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n', encoding='utf-8')
|
|
(fixture/'database/icdb.dat').write_bytes(b'deliberately-invalid-acceptance-fixture')
|
|
before, after = output/'inputs/before', output/'inputs/after'
|
|
original.update((str(p), _hash(p)) for root in (before, after) for p in root.rglob('*') if p.is_file())
|
|
report.update(evidence_kind='native-real-copies-and-invalid-database-fixture', expected_failed_items=[failed_name])
|
|
expected_names = sorted(args.expected_items + ([failed_name] if failed_name else []))
|
|
process = driver = main_window = None
|
|
|
|
def sql(query, params=()):
|
|
with sqlite3.connect(data/'task-center.db') as db:
|
|
db.row_factory = sqlite3.Row
|
|
return [dict(row) for row in db.execute(query, params)]
|
|
|
|
@contextmanager
|
|
def reader():
|
|
store = TaskStore(data/'task-center.db')
|
|
try: yield VerificationService(store, data)
|
|
finally: store.close()
|
|
|
|
def launch():
|
|
nonlocal process, driver, main_window
|
|
environment = dict(os.environ)
|
|
for name in ('PYTHONPATH', 'PYTHONHOME', 'DX_TASK_CENTER_HOME', 'QT_QPA_PLATFORM'): environment.pop(name, None)
|
|
p = subprocess.Popen([str(exe), '--data-root', str(data)], cwd=exe.parent, env=environment,
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=subprocess.CREATE_NO_WINDOW)
|
|
process = p
|
|
d = WindowDriver(p.pid, output); driver = d
|
|
def ready():
|
|
try: return d.main()
|
|
except TimeoutError:
|
|
require(p.poll() is None, 'Application exited before its main window appeared')
|
|
return None
|
|
window = wait(ready, timeout=90); main_window = window
|
|
d.click(d.find(automation_id='verification_navigation'))
|
|
return p, d, window
|
|
|
|
def close():
|
|
for window in driver.windows():
|
|
if win32gui.GetClassName(window.handle)=='#32770':
|
|
cancel = next((c for c in window.descendants(control_type='Button') if c.element_info.automation_id=='2'), None)
|
|
if cancel: win32gui.PostMessage(cancel.handle, win32con.BM_CLICK, 0, 0)
|
|
win32gui.PostMessage(main_window.handle, win32con.WM_CLOSE, 0, 0)
|
|
require(process.wait(45)==0, 'Application did not exit normally')
|
|
|
|
def run_record(run_id):
|
|
row = sql('SELECT * FROM verification_runs WHERE run_id=?', (run_id,))[0]
|
|
require(row['state']=='completed', 'Run stopped, blocked or interrupted')
|
|
return {'run_id': run_id, 'members': json.loads(row['members_json'])}
|
|
|
|
def finished(run_id):
|
|
rows = sql('SELECT state FROM verification_runs WHERE run_id=?', (run_id,))
|
|
return rows and rows[0]['state'] not in ('queued', 'running', 'stopping')
|
|
|
|
def export_report():
|
|
destination = output/'reports'; destination.mkdir(exist_ok=True)
|
|
previous = set(destination.glob('verification-*'))
|
|
driver.click(driver.find('导出报告', kind='Button'))
|
|
dialog = wait(lambda: next((w for w in driver.windows() if win32gui.GetClassName(w.handle)=='#32770'), None), timeout=15)
|
|
# The native folder picker has FolderName edit 1152, unlike file picker 1001.
|
|
field = next(c for c in dialog.descendants(control_type='Edit') if c.element_info.automation_id=='1152')
|
|
field.iface_value.SetValue(str(destination))
|
|
require(field.get_value()==str(destination), 'Folder picker path differs from the requested destination')
|
|
confirm = next(c for c in dialog.descendants(control_type='Button') if c.element_info.automation_id=='1')
|
|
win32gui.PostMessage(confirm.handle, win32con.BM_CLICK, 0, 0)
|
|
wait(lambda: not win32gui.IsWindow(dialog.handle), timeout=20)
|
|
return wait(lambda: next((p for p in destination.glob('verification-*') if p not in previous and (p/'task.json').is_file()), None), timeout=60)
|
|
|
|
def selected_cell(name):
|
|
tables = [w for kind in ('Table', 'DataGrid') for w in main_window.descendants(control_type=kind) if w.is_visible()]
|
|
table = min(tables, key=lambda w: w.rectangle().left)
|
|
return next((c for c in table.children() if c.window_text()==name), None)
|
|
|
|
def verify_report(scope, run, directory, label, *, selected_only=False):
|
|
with reader() as service:
|
|
validation = verify_closed_loop(service, task, scope, run, directory,
|
|
expected_verdicts={row['item_id']: args.expect_verdict for row in scope['items']}, selected_only=selected_only)
|
|
(output/(label+'.json')).write_text(json.dumps(validation, ensure_ascii=False, indent=2), encoding='utf-8')
|
|
report['checks'].extend(validation['checks'])
|
|
require(validation['passed'], label+' failed: '+json.dumps(validation['checks'], ensure_ascii=False))
|
|
|
|
def import_pairs_ui(*, create=True):
|
|
if create:
|
|
driver.click(wait(lambda: driver.find(automation_id='verificationNew')))
|
|
dialog = wait(lambda: next((w for w in driver.windows() if w.window_text()=='新建校验'), None))
|
|
ok = next(b for b in dialog.descendants(control_type='Button') if b.window_text().replace('&', '') in ('OK', '确定'))
|
|
driver.click(ok)
|
|
button = wait(lambda: driver.find(automation_id='verificationBatchImport'))
|
|
wait(lambda: button.is_enabled()); driver.click(button)
|
|
dialog = wait(lambda: next((w for w in driver.windows() if w.window_text()=='批量添加工程对'), None))
|
|
driver.type_text(driver.find(automation_id='verificationBeforeRoot', window=dialog), str(before))
|
|
driver.type_text(driver.find(automation_id='verificationAfterRoot', window=dialog), str(after))
|
|
driver.click(driver.find(automation_id='verificationScanPairs', window=dialog))
|
|
button = driver.find(automation_id='verificationImportPairs', window=dialog)
|
|
wait(lambda: button.is_enabled()); window_image(dialog.handle).save(output/'01-pairs.png')
|
|
driver.click(button)
|
|
wait(lambda: driver.find(f'本批导入完成:已导入 {len(expected_names)} 对,失败 0 对;尚未开始校验。', kind='Text', window=dialog), timeout=120)
|
|
driver.click(driver.find('关闭', kind='Button', window=dialog))
|
|
|
|
try:
|
|
process, driver, main_window = launch()
|
|
reduce = driver.find('减少透明度', kind='Button', optional=True)
|
|
if reduce: driver.click(reduce)
|
|
if args.handoff_data:
|
|
wait(lambda:len(sql('SELECT item_id FROM verification_items'))==len(args.expected_items),timeout=30)
|
|
report['evidence_kind']='native-packaged-real-saved-handoff'
|
|
else:
|
|
import_pairs_ui()
|
|
imported = sql('SELECT * FROM verification_items ORDER BY display_name')
|
|
require([r['display_name'] for r in imported]==expected_names, 'Imported names differ from the predeclared scope')
|
|
require(not sql('SELECT attempt_id FROM verification_attempts'), 'Import started verification automatically')
|
|
task = imported[0]['task_id']
|
|
with reader() as service: scope = freeze_inputs(service, task, expected_count=len(expected_names),historical_before=bool(args.handoff_data))
|
|
(output/'frozen-scope.json').write_text(json.dumps(dict(scope, expected_verdict=args.expect_verdict,
|
|
expected_failed_items=[failed_name] if failed_name else []), ensure_ascii=False, indent=2), encoding='utf-8')
|
|
driver.click(driver.find('开始全部', kind='Button'))
|
|
run_id = wait(lambda: next((r['run_id'] for r in sql('SELECT run_id FROM verification_runs')), None))
|
|
if args.control_scenario:
|
|
from tools.verification_control_acceptance import verify_stopped, verify_unknown
|
|
active = wait(lambda: next((r for r in sql("SELECT * FROM verification_attempts WHERE state='running'")), None), timeout=30)
|
|
pending = {r['attempt_id'] for r in sql("SELECT attempt_id FROM verification_attempts WHERE state='queued'")}
|
|
require(bool(pending), 'No undispatched member remains; this scenario is inconclusive')
|
|
raw_run = sql('SELECT members_json FROM verification_runs WHERE run_id=?', (run_id,))[0]
|
|
original_run = {'run_id':run_id,'members':json.loads(raw_run['members_json'])}
|
|
if args.control_scenario=='stop':
|
|
stop_button = driver.find('停止', kind='Button')
|
|
wait(lambda:stop_button.is_enabled(), timeout=10); driver.click(stop_button)
|
|
wait(lambda:finished(run_id), timeout=120)
|
|
with reader() as service: checked = verify_stopped(service, original_run, unstarted=pending)
|
|
require(checked['passed'], str(checked))
|
|
report['checks'].append({'check_id':'C08:stop','state':'passed','details':checked})
|
|
stopped_snapshot = sql('SELECT attempt_id,state,generation FROM verification_attempts ORDER BY attempt_id')
|
|
window_image(main_window.handle).save(output/'stop.png')
|
|
close(); process,driver,main_window=launch()
|
|
wait(lambda:selected_cell(sorted(args.expected_items)[0]), timeout=30)
|
|
time.sleep(2)
|
|
require(sql('SELECT attempt_id,state,generation FROM verification_attempts ORDER BY attempt_id')==stopped_snapshot,
|
|
'Restart replayed a stopped batch')
|
|
old_runs={r['run_id'] for r in sql('SELECT run_id FROM verification_runs')}
|
|
driver.click(driver.find('开始全部',kind='Button'))
|
|
continuation=wait(lambda:next((r['run_id'] for r in sql('SELECT run_id FROM verification_runs') if r['run_id'] not in old_runs),None),timeout=30)
|
|
wait(lambda:finished(continuation),timeout=900)
|
|
continued=run_record(continuation)
|
|
require({r['item_id'] for r in continued['members']}=={m['item_id'] for m in original_run['members'] if m['attempt_id'] in pending},
|
|
'Continue did not run exactly the previously undispatched items')
|
|
subset=dict(scope,items=[r for r in scope['items'] if r['item_id'] in {m['item_id'] for m in continued['members']}])
|
|
verify_report(subset,continued,export_report(),'continued',selected_only=True)
|
|
stopped_ids={r['item_id'] for r in sql("SELECT item_id FROM verification_attempts WHERE state='cancelled' AND reason!='run_stopped_before_dispatch'")}
|
|
for item_id in stopped_ids:
|
|
name=next(r['display_name'] for r in imported if r['item_id']==item_id)
|
|
driver.click(wait(lambda:selected_cell(name),timeout=20))
|
|
existing={r['run_id'] for r in sql('SELECT run_id FROM verification_runs')}
|
|
button=driver.find('重校验',kind='Button');wait(button.is_enabled,timeout=15);driver.click(button)
|
|
new_id=wait(lambda:next((r['run_id'] for r in sql('SELECT run_id FROM verification_runs') if r['run_id'] not in existing),None),timeout=30)
|
|
wait(lambda:finished(new_id),timeout=900)
|
|
subset=dict(scope,items=[r for r in scope['items'] if r['item_id']==item_id])
|
|
verify_report(subset,run_record(new_id),export_report(),'stopped-item-rerun',selected_only=True)
|
|
report['checks'].append({'check_id':'C08:continue-and-explicit-rerun','state':'passed'})
|
|
else:
|
|
import win32api,win32process
|
|
owner_path=data/'verification/attempts'/active['attempt_id']/'owner.json'
|
|
owner=json.loads(owner_path.read_text(encoding='utf-8'))
|
|
require(owner['process']['pid']==process.pid and owner['owner_id']==active['owner_id'], 'Owner does not belong to this test')
|
|
require(not (owner_path.parent/'resource-released.json').exists(), 'Too late: resources already released')
|
|
handle=win32api.OpenProcess(0x1000|0x100000|1,False,process.pid)
|
|
try:
|
|
require(str(win32process.GetProcessTimes(handle)['CreationTime'])==owner['process']['creation_time'], 'PID creation identity changed')
|
|
require(sql('SELECT state FROM verification_attempts WHERE attempt_id=?',(active['attempt_id'],))[0]['state']=='running','Attempt already finished')
|
|
# This Popen handle belongs exclusively to the disposable acceptance instance.
|
|
process.terminate();process.wait(30)
|
|
finally:handle.Close()
|
|
(output/'owner-exit.json').write_text(json.dumps(owner,ensure_ascii=False,indent=2),encoding='utf-8')
|
|
attempt_count=len(sql('SELECT attempt_id FROM verification_attempts'))
|
|
process,driver,main_window=launch()
|
|
wait(lambda:sql('SELECT resource_state FROM verification_attempts WHERE attempt_id=?',(active['attempt_id'],))[0]['resource_state']=='unknown',timeout=30)
|
|
with reader() as service: checked=verify_unknown(service,active['attempt_id'],expected_attempts=attempt_count)
|
|
require(checked['passed'],str(checked))
|
|
# New task creation must remain available while the native executor is blocked.
|
|
driver.click(driver.find(automation_id='verificationNew'))
|
|
dialog=wait(lambda:next((w for w in driver.windows() if w.window_text()=='新建校验'),None),timeout=15)
|
|
ok=next(b for b in dialog.descendants(control_type='Button') if b.window_text().replace('&','') in ('OK','确定'));driver.click(ok)
|
|
wait(lambda:len(sql('SELECT task_id FROM verification_tasks'))==2,timeout=15)
|
|
import_pairs_ui(create=False)
|
|
require(len(sql('SELECT item_id FROM verification_items'))==2*len(args.expected_items), 'Resource protection blocked input registration')
|
|
require(len(sql('SELECT attempt_id FROM verification_attempts'))==attempt_count, 'Unknown resources caused an automatic attempt')
|
|
require(not any((data/'verification/attempts'/aid).exists() for aid in pending),'Undispatched item launched native export')
|
|
window_image(main_window.handle).save(output/'resource-unknown.png')
|
|
report['checks'].append({'check_id':'C08:owner-exit-remains-unknown','state':'passed','details':checked})
|
|
require(all(_hash(Path(p))==sha for p,sha in original.items()), 'Source inputs changed')
|
|
require(sql('SELECT COUNT(*) total FROM execution_operations')[0]['total']==0, 'Replacement operations occurred')
|
|
close();report.update(passed=True,validation_scope='C08:'+args.control_scenario)
|
|
return 0
|
|
wait(lambda: finished(run_id), timeout=900)
|
|
run = run_record(run_id)
|
|
if failed_name:
|
|
failed_item = next(r['item_id'] for r in imported if r['display_name']==failed_name)
|
|
with reader() as service:
|
|
failure = service.read_item(failed_item)
|
|
require(run['members'][0]['item_id']==failed_item, 'Fault fixture did not run before the healthy projects')
|
|
require(failure['attempt_state']=='failed' and failure['verdict']=='INCOMPLETE'
|
|
and failure['attempt']['result']['resource_state']=='released', 'Invalid database did not fail and release resources as expected')
|
|
report['checks'].append({'check_id': 'C08:failed-native-item-continues', 'state': 'passed',
|
|
'item_id': failed_item, 'observed_state': failure['attempt_state'], 'observed_verdict': failure['verdict']})
|
|
report['not_run'][0] = 'C08:packaged-stop-and-uncertain-resources'
|
|
scope = dict(scope, items=[row for row in scope['items'] if row['item_id']!=failed_item])
|
|
run = dict(run, members=[row for row in run['members'] if row['item_id']!=failed_item])
|
|
name = sorted(args.expected_items)[0]
|
|
driver.click(wait(lambda: selected_cell(name)))
|
|
label = {'CONSISTENT': '一致', 'WARNING': '需关注', 'DIFFERENT': '异常'}[args.expect_verdict]
|
|
wait(lambda: driver.find(name+' · '+label, kind='Text'), timeout=60)
|
|
window_image(main_window.handle).save(output/'02-result.png')
|
|
directory = export_report(); verify_report(scope, run, directory, 'first-run', selected_only=bool(failed_name))
|
|
first_report_hashes = {str(p): _hash(p) for p in directory.iterdir() if p.is_file()}
|
|
attempts = sql('SELECT attempt_id,input_version,generation,state,result_json FROM verification_attempts ORDER BY attempt_id')
|
|
report['first_report'] = str(directory)
|
|
close(); process, driver, main_window = launch()
|
|
driver.click(wait(lambda: selected_cell(name)))
|
|
wait(lambda: driver.find(name+' · '+label, kind='Text'), timeout=60)
|
|
# Wait for startup recovery and at least one normal workspace polling interval.
|
|
time.sleep(2)
|
|
require(sql('SELECT attempt_id,input_version,generation,state,result_json FROM verification_attempts ORDER BY attempt_id')==attempts, 'Restart changed attempts or started another run')
|
|
verify_report(scope, run, directory, 'restart-readback', selected_only=bool(failed_name))
|
|
report['checks'].append({'check_id': 'C06:restart', 'state': 'passed'})
|
|
window_image(main_window.handle).save(output/'03-restart.png')
|
|
old_ids = {r['run_id'] for r in sql('SELECT run_id FROM verification_runs')}
|
|
driver.click(driver.find('重校验', kind='Button'))
|
|
rerun_id = wait(lambda: next((r['run_id'] for r in sql('SELECT run_id FROM verification_runs') if r['run_id'] not in old_ids), None))
|
|
wait(lambda: finished(rerun_id), timeout=900)
|
|
rerun = run_record(rerun_id)
|
|
subset = dict(scope, items=[r for r in scope['items'] if r['display_name']==name])
|
|
member = rerun['members'][0]
|
|
previous = next(r for r in attempts if r['attempt_id']==next(m['attempt_id'] for m in run['members'] if m['item_id']==member['item_id']))
|
|
latest = sql('SELECT * FROM verification_attempts WHERE attempt_id=?', (member['attempt_id'],))[0]
|
|
require(latest['generation']==previous['generation']+1 and latest['input_version']==previous['input_version'], 'Rerun did not create the next attempt on the same input')
|
|
require(all(_hash(Path(p))==sha for p, sha in first_report_hashes.items()), 'Rerun overwrote the first report')
|
|
require(sql('SELECT result_json FROM verification_attempts WHERE attempt_id=?', (previous['attempt_id'],))[0]['result_json']==previous['result_json'], 'Rerun changed the old result')
|
|
second_report = export_report()
|
|
verify_report(subset, rerun, second_report, 'rerun', selected_only=True)
|
|
report['checks'].append({'check_id': 'C07:rerun', 'state': 'passed'})
|
|
report['rerun_report'] = str(second_report)
|
|
require(all(_hash(Path(p))==sha for p, sha in original.items()), 'Input files changed during the loop')
|
|
require(sql('SELECT COUNT(*) total FROM execution_operations')[0]['total']==scope['replacement_operations_before'], 'Verification replayed replacement')
|
|
close(); report['passed'] = True
|
|
except Exception as error:
|
|
report.update(passed=False, reason=str(error), error=type(error).__name__)
|
|
if driver:
|
|
for n, window in enumerate(driver.windows()):
|
|
try: window_image(window.handle).save(output/f'failure-{n}.png')
|
|
except Exception: pass
|
|
if process and process.poll() is None and main_window:
|
|
try: close()
|
|
except Exception: report['process_left_running'] = process.pid
|
|
elif process and process.poll() is None:
|
|
report['process_left_running'] = process.pid
|
|
finally:
|
|
report['input_sha256'] = original
|
|
(output/'acceptance.json').write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding='utf-8')
|
|
print(json.dumps(report, ensure_ascii=False))
|
|
return 0 if report.get('passed') else 1
|
|
|
|
|
|
if __name__=='__main__': raise SystemExit(main())
|