"""Supercharge tests for ``muse sparse-checkout``. Covers gaps from the baseline test suite: - JSON output with ``duration_ms`` / ``exit_code`` on ALL subcommands - Exact JSON schema for every subcommand - ``stats`` subcommand (matching_files, excluded_files, efficiency, total_files) - Security: path traversal patterns (``../``), null bytes - Mode switching via ``init --no-cone`` on an already-initialised cone repo - Config corruption graceful recovery - Pattern edge cases: whitespace, very long patterns, unicode - Integration: stats against a real HEAD snapshot - Data integrity: set replaces cleanly; add deduplicates exactly - Stress: 500-pattern list, 1 000-file manifest stats """ from __future__ import annotations from collections.abc import Mapping import datetime import json import pathlib import pytest from muse.core.types import Manifest, blob_id from muse.core.object_store import write_object from muse.core.ids import hash_commit as compute_commit_id, hash_snapshot as compute_snapshot_id from muse.core.commits import ( CommitRecord, write_commit, ) from muse.core.snapshots import ( SnapshotRecord, write_snapshot, ) from muse.core.paths import muse_dir, ref_path from tests.cli_test_helper import CliRunner runner = CliRunner() cli = None _REPO_ID = "sparse-supercharge-test" # --------------------------------------------------------------------------- # Helpers # --------------------------------------------------------------------------- def _init_repo(path: pathlib.Path) -> pathlib.Path: muse = muse_dir(path) for d in ("commits", "snapshots", "objects", "refs/heads", "code"): (muse / d).mkdir(parents=True, exist_ok=True) (muse / "HEAD").write_text("ref: refs/heads/main", encoding="utf-8") (muse / "repo.json").write_text( json.dumps({"repo_id": _REPO_ID, "domain": "code"}), encoding="utf-8" ) return path def _env(repo: pathlib.Path) -> Mapping[str, str]: return {"MUSE_REPO_ROOT": str(repo)} def _invoke(args: list[str], repo: pathlib.Path) -> tuple[int, str, str]: result = runner.invoke(cli, args, env=_env(repo)) return result.exit_code, result.stdout, result.stderr def _sparse_config(repo: pathlib.Path) -> pathlib.Path: return muse_dir(repo) / "sparse-checkout" def _obj(repo: pathlib.Path, content: bytes) -> str: oid = blob_id(content) write_object(repo, oid, content) return oid def _snap(repo: pathlib.Path, manifest: Manifest) -> str: sid = compute_snapshot_id(manifest) write_snapshot( repo, SnapshotRecord( snapshot_id=sid, manifest=manifest, created_at=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), ), ) return sid def _commit(repo: pathlib.Path, sid: str, branch: str = "main") -> str: committed_at = datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc) cid = compute_commit_id( parent_ids=[], snapshot_id=sid, message="test", committed_at_iso=committed_at.isoformat(), author="gabriel",) write_commit( repo, CommitRecord( commit_id=cid, branch=branch, snapshot_id=sid, message="test", committed_at=committed_at, author="gabriel", parent_commit_id=None, ), ) ref = ref_path(repo, branch) ref.write_text(cid, encoding="utf-8") return cid def _make_repo_with_snapshot(tmp_path: pathlib.Path) -> tuple[pathlib.Path, str]: """Return (repo, snapshot_id) with 10 files: 5 under src/, 5 under tests/.""" repo = _init_repo(tmp_path) manifest: Manifest = {} for i in range(5): oid = _obj(repo, f"src content {i}".encode()) manifest[f"src/module_{i}.py"] = oid for i in range(5): oid = _obj(repo, f"test content {i}".encode()) manifest[f"tests/test_{i}.py"] = oid oid_root = _obj(repo, b"readme") manifest["README.md"] = oid_root sid = _snap(repo, manifest) _commit(repo, sid) return repo, sid # --------------------------------------------------------------------------- # TestJsonEnvelopeAllSubcommands # --------------------------------------------------------------------------- class TestJsonEnvelopeAllSubcommands: """Every subcommand must emit duration_ms and exit_code when --json is passed.""" def test_init_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) rc, out, _ = _invoke(["sparse-checkout", "init", "--json"], repo) assert rc == 0 data = json.loads(out) assert "duration_ms" in data, "init --json must include duration_ms" assert isinstance(data["duration_ms"], (int, float)) def test_init_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) rc, out, _ = _invoke(["sparse-checkout", "init", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["exit_code"] == 0 def test_set_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "set", "src/", "--json"], repo) assert rc == 0 data = json.loads(out) assert "duration_ms" in data def test_set_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "set", "src/", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["exit_code"] == 0 def test_add_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "add", "src/", "--json"], repo) assert rc == 0 data = json.loads(out) assert "duration_ms" in data def test_add_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "add", "src/", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["exit_code"] == 0 def test_disable_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "disable", "--json"], repo) assert rc == 0 data = json.loads(out) assert "duration_ms" in data def test_disable_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "disable", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["exit_code"] == 0 def test_list_json_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "list", "--json"], repo) assert rc == 0 data = json.loads(out) assert "duration_ms" in data def test_list_json_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "list", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["exit_code"] == 0 # --------------------------------------------------------------------------- # TestInitJsonSchema # --------------------------------------------------------------------------- class TestInitJsonSchema: """init --json must emit the correct schema in every scenario.""" def test_fresh_init_cone_mode(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) rc, out, _ = _invoke(["sparse-checkout", "init", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["mode"] == "cone" assert data["switched"] is False assert data["exit_code"] == 0 def test_fresh_init_pattern_mode(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) rc, out, _ = _invoke(["sparse-checkout", "init", "--no-cone", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["mode"] == "pattern" assert data["switched"] is False assert data["exit_code"] == 0 def test_idempotent_init_no_switch(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "init", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["switched"] is False assert data["mode"] == "cone" def test_mode_switch_cone_to_pattern(self, tmp_path: pathlib.Path) -> None: """init --no-cone on existing cone repo must switch mode and report it.""" repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "init", "--no-cone", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["switched"] is True assert data["mode"] == "pattern" assert data["previous_mode"] == "cone" def test_mode_switch_pattern_to_cone(self, tmp_path: pathlib.Path) -> None: """init (cone default) on existing pattern repo must switch and report.""" repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init", "--no-cone"], repo) rc, out, _ = _invoke(["sparse-checkout", "init", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["switched"] is True assert data["mode"] == "cone" assert data["previous_mode"] == "pattern" def test_mode_switch_preserves_patterns(self, tmp_path: pathlib.Path) -> None: """Mode switch must keep existing patterns.""" repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/", "tests/"], repo) _invoke(["sparse-checkout", "init", "--no-cone"], repo) cfg = json.loads(_sparse_config(repo).read_text()) assert "src/" in cfg["patterns"] assert "tests/" in cfg["patterns"] # --------------------------------------------------------------------------- # TestSetJsonSchema # --------------------------------------------------------------------------- class TestSetJsonSchema: """set --json must emit patterns, total, duration_ms, exit_code.""" def test_set_json_patterns_array(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "set", "src/", "tests/", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["patterns"] == ["src/", "tests/"] def test_set_json_total_count(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "set", "src/", "tests/", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["total"] == 2 def test_set_json_replaces_previous(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "old/", "--json"], repo) rc, out, _ = _invoke(["sparse-checkout", "set", "new/", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["patterns"] == ["new/"] assert data["total"] == 1 def test_set_without_init_fails(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) rc, _, _ = _invoke(["sparse-checkout", "set", "src/", "--json"], repo) assert rc != 0 # --------------------------------------------------------------------------- # TestAddJsonSchema # --------------------------------------------------------------------------- class TestAddJsonSchema: """add --json must emit added, skipped, patterns, total, duration_ms, exit_code.""" def test_add_json_added_count(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "add", "src/", "tests/", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["added"] == 2 assert data["skipped"] == 0 def test_add_json_skipped_count(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "add", "src/", "--json"], repo) rc, out, _ = _invoke(["sparse-checkout", "add", "src/", "docs/", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["added"] == 1 assert data["skipped"] == 1 def test_add_json_patterns_array(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "add", "src/", "--json"], repo) rc, out, _ = _invoke(["sparse-checkout", "add", "tests/", "--json"], repo) assert rc == 0 data = json.loads(out) assert "src/" in data["patterns"] assert "tests/" in data["patterns"] def test_add_json_total_is_cumulative(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "add", "src/", "tests/", "--json"], repo) rc, out, _ = _invoke(["sparse-checkout", "add", "docs/", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["total"] == 3 # --------------------------------------------------------------------------- # TestDisableJsonSchema # --------------------------------------------------------------------------- class TestDisableJsonSchema: """disable --json must emit was_enabled, duration_ms, exit_code.""" def test_disable_json_was_enabled_true(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "disable", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["was_enabled"] is True assert data["exit_code"] == 0 def test_disable_json_was_enabled_false_when_already_disabled(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) rc, out, _ = _invoke(["sparse-checkout", "disable", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["was_enabled"] is False assert data["exit_code"] == 0 def test_disable_removes_config(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "disable", "--json"], repo) assert not _sparse_config(repo).exists() # --------------------------------------------------------------------------- # TestStatsSubcommand # --------------------------------------------------------------------------- class TestStatsSubcommand: """stats subcommand reports matching/excluded file counts from HEAD snapshot.""" def test_stats_with_cone_filter(self, tmp_path: pathlib.Path) -> None: repo, _ = _make_repo_with_snapshot(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0, out data = json.loads(out) # 5 src/ files + 1 README.md (root file in cone) = 6 matching assert data["matching_files"] == 6 assert data["excluded_files"] == 5 # tests/ excluded assert data["total_files"] == 11 def test_stats_total_files(self, tmp_path: pathlib.Path) -> None: repo, _ = _make_repo_with_snapshot(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["total_files"] == data["matching_files"] + data["excluded_files"] def test_stats_efficiency_ratio(self, tmp_path: pathlib.Path) -> None: repo, _ = _make_repo_with_snapshot(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) expected = data["matching_files"] / data["total_files"] assert abs(data["efficiency"] - expected) < 0.001 def test_stats_disabled_means_all_match(self, tmp_path: pathlib.Path) -> None: """When sparse-checkout is disabled, all files match.""" repo, _ = _make_repo_with_snapshot(tmp_path) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["enabled"] is False assert data["matching_files"] == data["total_files"] assert data["excluded_files"] == 0 assert data["efficiency"] == 1.0 def test_stats_has_duration_ms(self, tmp_path: pathlib.Path) -> None: repo, _ = _make_repo_with_snapshot(tmp_path) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) assert "duration_ms" in data assert isinstance(data["duration_ms"], (int, float)) def test_stats_has_exit_code(self, tmp_path: pathlib.Path) -> None: repo, _ = _make_repo_with_snapshot(tmp_path) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["exit_code"] == 0 def test_stats_no_commits_exits_cleanly(self, tmp_path: pathlib.Path) -> None: """Stats on a repo with no commits must exit 0 with zeros.""" repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["total_files"] == 0 assert data["matching_files"] == 0 def test_stats_pattern_mode(self, tmp_path: pathlib.Path) -> None: repo, _ = _make_repo_with_snapshot(tmp_path) _invoke(["sparse-checkout", "init", "--no-cone"], repo) _invoke(["sparse-checkout", "set", "src/**"], repo) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) # pattern mode: only src/** matches; README.md excluded assert data["matching_files"] == 5 assert data["excluded_files"] == 6 # --------------------------------------------------------------------------- # TestSecurityValidation # --------------------------------------------------------------------------- class TestSecurityValidation: """Dangerous patterns must be rejected before being written to disk.""" def test_path_traversal_rejected_set(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, err = _invoke(["sparse-checkout", "set", "../etc/passwd"], repo) assert rc != 0, "path traversal pattern must be rejected" def test_path_traversal_rejected_add(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, err = _invoke(["sparse-checkout", "add", "../secret"], repo) assert rc != 0, "path traversal in add must be rejected" def test_path_traversal_nested_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, _ = _invoke(["sparse-checkout", "set", "src/../../etc"], repo) assert rc != 0, "nested path traversal must be rejected" def test_null_byte_pattern_rejected_set(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, _ = _invoke(["sparse-checkout", "set", "src/\x00malicious"], repo) assert rc != 0, "null byte in pattern must be rejected" def test_null_byte_pattern_rejected_add(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, _ = _invoke(["sparse-checkout", "add", "foo\x00bar"], repo) assert rc != 0, "null byte in add pattern must be rejected" def test_ansi_still_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, _ = _invoke(["sparse-checkout", "set", "src/\x1b[31mmalicious"], repo) assert rc != 0, "ANSI escape in pattern must still be rejected" def test_safe_pattern_not_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, _ = _invoke(["sparse-checkout", "set", "src/"], repo) assert rc == 0, "normal safe pattern must not be rejected" def test_error_message_mentions_traversal(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, err = _invoke(["sparse-checkout", "set", "../bad"], repo) assert rc != 0 combined = (out + err).lower() assert "traversal" in combined or ".." in combined, ( "error message must describe the path traversal issue" ) # --------------------------------------------------------------------------- # TestConfigCorruption # --------------------------------------------------------------------------- class TestConfigCorruption: """Malformed sparse-checkout config must produce a clear error, not a traceback.""" def test_malformed_json_exits_nonzero(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _sparse_config(repo).write_text("{invalid json", encoding="utf-8") rc, _, _ = _invoke(["sparse-checkout", "list", "--json"], repo) assert rc != 0, "malformed config must exit non-zero" def test_malformed_json_error_message(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _sparse_config(repo).write_text("{invalid json", encoding="utf-8") rc, out, err = _invoke(["sparse-checkout", "list", "--json"], repo) assert rc != 0 combined = out + err assert len(combined.strip()) > 0, "must emit an error message" def test_missing_mode_key(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _sparse_config(repo).write_text(json.dumps({"patterns": []}), encoding="utf-8") rc, _, _ = _invoke(["sparse-checkout", "list"], repo) assert rc != 0, "config missing 'mode' key must exit non-zero" def test_missing_patterns_key(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _sparse_config(repo).write_text(json.dumps({"mode": "cone"}), encoding="utf-8") rc, _, _ = _invoke(["sparse-checkout", "list"], repo) assert rc != 0, "config missing 'patterns' key must exit non-zero" # --------------------------------------------------------------------------- # TestModeSwitch # --------------------------------------------------------------------------- class TestModeSwitch: """Mode switching via re-init must work cleanly.""" def test_switch_updates_config_file(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "init", "--no-cone"], repo) cfg = json.loads(_sparse_config(repo).read_text()) assert cfg["mode"] == "pattern" def test_same_mode_reinit_is_not_switch(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, out, _ = _invoke(["sparse-checkout", "init", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["switched"] is False def test_switch_preserves_patterns_count(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "a/", "b/", "c/"], repo) _invoke(["sparse-checkout", "init", "--no-cone"], repo) cfg = json.loads(_sparse_config(repo).read_text()) assert len(cfg["patterns"]) == 3 # --------------------------------------------------------------------------- # TestPatternEdgeCases # --------------------------------------------------------------------------- class TestPatternEdgeCases: """Edge case patterns that might slip through validation.""" def test_whitespace_only_pattern_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, _ = _invoke(["sparse-checkout", "set", " "], repo) assert rc != 0, "whitespace-only pattern must be rejected" def test_empty_string_pattern_rejected(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) # argparse nargs="+" won't pass empty string, so simulate via direct config write _sparse_config(repo).write_text( json.dumps({"mode": "cone", "patterns": [""]}), encoding="utf-8" ) rc, out, _ = _invoke(["sparse-checkout", "list", "--json"], repo) # Reading an empty pattern is fine; it won't match anything useful assert rc == 0 # list itself doesn't re-validate stored patterns def test_very_long_pattern_accepted(self, tmp_path: pathlib.Path) -> None: """Patterns up to 1024 chars must be accepted (no arbitrary length limit).""" repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) long_pat = "a/" * 200 # 400 chars rc, _, _ = _invoke(["sparse-checkout", "set", long_pat], repo) assert rc == 0, "long but valid pattern must be accepted" def test_unicode_pattern_accepted(self, tmp_path: pathlib.Path) -> None: """Unicode patterns are valid (e.g. internationalised path names).""" repo = _init_repo(tmp_path) _invoke(["sparse-checkout", "init"], repo) rc, _, _ = _invoke(["sparse-checkout", "set", "música/", "--json"], repo) assert rc == 0, "unicode path pattern must be accepted" # --------------------------------------------------------------------------- # TestStressLargeManifest # --------------------------------------------------------------------------- class TestStressLargeManifest: """stats must handle large manifests without degrading.""" def test_stats_1000_file_manifest(self, tmp_path: pathlib.Path) -> None: repo = _init_repo(tmp_path) manifest: Manifest = {} for i in range(500): oid = _obj(repo, f"src {i}".encode()) manifest[f"src/file_{i}.py"] = oid for i in range(500): oid = _obj(repo, f"other {i}".encode()) manifest[f"other/file_{i}.py"] = oid sid = _snap(repo, manifest) _commit(repo, sid) _invoke(["sparse-checkout", "init"], repo) _invoke(["sparse-checkout", "set", "src/"], repo) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["total_files"] == 1000 assert data["matching_files"] == 500 assert data["excluded_files"] == 500 def test_stats_500_patterns(self, tmp_path: pathlib.Path) -> None: """500-pattern list must not degrade stats computation.""" repo = _init_repo(tmp_path) manifest: Manifest = {} for i in range(10): oid = _obj(repo, f"content {i}".encode()) manifest[f"dir_{i}/file.py"] = oid sid = _snap(repo, manifest) _commit(repo, sid) _invoke(["sparse-checkout", "init", "--no-cone"], repo) patterns = [f"dir_{i}/**" for i in range(500)] _invoke(["sparse-checkout", "set"] + patterns, repo) rc, out, _ = _invoke(["sparse-checkout", "stats", "--json"], repo) assert rc == 0 data = json.loads(out) assert data["matching_files"] == 10