"""The `worktrees` binding's query filters and argument checks. `worktrees.query` and `worktrees.list` ask the host once for the recorded worktrees and filter them in the kernel: a case-insensitive text match over the documented fields (including the owning session's name), a kind, and a repository substring. A defect hides a worktree a filter should keep, or keeps one it should drop. `agents` refuses a `worktree=` that is not a Worktree or a bool before asking the host. Both are pure kernel code, so a stub host stands in for the daemon; recording, listing and removing worktrees in real repositories is apps/albedo/test/albedo/workspace/worktree_vcs_test.gleam. """ import asyncio from pathlib import Path import sys from typing import cast import unittest sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "python")) sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import hypothesis_profiles # noqa: E402 from hypothesis import given, settings, strategies as st # noqa: E402 from albedo_plugins import agents, worktrees # noqa: E402 WORDS = ["feat", "Feat-One", "fix", "jj", "git", "lab", "Ana"] words = st.sampled_from(WORDS) records = st.fixed_dictionaries( { "id": words.map(lambda w: "wt-" + w), "name": words, "path": words.map(lambda w: "/home/u/.albedo/wt/" + w), "repo": st.sampled_from(["/src/albedo", "/src/Other", "/var/lab"]), "kind": st.sampled_from(["git", "jj", "Git"]), "revision": st.none() | words, "branch": st.none() | words, "session": st.none() | st.sampled_from(["s1", "s2"]), } ) sessions = st.fixed_dictionaries( {"s1": st.fixed_dictionaries({"name": words}), "s2": st.just({"name": ""})} ) class Stub: """The host's `worktrees.list`, answering `listed` and keeping what it was asked.""" def __init__(self, listed): self.listed = listed self.asked = [] async def __call__(self, name, arguments): self.asked.append((name, arguments)) return self.listed def expected(items, owners, query, kind, repo): """The documented filters, applied one at a time.""" kept = [] for item in items: if kind is not None and item["kind"].lower() != kind.lower(): continue if repo is not None and repo.lower() not in item["repo"].lower(): continue owner = owners.get(item["session"] or "", {}).get("name", "") fields = [ item["id"], item["name"], item["path"], item["path"], item["repo"], item["kind"], item["revision"] or "", item["branch"] or "", item["session"] or "", owner, ] if query and not any(query.lower() in field.lower() for field in fields): continue kept.append(item["id"]) return kept class WorktreeQueryTest(unittest.TestCase): @settings(max_examples=hypothesis_profiles.cases(150)) @given( st.lists(records, max_size=6), sessions, st.sampled_from(["", "feat", "FEAT", "ana", "zzz", "wt-fix"]), st.none() | st.sampled_from(["git", "JJ", "darcs"]), st.none() | st.sampled_from(["albedo", "OTHER", "nowhere"]), ) def test_query_keeps_exactly_the_worktrees_every_filter_matches( self, items, owners, query, kind, repo ): stub = Stub({"worktrees": items, "sessions": owners}) worktrees.host = stub found = asyncio.run(worktrees.worktrees.query(query, kind=kind, repo=repo)) ids = [wt.id for wt in cast(list, found.worktrees)] self.assertEqual(ids, expected(items, owners, query, kind, repo)) self.assertEqual(found.count, len(ids)) listed = asyncio.run(worktrees.worktrees.list(query, kind=kind, repo=repo)) self.assertEqual([wt.id for wt in listed], ids) # One host call per question; a repository substring searches them all. self.assertEqual( [name for name, _ in stub.asked], ["worktrees.list", "worktrees.list"] ) if repo is not None: self.assertTrue(stub.asked[0][1].get("all")) def test_spawn_refuses_a_worktree_that_is_not_a_worktree_or_bool(self): for bad in ("bad", 1, {"id": "wt-1"}): with self.assertRaises(TypeError) as refused: agents._worktree_arg(bad) self.assertIn("worktree must be a Worktree or bool", str(refused.exception)) self.assertIsNone(agents._worktree_arg(False)) self.assertIs(agents._worktree_arg(True), True) if __name__ == "__main__": unittest.main()