File size: 1,959 Bytes
031e289
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""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()