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

164 lines
8.8 KiB
Python

import json
import os
import zipfile
import pytest
from dx_productivity.trial_task_center import diagnostics_bundle as bundle
from dx_productivity.trial_task_center.execution_feedback import read_feedback
from dx_productivity.trial_task_center.ui.execution_feedback import format_feedback
def write(root, name, text):
path = root / name
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(text, encoding='utf-8')
return path
def test_bundle_exports_execution_closure_and_inventory_evidence(tmp_path):
names = {
'tasks/t/quick-run/worker-progress.json': '{"stage":"save_and_close"}',
'tasks/t/quick-run/quick-part-replace.log': 'native returned token=private-token',
'tasks/t/quick-run/project-commit-' + 'a' * 32 + '.tsv': 'saved_ok\tjob1',
'tasks/t/quick-run/project-pause-' + 'a' * 32 + '.tsv': 'paused\tjob2',
'tasks/t/quick-run/post-state-job1.tsv': 'R1\tNEW',
'tasks/t/quick-run/value-readback-job1.json': '{"verified":true}',
'execution-proof/proof1/project-commit-' + 'a' * 32 + '.tsv': 'saved_ok\tjob1',
'execution-proof/proof1/completion.json': '{"verified":true}',
'tasks/t/quick-run-history/native-inventory-a/native-inventory.json': '{"state":"ready"}',
'tasks/t/quick-run-history/native-inventory-a/output/scope.txt': 'R1\tUID1',
'tasks/t/quick-run-history/native-inventory-a/output/effective.txt': 'R1\tNEW',
'tasks/t/netlist-checks/run1/verification.json': '{"verified":true}',
'tasks/t/netlist-checks/run1/after-export/netlist.json': '{"complete":true}',
}
for name, text in names.items():
write(tmp_path, name, text)
write(tmp_path, 'tasks/t/netlist-checks/run1/before/project.prj', 'private project')
output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip')
with zipfile.ZipFile(output) as archive:
assert set(names).issubset(archive.namelist())
assert 'private-token' not in archive.read('tasks/t/quick-run/quick-part-replace.log').decode()
assert all(not name.endswith('.prj') for name in archive.namelist())
def test_bundle_tails_active_logs_and_keeps_latest_evidence_under_budget(tmp_path, monkeypatch):
monkeypatch.setattr(bundle, 'MAX_FILE_BYTES', 160)
monkeypatch.setattr(bundle, 'MAX_FILES', 2)
for index in range(4):
path = write(tmp_path, f'tasks/{index}/quick-run/worker-progress.json', json.dumps({'stage': str(index)}))
os.utime(path, (100 + index, 100 + index))
log = write(tmp_path, 'tasks/latest/quick-run/quick-part-replace.log',
'old heartbeat\n' * 100 + 'last failure token=secret-value\n')
os.utime(log, (500, 500))
output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip')
with zipfile.ZipFile(output) as archive:
text = archive.read('tasks/latest/quick-run/quick-part-replace.log').decode()
assert 'last failure' in text and 'secret-value' not in text
assert 'tasks/3/quick-run/worker-progress.json' in archive.namelist()
manifest = json.loads(archive.read('manifest.json'))
assert manifest['truncated'][0]['mode'] == 'tail'
@pytest.mark.parametrize('record,reason,wait', [
({'stage': 'native_replace', 'stop_requested': True}, 'inactivity', 'native_call'),
({'stage': 'save_and_close', 'stopping_reason': 'user_pause'}, 'user_pause', 'save'),
({'stage': 'verify_saved_project'}, '', 'readback'),
({'stage': 'private_shutdown', 'stopping_reason': 'failure_cleanup', 'wait_reason': 'mentor_exit'},
'failure_cleanup', 'mentor_exit'),
({'stage': 'awaiting_reconciliation', 'primary_error': 'native failed'}, '', 'reconciliation'),
])
def test_feedback_classifies_stop_and_actual_wait_without_generic_save(tmp_path, record, reason, wait):
write(tmp_path, 'quick-run/worker-progress.json', json.dumps(record))
value = read_feedback('t', 1, 1, 'running', tmp_path, 0)
assert value.stopping_reason == reason
assert value.wait_reason == wait
text = format_feedback(value, 'board', 4, True)
if wait != 'save':
assert '保存' not in text
if record.get('primary_error'):
assert record['primary_error'] in text
def test_operation_events_preserve_last_success_and_incomplete_call(tmp_path):
from dx_productivity.trial_task_center.operation_diagnostics import record_operation
assert record_operation(tmp_path / 'quick-run', 'item', 'return', item_id='job1')
assert record_operation(tmp_path / 'quick-run', 'save', 'enter', item_id='job2')
assert record_operation(tmp_path / 'quick-run', 'mentor_exit', 'enter')
assert record_operation(tmp_path / 'quick-run', 'mentor_exit', 'error', error='cleanup failed')
output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip')
with zipfile.ZipFile(output) as archive:
summary = json.loads(archive.read('closure-summary.json'))['operations']['quick-run']
assert summary['last_successful_item'] == 'job1'
assert summary['incomplete_operations'][0]['operation'] == 'save'
assert summary['errors'][0]['error'] == 'cleanup failed'
def test_stop_control_keeps_failure_reason_and_first_error(tmp_path):
from dx_productivity.trial_task_center.stop_control import StopControl, wait_for_resolution, MentorReleaseRequired
control = StopControl()
with pytest.raises(MentorReleaseRequired):
wait_for_resolution(control, tmp_path, 'native failed', release_on_failure=True)
assert control.stopping_reason == 'failure_cleanup'
assert control.primary_error == 'native failed'
control.request_failure_cleanup(tmp_path, 'later cleanup failure')
assert control.primary_error == 'native failed'
record = json.loads((tmp_path / 'worker-progress.json').read_text())
assert record['primary_error'] == 'native failed'
assert record['wait_reason'] == 'reconciliation'
def test_closing_desktop_overrides_old_save_wait(tmp_path):
write(tmp_path, 'quick-run/worker-progress.json', json.dumps({
'stage': 'save_and_close', 'wait_reason': 'save', 'stopping_reason': 'failure_cleanup'}))
write(tmp_path, 'quick-run/private-desktop.json', '{"state":"closing"}')
value = read_feedback('t', 1, 1, 'running', tmp_path, 0)
assert value.wait_reason == 'desktop_release'
assert '保存' not in format_feedback(value, 'board', 4, True)
def test_bundle_keeps_complete_event_tail_and_latest_successful_status(tmp_path):
write(tmp_path, 'quick-run/operation-events.jsonl',
'{"operation":"save","event":"enter","item_id":"job2"}\n{"broken":')
write(tmp_path, 'quick-run/status.tsv',
'job1\tsaved_ok\tsession_verified\tdone\njob2\tpending_save\tsession_staged\tstaged\n')
output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip')
with zipfile.ZipFile(output) as archive:
summary = json.loads(archive.read('closure-summary.json'))
assert summary['items']['quick-run']['last_successful_item'] == 'job1'
assert summary['items']['quick-run']['latest_item'] == {'job_id': 'job2', 'state': 'pending_save'}
assert summary['operations']['quick-run']['incomplete_operations'][0]['operation'] == 'save'
manifest = json.loads(archive.read('manifest.json'))
assert any(row['reason'] == 'invalid_jsonl_lines' for row in manifest['omissions'])
def test_bundle_keeps_valid_call_when_other_event_has_invalid_shape(tmp_path):
write(tmp_path, 'quick-run/operation-events.jsonl',
'{"operation":[],"event":"enter","item_id":{}}\n'
'{"operation":"save","event":"enter","item_id":"job1"}\n')
output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip')
with zipfile.ZipFile(output) as archive:
summary = json.loads(archive.read('closure-summary.json'))
assert summary['operations']['quick-run']['incomplete_operations'][0]['operation'] == 'save'
def test_bundle_reads_native_ascii_report_using_local_windows_encoding(tmp_path):
name = 'quick-run-history/native-inventory-a/output/scope.txt'
path = write(tmp_path, name, '')
path.write_bytes('R1 中文说明'.encode('mbcs' if os.name == 'nt' else 'cp1252', errors='replace'))
output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip')
with zipfile.ZipFile(output) as archive:
assert name in archive.namelist()
assert 'R1 ' in archive.read(name).decode('utf-8')
def test_redaction_growth_does_not_discard_latest_failure_tail(tmp_path, monkeypatch):
monkeypatch.setattr(bundle, 'MAX_FILE_BYTES', 128)
name = 'quick-run/quick-part-replace.log'
write(tmp_path, name, 'token=x\n' * 100 + 'native failed token=y\n')
output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip')
with zipfile.ZipFile(output) as archive:
text = archive.read(name).decode()
assert 'native failed' in text
assert len(archive.read(name)) <= 128