Files
dxdesigner-part-replacement/tests/packaging/test_random_cad_acceptance.py
T

408 lines
22 KiB
Python

import importlib.util
import hashlib
import json
import sqlite3
from pathlib import Path
import pytest
def module():
spec = importlib.util.spec_from_file_location('random_cad', Path(__file__).parents[2] / 'tools/accept_random_cad.py')
result = importlib.util.module_from_spec(spec)
spec.loader.exec_module(result)
return result
def fixture(tmp_path):
source = tmp_path / 'original'
source.mkdir()
(source / 'board.prj').write_text('original project')
(source / 'database').mkdir()
(source / 'database/icdb.dat').write_bytes(b'authentic database')
graph = {'components': {f'U{i}': {'refdes': f'R{i}', 'attributes': {'Part Number': f'4100-{i}', 'Value': f'{i}K'}, 'pins': {}} for i in range(1, 8)}}
return source / 'board.prj', graph
def test_replay_varies_semantic_inputs_and_preserves_sources(tmp_path):
m = module()
source, graph = fixture(tmp_path)
before = m.fingerprint(source.parent)
a = m.prepare(tmp_path / 'a', source, graph, ['TARGET'], seed=123)
b = m.prepare(tmp_path / 'b', source, graph, ['TARGET'], seed=123)
c = m.prepare(tmp_path / 'c', source, graph, ['TARGET'], seed=456)
semantic = lambda manifest: [{k: case[k] for k in ('kind', 'requests', 'target_cell')} for case in manifest['cases']]
assert semantic(a) == semantic(b)
assert sum(x != y for x, y in zip(semantic(a), semantic(c))) >= 15
assert len(a['cases']) >= 20
assert len({json.dumps(case['requests'], sort_keys=True) for case in a['cases']}) == len(a['cases'])
assert m.fingerprint(source.parent) == before
assert (tmp_path / 'a/seed.json').is_file()
for case in a['cases']:
assert Path(case['project']).resolve().is_relative_to((tmp_path / 'a/projects').resolve())
def test_refuses_source_ancestry_or_existing_output(tmp_path):
m = module()
source, graph = fixture(tmp_path)
with pytest.raises(ValueError):
m.prepare(source.parent / 'copy', source, graph, ['TARGET'])
with pytest.raises(ValueError):
m.prepare(tmp_path, source, graph, ['TARGET'])
def test_code_fingerprint_prunes_installed_dependencies_but_keeps_assets(tmp_path):
m = module()
(tmp_path / 'node_modules').mkdir()
(tmp_path / 'node_modules/dependency.js').write_text('installed package')
(tmp_path / '__pycache__').mkdir()
(tmp_path / '__pycache__/module.pyc').write_bytes(b'cache')
(tmp_path / 'source.py').write_text('source')
(tmp_path / 'icon.ico').write_bytes(b'asset')
assert set(m.code_fingerprint(tmp_path)) == {'source.py', 'icon.ico'}
assert set(m.fingerprint(tmp_path)) == {'source.py', 'icon.ico', 'node_modules/dependency.js', '__pycache__/module.pyc'}
def test_rule_mode_selects_one_globally_unique_root_resistor_with_real_defaults(tmp_path):
m = module()
source, graph = fixture(tmp_path)
for comp in graph['components'].values():
comp['pins'] = {'1': {}, '2': {}}
comp['instance_path'] = 'child'
graph['components']['U1'].pop('instance_path')
graph['components']['U2'].pop('instance_path')
graph['components']['child_duplicate'] = dict(graph['components']['U1'], instance_path='child2')
before = m.fingerprint(source.parent)
result = m.prepare(tmp_path / 'rule', source, graph, ['4100-CA0000-T0'], count=1, supplemental='rule_mode')
case, = result['cases']
request, = case['requests']
assert request == {'refdes': 'R2', 'old_part_number': '4100-2', 'new_part_number': '4100-CA0000-T0',
'value_mode': 'preserve', 'custom_value': '', 'sync_nc': True, 'enabled': True}
assert case['kind'] == 'rule_mode' and not case['target_cell']
assert json.loads((tmp_path / 'rule/seed.json').read_text())['system_seed'] is True
assert m.fingerprint(source.parent) == before
@pytest.mark.parametrize('count,targets', [(2, ['4100-CA0000-T0']), (1, ['OTHER'])])
def test_rule_mode_is_one_real_t0_supplement(tmp_path, count, targets):
m = module()
source, graph = fixture(tmp_path)
with pytest.raises(ValueError, match='rule_mode'):
m.prepare(tmp_path / 'rule', source, graph, targets, count=count, supplemental='rule_mode')
def test_rule_mode_refuses_ambiguous_all_match_scope(tmp_path):
m = module()
source, graph = fixture(tmp_path)
for comp in graph['components'].values():
comp['attributes']['Part Number'] = '4100-SHARED'
comp['pins'] = {'1': {}, '2': {}}
with pytest.raises(ValueError, match='unique root resistor'):
m.prepare(tmp_path / 'rule', source, graph, ['4100-CA0000-T0'], count=1, supplemental='rule_mode')
@pytest.mark.parametrize('fault', [None, 'missing_rule_snapshot', 'wrong_uid', 'cancelled', 'missing_after'])
def test_rule_mode_requires_application_snapshot_bound_uid_and_wholegraph_report(tmp_path, fault):
m = module()
project = tmp_path / 'copy.prj'
project.touch()
request = {'refdes': 'RV15', 'old_part_number': '4100-CA1020-T7', 'new_part_number': '4100-CA0000-T0',
'value_mode': 'preserve', 'custom_value': '', 'sync_nc': True, 'enabled': True}
case = {'project': str(project), 'requests': [request]}
before = {'components': {'u1': {'refdes': 'RV15', 'attributes': {'Part Number': request['old_part_number']}}}}
directory = tmp_path / 'data/tasks/task/netlist-checks/queue'
directory.mkdir(parents=True)
plan = {'rows': [], 'copy_value': False, 'project_path': str(project), 'rules': [{
'old_part': request['old_part_number'].casefold(), 'new_part': request['new_part_number'].casefold(),
'value_mode': 'preserve', 'custom_value': '', 'sync_nc': '1', 'target_cell': ''}]}
report = {'status': 'verified', 'verified': True, 'before': {'status': 'success', 'complete': True},
'after': {'status': 'success', 'complete': True}, 'expected_changes': {'u1': {}},
'rule_scopes': [{'project_path': str(project), 'schematic_name': 'MAIN', 'sheet_number': '1',
'old_part': request['old_part_number'], 'new_part': request['new_part_number'], 'source_uids': ['u1']}]}
if fault == 'missing_rule_snapshot':
plan.pop('rules')
elif fault == 'wrong_uid':
report['rule_scopes'][0]['source_uids'] = ['other']
elif fault == 'cancelled':
report.update(status='cancelled', verified=False)
elif fault == 'missing_after':
report['after'] = None
(directory / 'plan.json').write_text(json.dumps(plan))
path = directory / 'verification.json'
path.write_text(json.dumps(report))
task = {'task_id': 'task', 'target_plan_mode': 0, 'copy_part_number_to_value': 0}
entry = {'netlist_verification': {'report_path': str(path)}}
if fault:
with pytest.raises((AssertionError, KeyError, TypeError)):
m.rule_netlist_evidence(tmp_path, task, entry, case, before)
else:
assert m.rule_netlist_evidence(tmp_path, task, entry, case, before)['bound_uid'] == 'u1'
def test_verifier_reads_each_queues_own_per_task_netlist_mapping(tmp_path):
m = module()
queues = tmp_path / 'data/task-queues'
queues.mkdir(parents=True)
(queues / 'first.json').write_text(json.dumps({'entries': {'A': {'state': 'completed'}},
'netlist_verification': {'A': {'report_path': 'own-A', 'status': 'verified'}}}))
(queues / 'second.json').write_text(json.dumps({'entries': {'B': {'state': 'completed'}},
'netlist_verification': {'A': {'report_path': 'foreign-A', 'status': 'verified'}}}))
entries = m.queue_entries(tmp_path)
assert entries['A']['netlist_verification']['report_path'] == 'own-A'
assert entries['B'] == {'state': 'completed'}
def test_verifier_can_pin_the_actual_new_execution_and_keep_old_interruption(tmp_path):
m = module()
queues = tmp_path / 'data/task-queues'
queues.mkdir(parents=True)
old = queues / ('a' * 32 + '.json')
old.write_text(json.dumps({'entries': {'A': {'state': 'blocked'}}}))
current = queues / ('b' * 32 + '.json')
current.write_text(json.dumps({'entries': {'A': {'state': 'completed'}},
'netlist_verification': {'A': {'report_path': 'current', 'status': 'verified'}}}))
before = old.read_bytes()
assert m.queue_entries(tmp_path, queue_id='b' * 32)['A']['netlist_verification']['report_path'] == 'current'
assert old.read_bytes() == before
with pytest.raises((ValueError, FileNotFoundError)):
m.queue_entries(tmp_path, queue_id='../outside')
with pytest.raises(FileNotFoundError):
m.queue_entries(tmp_path, queue_id='c' * 32)
@pytest.mark.parametrize('state,cleanup,passed', [
('released', None, True), ('released', {}, True),
('released', {'errors': ['actual cleanup failure']}, False),
('released', [], False), ('blocked', None, False)])
def test_verifier_handles_optional_cleanup_without_ignoring_resource_errors(tmp_path, state, cleanup, passed):
m = module()
source, _ = fixture(tmp_path)
baseline = tmp_path / 'baseline.json'
baseline.write_text('{"components":{}}')
baseline.with_name('effective.txt').write_text('')
(tmp_path / 'manifest.json').write_text(json.dumps({'baseline': str(baseline), 'source': str(source),
'source_hashes': m.fingerprint(source.parent), 'cases': []}))
data = tmp_path / 'data'
data.mkdir()
with sqlite3.connect(data / 'task-center.db') as db:
db.execute('CREATE TABLE tasks (task_id TEXT)')
db.execute('CREATE TABLE projects (project_id TEXT)')
session = data / 'batch-resources' / ('a' * 32) / 'session.json'
session.parent.mkdir(parents=True)
session.write_text(json.dumps({'state': state, 'cleanup': cleanup}))
report = m.verify(tmp_path)
assert report['passed'] is passed
assert bool(report['resource_errors']) is not passed
def saved_fixture(root, jobs_and_uids):
events = root / 'data/tasks/task/events.jsonl'
events.parent.mkdir(parents=True)
with sqlite3.connect(root / 'data/task-center.db') as db:
db.execute('CREATE TABLE execution_rounds (task_id TEXT, round_id TEXT)')
db.execute('CREATE TABLE execution_operations (task_id TEXT, round_id TEXT, command_json TEXT, receipt_json TEXT)')
db.execute("INSERT INTO execution_rounds VALUES ('task','current')")
for job, uid in jobs_and_uids:
command = {'target_scope': 'targets', 'target_selectors': uid+'\x1fR1\x1f10K'}
receipt = {'job_id': job, 'native_state': 'saved_ok'}
db.execute('INSERT INTO execution_operations VALUES (?,?,?,?)', ('task', 'current', json.dumps(command), json.dumps(receipt)))
events.write_text('\n'.join(json.dumps({'operation': 'quick_run_executed', 'message': 'identical display message',
'details': {'state': 'saved_ok', 'timing': {'job_id': job}}}) for job, uid in jobs_and_uids), encoding='utf-8')
return events
def test_saved_uid_counts_bind_identical_messages_to_distinct_job_selectors(tmp_path):
m = module()
saved_fixture(tmp_path, [('job1', '$2I1'), ('job2', '$2I2')])
assert m.saved_uids(tmp_path) == {'task:$2I1': 1, 'task:$2I2': 1}
def test_saved_uid_replay_with_new_job_increments_original_uid(tmp_path):
m = module()
saved_fixture(tmp_path, [('job1', '$2I1'), ('job2', '$2I1')])
assert m.saved_uids(tmp_path) == {'task:$2I1': 2}
@pytest.mark.parametrize('fault', ['missing_job', 'wrong_receipt_job', 'past_round', 'missing_uid'])
def test_saved_uid_missing_current_round_binding_is_insufficient_not_message_guess(tmp_path, fault):
m = module()
path = saved_fixture(tmp_path, [('job1', '$2I1')])
if fault == 'missing_job':
event = json.loads(path.read_text())
event['details']['timing'] = {}
path.write_text(json.dumps(event))
else:
with sqlite3.connect(tmp_path / 'data/task-center.db') as db:
if fault == 'past_round':
db.execute("UPDATE execution_operations SET round_id='old'")
elif fault == 'wrong_receipt_job':
db.execute('UPDATE execution_operations SET receipt_json=?', (json.dumps({'job_id': 'other', 'native_state': 'saved_ok'}),))
else:
db.execute('UPDATE execution_operations SET command_json=?', (json.dumps({'target_scope': 'targets', 'target_selectors': ''}),))
with pytest.raises(ValueError, match='Acceptance evidence insufficient'):
m.saved_uids(tmp_path)
@pytest.mark.parametrize('tampered', [False, True])
def test_saved_rule_uid_comes_from_hash_bound_native_snapshot(tmp_path, tampered):
m = module()
saved_fixture(tmp_path, [('job1', '$2I1')])
project = tmp_path / 'board.prj'
snapshot = tmp_path / 'data/tasks/task/execution-proof/snapshot-job1.tsv'
snapshot.parent.mkdir()
snapshot.write_text('\t'.join(['M', str(project), 'OLD', 'NEW', 'MAIN:1', 'preserve', '', '1'])+'\n'
+'\t'.join(['C', '$2I1', 'R1', '0', '0', '0', '0', ''])+'\n', encoding='utf-8')
proof = Path(str(snapshot)+'.native.json')
native = {'ok': True, 'stage': 'native_replace_verified', 'batch_attempt': {'task_id': 'task', 'job_id': 'job1'},
'transaction': {'snapshot_sha256': hashlib.sha256(snapshot.read_bytes()).hexdigest(),
'project_path': m.os.path.normcase(m.os.path.abspath(project)), 'sheet_identity': 'MAIN:1',
'old_part_number': 'OLD', 'new_part_number': 'NEW', 'source_uids': ['$2I1'],
'target_selectors': None, 'library_row': ['R', 'NEW', 'lib:r', '', 'NAME', 'LABEL']}}
proof.write_text(json.dumps(native), encoding='utf-8')
receipt = {'job_id': 'job1', 'native_state': 'saved_ok', 'proof': {'files': [
{'path': str(p), 'sha256': hashlib.sha256(p.read_bytes()).hexdigest()} for p in (snapshot, proof)]}}
command = {'target_scope': 'all', 'target_selectors': '', 'project_path': str(project),
'old_part': 'OLD', 'new_part': 'NEW', 'schematic_name': 'MAIN', 'sheet_number': '1'}
with sqlite3.connect(tmp_path / 'data/task-center.db') as db:
db.execute('UPDATE execution_operations SET command_json=?,receipt_json=?', (json.dumps(command), json.dumps(receipt)))
if tampered:
snapshot.write_text(snapshot.read_text().replace('$2I1', '$2I2'))
with pytest.raises(ValueError, match='Acceptance evidence insufficient'):
m.saved_uids(tmp_path)
else:
assert m.saved_uids(tmp_path) == {'task:$2I1': 1}
@pytest.mark.parametrize('missing', [None, 'force', 'recovery','session_pid','frame'])
@pytest.mark.parametrize('report_name', ['packaged-ui-report.json', 'terminal-ui-recheck.json'])
def test_force_oracle_reads_actual_packaged_driver_steps(tmp_path, missing, report_name):
m = module()
executable=tmp_path/'candidate.exe'
executable.write_bytes(b'frozen test candidate')
sha=hashlib.sha256(executable.read_bytes()).hexdigest()
steps = [{'step':'real-ui-force-confirmed','frames':[{'stage':'native_replace','token':'a'*32,'job_id':'actual_job','sequence':4}]}, {'step':'real-ui-recovery-finished'}]
if missing in {'force','recovery'}:
steps = [s for s in steps if missing not in s['step']]
if missing=='frame':
steps[0]['frames'][0]['sequence']=0
(tmp_path/'ui-session.json').write_text(json.dumps({'pid':99 if missing=='session_pid' else 42,'executable':str(executable),'exe_sha256':sha}))
(tmp_path/report_name).write_text(json.dumps({'stage':'execute','passed':True,'steps':steps,
'mode':'frozen EXE external Windows UIA','root':str(tmp_path),'pid':42,'exe':str(executable),'exe_sha256':sha}))
options = {'ui_report': report_name} if report_name != 'packaged-ui-report.json' else {}
if missing:
with pytest.raises(AssertionError):
m.force_recovery_evidence(tmp_path, **options)
else:
assert m.force_recovery_evidence(tmp_path, **options)['recovery_observed'] is True
def test_pon_root_preparation_never_selects_child_or_reused_uid_or_part(tmp_path):
m = module()
source, graph = fixture(tmp_path)
for comp in graph['components'].values():
comp['instance_path']='child'
comp['pins']={'1':{},'2':{}}
graph['components']['U1'].pop('instance_path')
graph['components']['U2'].pop('instance_path')
graph['components']['child-reused'] = dict(graph['components']['U1'],source_uid='U1',refdes='CHILD',
attributes={'Part Number':'OTHER'},instance_path='child')
result=m.prepare(tmp_path/'pon',source,graph,['4100-CA0000-T0'],count=1,supplemental='pon_root')
case,=result['cases']
request,=case['requests']
assert request['refdes']=='R2' and case['kind']=='pon_root'
def test_identity_raw_oracle_preserves_duplicate_gates_and_child_attributes(tmp_path):
m = module()
text = (' Symbol $1I1\tref des R1\tpage \n\tPath root\n\tSymbol name r.1 type Primitive\n'
'\tProp Ref Designator R1\n\tProp Part Number OLD\n\tProp Value 10K\n'
' Symbol $2I2\tref des U1\tpage \n\tPath child\n\tSymbol name ic.1 type Primitive\n'
'\tProp Ref Designator U1\n\tProp Part Number IC\n\tProp Part Name \n\tProp Part Name ICNAME\n'
' Symbol $2I3\tref des U1\tpage \n\tPath child\n\tSymbol name ic.2 type Primitive\n'
'\tProp Ref Designator U1\n\tProp Part Number IC\n')
before=tmp_path/'before.txt'; after=tmp_path/'after.txt'
before.write_text(text); after.write_text(text.replace('Part Number OLD','Part Number NEW').replace('Value 10K','Value NATIVE'))
a=m.raw_identity_attributes(before); b=m.raw_identity_attributes(after)
assert m.unselected_raw_identities(a,{'$1I1'}) == m.unselected_raw_identities(b,{'$1I1'})
assert m.selected_root_attributes(b,{'$1I1'}) == {'R1':{'Part Number':'NEW','Value':'NATIVE'}}
after.write_text(after.read_text().replace('Part Name ICNAME','Part Name CHANGED'))
assert m.unselected_raw_identities(a,{'$1I1'}) != m.unselected_raw_identities(m.raw_identity_attributes(after),{'$1I1'})
def test_identity_raw_multiset_does_not_collapse_repeated_ground_symbols(tmp_path):
m=module()
symbol=' Symbol GND\tref des \tpage \n\tPath root\n\tSymbol name gnd.1 type \n\tProp Value GND\n'
path=tmp_path/'raw.txt'
path.write_text(symbol*2)
before=m.unselected_raw_identities(m.raw_identity_attributes(path),set())
path.write_text(symbol)
after=m.unselected_raw_identities(m.raw_identity_attributes(path),set())
assert sum(before.values())==2 and sum(after.values())==1 and before!=after
def observation(sequence, stage, pn, value='NATIVE', *, count=None, window=None):
fields = [('Part Number', pn), ('Part Label', 'LABEL')]
if value is not None:
fields.append(('Value', value))
if window is None:
window = ('before_native' if pn == 'OLD' else 'saved_readback' if stage == 'open_sheet'
else 'restoring' if stage == 'snapshot_attributes' else 'pre_restore')
lines = [f'O\tjob\tu1\t{stage}\t{sequence}.5\t{sequence}\t{window}']
lines += [f'E\tu1\t{i}\t{name}\t{text}\t\tunknown' for i, (name, text) in enumerate(fields)]
lines.append(f'Z_OBS\tjob\tu1\t{stage}\t{sequence}\t{len(fields) if count is None else count}\t{window}')
return '\n'.join(lines)+'\n'
def test_native_value_observation_is_count_bound_and_precedes_restoration():
m = module()
text = observation(1, 'query_components', 'OLD', 'OLD_VALUE')
text += observation(2, 'query_components', 'NEW', None)
text += observation(3, 'snapshot_attributes', 'NEW', None)
text += observation(4, 'open_sheet', 'NEW', None)
actual = m.native_value_observation(text, job='job', uid='u1', old='OLD', new='NEW')
assert actual['value'] == '' and actual['present'] is False
assert actual['sequence'] == 2 and actual['count'] == 2
assert actual['reopened_present'] is False
def test_native_window_remains_before_restoration_when_another_uid_changed_stage_label():
m = module()
text = observation(1, 'query_components', 'OLD', 'OLD_VALUE')
text += observation(2, 'snapshot_attributes', 'NEW', window='pre_restore')
text += observation(3, 'query_components', 'NEW', window='restoring')
text += observation(4, 'open_sheet', 'NEW')
assert m.native_value_observation(text, job='job', uid='u1', old='OLD', new='NEW')['sequence'] == 2
@pytest.mark.parametrize('fault', ['truncated', 'wrong_job', 'wrong_uid', 'wrong_part', 'wrong_count',
'missing_ordinal', 'postrestore', 'wrong_sequence', 'legacy', 'wrong_window', 'stale_stage'])
def test_native_value_observation_rejects_incomplete_foreign_or_late_baseline(fault):
m = module()
before = observation(1, 'query_components', 'OLD', 'OLD_VALUE')
native = observation(2, 'query_components', 'NEW')
if fault == 'truncated':
native = native[:native.index('Z_OBS')]
elif fault == 'wrong_job':
native = native.replace('Z_OBS\tjob', 'Z_OBS\tother')
elif fault == 'wrong_uid':
native = native.replace('E\tu1\t0', 'E\tu2\t0')
elif fault == 'wrong_part':
native = native.replace('Part Number\tNEW', 'Part Number\tOTHER')
elif fault == 'wrong_count':
native = observation(2, 'query_components', 'NEW', count=4)
elif fault == 'missing_ordinal':
native = native.replace('E\tu1\t1', 'E\tu1\t9')
elif fault == 'postrestore':
native = observation(2, 'snapshot_attributes', 'NEW')
elif fault == 'wrong_sequence':
native = native.replace('Z_OBS\tjob\tu1\tquery_components\t2', 'Z_OBS\tjob\tu1\tquery_components\t1')
elif fault == 'legacy':
native = native[:native.index('Z_OBS')].replace('\t2.5\t2\tpre_restore\n', '\t2.5\n')
elif fault == 'wrong_window':
native = native.replace('\t3\tpre_restore\n', '\t3\tbefore_native\n')
elif fault == 'stale_stage':
native = observation(2, 'query_components', 'NEW', window='restoring')
with pytest.raises(ValueError):
m.native_value_observation(before+native+observation(3, 'open_sheet', 'NEW'),
job='job', uid='u1', old='OLD', new='NEW')