test_named_targets.py¶
tests/test_named_targets.py
One repository root may hold several named indexes.
test_a_first_party_target_adds_nested_excludes
None test_named_targets.test_a_first_party_target_adds_nested_excludes(Path tmp_path)
A first-party target adds the nested trees to the caller's exclusions.
tests/test_named_targets.py:234
Checks
assert "build" in got_exclude, "the operator's own exclusion was dropped"assert "extern/dep" in got_exclude, "the nested tree was not excluded from first-party"assert got_options is None
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_a_first_party_target_adds_nested_excludes → _sub_index_scope → declared_sub_index_excludes |
| REQ-DDB-GUARD-012 | inferred | test_a_first_party_target_adds_nested_excludes → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-INDEX-002 | measured | test_a_first_party_target_adds_nested_excludes → _sub_index_scope → derive_sub_indexes |
| REQ-DDB-MCP-001 | measured | test_a_first_party_target_adds_nested_excludes → target_for |
| REQ-DDB-WIKI-054 | inferred | test_a_first_party_target_adds_nested_excludes → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 25 lines
def test_a_first_party_target_adds_nested_excludes(tmp_path: Path) -> None:
"""ADDS, never replaces. The operator's own exclusions and the derived ones answer different
questions — "what I do not want indexed" and "what belongs to another index" — and dropping
either produces a build that is quietly wrong in a different direction.
@brief First-party excludes the nested trees on top of the caller's list.
@version 1
"""
from clew.mcp_server.server import _sub_index_scope
from clew.mcp_server.state import target_for
from clew.scope import FIRST_PARTY_INDEX
repo = tmp_path / "vendored"
(repo / "app").mkdir(parents=True)
nested = repo / "extern" / "dep"
nested.mkdir(parents=True)
(nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")
first = target_for(repo, tmp_path / "state", name=FIRST_PARTY_INDEX)
got_exclude, got_options = _sub_index_scope(first, repo.resolve(), ["build"], None)
assert "build" in got_exclude, "the operator's own exclusion was dropped"
assert "extern/dep" in got_exclude, "the nested tree was not excluded from first-party"
assert got_options is None
Activity · 13 steps
test_a_hostile_name_cannot_escape_the_state_directory
None test_named_targets.test_a_hostile_name_cannot_escape_the_state_directory(Path tmp_path)
A name that would escape the state directory is refused or neutralised.
tests/test_named_targets.py:117
Checks
assert resolved.is_relative_to(state), (f"name {hostile!r} placed a database outside the state root: {resolved}" )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured | test_a_hostile_name_cannot_escape_the_state_directory → target_for |
Source · 26 lines
def test_a_hostile_name_cannot_escape_the_state_directory(tmp_path: Path) -> None:
"""THE NAME REACHES A FILESYSTEM PATH, so it is untrusted input to a path join. A name of
`../../etc` would otherwise place a database outside the state root — and sub-index names
come from a repository's own `.clew.yaml`, which is exactly the "target repo influences the
build" surface this project keeps hardening.
Asserted as containment rather than as a specific rejection, so either refusing or
sanitising satisfies it — what must not happen is a path outside the root.
@brief A sub-index database stays under the state root.
@version 1
"""
repo = tmp_path / "myproj"
repo.mkdir()
state = (tmp_path / "state").resolve()
for hostile in ("../../etc", "..", "a/b", "with space", "sub\x00idx"):
try:
got = target_for(repo, state, name=hostile)
except (ValueError, OSError):
continue
resolved = Path(got.db_path).resolve()
assert resolved.is_relative_to(state), (
f"name {hostile!r} placed a database outside the state root: {resolved}"
)
Activity · 10 steps
test_a_legacy_record_without_repo_path_still_reloads
None test_named_targets.test_a_legacy_record_without_repo_path_still_reloads(Path tmp_path)
A registry written before sub-indexes existed still reloads.
tests/test_named_targets.py:178
Checks
assert len(got) == 1assert got[0].repo_path == "/some/repo", "a legacy record lost its repo path"assert got[0].name is None, "a legacy record must read as the whole repository"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured |
Source · 23 lines
def test_a_legacy_record_without_repo_path_still_reloads(tmp_path: Path) -> None:
"""Every registry on disk today stores `{slug, db_path}` and nothing else, keyed by the repo
path. The reader must keep falling back to the KEY for those, or an upgrade silently forgets
every target the user already has.
@brief Pre-sub-index records keep working.
@version 1
"""
import json
from clew.mcp_server.state import TargetRegistry
home = tmp_path / "state"
home.mkdir(parents=True)
legacy = {"/some/repo": {"slug": "repo-abc123", "db_path": "/some/state/repo-abc123/clew.db"}}
(home / "targets.json").write_text(json.dumps(legacy), encoding="utf-8")
got = TargetRegistry(home=home).targets()
assert len(got) == 1
assert got[0].repo_path == "/some/repo", "a legacy record lost its repo path"
assert got[0].name is None, "a legacy record must read as the whole repository"
Activity · 10 steps
test_a_named_target_is_distinct
None test_named_targets.test_a_named_target_is_distinct(Path tmp_path)
A named target gets its own slug and its own database.
tests/test_named_targets.py:73
Checks
assert len({plain.slug, first.slug, vendor.slug}) == 3, (f"slugs collided: {plain.slug}, {first.slug}, {vendor.slug}" )assert len({plain.db_path, first.db_path, vendor.db_path}) == 3, "db paths collided"assert first.repo_path == vendor.repo_path == plain.repo_path
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured | test_a_named_target_is_distinct → target_for |
Source · 26 lines
def test_a_named_target_is_distinct(tmp_path: Path) -> None:
"""Two sub-indexes of ONE root must not share a database — that is the whole feature. The
assertion is on both the slug and the db path, because the slug is what a caller addresses
and the path is what the build writes; a change that made them disagree would route reads
and writes to different files.
@brief Named targets differ from the unnamed one and from each other.
@version 1
"""
repo = tmp_path / "myproj"
repo.mkdir()
state = tmp_path / "state"
plain = target_for(repo, state)
first = target_for(repo, state, name="first-party")
vendor = target_for(repo, state, name="llama-cpp")
assert len({plain.slug, first.slug, vendor.slug}) == 3, (
f"slugs collided: {plain.slug}, {first.slug}, {vendor.slug}"
)
assert len({plain.db_path, first.db_path, vendor.db_path}) == 3, "db paths collided"
## Every sub-index still names the SAME repository — that is what makes them siblings, and
## what Phase 2's cross-index lookup will use to find them.
assert first.repo_path == vendor.repo_path == plain.repo_path
Activity · 9 steps
test_a_vendored_target_is_scoped_to_its_tree
None test_named_targets.test_a_vendored_target_is_scoped_to_its_tree(Path tmp_path)
A vendored target is scoped to its own tree.
tests/test_named_targets.py:261
Checks
assert got_options["index_scope"] == {"roots": ["extern/dep"]}
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | measured | test_a_vendored_target_is_scoped_to_its_tree → _sub_index_scope → declared_sub_index_excludes |
| REQ-DDB-GUARD-012 | inferred | test_a_vendored_target_is_scoped_to_its_tree → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-INDEX-002 | measured | test_a_vendored_target_is_scoped_to_its_tree → _sub_index_scope → derive_sub_indexes |
| REQ-DDB-MCP-001 | measured | test_a_vendored_target_is_scoped_to_its_tree → target_for |
| REQ-DDB-WIKI-054 | inferred | test_a_vendored_target_is_scoped_to_its_tree → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 18 lines
def test_a_vendored_target_is_scoped_to_its_tree(tmp_path: Path) -> None:
"""@brief A named vendored sub-index declares roots covering only itself.
@version 1
"""
from clew.mcp_server.server import _sub_index_scope
from clew.mcp_server.state import target_for
repo = tmp_path / "vendored"
(repo / "app").mkdir(parents=True)
nested = repo / "extern" / "dep"
nested.mkdir(parents=True)
(nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")
vendored = target_for(repo, tmp_path / "state", name="extern-dep")
_got_exclude, got_options = _sub_index_scope(vendored, repo.resolve(), None, None)
assert got_options["index_scope"] == {"roots": ["extern/dep"]}
Activity · 10 steps
test_a_whole_repo_target_keeps_the_callers_scope
None test_named_targets.test_a_whole_repo_target_keeps_the_callers_scope(Path tmp_path)
A whole-repo target's build scope is left exactly as the caller stated it.
tests/test_named_targets.py:203
Checks
assert _sub_index_scope(whole, repo.resolve(), None, None) == (None, None)assert _sub_index_scope(whole, repo.resolve(), ["build"], {"kconfig": {}}) == (["build"], {"kconfig": {}}, )
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-CONFIG-001 | inferred | test_a_whole_repo_target_keeps_the_callers_scope → _sub_index_scope → declared_sub_index_excludes |
| REQ-DDB-GUARD-012 | inferred | test_a_whole_repo_target_keeps_the_callers_scope → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_requirements → _validate_node |
| REQ-DDB-INDEX-002 | inferred | test_a_whole_repo_target_keeps_the_callers_scope → _sub_index_scope → derive_sub_indexes |
| REQ-DDB-MCP-001 | measured | test_a_whole_repo_target_keeps_the_callers_scope → target_for |
| REQ-DDB-WIKI-054 | inferred | test_a_whole_repo_target_keeps_the_callers_scope → _sub_index_scope → declared_sub_index_excludes → load_declaration → load_declaration_located → _checked_sections → _checked_section → _checked_list_section → all |
Source · 29 lines
def test_a_whole_repo_target_keeps_the_callers_scope(tmp_path: Path) -> None:
"""THE #511 GUARD. A repository that COULD be split must keep building whole until someone
builds its named targets. Narrowing it here would shrink an index nobody asked to shrink and
report success — a smaller index that reads as healthy, which is the silent-narrowing failure
that task records.
`exclude=None` must survive as None, not become []: None means "inherit what an earlier
session stated", and flattening it would silently withdraw the operator's own decision.
@brief An unnamed target's scope passes through untouched.
@version 1
"""
from clew.mcp_server.server import _sub_index_scope
from clew.mcp_server.state import target_for
repo = tmp_path / "vendored"
(repo / "app").mkdir(parents=True)
nested = repo / "extern" / "dep"
nested.mkdir(parents=True)
(nested / ".git").write_text("gitdir: ../../.git/modules/dep\n", encoding="utf-8")
whole = target_for(repo, tmp_path / "state")
assert _sub_index_scope(whole, repo.resolve(), None, None) == (None, None)
assert _sub_index_scope(whole, repo.resolve(), ["build"], {"kconfig": {}}) == (
["build"],
{"kconfig": {}},
)
Activity · 10 steps
test_an_unnamed_target_is_unchanged
None test_named_targets.test_an_unnamed_target_is_unchanged(Path tmp_path)
An unnamed target keeps exactly the slug and path it has always had.
tests/test_named_targets.py:54
Checks
assert got.slug == _legacy_slug(repo.resolve()), ("the unnamed slug rule changed; every index already built is now unreachable" )assert got.db_path.endswith(f"targets/{got.slug}/clew.db")
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured | test_an_unnamed_target_is_unchanged → target_for |
Source · 17 lines
def test_an_unnamed_target_is_unchanged(tmp_path: Path) -> None:
"""THE COMPATIBILITY GATE. Every index already on disk lives at a path derived from this
rule, and nothing records where it came from — so if this moves, those indexes are not
migrated, they are lost, and the failure reads as "never built" on a repo that has one.
@brief The historical slug derivation is preserved.
@version 1
"""
repo = tmp_path / "myproj"
repo.mkdir()
got = target_for(repo, tmp_path / "state")
assert got.slug == _legacy_slug(repo.resolve()), (
"the unnamed slug rule changed; every index already built is now unreachable"
)
assert got.db_path.endswith(f"targets/{got.slug}/clew.db")
Activity · 5 steps
test_named_allocation_is_deterministic
None test_named_targets.test_named_allocation_is_deterministic(Path tmp_path)
The same name yields the same target, every time.
tests/test_named_targets.py:101
Checks
assert target_for(repo, state, name="vendor") == target_for(repo, state, name="vendor")
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured | test_named_allocation_is_deterministic → target_for |
Source · 14 lines
def test_named_allocation_is_deterministic(tmp_path: Path) -> None:
"""A build and a later query must land on the same database. Nothing persists the mapping
from name to path, so determinism IS the mapping — exactly as it already is for the unnamed
form.
@brief Allocation is a pure function of (path, name).
@version 1
"""
repo = tmp_path / "myproj"
repo.mkdir()
state = tmp_path / "state"
assert target_for(repo, state, name="vendor") == target_for(repo, state, name="vendor")
Activity · 4 steps
test_the_registry_holds_sub_indexes_of_one_root
None test_named_targets.test_the_registry_holds_sub_indexes_of_one_root(Path tmp_path)
A registry holds several sub-indexes of one root, each with its own database.
tests/test_named_targets.py:145
Checks
assert len(loaded) == 3, f"expected three records, got {sorted(loaded)}"assert back.repo_path == str(repo.resolve()), (f"{allocated.slug} reloaded with repo_path {back.repo_path!r} — a composite key " "leaked into a field that is used as a filesystem path" )assert back.name == allocated.nameassert Path(back.db_path).parent.is_dir(), "the db directory was not created"
Result: passed
Verifies
| requirement | linked | path to the implementer |
|---|---|---|
| REQ-DDB-MCP-001 | measured |
Source · 31 lines
def test_the_registry_holds_sub_indexes_of_one_root(tmp_path: Path) -> None:
"""THE FIELD THAT MAKES THE COMPOSITE KEY SAFE is `repo_path` inside the record. `targets()`
used to reconstruct a Target's repo_path FROM THE KEY, which is correct while the key is a
bare path and wrong the moment it carries a name — the result would be a path that does not
exist, handed to `Path()` and stamped into replies as the repository that answered.
@brief Sub-indexes register and reload with the true repo path.
@version 1
"""
from clew.mcp_server.state import TargetRegistry
repo = tmp_path / "myproj"
repo.mkdir()
reg = TargetRegistry(home=tmp_path / "state")
whole = reg.register(repo)
first = reg.register(repo, name="first-party")
vendor = reg.register(repo, name="llama-cpp")
loaded = {t.slug: t for t in reg.targets()}
assert len(loaded) == 3, f"expected three records, got {sorted(loaded)}"
for allocated in (whole, first, vendor):
back = loaded[allocated.slug]
assert back.repo_path == str(repo.resolve()), (
f"{allocated.slug} reloaded with repo_path {back.repo_path!r} — a composite key "
"leaked into a field that is used as a filesystem path"
)
assert back.name == allocated.name
assert Path(back.db_path).parent.is_dir(), "the db directory was not created"