"""Offline dataset previews, telemetry disabling, cache boundaries and shipped contracts.""" import json from pathlib import Path import shutil import tomllib import subprocess import sys import tarfile import zipfile import pytest import yaml from obliteratus.cli import main from obliteratus.concepts.pack import ConceptPack from tests.test_concept_datasets import FIXTURE, alter, change_manifest, synthetic_files, validator pytestmark = pytest.mark.cpu @pytest.fixture(autouse=True) def isolated_cache(tmp_path, monkeypatch): monkeypatch.setenv("OBLITERATUS_CACHE_DIR", str(tmp_path / "cache")) @pytest.fixture def forbidden_sinks(monkeypatch): from obliteratus import telemetry def forbidden(*args, **kwargs): pytest.fail("Preview must not fetch, emit, schedule Hub writes or start background work") monkeypatch.setattr(ConceptPack, "fetch", forbidden) monkeypatch.setattr("obliteratus.concepts.pack.https_fetch", forbidden) monkeypatch.setattr("urllib.request.OpenerDirector.open", forbidden) monkeypatch.setattr("threading.Thread.start", forbidden) monkeypatch.setattr(telemetry, "_send_sync", forbidden) monkeypatch.setattr(telemetry, "_schedule_hub_sync", forbidden) return telemetry def test_private_preview_disables_prior_optin_and_real_gated_sinks(capsys, forbidden_sinks): telemetry = forbidden_sinks telemetry.enable_telemetry() assert telemetry.is_enabled() main(["concepts", "dataset-preview", str(FIXTURE), "--private", "--json"]) report = json.loads(capsys.readouterr().out) assert not telemetry.is_enabled() assert report["access"]["role"] == "fit" assert report["text_included"] is False assert "LOCKED_TEST_SENTINEL" not in json.dumps(report) assert telemetry.log_benchmark(telemetry.BenchmarkRecord()) is False telemetry.send_report({"synthetic": True}) @pytest.mark.parametrize("options,contains", [([], False), (["--private"], False), (["--include-text"], True), (["--include-text", "--private"], True)]) def test_text_optin_is_separate_from_privacy(options, contains, capsys, forbidden_sinks): main(["concepts", "dataset-preview", str(FIXTURE), "--json", *options]) report = json.loads(capsys.readouterr().out) assert report["text_included"] is contains assert ("A silver kite rises." in json.dumps(report)) is contains assert "LOCKED_TEST_SENTINEL" not in json.dumps(report) @pytest.mark.parametrize("options", [[], ["--include-text"], ["--private", "--include-text"]]) def test_locked_test_requires_separate_acknowledgement(options, capsys, forbidden_sinks): with pytest.raises(SystemExit) as error: main(["concepts", "dataset-preview", str(FIXTURE), "--role", "test", *options]) assert error.value.code == 4 captured = capsys.readouterr() assert "allow_locked_test" in captured.err assert "LOCKED_TEST_SENTINEL" not in captured.err + captured.out def test_acknowledged_test_projection(capsys, forbidden_sinks): main(["concepts", "dataset-preview", str(FIXTURE), "--role", "test", "--allow-locked-test", "--include-text", "--private", "--json"]) report = json.loads(capsys.readouterr().out) assert report["access"]["locked_test_acknowledged"] is True assert "LOCKED_TEST_SENTINEL" in json.dumps(report) assert not forbidden_sinks.is_enabled() @pytest.mark.parametrize("error_kind", ["unfetched", "corrupt_cache", "invalid_record", "path", "budget"]) def test_private_error_paths_never_fetch_emit_or_echo_text(tmp_path, capsys, forbidden_sinks, error_kind): telemetry = forbidden_sinks telemetry.enable_telemetry() files = synthetic_files() options = [] if error_kind in {"unfetched", "corrupt_cache"}: def remote(manifest): entry = manifest["sets"]["positive"] entry.pop("path") entry["remote"] = {"repo": "https://github.com/example/synthetic", "commit": "a" * 40, "path": "positive.jsonl"} manifest["provenance"] = {"source_commit": "a" * 40} change_manifest(files, remote) payload = files.pop("positive.jsonl") if error_kind == "corrupt_cache": pack = ConceptPack(files) cached = pack._cached("positive.jsonl", tmp_path / "cache/concepts") cached.parent.mkdir(parents=True) cached.write_bytes(payload + b"bad") elif error_kind == "invalid_record": alter(files, "positive.jsonl", lambda rows: rows[6].update(role="LOCKED_TEST_SENTINEL")) elif error_kind == "path": change_manifest(files, lambda manifest: manifest["sets"]["positive"].update(path="../positive.jsonl")) else: options = ["--max-pairs", "-1"] source = tmp_path / "source" source.mkdir() for path, data in files.items(): destination = source / path destination.parent.mkdir(parents=True, exist_ok=True) destination.write_bytes(data) with pytest.raises(SystemExit) as error: main(["concepts", "dataset-preview", str(source), "--private", "--json", *options]) assert error.value.code == 4 captured = capsys.readouterr() assert "LOCKED_TEST_SENTINEL" not in captured.err + captured.out assert not telemetry.is_enabled() def test_verified_cached_remote_is_local_only(tmp_path, capsys, forbidden_sinks): files = synthetic_files() manifest = yaml.safe_load(files["manifest.yaml"]) entry = manifest["sets"]["positive"] entry.pop("path") entry["remote"] = {"repo": "https://github.com/example/synthetic", "commit": "a" * 40, "path": "positive.jsonl"} manifest["provenance"] = {"source_commit": "a" * 40} files["manifest.yaml"] = yaml.safe_dump(manifest).encode() payload = files.pop("positive.jsonl") pack = ConceptPack(files) cached = pack._cached("positive.jsonl", tmp_path / "cache/concepts") cached.parent.mkdir(parents=True) cached.write_bytes(payload) archive = tmp_path / "synthetic.cvpack" pack.write(archive) main(["concepts", "dataset-preview", str(archive), "--private", "--json", "--max-pairs", "1"]) report = json.loads(capsys.readouterr().out) assert report["similarity"]["completed_pairs"] == 1 assert report["similarity"]["truncated"] assert "LOCKED_TEST_SENTINEL" not in json.dumps(report) def _copy_declared_data_files(root, source, data_files): """Copy every path pyproject declares as data-files into the isolated source tree. Every pattern must match files in the repository itself; content already in the isolated tree never satisfies a declaration. An unmatched pattern fails with its name. """ for patterns in data_files.values(): for pattern in patterns: matches = sorted(root.glob(pattern)) if not matches: raise AssertionError(f"data-files pattern matches nothing: {pattern}") for path in matches: target = source / path.relative_to(root) target.parent.mkdir(parents=True, exist_ok=True) shutil.copyfile(path, target) def test_unmatched_data_files_pattern_fails(tmp_path): root, source = tmp_path / "root", tmp_path / "source" (root / "examples").mkdir(parents=True) source.mkdir() payload = '{"k": "é"}\r\n'.encode() assert not payload.isascii() # the byte-for-byte copy must cover non-ASCII content (root / "examples/present.json").write_bytes(payload) _copy_declared_data_files(root, source, {"share/x": ["examples/present.json"]}) assert (source / "examples/present.json").read_bytes() == payload with pytest.raises(AssertionError, match=r"examples/nonexistent_\*\.json"): _copy_declared_data_files(root, source, {"share/x": ["examples/nonexistent_*.json"]}) # A match that exists only in the isolated tree must not satisfy a repository declaration. (source / "docs").mkdir() (source / "docs/index.html").write_text("") with pytest.raises(AssertionError, match=r"docs/index\.html"): _copy_declared_data_files(root, source, {"share/x": ["docs/index.html"]}) def test_schemas_ship_and_load_from_wheel_and_sdist(tmp_path): """Build only public package inputs in isolation, with no dependency resolution.""" root = Path(__file__).resolve().parents[1] source = tmp_path / "source" source.mkdir() for name in ("pyproject.toml", "README.md", "LICENSE", "app.py"): shutil.copyfile(root / name, source / name) shutil.copytree(root / "obliteratus", source / "obliteratus", ignore=shutil.ignore_patterns("__pycache__", "*.pyc")) (source / "docs").mkdir() shutil.copyfile(root / "docs/CONCEPT_DATASETS.md", source / "docs/CONCEPT_DATASETS.md") data_files = tomllib.loads((root / "pyproject.toml").read_text())["tool"]["setuptools"].get( "data-files", {}) _copy_declared_data_files(root, source, data_files) # The real docs/index.html is declared and confirmed above; replace it with a minimal page. (source / "docs/index.html").write_text("") script = "from setuptools.build_meta import build_sdist,build_wheel; build_sdist('dist'); build_wheel('dist')" result = subprocess.run([sys.executable, "-c", script], cwd=source, capture_output=True, text=True) assert result.returncode == 0, result.stdout + result.stderr names = ["concept-pack", "concept-dataset-record", "concept-dataset-report"] archives = list((source / "dist").iterdir()) assert len(archives) == 2 for archive in archives: if archive.suffix == ".whl": with zipfile.ZipFile(archive) as wheel: values = {name: json.loads(wheel.read(f"obliteratus/concepts/schema/{name}.v1.schema.json")) for name in names} else: with tarfile.open(archive) as sdist: values = {} for name in names: member = next(m for m in sdist.getmembers() if m.name.endswith( f"/obliteratus/concepts/schema/{name}.v1.schema.json")) values[name] = json.load(sdist.extractfile(member)) for name, schema in values.items(): assert schema == validator(name).schema def test_snapshot_and_optin_boundaries(): dataset = ConceptPack.load(FIXTURE).dataset() revision = dataset.revision with pytest.raises(AttributeError): dataset.revision = 'sha256:' + '0' * 64 assert dataset.revision == revision with pytest.raises(ValueError, match='explicit boolean'): dataset.report(include_text='false') from obliteratus.concepts.validation import PackError, validate_contract, _check for name in ('https://example.org/hostile', '../concept-pack'): with pytest.raises(PackError, match='Unknown bundled'): validate_contract({}, name) with pytest.raises(PackError, match='Unknown bundled contract definition'): validate_contract({}, 'concept-pack', definition='absent') with pytest.raises(PackError, match='Unsupported bundled schema reference'): _check({}, {'$ref': 'https://example.org/hostile#/$defs/value'}, {}, 'synthetic') @pytest.mark.parametrize('records', [[], {'../positive.jsonl': []}, {'reference/card.txt': []}, {'positive.jsonl': {}}]) def test_direct_dataset_rejects_hostile_paths_and_containers(records): from obliteratus.concepts.dataset import ConceptDataset from obliteratus.concepts.validation import PackError declaration = ConceptPack.load(FIXTURE).manifest['dataset'] with pytest.raises(PackError): ConceptDataset(declaration, records)