#!/usr/bin/env python3 """Tests for pisg-autoalias.py: python3 scripts/pisg-autoalias-test.py Builds small logs in a temporary folder and checks each rule; touches nothing else.""" import os import subprocess import sys import tempfile HERE = os.path.dirname(os.path.abspath(__file__)) SCRIPT = os.path.join(HERE, "pisg-autoalias.py") passed = failed = 0 def ok(cond, msg): global passed, failed if cond: passed += 1 else: failed += 1 print(" FAIL:", msg) with tempfile.TemporaryDirectory() as tmp: os.mkdir(os.path.join(tmp, "logs")) L = [] join = lambda n, h: L.append(f"[10:00:00] {n} (~u@{h}) joined #Test.") part = lambda n, h: L.append(f"[11:00:00] {n} (~u@{h}) left #Test.") say = lambda n: L.append(f"[10:30:00] <{n}> hello there") join("Alice", "alice.users.undernet.org"); join("Alice_", "alice.users.undernet.org"); part("+Al", "alice.users.undernet.org") for _ in range(5): say("Alice") say("Alice_") join("Gina", "irccloud.example.net"); join("Hank", "irccloud.example.net") join("Bob", "bob.users.undernet.org"); join("Bobby", "bob.users.undernet.org") join("Carl", "shared.users.undernet.org"); join("Carla", "shared.users.undernet.org") for i in range(31): join(f"Crowd{i}", "crowd.users.undernet.org") join("Solo", "solo.users.undernet.org") join("%Dan", "DAN.users.undernet.org"); join("Dan2", "dan.users.undernet.org"); say("Dan2"); say("Dan2") open(os.path.join(tmp, "logs", "t.log.20260101"), "w").write("\n".join(L) + "\n") open(os.path.join(tmp, "logs", ".hidden.tmp"), "w").write( "[10:00:00] Zed (~u@zed.users.undernet.org) joined #Test.\n[10:00:00] Zed2 (~u@zed.users.undernet.org) joined #Test.\n") open(os.path.join(tmp, "manual.cfg"), "w").write('# mine\n\n\n\n') out = os.path.join(tmp, "out.cfg") run = lambda: subprocess.run([sys.executable, SCRIPT, "--logdir", os.path.join(tmp, "logs"), "--prefix", "t.log.", "--manual", os.path.join(tmp, "manual.cfg"), "--out", out], capture_output=True, text=True) r = run() ok(r.returncode == 0, "runs: " + r.stderr[-200:]) t = open(out).read() ok('nick="Alice" alias="Al Alice_"' in t, "busiest nick names the group; the +Al prefix is stripped and merged") ok("Gina" not in t and "Hank" not in t, "a shared non-X host is never merged") ok('nick="Bob" alias="Bobby"' in t and "B2" not in t, "a group touching your entry joins YOUR entry") ok("Carl" not in t and "Carla" not in t, "a group touching two of your entries is skipped") ok("Crowd" not in t, "an oversized group is skipped") ok("Solo" not in t, "a single-nick account produces nothing") ok('nick="Dan2" alias="Dan"' in t, "host match ignores case; %-prefix stripped; busiest chatter named") ok("Zed" not in t, "hidden and temporary files in the log folder are ignored") first = [l for l in t.splitlines() if "Generated by" not in l] run() second = [l for l in open(out).read().splitlines() if "Generated by" not in l] ok(first == second, "a second run gives the same result") ok(not [f for f in os.listdir(tmp) if f.startswith(".autoalias")], "no temporary file left behind") print(f"{passed} passed, {failed} failed") sys.exit(1 if failed else 0)