from ailed.affect import Affect, AffectConfig, update, decay CFG = AffectConfig() def run(losses, rewards=None, cfg=CFG): s = Affect() rewards = rewards or [0.0] * len(losses) out = [] for l, r in zip(losses, rewards): s = update(s, l, cfg, reward=r) out.append(s) return out def test_first_step_is_not_a_surprise(): s = update(Affect(), 5.0, CFG) assert s.arousal == 0.0 assert s.fast == 5.0 and s.slow == 5.0 def test_constant_loss_settles_at_zero(): s = run([1.0] * 200)[-1] assert abs(s.arousal) < 1e-6 assert abs(s.valence) < 1e-6 def test_spike_raises_arousal_then_decays(): hist = run([1.0] * 100 + [10.0] + [1.0] * 60) assert hist[100].arousal > 0.4 assert hist[-1].arousal < 0.05 def test_decreasing_loss_gives_positive_valence(): losses = [1.0 - 0.004 * i for i in range(200)] assert run(losses)[-1].valence > 0.05 def test_increasing_loss_gives_negative_valence(): losses = [0.2 + 0.004 * i for i in range(200)] assert run(losses)[-1].valence < -0.05 def test_reward_moves_valence_with_flat_loss(): s = run([1.0] * 100, rewards=[0.0] * 50 + [1.0] * 50)[-1] assert s.valence > 0.3 def test_decay_moves_toward_neutral(): s = Affect(valence=0.8, arousal=0.6, fast=1.0, slow=1.0, steps=10) d = decay(s, CFG) assert d.valence == 0.8 * (1 - CFG.decay) assert d.arousal == 0.6 * (1 - CFG.decay) assert d.fast == 1.0 and d.slow == 1.0 def test_update_is_pure(): s = Affect(valence=0.1, arousal=0.2, fast=1.0, slow=1.0, steps=5) update(s, 3.0, CFG) assert s == Affect(valence=0.1, arousal=0.2, fast=1.0, slow=1.0, steps=5) def test_axes_by_name(): s = Affect(valence=0.3, arousal=0.7) assert s.axes == (0.3, 0.7)