casebench
test cases straight onto the board.
git clone https://owenrusk.dev/casebench.git
commit 78f20024a82ac18c71d5198008b0c73297884842 parent 60a4f777fb66e6538aa77f4c441f7278ae3b8fcf author Owen Rusk <owen@papermothgames.com> date 2026-09-29 15:12:47 -0500
lock: --any-order, for answers with no order of their own a day and a time can go on either line. every order is hashed too, so it still counts against the 64.
| README.md | +2 | -1 |
| casebench/cli.py | +2 | -1 |
| casebench/lock.py | +9 | -3 |
| tests/test_lock.py | +18 | -0 |
diff --git a/README.md b/README.md index 16b8e44..5046df4 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Test cases go straight to the board. The runtime never sees them. Reaches the bo casebench put FOLDER --player HANDLE casebench note CASE "text" [--item ITEM] --player HANDLE casebench note CASE -f note.md [--item ITEM] --player HANDLE- casebench lock CASE [--answer A ...] [--rest 3/60] [--text locked.] --player HANDLE+ casebench lock CASE [--answer A ...] [--any-order] [--rest 3/60] [--text locked.] --player HANDLE casebench unlock CASE --player HANDLE casebench list [--player HANDLE] casebench pull CASE --player HANDLE @@ -17,6 +17,7 @@ every command that writes takes `--player` (more than once is fine) or `--all`, - `put` puts the case on the board, or replaces its title and items if it's there already. notes and a lock stay. it won't put a case over one the runtime put there. - `note` writes under the case's title, or on an item with `--item`. `-f -` reads stdin. - `lock` takes one `--answer` per line of the lock, or asks for them if there's none. `a|b` takes either. lines are checked together, so a wrong try never says which line was right. only salted hashes are stored. +- `--any-order`: the lines open it in any order, for answers that have none of their own, like a day and a time. every order counts toward the 64 combinations. - `--rest 3/60`: after 3 wrong tries in a row the lock takes nothing for 60 minutes. - `pull` only pulls what the bench put there. diff --git a/casebench/cli.py b/casebench/cli.py index 51a2382..ca7fc5a 100644 --- a/casebench/cli.py +++ b/casebench/cli.py @@ -39,6 +39,7 @@ def parser() -> argparse.ArgumentParser: " (default: $CASEBENCH_REST, or 3/60)", ) locker.add_argument("--text", default="locked.", help="what the lock says (default: locked.)") + locker.add_argument("--any-order", action="store_true", help="the lines open it in any order") boards(locker) locker.set_defaults(run=run_lock) @@ -143,7 +144,7 @@ def run_lock(store: Store, args: argparse.Namespace) -> None: players = targets(store, args) for who in players: on_board(store, who, args.case)- new = lock.make(args.answer or ask(), rest, args.text)+ new = lock.make(args.answer or ask(), rest, args.text, args.any_order) for who in players: store.lock(who, args.case, new) print(f"{who.handle}: locked {args.case}") diff --git a/casebench/lock.py b/casebench/lock.py index 5c78e1b..a8ddf06 100644 --- a/casebench/lock.py +++ b/casebench/lock.py @@ -48,16 +48,22 @@ def digest(lines: list[str], salt: bytes) -> str: return hashlib.scrypt(joined.encode(), salt=salt, n=2**14, r=8, p=1, dklen=32).hex()-def make(answers: list[str], rest: Rest, text: str = "locked.") -> Lock:+def make(answers: list[str], rest: Rest, text: str = "locked.", any_order: bool = False) -> Lock: # a line can take more than one answer: "grey|gray". every combination # of the lines is hashed, so the lines are still checked as one. + # any_order: for answers with no order of their own, like a day and a + # time. every order of every combination is hashed too. choices = [[a for a in (normalize(part) for part in line.split("|")) if a] for line in answers] if not choices or not all(choices): raise ValueError("a lock needs an answer on every line")- if math.prod(len(choice) for choice in choices) > MAX_COMBINATIONS:+ orders = math.factorial(len(choices)) if any_order else 1 + if math.prod(len(choice) for choice in choices) * orders > MAX_COMBINATIONS: raise ValueError(f"more than {MAX_COMBINATIONS} combinations of answers") salt = os.urandom(16)- hashes = sorted(digest(list(combination), salt) for combination in itertools.product(*choices))+ combinations = itertools.product(*choices) + if any_order: + combinations = (order for combination in combinations for order in itertools.permutations(combination)) + hashes = sorted({digest(list(combination), salt) for combination in combinations}) return Lock(text, len(answers), salt, tuple(hashes), rest) diff --git a/tests/test_lock.py b/tests/test_lock.py index 6777d45..1a6057b 100644 --- a/tests/test_lock.py +++ b/tests/test_lock.py @@ -69,6 +69,18 @@ class AttemptTest(unittest.TestCase): self.assertEqual(lock.attempt(either, typed, T0)[0], "open") self.assertEqual(lock.attempt(either, ["grey", "hat"], T0)[0], "wrong") + def test_any_order(self): + either = lock.make(["tuesday", "114|one fourteen"], lock.Rest(3, 60), any_order=True) + self.assertEqual(len(either.hashes), 4) + for typed in (["Tuesday", "114"], ["114", "tuesday"], ["one fourteen", "tuesday"]): + self.assertEqual(lock.attempt(either, typed, T0)[0], "open") + self.assertEqual(lock.attempt(either, ["tuesday", "tuesday"], T0)[0], "wrong") + self.assertEqual(lock.attempt(self.lock, ["114", "tuesday"], T0)[0], "wrong") + + def test_any_order_counts_every_order(self): + with self.assertRaisesRegex(ValueError, "combinations"): + lock.make(["a|b", "c|d", "e|f", "g"], lock.Rest(3, 60), any_order=True) + def test_too_many_combinations(self): with self.assertRaisesRegex(ValueError, "combinations"): lock.make(["a|b|c|d"] * 4, lock.Rest(3, 60)) @@ -112,6 +124,12 @@ class LockCommandTest(unittest.TestCase): self.assertEqual(code, 0) self.assertIsNotNone(self.store.get_lock(self.owen, "case-x")) + def test_any_order(self): + args = ("--answer", "tuesday", "--answer", "114", "--any-order", "--player", "owen") + run(self.store, "lock", "test-lost-umbrella", *args) + held = self.store.get_lock(self.owen, "test-lost-umbrella") + self.assertEqual(lock.attempt(held, ["114", "tuesday"], T0)[0], "open") + def test_rest_from_the_environment(self): with mock.patch.dict(os.environ, {"CASEBENCH_REST": "2/15"}): run(self.store, "lock", "test-lost-umbrella", "--answer", "a", "--player", "owen")