"""The phrase: shape, entropy, and the rejection of anything unusable.""" from __future__ import annotations import re import pytest from hhstream_live import phrase SHAPE = re.compile(r"^[a-z]+(-[a-z]+){3}-[a-z0-9]{5}$") def test_generated_phrase_has_the_agreed_shape(): for _ in range(50): generated = phrase.generate() assert SHAPE.match(generated), generated head, _, tail = generated.rpartition("-") assert len(tail) == 5 assert all(word in phrase.words() for word in head.split("-")) def test_entropy_is_what_the_plan_claims(): # Four EFF-large words plus a five-character base-36 tail, ~77.5 bits. assert phrase.entropy_bits() > 75 def test_wordlist_is_the_eff_large_list(): words = phrase.words() assert len(words) == 7776 assert len(set(words)) == 7776 assert "abacus" in words def test_generate_is_not_reproducible_from_a_seed(): # SystemRandom/secrets: seeding the module RNG must not pin the output. import random random.seed(1234) first = phrase.generate() random.seed(1234) second = phrase.generate() assert first != second def test_normalize_lowercases_and_trims(): assert phrase.normalize(" Amber-Otter-7K2 ") == "amber-otter-7k2" def test_validate_accepts_a_good_phrase_and_returns_the_normalized_form(): assert phrase.validate("Amber-Otter-Lantern-7K2QP") == "amber-otter-lantern-7k2qp" @pytest.mark.parametrize("bad", ["", "single", "two-tokens", "has space-phrase-x", "dash--double-phrase", "-leading-hyphen", "trailing-hyphen-", "under_score-in-phrase"]) def test_validate_rejects_a_phrase_that_is_not_the_shape(bad): with pytest.raises(ValueError): phrase.validate(bad) def test_validate_rejects_an_overlong_phrase(): with pytest.raises(ValueError): phrase.validate("-".join(["averyverylongword"] * 8))