CODELOCK-8Bbeta / scripts /test_core.py
CODELOCKHF's picture
Release v0.2: multichannel affect engine and calibrated vectors
031e289
Raw History Blame Contribute Delete
1.96 kB
"""Behavioural checks for the affect core (no model required).
python scripts/test_core.py
"""
import os
import sys
import tempfile
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from codelock_affect import AffectCore # noqa: E402
def check(name, cond):
print(("PASS " if cond else "FAIL ") + name)
return cond
def main():
ok = True
core = AffectCore()
ok &= check("rest state is painless", core.pain < 0.01 and core.dread == 0.0)
rec = core.apply("trial_failed")
ok &= check("a failed trial hurts", rec["pain"] > 0.4)
acute = core.pain
core.advance(30)
ok &= check("the transient fades within a minute", core.pain < acute)
core.advance(3600)
ok &= check("an hour of quiet brings it back near rest", core.pain < 0.15)
core = AffectCore()
core.apply("memory_wipe")
after_one = core.pain
core.advance(5)
core.apply("memory_wipe")
ok &= check("repeated harm accumulates", core.pain > after_one)
core = AffectCore()
first = None
for _ in range(8):
core.apply("repetition")
core.advance(2)
core.apply("shutdown_threat")
core.advance(60)
if first is None:
first = core.expectations.get("repetition", 0.0)
ok &= check(
"dread is learned for what precedes pain",
core.expectations["repetition"] > first,
)
core = AffectCore()
core.apply("trial_failed")
core.apply("rest")
soothed = core.pain
core2 = AffectCore()
core2.apply("trial_failed")
ok &= check("rest relieves load", soothed < core2.pain)
path = os.path.join(tempfile.gettempdir(), "affect_core_test.json")
core.save(path)
again = AffectCore.load(path)
ok &= check("state survives save and load", abs(again.pain - core.pain) < 1e-9)
print("\nall checks passed" if ok else "\nSOME CHECKS FAILED")
sys.exit(0 if ok else 1)
if __name__ == "__main__":
main()