Files
dxdesigner-part-replacement/tests/v1_1/test_component_scope.py
T

296 lines
17 KiB
Python

"""Freeze raw all-match rules into actual component identities before mutation."""
import csv
from dataclasses import replace
from pathlib import Path
import subprocess
import pytest
from dx_productivity.trial_task_center.errors import TaskCenterError
from dx_productivity.trial_task_center.execution_journal import read_command_rows
from dx_productivity.trial_task_center.models import ProjectRecord, ProjectStatus, RuleRecord
from dx_productivity.trial_task_center.quick_run import (
ProjectDiscovery, QuickRunPreparer, _path_key, _queue_matches_inputs, _read_commands,
)
from dx_productivity.trial_task_center.target_plan import ComponentTarget, TargetRule
@pytest.fixture
def scope_case(tmp_path, monkeypatch):
project = tmp_path / 'projects' / 'Board' / 'board.prj'
project.parent.mkdir(parents=True)
project.write_text('KEY FrontEndSnapshot "DxD"\nKEY iCDBDir "database"\n')
source = tmp_path / 'runtime.vbs'
source.write_text("' controlled runtime")
selected = ProjectRecord('p', 't', 'Board', project, project.parent, True, True, ProjectStatus.PENDING, 0)
rules = (RuleRecord('r', 't', 0, 'OLD', 'NEW', True),)
components = tuple(ComponentTarget(project, 'MAIN', '1', f'uid-{i}', f'R{i}', 'OLD', '10K') for i in (1, 2))
calls, requested = [], []
def worker(command, timeout):
root = Path(command[3])
row = read_command_rows(root / 'current-command.tsv')[0]
assert row['target_selectors'], 'An empty inventory marker must never reach a mutating worker'
calls.append(row)
with (root / 'status.tsv').open('a', encoding='utf8') as stream:
stream.write(f'{command[-1]}\tawaiting_native_replace\t\tcaptured\n')
stream.write(f'{command[-1]}\tsaved_ok\tbatch_closed\tcontrolled save\n')
return subprocess.CompletedProcess(command, 0, '', '')
preparer = QuickRunPreparer(tmp_path / 'data', script_source=source,
process_runner=worker, cscript_resolver=lambda: Path('controlled-cscript'), run_token_factory=lambda: 'scope')
def discover(path, parts, *, collect_components=False, **kwargs):
requested.append(collect_components)
return ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): frozenset({'old'})}, components)
monkeypatch.setattr(preparer, 'discover_project', discover)
return preparer, selected, rules, components, requested, calls
def test_new_raw_rule_freezes_two_equal_value_targets_as_separate_uids(scope_case):
preparer, project, rules, _, requested, _ = scope_case
handoff = preparer.ensure_batch_handoff('t', (project,), rules)
rows = read_command_rows(handoff.command_path)
assert requested == [True]
assert len(rows) == 2
assert [row['target_selectors'] for row in rows] == ['uid-1\x1fR1\x1f10K', 'uid-2\x1fR2\x1f10K']
assert all(row['target_scope'] == 'targets' and row['execution_unit'] == 'component' for row in rows)
assert handoff.command_path.with_name('component-scope.json').is_file()
assert _queue_matches_inputs(handoff.command_path, _read_commands(handoff.command_path), (project,), rules)
def test_explicit_cell_preflight_uses_cache_shared_with_native_helper(scope_case, tmp_path, monkeypatch):
from contextlib import closing
from dx_productivity.trial_task_center import cell_catalog, quick_run
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
preparer, project, *_ = scope_case
project.path.write_text(project.path.read_text() + 'KEY CentralLibrary "library/lib.lmc"\n')
data = preparer.data_root
def boundary(*args, **kwargs):
raise RuntimeError('native dispatch boundary')
monkeypatch.setattr(quick_run, '_run_process', boundary)
preparer._process_runner = boundary
def read(library, part, root, **kwargs):
assert root == data / 'cell-catalog'
raise RuntimeError('shared library cache reached')
monkeypatch.setattr(cell_catalog, 'read_library_cell_catalog', read)
with closing(TaskStore(tmp_path / 'scope.db')) as store:
service = TaskCenterService(store, configure_logging(data), quick_run_preparer=preparer)
task = service.create_project_tasks(project.path.parent)[0]
service.add_rule(task.task_id, 'OLD', 'NEW', target_cell='CELL')
child = service.for_project_task(task.task_id)._quick_run_preparer
monkeypatch.setattr(child, 'discover_project', preparer.discover_project)
projects, rules = store.list_projects(task.task_id), store.list_rules(task.task_id)
session = child.begin_batch_session(task.task_id, projects, rules)
with pytest.raises(RuntimeError, match='shared library cache reached'):
child.execute_step(task.task_id, projects, rules, batch_mode=True, batch_session=session)
@pytest.mark.parametrize('read_failed', [False, True])
def test_missing_project_sheet_manifest_is_a_controlled_scope_error(scope_case, read_failed):
from dx_productivity.trial_task_center.component_scope import freeze
preparer, project, rules, *_ = scope_case
key = _path_key(project.path)
discovery = preparer.discover_project(project.path, ('OLD',), collect_components=True)
with pytest.raises(TaskCenterError, match='component_scope'):
freeze((project,), rules, {}, {key: discovery}, {key} if read_failed else set(), 'preserve')
assert not (preparer.data_root / 'quick-run/command.tsv').exists()
@pytest.mark.parametrize('change', [{'custom_value_enabled': True, 'custom_value': 'changed'}, {'sync_nc': False}, {'target_cell': 'QFN'}])
def test_original_source_rule_option_changes_cannot_resume_frozen_scope(scope_case, change):
preparer, project, rules, *_ = scope_case
handoff = preparer.ensure_batch_handoff('t', (project,), rules)
changed = (replace(rules[0], **change),)
with pytest.raises(TaskCenterError, match='source|scope|来源|冻结'):
preparer.ensure_batch_handoff('t', (project,), changed)
def test_snapshot_and_resume_use_frozen_uids_without_rediscovering_changed_parts(scope_case, monkeypatch):
preparer, project, rules, *_ = scope_case
handoff = preparer.ensure_batch_handoff('t', (project,), rules)
rows = read_command_rows(handoff.command_path)
with handoff.status_path.open('a', encoding='utf8') as stream:
stream.write(f"{rows[0]['job_id']}\tsaved_ok\tbatch_closed\talready saved\n")
def forbidden(*args, **kwargs):
raise AssertionError('Completed targets now have new Part Numbers; do not rediscover them')
monkeypatch.setattr(preparer, 'discover_project', forbidden)
before = handoff.command_path.read_bytes()
assert preparer.ensure_batch_handoff('t', (project,), rules).command_path.read_bytes() == before
snapshot = preparer.read_snapshot('t', (project,), rules)
assert not snapshot.interrupted
assert snapshot.progress.total_items == 2 and snapshot.progress.completed_items == 1
assert snapshot.progress.next_old_part_number == 'OLD'
def test_mixed_no_match_is_preterminal_and_other_uid_commands_remain_dispatchable(scope_case):
preparer, project, rules, _, _, calls = scope_case
missing = RuleRecord('missing', 't', 0, 'ABSENT', 'DEST', True)
session = preparer.begin_batch_session('t', (project,), (missing, *rules))
rows = read_command_rows(session.handoff.command_path)
empty = rows[0]
assert empty['execution_unit'] == 'inventory_empty'
assert empty['target_scope'] == 'targets' and empty['target_selectors'] == ''
assert session.latest[empty['job_id']] == ('batch_skipped', 'inventory_empty')
assert session.commands[session.next_index][1] == rows[1]['job_id']
# Without a journal the normal batch gate conservatively blocks after any
# failure; dispatch the pending current command to verify its actual scope.
from dx_productivity.trial_task_center.quick_run import _write_current_command
_write_current_command(session, session.commands[session.next_index])
preparer._process_runner(('controlled', '', '', str(session.handoff.command_path.parent), rows[1]['job_id']), 10)
assert len(calls) == 1 and calls[0]['target_selectors'].startswith('uid-1\x1f')
progress = preparer.read_snapshot('t', (project,), (missing, *rules)).progress
assert progress.failed_items == 1 and progress.saved_items == 1
def test_legacy_page_all_match_queue_resumes_without_new_expansion(scope_case, monkeypatch):
preparer, project, rules, *_ = scope_case
handoff = preparer.prepare('t', (project,), rules, sheet_targets={_path_key(project.path): (('MAIN', '1'),)})
before = handoff.command_path.read_bytes()
monkeypatch.setattr(preparer, 'discover_project', lambda *args, **kwargs: pytest.fail('Legacy queue must keep its original shape'))
assert preparer.ensure_batch_handoff('t', (project,), rules).command_path.read_bytes() == before
assert 'target_scope' not in read_command_rows(handoff.command_path)[0]
assert not handoff.command_path.with_name('component-scope.json').exists()
def test_retry_preparation_inherits_frozen_scope_without_new_inventory(scope_case):
preparer, project, rules, *_ = scope_case
handoff = preparer.ensure_batch_handoff('t', (project,), rules)
staged = preparer.for_data_root(preparer.data_root / 'retry-stage')
retried = staged.prepare('t', (project,), rules,
sheet_targets={_path_key(project.path): (('MAIN', '1'),)}, component_scope_from=handoff.command_path.parent)
assert read_command_rows(retried.command_path) == read_command_rows(handoff.command_path)
assert retried.command_path.with_name('component-scope.json').read_bytes() == handoff.command_path.with_name('component-scope.json').read_bytes()
def test_incomplete_component_manifest_fails_closed_instead_of_all_match(scope_case, monkeypatch):
preparer, project, rules, *_ = scope_case
monkeypatch.setattr(preparer, 'discover_project', lambda *args, **kwargs:
ProjectDiscovery((('MAIN', '1'),), {('MAIN', '1'): frozenset({'old'})}))
with pytest.raises(TaskCenterError) as error:
preparer.ensure_batch_handoff('t', (project,), rules)
assert error.value.code == 'component_scope'
assert not (preparer.data_root / 'quick-run/command.tsv').exists()
def test_missing_terminal_marker_never_dispatches_empty_target_selectors(scope_case):
preparer, project, rules, _, _, calls = scope_case
missing = RuleRecord('missing', 't', 0, 'ABSENT', 'DEST', True)
handoff = preparer.ensure_batch_handoff('t', (project,), (missing, *rules))
handoff.status_path.write_text('job_id\tstate\tcode\tmessage\n')
session = preparer.begin_batch_session('t', (project,), (missing, *rules))
with pytest.raises(TaskCenterError) as error:
preparer.execute_step('t', (project,), (missing, *rules), batch_mode=True, batch_session=session)
assert error.value.code == 'component_scope'
assert not calls
def test_empty_command_proof_requires_the_exact_full_frozen_queue(scope_case):
import json
from dx_productivity.trial_task_center.component_scope import validate_empty_command
preparer, project, rules, *_ = scope_case
missing = RuleRecord('missing', 't', 0, 'ABSENT', 'DEST', True)
handoff = preparer.ensure_batch_handoff('t', (project,), (missing, *rules))
empty = read_command_rows(handoff.command_path)[0]
assert validate_empty_command(handoff.command_path.parent, empty)
scope_path = handoff.command_path.with_name('component-scope.json')
scope = json.loads(scope_path.read_text())
scope['projects'][0]['components'].pop()
scope_path.write_text(json.dumps(scope))
assert not validate_empty_command(handoff.command_path.parent, empty)
def test_real_journal_keeps_empty_rule_manual_and_executes_other_exact_uids(scope_case, tmp_path, monkeypatch):
from threading import Event
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.task_queue import execute_task_queue
preparer, project, _, _, _, calls = scope_case
store = TaskStore(tmp_path / 'tasks.db')
service = TaskCenterService(store, configure_logging(preparer.data_root), quick_run_preparer=preparer)
try:
task = service.create_project_tasks(project.path.parent)[0]
service.add_rule(task.task_id, 'ABSENT', 'DEST')
service.add_rule(task.task_id, 'OLD', 'NEW')
child = service.for_project_task(task.task_id)
monkeypatch.setattr(child._quick_run_preparer, 'discover_project', preparer.discover_project)
result = execute_task_queue(service, (task.task_id,), stop_event=Event())
assert not result.paused and result.entries[0].state == 'failed'
assert [row['target_selectors'] for row in calls] == ['uid-1\x1fR1\x1f10K', 'uid-2\x1fR2\x1f10K']
progress = service.quick_run_progress(task.task_id)
assert progress.queue_complete and progress.failed_items == 1 and progress.saved_items == 2
issues = service.execution_problems(task.task_id)
assert len(issues) == 1 and issues[0]['code'] == 'inventory_empty' and issues[0]['classification'] == 'manual'
assert service.reserve_problem_retry(task.task_id, source='dsh') == ()
execute_task_queue(service, (task.task_id,), stop_event=Event(), queue_id=result.queue_id)
assert len(calls) == 2
finally:
store.close()
def test_real_raw_retry_inherits_original_uids_and_does_not_repeat_success(scope_case, tmp_path, monkeypatch):
from threading import Event
from dx_productivity.trial_task_center.logging_setup import configure_logging
from dx_productivity.trial_task_center.service import TaskCenterService
from dx_productivity.trial_task_center.store import TaskStore
from dx_productivity.trial_task_center.task_queue import execute_task_queue
preparer, project, _, _, _, calls = scope_case
original_worker = preparer._process_runner
failed = False
def worker(command, timeout):
nonlocal failed
root = Path(command[3])
row = read_command_rows(root / 'current-command.tsv')[0]
if row['target_selectors'].startswith('uid-1\x1f') and not failed:
failed = True
calls.append(row)
with (root / 'status.tsv').open('a', encoding='utf8') as stream:
stream.write(f'{command[-1]}\tbatch_failed\tcad_attachment_failed\tcontrolled prewrite failure\n')
return subprocess.CompletedProcess(command, 0, '', '')
return original_worker(command, timeout)
preparer._process_runner = worker
store = TaskStore(tmp_path / 'tasks.db')
service = TaskCenterService(store, configure_logging(preparer.data_root), quick_run_preparer=preparer)
try:
task = service.create_project_tasks(project.path.parent)[0]
service.add_rule(task.task_id, 'OLD', 'NEW')
child = service.for_project_task(task.task_id)
monkeypatch.setattr(child._quick_run_preparer, 'discover_project', preparer.discover_project)
result = execute_task_queue(service, (task.task_id,), stop_event=Event(), defer_problem_pass=True)
assert not result.paused and len(calls) == 2
original_scope = (child._quick_run_preparer.data_root / 'quick-run/component-scope.json').read_bytes()
monkeypatch.setattr(child._quick_run_preparer, 'discover_project', lambda *args, **kwargs: pytest.fail('Retry must inherit frozen inventory'))
reserved = service.reserve_problem_retry(task.task_id, source='dsh')
assert len(reserved) == 1
service.prepare_failed_quick_run(task.task_id, operation_keys=reserved)
assert (child._quick_run_preparer.data_root / 'quick-run/component-scope.json').read_bytes() == original_scope
retried = execute_task_queue(service, (task.task_id,), stop_event=Event())
assert not retried.paused and retried.entries[0].state == 'completed'
assert [row['target_selectors'].split('\x1f')[0] for row in calls] == ['uid-1', 'uid-2', 'uid-1']
finally:
store.close()
def test_retry_inherits_legacy_grouped_target_shape(scope_case):
preparer, project, _, components, *_ = scope_case
rules = (TargetRule('OLD', 'NEW', components),)
sheets = {_path_key(project.path): (('MAIN', '1'),)}
original = preparer.prepare('t', (project,), rules, sheet_targets=sheets, split_targets=False)
staged = preparer.for_data_root(preparer.data_root / 'retry-stage')
retried = staged.prepare('t', (project,), rules, sheet_targets=sheets, component_scope_from=original.command_path.parent)
assert read_command_rows(retried.command_path) == read_command_rows(original.command_path)
assert len(read_command_rows(retried.command_path)) == 1
def test_retry_missing_frozen_sidecar_cannot_become_an_all_match_queue(scope_case):
preparer, project, rules, *_ = scope_case
original = preparer.ensure_batch_handoff('t', (project,), rules)
original.command_path.with_name('component-scope.json').unlink()
staged = preparer.for_data_root(preparer.data_root / 'retry-stage')
with pytest.raises(TaskCenterError) as error:
staged.prepare('t', (project,), rules, sheet_targets={_path_key(project.path): (('MAIN', '1'),)},
component_scope_from=original.command_path.parent)
assert error.value.code == 'component_scope'
assert not (staged.data_root / 'quick-run/command.tsv').exists()