import json from pathlib import Path import zipfile import pytest from dx_productivity.trial_task_center import diagnostics_bundle as bundle def test_bundle_contains_read_trace_and_worker_state_but_not_full_database_reports(tmp_path): root = tmp_path / 'data' evidence = root / 'quick-run-history/native-inventory-abc' evidence.mkdir(parents=True) contents = {'read-trace.jsonl': '{"uid":"$1I44","project":"C:\\\\Users\\\\Alice\\\\board.prj"}\n', 'read-state.json': '{"stage":"effective_parse"}', 'native-inventory.json': '{"state":"fallback"}', 'worker-progress.json': '{"stage":"open_project"}', 'worker-output.txt': 'native worker output', 'quick-part-replace.log': 'inventory_skipped_no_ref uid=POWER_IN', 'project-read-release.json': '{"resources_released":true}'} for name, text in contents.items(): (evidence / name).write_text(text, encoding='utf-8') (evidence / 'output').mkdir() (evidence / 'output/effective.txt').write_text('FULL DESIGN REPORT') archive = bundle.export_diagnostic_bundle(root, tmp_path / 'support.zip') with zipfile.ZipFile(archive) as z: assert all('quick-run-history/native-inventory-abc/' + name in z.namelist() for name in contents) text = '\n'.join(z.read(name).decode('utf-8') for name in z.namelist()) assert 'Alice' not in text and 'FULL DESIGN REPORT' not in text def test_bundle_allowlist_redacts_secrets_and_home_and_omits_inputs(tmp_path): root = tmp_path / 'data' session = root / 'batch-resources' / 'batch1' session.mkdir(parents=True) (root / 'logs').mkdir() (root / 'logs/application.log.1').write_text('user=C:\\Users\\Alice token=super-secret password="a b"', encoding='utf-8') (session / 'session.json').write_text(json.dumps({'pid': 123, 'api_key': 'hidden', 'input': 'C:\\Users\\Alice\\work\\bom.xlsx'})) (session / 'source.xlsx').write_bytes(b'private BOM') (root / 'task-center.db').write_bytes(b'private DB') target = tmp_path / 'support.zip' assert bundle.export_diagnostic_bundle(root, target) == target with zipfile.ZipFile(target) as archive: text = '\n'.join(archive.read(name).decode('utf-8') for name in archive.namelist()) assert 'super-secret' not in text and 'hidden' not in text and 'a b' not in text assert 'Alice' not in text and 'private BOM' not in text and 'private DB' not in text assert json.loads(archive.read('batch-resources/batch1/session.json'))['pid'] == 123 manifest = json.loads(archive.read('manifest.json')) assert manifest['omissions'] and manifest['unknowns'] with pytest.raises(FileExistsError): bundle.export_diagnostic_bundle(root, target) def test_bundle_rejects_non_zip_destination(tmp_path): with pytest.raises(ValueError): bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'output.txt') def test_bundle_does_not_follow_links_or_export_binary_logs(tmp_path): root = tmp_path / 'data' (root / 'logs').mkdir(parents=True) (root / 'logs/binary.log').write_bytes(b'\x00\xff\x00') external = tmp_path / 'secret.log' external.write_text('outside data root') try: (root / 'logs/link.log').symlink_to(external) except OSError: pass output = bundle.export_diagnostic_bundle(root, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: assert 'logs/binary.log' not in archive.namelist() assert 'logs/link.log' not in archive.namelist() def test_bundle_redacts_nested_client_secret_and_preserves_support_ids(tmp_path): root = tmp_path / 'data' directory = root / 'attempts' directory.mkdir(parents=True) (directory / 'attempt.json').write_text(json.dumps({'client_secret': 'oauth-private', 'support_id': 'DXT-123', 'username': 'ExternalUser', 'nested': {'authorization': 'Bearer a.b.c'}})) output = bundle.export_diagnostic_bundle(root, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: text = archive.read('attempts/attempt.json').decode() assert 'oauth-private' not in text and 'a.b.c' not in text assert 'ExternalUser' not in text assert 'DXT-123' in text def test_bundle_excludes_hardlinks_and_redacts_jsonl_credentials(tmp_path): import os root = tmp_path / 'data' logs = root / 'logs' logs.mkdir(parents=True) external = tmp_path / 'private.log' external.write_text('private content') os.link(external, logs / 'linked.log') tasks = root / 'tasks' / 'task1' tasks.mkdir(parents=True) (tasks / 'events.jsonl').write_text(json.dumps({'token': 'private-token'}) + '\n') output = bundle.export_diagnostic_bundle(root, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: assert 'logs/linked.log' not in archive.namelist() assert 'private-token' not in archive.read('tasks/task1/events.jsonl').decode() assert json.loads(archive.read('tasks/task1/events.jsonl'))['token'] == '' def test_bundle_size_limit_omits_oversized_files(tmp_path, monkeypatch): (tmp_path / 'logs').mkdir() (tmp_path / 'logs/application.log').write_text('too large for configured limit') monkeypatch.setattr(bundle, 'MAX_FILE_BYTES', 4) output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: assert 'logs/application.log' not in archive.namelist() assert any(row['reason'] == 'size_or_count_limit' for row in json.loads(archive.read('manifest.json'))['omissions']) def test_latest_task_evidence_wins_when_file_count_is_limited(tmp_path, monkeypatch): import os root = tmp_path / 'data' for task, timestamp in [('aaa-old', 1000000000), ('zzz-recent', 2000000000)]: directory = root / 'tasks' / task directory.mkdir(parents=True) events = directory / 'events.jsonl' events.write_text('{}\n') os.utime(directory, (timestamp, timestamp)) os.utime(events, (timestamp, timestamp)) monkeypatch.setattr(bundle, 'MAX_FILES', 1) output = bundle.export_diagnostic_bundle(root, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: assert 'tasks/zzz-recent/events.jsonl' in archive.namelist() assert 'tasks/aaa-old/events.jsonl' not in archive.namelist() def test_large_application_log_includes_redacted_recent_tail(tmp_path, monkeypatch): root = tmp_path / 'data' (root / 'logs').mkdir(parents=True) (root / 'logs/application.log').write_text('old line\n' * 2000 + 'recent failure password=secret\n', encoding='utf-8') monkeypatch.setattr(bundle, 'MAX_FILE_BYTES', 4096) output = bundle.export_diagnostic_bundle(root, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: text = archive.read('logs/application.log').decode('utf-8') assert 'recent failure' in text and 'secret' not in text and len(text.encode()) <= 4096 assert json.loads(archive.read('manifest.json'))['truncated'] def test_bundle_includes_real_nested_attempt_queue_and_protection_locations(tmp_path): names = ['batch-resources/batch1/attempts/g1/attempt1/job1.json', 'task-queues/queue1.json', 'tasks/task1/quick-run/project-checkpoint.json', 'quick-run-history/recovery1/uncertain.flag'] for name in names: path = tmp_path / name path.parent.mkdir(parents=True, exist_ok=True) path.write_text('{}') output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: assert set(names).issubset(archive.namelist()) def test_plain_log_authorization_redacts_bearer_value(tmp_path): (tmp_path / 'logs').mkdir() (tmp_path / 'logs/application.log').write_text('Authorization=Bearer private-bearer-value\n') output = bundle.export_diagnostic_bundle(tmp_path, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: assert 'private-bearer-value' not in archive.read('logs/application.log').decode() def test_catalog_proof_is_exported_but_library_inputs_are_excluded(tmp_path): import json import zipfile from dx_productivity.trial_task_center.diagnostics_bundle import export_diagnostic_bundle root = tmp_path / 'data' folder = root / 'cell-catalog' / 'part-checks' / 'check1' (folder / 'library').mkdir(parents=True) (folder / 'clone-proof.json').write_text(json.dumps({'clone_content_differences': ['lib.cfg']}), encoding='utf-8') (folder / 'library' / 'parts.pdb').write_bytes(b'private library') archive = export_diagnostic_bundle(root, tmp_path / 'support.zip') with zipfile.ZipFile(archive) as bundle: assert 'cell-catalog/part-checks/check1/clone-proof.json' in bundle.namelist() assert not any(name.endswith('.pdb') for name in bundle.namelist())