"""Tests for tools/awc_names.py, built from synthetic AWC stream tables and words (no game data). python -I tests/test_awc_names.py """ import contextlib import csv import importlib.util import io import json import os import struct import sys import tempfile import unittest from unittest import mock ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) def load_module(): path = os.path.join(ROOT, "tools", "awc_names.py") spec = importlib.util.spec_from_file_location("awc_names_under_test", path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module A = load_module() def make_bank(names, flags=0, extra_ids=()): """A minimal AWC: header, the optional tables ``flags`` selects, and a stream table without tags.""" ids = [A.joaat(n) & A.M29 for n in names] + list(extra_ids) n = len(ids) out = bytearray(b"ADAT" + struct.pack(" accepted (horn_loop_02/_03 are not words but extend the family) found = A.attack(d, bases, self.index({"b": ["horn_loop_01", "horn_loop_02", "horn_loop_03"]}), "") self.assertEqual(sorted(v[0] for v in found.values()), ["horn_loop_01", "horn_loop_02", "horn_loop_03"]) # spread over banks -> no family anywhere found = A.attack(d, bases, self.index({"b1": ["horn_loop_01"], "b2": ["horn_loop_02"], "b3": ["horn_loop_03"]}), "") self.assertEqual(found, {}) def test_family_without_verbatim_member_is_rejected(self): d = A.Dictionary(self.common_words("horn", "loop") + ["horn_loop_77"]) found = A.attack(d, d.bases(A.MIN_TOKEN), self.index({"b": ["horn_loop_01", "horn_loop_02", "horn_loop_03"]}), "") self.assertEqual(found, {}) def test_rare_token_base_is_not_wordy(self): d = A.Dictionary(self.common_words("horn") + ["horn_qqq_01"]) self.assertNotIn("horn_qqq", d.bases(A.MIN_TOKEN)) self.assertIn("horn_qqq", d.bases(1)) def test_a_digit_that_ends_the_base_is_not_a_member_number(self): # "2 with adjacent numbers" counts the members' numbered suffixes. The unsuffixed member of a base that ends # in a digit has no suffix number either: amb_1 + amb_1_02 is no family, as horn + horn_02 is none. d = A.Dictionary(["amb_1_02", "horn_02"]) for base, names in (("amb_1", ["amb_1", "amb_1_02"]), ("horn", ["horn", "horn_02"])): with self.subTest(base): self.assertEqual(A.attack(d, [base], self.index({"b": names}), ""), {}) # Two adjacent suffixes, or three members, still make a family on such a base. for names in (["amb_1_01", "amb_1_02"], ["amb_1", "amb_1_02", "amb_1_05"]): with self.subTest(names): found = A.attack(d, ["amb_1"], self.index({"b": names}), "") self.assertEqual(sorted(v[0] for v in found.values()), sorted(names)) def test_family_ok(self): self.assertTrue(A.family_ok(["a_01", "a_02"])) self.assertFalse(A.family_ok(["a_01", "a_03"])) self.assertTrue(A.family_ok(["a_01", "a_05", "a_09"])) self.assertFalse(A.family_ok(["a_01"])) def test_control_salt_changes_every_name(self): d = A.Dictionary(["zz_rootau_abcd_01"]) found = A.attack(d, [], self.index({"b": ["zz_rootau_abcd_01"]}), A.SALT) self.assertEqual(found, {}) def test_name_source_keeps_rules_with_quiet_controls(self): words = self.common_words("horn", "loop") + ["horn_loop_01", "zz_rootau_abcd_01"] d = A.Dictionary(words) index = self.index({"b": ["horn_loop_01", "horn_loop_02", "zz_rootau_abcd_01"]}) found, stats = A.name_source(d, d.bases(A.MIN_TOKEN), index) self.assertEqual(stats["named"], 3) self.assertTrue(stats["dialogue_rule"]) self.assertEqual(stats["family_min_size"], 2) self.assertEqual(stats["control_dialogue"] + stats["control_family"], 0) def test_rejected_dialogue_hit_does_not_shadow_accepted_family_member(self): d = A.Dictionary(["zz_fooau_abcd_01"]) bases = ["zz_fooau_abcd"] # Including SALT + zz_fooau_abcd_01 makes the salted control reject the dialogue rule; # zz_fooau_abcd_01 must still be named via its 3-member family alongside _02 and _03. index = self.index({ "b0": [ "zz_fooau_abcd_01", "zz_fooau_abcd_02", "zz_fooau_abcd_03", A.SALT + "zz_fooau_abcd_01", ] }) real, stats = A.name_source(d, bases, index) self.assertFalse(stats["dialogue_rule"]) self.assertEqual(stats["family_min_size"], 2) self.assertEqual(stats["named"], 3) self.assertEqual( sorted(real.values()), [ ("zz_fooau_abcd_01", "family", 3), ("zz_fooau_abcd_02", "family", 3), ("zz_fooau_abcd_03", "family", 3), ], ) class TestConversations(unittest.TestCase): def test_waves_per_label(self): blob = (b"junk\x00ZZ_ROOTAU_ABCD\x00ZZ_LINE_A\x00ZZ_ROOTAU_ABCD\x00ZZ_LINE_B\x00" b"ZZ_ROOTAU_ABCD\x00ZZ_LINE_A\x00YY_XAU_EFGH\x00YY_ONLY\x00") with tempfile.TemporaryDirectory() as tmp: write(os.path.join(tmp, "c.pso"), blob) waves = sorted(A.conversation_waves([tmp])) # a repeated label is one variation, not two self.assertEqual(waves, ["yy_xau_efgh_01", "zz_rootau_abcd_01", "zz_rootau_abcd_02"]) @staticmethod def pairs(*labels): return [b"ZZ_ROOTAU_ABCD\x00" + label + b"\x00" for label in labels] def carve(self, tmp, streams): """A carve folder: one `.bin` per stream, laid out at 64 KiB steps as rpf9_carve.py names them.""" folder = os.path.join(tmp, "story000") os.makedirs(folder) for k, data in enumerate(streams): write(os.path.join(folder, f"{k * 0x10000:012x}.bin"), data) return folder def full_stream(self, tail, head=b"PSIN" + bytes(12), size=65536): """`size` bytes that start with `head` and end with `tail`.""" return head + b"\x01" * (size - len(head) - len(tail)) + tail def test_pair_across_a_continuation_boundary(self): # A carved stream of a whole number of 64 KiB blocks continues in the next file (docs/page_directory.md): # the third pair is cut after 10 bytes and must still count, so the key has four waves (docs/audio.md). one, two, three, four = self.pairs(b"ZZ_LINE_A", b"ZZ_LINE_B", b"ZZ_LINE_C", b"ZZ_LINE_D") for size in (65536, 2 * 65536): with self.subTest(size=size), tempfile.TemporaryDirectory() as tmp: folder = self.carve(tmp, [self.full_stream(one + two + three[:10], size=size), three[10:] + b"\x02" * 8 + four]) waves = sorted(A.conversation_waves([folder])) self.assertEqual(waves, [f"zz_rootau_abcd_{n:02d}" for n in range(1, 5)]) def test_streams_that_do_not_continue_are_matched_apart(self): # A stream short of a whole block ends its blob, and a new PSIN stream starts one: no pair is made across. one, two = self.pairs(b"ZZ_LINE_A", b"ZZ_LINE_B") for first, second in ((self.full_stream(one + two[:10], size=65535), two[10:]), (self.full_stream(one + two[:10]), b"PSIN" + two[10:])): with self.subTest(first=len(first)), tempfile.TemporaryDirectory() as tmp: folder = self.carve(tmp, [first, second]) self.assertEqual(A.conversation_waves([folder]), ["zz_rootau_abcd_01"]) class TestCli(unittest.TestCase): def test_incomplete_bank_walk_preserves_report(self): with tempfile.TemporaryDirectory() as tmp: banks = os.path.join(tmp, "banks") os.mkdir(banks) write(os.path.join(banks, "ok.awc"), make_bank(["sample_name"])) words = write(os.path.join(tmp, "words.txt"), "sample_name\n") out = write(os.path.join(tmp, "report.json"), "previous report") def partial_walk(path, *, onerror=None): yield banks, [], ["ok.awc"] if onerror: onerror(PermissionError("blocked subtree")) err = io.StringIO() with mock.patch.object(A.os, "walk", partial_walk), contextlib.redirect_stdout(io.StringIO()), \ contextlib.redirect_stderr(err): rc = A.main([banks, "--words", words, "--out", out]) self.assertEqual(rc, 2) self.assertIn("blocked subtree", err.getvalue()) with open(out, encoding="utf-8") as fh: self.assertEqual(fh.read(), "previous report") def test_sources_with_same_basename_keep_separate_summaries_and_streams(self): with tempfile.TemporaryDirectory() as tmp: sources = [] for parent, count in (("first", 1), ("second", 2)): folder = os.path.join(tmp, parent, "banks") os.makedirs(folder) sources.append(folder) write(os.path.join(folder, "one.awc"), make_bank(["zz_rootau_abcd_01"], extra_ids=range(count - 1))) words = write(os.path.join(tmp, "words.txt"), "zz_rootau_abcd_01\n") out = os.path.join(tmp, "names.json") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(A.main([*sources, "--words", words, "--out", out]), 0) with open(out, encoding="utf-8") as fh: doc = json.load(fh) self.assertEqual({k: v["streams"] for k, v in doc["summary"].items()}, {"first/banks": 1, "second/banks": 2}) self.assertEqual([row["source"] for row in doc["streams"]], ["first/banks", "second/banks"]) def test_end_to_end(self): with tempfile.TemporaryDirectory() as tmp: banks = os.path.join(tmp, "banks") os.mkdir(banks) write(os.path.join(banks, "one.awc"), make_bank(["zz_rootau_abcd_01", "zz_rootau_abcd_02"], extra_ids=[12345, 12345])) conv = write(os.path.join(tmp, "c.pso"), b"ZZ_ROOTAU_ABCD\x00ZZ_A\x00ZZ_ROOTAU_ABCD\x00ZZ_B\x00") words = write(os.path.join(tmp, "words.txt"), "unrelated\nother_word\n") out = os.path.join(tmp, "names.csv") with contextlib.redirect_stdout(io.StringIO()) as log: self.assertEqual(A.main([banks, "--words", words, "--conversations", conv, "--out", out]), 0) self.assertIn("3 distinct bank/ID pairs, 2 named", log.getvalue()) with open(out, encoding="utf-8", newline="") as fh: rows = list(csv.DictReader(fh)) self.assertEqual([(r["bank"], r["name"], r["rule"]) for r in rows], [("one", "zz_rootau_abcd_01", "dialogue"), ("one", "zz_rootau_abcd_02", "dialogue")] if A.joaat("zz_rootau_abcd_01") & A.M29 < A.joaat("zz_rootau_abcd_02") & A.M29 else [("one", "zz_rootau_abcd_02", "dialogue"), ("one", "zz_rootau_abcd_01", "dialogue")]) json_out = os.path.join(tmp, "names.json") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(A.main([banks, "--conversations", conv, "--out", json_out]), 0) with open(json_out, encoding="utf-8") as fh: self.assertEqual(json.load(fh)["summary"]["banks"]["streams"], 3) def test_rejects_output_aliasing_conversation(self): with tempfile.TemporaryDirectory() as tmp: bank = write(os.path.join(tmp, "one.awc"), make_bank(["zz_rootau_abcd_01"])) original = b"ZZ_ROOTAU_ABCD\x00ZZ_A\x00" conv = write(os.path.join(tmp, "c.pso"), original) alias = os.path.join(tmp, ".", "c.pso") err = io.StringIO() with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err): with self.assertRaises(SystemExit) as cm: A.main([bank, "--conversations", conv, "--out", alias]) self.assertEqual(cm.exception.code, 2) self.assertIn("--out must not overwrite an input", err.getvalue()) with open(conv, "rb") as f: self.assertEqual(f.read(), original) def test_rejects_output_aliasing_recursive_inputs(self): for source in ("bank", "conversation"): for alias in ("path", "parent", "hardlink"): with self.subTest(source=source, alias=alias), tempfile.TemporaryDirectory() as tmp: banks = os.path.join(tmp, "banks") conversations = os.path.join(tmp, "conversations") os.makedirs(os.path.join(banks, "nested")) os.makedirs(os.path.join(conversations, "nested")) bank_data = make_bank(["zz_rootau_abcd_01"]) conv_data = b"ZZ_ROOTAU_ABCD\x00ZZ_A\x00" bank = write(os.path.join(banks, "nested", "one.AWC"), bank_data) conv = write(os.path.join(conversations, "nested", "labels.bin"), conv_data) target = bank if source == "bank" else conv if alias == "parent": out = os.path.join(os.path.dirname(target), "..", "nested", os.path.basename(target)) elif alias == "hardlink": out = os.path.join(tmp, "names.csv") try: os.link(target, out) except (OSError, NotImplementedError) as e: self.skipTest(f"hard links unavailable: {e}") else: out = target err = io.StringIO() with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err): with self.assertRaises(SystemExit) as cm: A.main([banks, "--conversations", conversations, "--out", out]) self.assertEqual(cm.exception.code, 2) self.assertIn("--out must not overwrite an input", err.getvalue()) for path, original in ((bank, bank_data), (conv, conv_data)): with open(path, "rb") as fh: self.assertEqual(fh.read(), original) def test_allows_report_inside_bank_directory(self): for existing in (False, True): with self.subTest(existing=existing), tempfile.TemporaryDirectory() as tmp: banks = os.path.join(tmp, "banks") os.makedirs(os.path.join(banks, "nested")) bank_data = make_bank(["zz_rootau_abcd_01"]) bank = write(os.path.join(banks, "nested", "one.awc"), bank_data) conv = write(os.path.join(tmp, "labels.bin"), b"ZZ_ROOTAU_ABCD\x00ZZ_A\x00") out = os.path.join(banks, "nested", "names.csv") if existing: write(out, "old report\n") with contextlib.redirect_stdout(io.StringIO()): self.assertEqual(A.main([banks, "--conversations", conv, "--out", out]), 0) with open(out, encoding="utf-8", newline="") as fh: self.assertEqual(len(list(csv.DictReader(fh))), 1) with open(bank, "rb") as fh: self.assertEqual(fh.read(), bank_data) def test_hashdict_json_words(self): with tempfile.TemporaryDirectory() as tmp: path = write(os.path.join(tmp, "hashes.json"), json.dumps({"0000abcd": "Some_Word"})) self.assertEqual(A.read_words(path), ["Some_Word"]) def test_needs_a_dictionary(self): with contextlib.redirect_stderr(io.StringIO()), self.assertRaises(SystemExit): A.main([ROOT]) def test_malformed_banks_json(self): with tempfile.TemporaryDirectory() as tmp: words = write(os.path.join(tmp, "words.json"), json.dumps(["x"])) for doc in ([1], [["Path"]]): banks = write(os.path.join(tmp, "banks.json"), json.dumps(doc)) err = io.StringIO() with contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err): self.assertEqual(A.main(["--words", words, banks]), 2) self.assertEqual(err.getvalue().count("\n"), 1, err.getvalue()) self.assertIn("awc_names: error:", err.getvalue()) def test_missing_input(self): with contextlib.redirect_stderr(io.StringIO()): self.assertEqual(A.main(["/nonexistent", "--words", "/nonexistent.txt"]), 2) def test_min_token_below_one_is_rejected(self): # --min-token counts the dictionary words a token must appear in. 0 or a negative used to pass # type=int and silently disable the common-token filter (every token passes tokens[t] >= min_token). with tempfile.TemporaryDirectory() as tmp: bank = write(os.path.join(tmp, "b.awc"), make_bank(["abc_01", "abc_02", "abc_03"])) words = write(os.path.join(tmp, "w.txt"), "abc_01\nabc_02\nabc_03\n") for bad in ("0", "-1", "-5"): with self.subTest(min_token=bad): err = io.StringIO() with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()): with self.assertRaises(SystemExit) as cm: A.main([bank, "--words", words, "--min-token", bad]) self.assertEqual(cm.exception.code, 2) self.assertIn("at least 1", err.getvalue()) self.assertNotIn("Traceback", err.getvalue()) err = io.StringIO() with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()): with self.assertRaises(SystemExit) as cm: A.main([bank, "--words", words, "--min-token", "x"]) self.assertEqual(cm.exception.code, 2) self.assertNotIn("Traceback", err.getvalue()) # 1 is the smallest meaningful value and still runs. with contextlib.redirect_stderr(io.StringIO()), contextlib.redirect_stdout(io.StringIO()): self.assertEqual(A.main([bank, "--words", words, "--min-token", "1"]), 0) @unittest.skipUnless(sys.platform == "win32", "the exclusive-lock repro needs Windows") def test_locked_words_input_reports_rc_2_without_traceback(self): # An exclusively locked (share-none) existing file passes the os.path.exists check, so the # read_words() open must turn PermissionError into the one-line rc-2 error, not a traceback. import ctypes with tempfile.TemporaryDirectory() as tmp: blob = os.path.join(tmp, "locked.awc") with open(blob, "wb") as fh: fh.write(b"\x00" * 16) k32 = ctypes.windll.kernel32 handle = k32.CreateFileW(blob, 0x80000000, 0, None, 3, 0x80, None) self.assertNotIn(handle, (0, -1), "could not lock the probe file") try: err = io.StringIO() with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()): rc = A.main([blob, "--words", blob]) finally: k32.CloseHandle(handle) self.assertEqual(rc, 2) self.assertIn("cannot read", err.getvalue()) self.assertIn("Permission denied", err.getvalue()) self.assertNotIn("Traceback", err.getvalue()) def test_unreadable_json_words_report_rc_2_without_traceback(self): with tempfile.TemporaryDirectory() as tmp: words = write(os.path.join(tmp, "words.json"), "{not json") err = io.StringIO() with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()): rc = A.main([tmp, "--words", words]) self.assertEqual(rc, 2) self.assertIn("cannot read", err.getvalue()) self.assertNotIn("Traceback", err.getvalue()) def test_scalar_json_words_report_rc_2_without_traceback(self): # A JSON scalar is neither a hashdict object nor a word list: one error line, exit 2. A string # must not be read as one word per character. with tempfile.TemporaryDirectory() as tmp: out = os.path.join(tmp, "o.csv") for doc in ("42", "null", "true", '"abc"'): with self.subTest(doc=doc): words = write(os.path.join(tmp, "words.json"), doc) err = io.StringIO() with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()): rc = A.main([tmp, "--words", words, "--out", out]) self.assertEqual(rc, 2) self.assertEqual(err.getvalue().count("\n"), 1, err.getvalue()) self.assertIn("awc_names: error: cannot read", err.getvalue()) self.assertFalse(os.path.exists(out)) def test_word_dictionary_preambles(self): with tempfile.TemporaryDirectory() as tmp: cases = [ ("hashdict", "\ufeff" + " " * 8192 + '\n{"123": "voice_a"}', ["voice_a"]), ("words.json", '\ufeff ["voice_b"]', ["voice_b"]), ("words.txt", "\ufeffvoice_c\nvoice_d\n", ["voice_c", "voice_d"]), ] for filename, contents, expected in cases: with self.subTest(filename=filename): path = write(os.path.join(tmp, filename), contents.encode("utf-8")) self.assertEqual(A.read_words(path), expected) def test_json_word_list_still_reads(self): with tempfile.TemporaryDirectory() as tmp: path = write(os.path.join(tmp, "words.json"), json.dumps(["a_word", 2])) self.assertEqual(A.read_words(path), ["a_word", "2"]) def test_json_write_failure_preserves_previous_report(self): with tempfile.TemporaryDirectory() as tmp: previous = '{"previous": true}' path = os.path.join(tmp, "names.json") with open(path, "w", encoding="utf-8", newline="\n") as fh: fh.write(previous) with mock.patch.object(A, "replace_retry", side_effect=OSError(13, "Permission denied")): with self.assertRaises(OSError): A.write_json_report(path, {"summary": {}, "streams": []}) with open(path, encoding="utf-8") as fh: self.assertEqual(fh.read(), previous) def test_csv_write_failure_preserves_previous_report(self): with tempfile.TemporaryDirectory() as tmp: previous = "source,bank,stream_id,name,rule\nkeep,b,1,n,dialogue\n" path = os.path.join(tmp, "names.csv") with open(path, "w", encoding="utf-8", newline="") as fh: fh.write(previous) with mock.patch.object(A, "replace_retry", side_effect=OSError(13, "Permission denied")): with self.assertRaises(OSError): A.write_csv_report(path, []) with open(path, encoding="utf-8") as fh: self.assertEqual(fh.read(), previous) def test_publish_cleans_up_staged_file_when_replace_fails_even_if_readonly(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "names.json") with open(path, "w", encoding="utf-8") as f: f.write('{"initial": 1}\n') os.chmod(path, 0o444) real_unlink = os.unlink def windows_strict_unlink(p): mode = os.stat(p).st_mode if not (mode & 0o200): raise PermissionError(13, f"Access is denied: {p}") return real_unlink(p) with mock.patch.object(A, "replace_retry", side_effect=OSError(13, "Permission denied")), \ mock.patch("os.unlink", side_effect=windows_strict_unlink): with self.assertRaises(OSError): A.write_json_report(path, {"summary": {}, "streams": []}) leftover = [f for f in os.listdir(tmp) if f.startswith(".awc_names-")] self.assertEqual(leftover, []) def test_publish_applies_published_mode_on_windows(self): with tempfile.TemporaryDirectory() as tmp: path = os.path.join(tmp, "names_win.json") chmod_calls = [] real_chmod = os.chmod def track_chmod(p, mode): chmod_calls.append((p, mode)) real_chmod(p, mode) with mock.patch("os.name", "nt"), mock.patch("os.chmod", side_effect=track_chmod): A.write_json_report(path, {"summary": {}, "streams": []}) self.assertTrue(len(chmod_calls) > 0) if __name__ == "__main__": unittest.main()