import json from pathlib import Path import zipfile import pytest from dx_productivity.trial_task_center import diagnostics_bundle as bundle def test_nonblocking_queue_and_owner_loss_evidence_are_exported_without_scope_inputs(tmp_path): root = tmp_path / 'data' queue = root / 'workspace-queues' queue.mkdir(parents=True) (queue / ('a' * 32 + '.json')).write_text(json.dumps({'phase': 'problems'})) owner = root / 'batch-resources' / ('b' * 32) owner.mkdir(parents=True) (owner / 'owner-loss.json').write_text(json.dumps({'reason': 'parent_exited', 'token': 'private'})) (owner / 'force-request.json').write_text(json.dumps({'confirmed': True})) evidence = root / 'tasks' / 't' / 'quick-run' evidence.mkdir(parents=True) (evidence / 'project-read-release.json').write_text(json.dumps({'released': True})) (evidence / ('retained-worker-' + 'c' * 32 + '.json')).write_text(json.dumps({'state': 'blocked'})) (evidence / 'component-scope.json').write_text('private component inventory') output = bundle.export_diagnostic_bundle(root, tmp_path / 'support.zip') with zipfile.ZipFile(output) as archive: assert 'workspace-queues/' + 'a' * 32 + '.json' in archive.namelist() assert 'batch-resources/' + 'b' * 32 + '/owner-loss.json' in archive.namelist() assert 'batch-resources/' + 'b' * 32 + '/force-request.json' in archive.namelist() assert 'tasks/t/quick-run/retained-worker-' + 'c' * 32 + '.json' in archive.namelist() assert 'tasks/t/quick-run/project-read-release.json' in archive.namelist() assert 'tasks/t/quick-run/component-scope.json' not in archive.namelist() assert b'private' not in archive.read('batch-resources/' + 'b' * 32 + '/owner-loss.json') 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_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()