Tests: sisi pick only returns offered models (black-box, 8 cases; fail until the feature lands)
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3a349a3859
commit
b95c9ee37f
1 file changed
+154
@@ -0,0 +1,154 @@
|
||||
"""`sisi pick` only returns models the running harness offers: the harness's raw model list is the source of truth,
|
||||
the researched table is a hint. Black-box CLI tests in a temporary home and store."""
|
||||
import glob, hashlib, json, os, re, shutil, subprocess, sys, tempfile, unittest
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
REAL_HOME = os.path.expanduser("~")
|
||||
HEADER = "| worker | how to call it | use for | cost | good at, bad at | notes |\n|---|---|---|---|---|---|\n"
|
||||
ALPHA = "| alpha | host subagent model `alpha` | routine | free | quick | |\n"
|
||||
BETA = "| beta | host subagent model `beta` | routine | free | quick | |\n"
|
||||
GAMMA = "| gamma | host subagent model `gamma` | routine | free | quick | |\n"
|
||||
RAW = "alpha\nbeta\n"
|
||||
|
||||
|
||||
def tree():
|
||||
"""A fingerprint of the product tree, so a test that writes into it is caught."""
|
||||
out = hashlib.sha1()
|
||||
for p in sorted(glob.glob(os.path.join(REPO, "sisi", "*")) + glob.glob(os.path.join(REPO, "canon", "*"))):
|
||||
if os.path.isfile(p):
|
||||
with open(p, "rb") as f:
|
||||
out.update(p.encode() + f.read())
|
||||
return out.hexdigest()
|
||||
|
||||
|
||||
class Offered(unittest.TestCase):
|
||||
def setUp(self):
|
||||
before = tree()
|
||||
self.addCleanup(lambda: self.assertEqual(tree(), before, "a test wrote into the product tree"))
|
||||
book = os.path.join(REAL_HOME, ".claude", "CLAUDE.md")
|
||||
mine = self.read(book) if os.path.exists(book) else None
|
||||
self.addCleanup(lambda: self.assertEqual(self.read(book) if os.path.exists(book) else None, mine,
|
||||
"a test wrote the person's own rules file"))
|
||||
self.tmp = tempfile.mkdtemp(prefix="sisi-test-")
|
||||
self.addCleanup(shutil.rmtree, self.tmp, True)
|
||||
self.data = os.path.join(self.tmp, "data")
|
||||
os.makedirs(self.data)
|
||||
self.write(os.path.join(self.data, "config.json"), json.dumps({"core": {"enforce": False}}))
|
||||
self.env = dict(os.environ, HOME=self.tmp, SISI_DATA=self.data, SISI_STATE=os.path.join(self.tmp, "state"),
|
||||
SISI_LIB=os.path.join(self.tmp, "lib"), CLAUDE_CONFIG_DIR=os.path.join(self.tmp, "claude"))
|
||||
for name in ("SISI_SESSION_ID", "CLAUDE_ENV_FILE", "XDG_DATA_HOME", "XDG_STATE_HOME", "CODEX_HOME",
|
||||
"CLAUDE_CODE_ENTRYPOINT"):
|
||||
self.env.pop(name, None) # the harness is "unknown" unless a test sets it
|
||||
self.proj = os.path.join(self.tmp, "proj")
|
||||
os.makedirs(self.proj)
|
||||
subprocess.run(["git", "init", "-q"], cwd=self.proj, check=True, capture_output=True)
|
||||
|
||||
def sisi(self, *args, stdin="", env=None):
|
||||
r = subprocess.run([sys.executable, os.path.join(REPO, "bin", "sisi")] + list(args), cwd=self.proj,
|
||||
env=env or self.env, input=stdin, capture_output=True, text=True)
|
||||
return r.returncode, r.stdout, r.stderr
|
||||
|
||||
def table(self, text):
|
||||
self.write(os.path.join(self.data, "models.md"), text)
|
||||
|
||||
def offered_file(self, raw, harness="unknown", when=None):
|
||||
"""A stored list as `sisi models offered` writes it: a dated header line, then the raw text."""
|
||||
when = when or datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
|
||||
os.makedirs(os.path.join(self.data, "offered"), exist_ok=True)
|
||||
self.write(os.path.join(self.data, "offered", harness + ".txt"), "listed %s by harness %s\n%s" % (when, harness, raw))
|
||||
|
||||
def picked(self, out):
|
||||
m = re.search(r"^worker: (\S+)", out, re.M)
|
||||
return m.group(1) if m else None
|
||||
|
||||
def read(self, *parts):
|
||||
with open(os.path.join(*parts), encoding="utf-8") as f:
|
||||
return f.read()
|
||||
|
||||
def write(self, path, text):
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(text)
|
||||
return path
|
||||
|
||||
def test_1_no_list_refuses_the_pick_with_one_line_naming_the_command(self):
|
||||
self.table(HEADER + ALPHA + BETA)
|
||||
rc, out, _ = self.sisi("pick", "routine")
|
||||
self.assertEqual(rc, 1, out)
|
||||
lines = out.splitlines()
|
||||
self.assertEqual(len(lines), 1, out)
|
||||
self.assertIn("sisi models offered", lines[0])
|
||||
|
||||
def test_2_only_listed_workers_are_ever_picked(self):
|
||||
self.table(HEADER + GAMMA + ALPHA + BETA) # gamma is first in the table's order and is not on the list
|
||||
self.offered_file(RAW)
|
||||
seen = set()
|
||||
for _ in range(30):
|
||||
rc, out, _ = self.sisi("pick", "routine")
|
||||
self.assertEqual(rc, 0, out)
|
||||
seen.add(self.picked(out))
|
||||
self.assertNotIn("gamma", seen)
|
||||
self.assertTrue(seen <= {"alpha", "beta"}, seen)
|
||||
|
||||
def test_3_a_list_older_than_24_hours_refuses_the_pick(self):
|
||||
self.table(HEADER + ALPHA + BETA)
|
||||
old = (datetime.now() - timedelta(hours=25)).strftime("%Y-%m-%dT%H:%M:%S")
|
||||
self.offered_file(RAW, when=old)
|
||||
rc, out, _ = self.sisi("pick", "routine")
|
||||
self.assertEqual(rc, 1, out)
|
||||
self.assertIn("h old", out)
|
||||
|
||||
def test_4_a_list_stored_for_unknown_does_not_serve_another_harness(self):
|
||||
self.table(HEADER + ALPHA + BETA)
|
||||
self.offered_file(RAW) # stored for "unknown"
|
||||
env = dict(self.env, CLAUDE_CODE_ENTRYPOINT="x")
|
||||
rc, out, _ = self.sisi("pick", "routine", env=env)
|
||||
self.assertEqual(rc, 1, out)
|
||||
|
||||
def test_5_a_record_naming_an_id_absent_from_the_list_changes_nothing(self):
|
||||
self.table(HEADER + ALPHA + BETA)
|
||||
rc, out, err = self.sisi("models", "offered", stdin=RAW)
|
||||
self.assertEqual(rc, 0, out + err)
|
||||
bad = self.write(os.path.join(self.tmp, "bad.md"), HEADER + "| ghost | `ghost run \"<brief>\"` | routine | free | quick | |\n")
|
||||
rc, out, err = self.sisi("models", "offered", "--record", bad)
|
||||
self.assertEqual(rc, 1, out + err)
|
||||
self.assertIn("Nothing was changed", out + err)
|
||||
self.assertFalse(os.path.exists(os.path.join(self.data, "offered", "unknown.wheel.md")))
|
||||
|
||||
def test_6_a_valid_record_makes_pick_print_the_wheel_rows_call(self):
|
||||
self.table(HEADER + ALPHA + BETA)
|
||||
self.sisi("models", "offered", stdin=RAW)
|
||||
cell = "`alpha run \"<brief>\"`"
|
||||
good = self.write(os.path.join(self.tmp, "good.md"), HEADER + "| alpha | %s | routine | free | quick | |\n" % cell)
|
||||
rc, out, err = self.sisi("models", "offered", "--record", good)
|
||||
self.assertEqual(rc, 0, out + err)
|
||||
self.assertTrue(os.path.exists(os.path.join(self.data, "offered", "unknown.wheel.md")))
|
||||
rc, out, _ = self.sisi("pick", "routine")
|
||||
self.assertEqual(rc, 0, out)
|
||||
self.assertIn("how to call it: " + cell, out)
|
||||
|
||||
def test_7_piped_list_prints_the_chore_line_with_an_id(self):
|
||||
self.table(HEADER + ALPHA)
|
||||
rc, out, err = self.sisi("models", "offered", stdin="list\n")
|
||||
self.assertEqual(rc, 0, out + err)
|
||||
self.assertRegex(out, r"sisi chore: models offered [0-9a-f]{8}")
|
||||
|
||||
def test_8_a_provisional_row_for_a_new_offered_model_gets_picked(self):
|
||||
self.table(HEADER + ALPHA + BETA)
|
||||
self.offered_file("alpha\nsubq/subq-savant-latest\n")
|
||||
cell = "`opencode run -m subq/subq-savant-latest \"<brief>\"`"
|
||||
rec = self.write(os.path.join(self.tmp, "rec.md"), HEADER + ALPHA +
|
||||
"| subq/subq-savant-latest | %s | routine | free trial | fast | new: listed by the harness, not yet researched |\n" % cell)
|
||||
rc, out, err = self.sisi("models", "offered", "--record", rec)
|
||||
self.assertEqual(rc, 0, out + err)
|
||||
self.assertIn("sisi models research", out + err)
|
||||
seen = set()
|
||||
for _ in range(30):
|
||||
rc, out, _ = self.sisi("pick", "routine")
|
||||
self.assertEqual(rc, 0, out)
|
||||
seen.add(self.picked(out))
|
||||
self.assertIn("subq/subq-savant-latest", seen)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user