"""Tests for spire:adepts roster.sh base-folder resolution (quire, with workpad fallbacks).""" import os import shutil import subprocess import tempfile import unittest from pathlib import Path ROSTER = Path(__file__).resolve().parents[2] / "adepts" / "roster.sh" class RosterTest(unittest.TestCase): def setUp(self): self.tmp = Path(tempfile.mkdtemp(prefix="roster-")) self.addCleanup(shutil.rmtree, self.tmp, True) self.cwd = self.tmp / "proj" self.cwd.mkdir() bindir = self.tmp / "bin" bindir.mkdir() stub = bindir / "just" stub.write_text("#!/bin/sh\nexit 1\n", encoding="utf-8") # prints nothing, fails stub.chmod(0o755) self.bindir = bindir def adept(self, base): d = self.cwd / base / "adepts" / "foo" d.mkdir(parents=True) (d / "CLAUDE.md").write_text("---\nsummary: test adept\nstatus: active\n---\n", encoding="utf-8") def project_md(self, label, base): (self.cwd / ".claude" / "spire").mkdir(parents=True) (self.cwd / ".claude" / "spire" / "project.md").write_text( f"- **{label}**: `{base}`\n", encoding="utf-8") def run_roster(self, **env): e = {k: v for k, v in os.environ.items() if k not in ("QUIRE_FOLDER", "WORKPAD_FOLDER")} e["PATH"] = f"{self.bindir}{os.pathsep}{e['PATH']}" e.update(env) r = subprocess.run(["bash", str(ROSTER)], cwd=self.cwd, env=e, capture_output=True, text=True) self.assertEqual(r.returncode, 0, r.stderr) return r.stdout def test_quire_alone(self): self.adept("docs/quire") out = self.run_roster() self.assertIn("docs/quire/adepts/", out) self.assertIn("**foo**", out) def test_workpad_alone(self): self.adept("docs/workpad") out = self.run_roster() self.assertIn("docs/workpad/adepts/", out) self.assertIn("**foo**", out) def test_both_quire_wins_with_note(self): self.adept("docs/quire") self.adept("docs/workpad") out = self.run_roster() self.assertIn("## Adept roster — docs/quire/adepts/", out) self.assertIn("docs/workpad", out.replace("## Adept roster — docs/quire/adepts/", "")) self.assertIn("also exists", out) def test_quire_label(self): self.adept("custom/q") self.project_md("Quire", "custom/q") self.assertIn("custom/q/adepts/", self.run_roster()) def test_workpad_label(self): self.adept("custom/w") self.project_md("Workpad", "custom/w") self.assertIn("custom/w/adepts/", self.run_roster()) def test_quire_folder_over_workpad_folder(self): self.adept("qf") self.adept("wf") out = self.run_roster(QUIRE_FOLDER="qf", WORKPAD_FOLDER="wf") self.assertIn("## Adept roster — qf/adepts/", out) def test_just_quire_folder_recipe(self): # the stub answers only `just quire-folder`; docs/quire exists so the recipe must beat detection (self.bindir / "just").write_text( '#!/bin/sh\n[ "$1" = quire-folder ] && { echo qf; exit 0; }\nexit 1\n', encoding="utf-8") self.adept("qf") self.adept("docs/quire") out = self.run_roster() self.assertIn("## Adept roster — qf/adepts/", out) def test_workpad_folder_alone(self): # docs/quire exists so the fallback must beat folder detection self.adept("wf") self.adept("docs/quire") out = self.run_roster(WORKPAD_FOLDER="wf") self.assertIn("## Adept roster — wf/adepts/", out) def test_quire_folder_beats_existing_workpad_dir(self): self.adept("qf") self.adept("docs/workpad") out = self.run_roster(QUIRE_FOLDER="qf") self.assertIn("## Adept roster — qf/adepts/", out) def test_label_beats_folder_detection(self): self.adept("custom/q") self.adept("docs/quire") self.project_md("Quire", "custom/q") self.assertIn("## Adept roster — custom/q/adepts/", self.run_roster()) def test_dot_quire_beats_dot_workpad(self): self.adept(".quire") self.adept(".workpad") self.assertIn("## Adept roster — .quire/adepts/", self.run_roster()) def test_dot_workpad_found_alone(self): self.adept(".workpad") self.assertIn("## Adept roster — .workpad/adepts/", self.run_roster()) def test_none_found_names_docs_quire(self): out = self.run_roster() self.assertIn("none (no docs/quire/adepts/)", out) if __name__ == "__main__": unittest.main()