# ============================================================================= # test_claim1_needs.py # ----------------------------------------------------------------------------- # Responsible for: Verifying Claim 1 -- the Psychological Need System's # structure (5 categories, 13 sub-dimensions), its grounding in # Maslow/PERMA, the trait mapping, and the gap/SVO math. # Role in project: These tests ARE the Claim 1 evidence. Each one pins a specific # sentence of arXiv:2606.07948 Sec 3.3 / Appendix E.1 / Table 9. # Assumptions: Pure/deterministic -- no LLM, no network. # ============================================================================= """Claim 1 verification tests. Claim 1 (as assigned): "EduMirror is built on the Concordia simulation engine and grounds agents in psychological theories including Maslow's Hierarchy of Needs and the PERMA model, formalized as a Psychological Need System with five categories and 13 sub-dimensions." What these tests can and cannot establish: CAN: that the Need System the paper *describes* is internally consistent, fully specified, and implementable exactly as written -- 5 categories, 13 sub-dimensions, each mapped to the Table 9 trait, with the stated scales, mappings, and gap formula. CANNOT: that the authors' unreleased code is literally built on Concordia. That is an assertion about an artifact we do not have. See test_claim1_concordia_scope below. """ import math import random import pytest from edumirror.needs import ( ALL_NEEDS, CATEGORY_ALIASES, DEGREE_TO_EXPECTED, NEED_TAXONOMY, NEED_TO_CATEGORY, NEED_TO_TRAIT, S_MAX, NeedState, init_need_state, init_need_state_from_profile, qualitative_description, ) from edumirror.svo import ( SVO_PROFILES, SocialValueSystem, bounded_sum, orientation_angle, theta_to_degrees, ) # ----------------------------------------------------------------------------- # The structural core of Claim 1. # ----------------------------------------------------------------------------- def test_five_major_categories(): """ Scenario: The paper names exactly five major need categories. Why it matters: This is half of the "five categories and 13 sub-dimensions" claim. It pins the count AND the names against Sec 3.3's sentence, so a later refactor that silently adds or renames a category fails loudly. """ assert len(NEED_TAXONOMY) == 5 assert set(NEED_TAXONOMY) == { "Safety", "Mental Health", "Self-Esteem", "Social Belonging", "Meaning and Growth", } def test_thirteen_subdimensions_total(): """ Scenario: The five categories decompose into exactly 13 sub-dimensions. Why it matters: This is the other half of Claim 1, and it is the number most likely to be wrong in a reconstruction -- Appendix E.1's prose lists "sense of meaning, control, passion, and motivation" (reading as four) while Table 9 lists three rows for that category ("passion and motivation" is one dimension). We follow Table 9, which is the only place that yields 13. """ assert len(ALL_NEEDS) == 13 assert len(set(ALL_NEEDS)) == 13, "sub-dimension names must be unique" def test_subdimension_counts_per_category(): """ Scenario: The 13 dimensions distribute across categories as Table 9 lists. Why it matters: 13 could be reached by many groupings; this pins the actual partition (2+3+2+3+3) so the category-level plots (Figures 5, 6) aggregate the same way the paper's do. """ assert {c: len(d) for c, d in NEED_TAXONOMY.items()} == { "Safety": 2, "Social Belonging": 3, "Self-Esteem": 2, "Meaning and Growth": 3, "Mental Health": 3, } assert sum(len(d) for d in NEED_TAXONOMY.values()) == 13 def test_every_subdimension_has_a_table9_trait(): """ Scenario: Table 9 maps each psychological need to an associated trait. Why it matters: The trait mapping is what makes expected values (v*) personality-relative rather than uniform. A dimension without a trait would have no principled v*, breaking the initialization the paper describes. """ assert set(NEED_TO_TRAIT) == set(ALL_NEEDS) assert len(set(NEED_TO_TRAIT.values())) == 13, "each need maps to a distinct trait" def test_category_index_is_consistent(): """ Scenario: The reverse index (dimension -> category) covers every dimension. Why it matters: Guards the aggregation used by category_means(); a missing entry would silently drop a dimension from the plotted averages. """ assert set(NEED_TO_CATEGORY) == set(ALL_NEEDS) for cat, dims in NEED_TAXONOMY.items(): for d in dims: assert NEED_TO_CATEGORY[d] == cat def test_appendix_category_aliases_recorded(): """ Scenario: Main text and appendix use different labels for two categories. Why it matters: Sec 3.3 says "Mental Health"/"Self-Esteem"; Appendix E.1 says "Psychological Health Needs"/"Esteem". Recording the aliases documents that we noticed the discrepancy and treated them as the same construct rather than as extra categories (which would have given 7, not 5). """ assert CATEGORY_ALIASES["Mental Health"] == "Psychological Health Needs" assert CATEGORY_ALIASES["Self-Esteem"] == "Esteem" for canonical in CATEGORY_ALIASES: assert canonical in NEED_TAXONOMY def test_maslow_and_perma_constructs_present(): """ Scenario: The taxonomy should reflect BOTH cited theories, not just one. Why it matters: Claim 1 says the system draws on Maslow AND PERMA. Maslow contributes the safety/belonging/esteem/self-actualization ladder; PERMA contributes positive-psychology constructs (positive emotion, engagement, meaning). If the taxonomy were pure Maslow, the PERMA half of the claim would be decorative. Here: 'meaning'/'passion and motivation'/'emotional wellbeing' are the PERMA-side constructs, and they exist. """ # Maslow-side ladder rungs. assert "psychological safety" in ALL_NEEDS # Safety assert "group acceptance" in ALL_NEEDS # Belonging assert "self worth" in ALL_NEEDS # Esteem # PERMA-side constructs (P=positive emotion, E=engagement, M=meaning). assert "emotional wellbeing" in ALL_NEEDS assert "passion and motivation" in ALL_NEEDS assert "sense of meaning" in ALL_NEEDS # ----------------------------------------------------------------------------- # Scales, mappings, and initialization. # ----------------------------------------------------------------------------- def test_degree_adverb_mapping_matches_appendix_e1(): """ Scenario: Appendix E.1 gives an exact adverb -> expected-value mapping. Why it matters: These four constants (7.5/8/8.5/9) determine every agent's sensitivity to deficit. They are stated numerically in the paper, so they are directly checkable -- one of the few places we can verify an exact value. """ assert DEGREE_TO_EXPECTED == { "slightly": 7.5, "moderately": 8.0, "quite": 8.5, "extremely": 9.0, } def test_expected_values_initialize_in_paper_range(): """ Scenario: Sampled agents' expected values land in the paper's [7.5, 9.0]. Why it matters: The paper says v* is "intentionally initialized in a high range ([7.5, 9.0]) to represent the agent's desired state of well-being". If v* could land low, agents would start satisfied and produce no dynamics. """ rng = random.Random(0) for _ in range(50): state = init_need_state(rng) assert len(state.expected) == 13 for dim, v in state.expected.items(): assert 7.5 <= v <= 9.0, f"{dim} expected value {v} outside [7.5, 9.0]" def test_current_values_initialize_across_full_likert_range(): """ Scenario: v_0 is "randomly sampled within [0, 10]" (Appendix E.1). Why it matters: The wide v_0 range is what produces Figure 5's contrast between resilient (high-start) and volatile (low-start) victims. A narrow initialization would flatten that finding. We check both bounds and actual spread, since a buggy sampler could return a constant and still be in range. """ rng = random.Random(1) seen = [] for _ in range(50): state = init_need_state(rng) for v in state.current.values(): assert 0.0 <= v <= 10.0 seen.append(v) assert min(seen) < 2.0 and max(seen) > 8.0, "v_0 should span the Likert range" def test_profile_init_pins_initial_conditions(): """ Scenario: Intervention experiments need "identical initial settings" (Sec 4.3). Why it matters: Figure 6 compares four intervention arms across 20 scenarios with matched initial conditions. Without pinnable v_0 and v*, arm differences would be confounded by initialization noise and the comparison would be void. """ a = init_need_state_from_profile( random.Random(7), degree_by_need={"psychological safety": "extremely"}, baseline_current=5.0, ) b = init_need_state_from_profile( random.Random(99), degree_by_need={"psychological safety": "extremely"}, baseline_current=5.0, ) assert a.expected["psychological safety"] == 9.0 assert b.expected["psychological safety"] == 9.0 # Different seeds, but the pinned parts must match exactly. assert a.current == b.current assert all(v == 5.0 for v in a.current.values()) # ----------------------------------------------------------------------------- # The unmet-need gap: Delta_t(d) = clip(v*(d) - v_t(d), 0, S_max). # ----------------------------------------------------------------------------- def test_gap_formula_basic(): """ Scenario: A need below its expectation yields a positive gap of exactly the difference. Why it matters: This is Sec 3.3's formula verbatim and the planner's whole objective. An off-by-sign here would make agents seek dissatisfaction. """ state = NeedState( current={d: 3.0 for d in ALL_NEEDS}, expected={d: 8.0 for d in ALL_NEEDS}, ) assert state.gap("psychological safety") == pytest.approx(5.0) def test_gap_clips_at_zero_when_oversatisfied(): """ Scenario: A need ABOVE its expectation must produce a gap of 0, not a negative. Why it matters: The clip at 0 encodes "over-satisfaction creates no drive". Without it, a satisfied dimension would contribute negative gap and could cancel out a genuine deficit elsewhere in the S_self sum, making a distressed agent look content. """ state = NeedState( current={d: 10.0 for d in ALL_NEEDS}, expected={d: 8.0 for d in ALL_NEEDS}, ) assert state.gap("self worth") == 0.0 assert all(g == 0.0 for g in state.gaps().values()) def test_gap_never_exceeds_s_max(): """ Scenario: The maximum possible gap is bounded by S_max. Why it matters: The paper's clip has an upper bound too. With v* <= 9 and v >= 0 the natural max is 9, but the bound must hold for any construction. """ state = NeedState( current={d: 0.0 for d in ALL_NEEDS}, expected={d: 100.0 for d in ALL_NEEDS}, # deliberately out of range ) assert all(g <= S_MAX for g in state.gaps().values()) def test_gaps_cover_all_thirteen_dimensions(): """ Scenario: The gap vector Delta_t spans every dimension. Why it matters: The planner and SVO both consume Delta_t; a short vector would silently exclude dimensions from the agent's motivation. """ state = init_need_state(random.Random(3)) assert len(state.gaps()) == 13 def test_apply_deltas_clamps_and_ignores_unknown_dimensions(): """ Scenario: The LLM update proposes a huge delta and a hallucinated dimension. Why it matters: Real open models do both. Values must stay on the 0-10 Likert scale (an out-of-range value would corrupt every downstream gap and plot), and an invented dimension must not enter the registry -- which would break the "13 sub-dimensions" invariant mid-run. """ state = NeedState( current={d: 5.0 for d in ALL_NEEDS}, expected={d: 8.0 for d in ALL_NEEDS}, ) state.apply_deltas({"self worth": 99.0, "made up dimension": 5.0, "sense of respect": -99.0}) assert state.current["self worth"] == 10.0 # clamped to VALUE_MAX assert state.current["sense of respect"] == 0.0 # clamped to VALUE_MIN assert "made up dimension" not in state.current assert len(state.current) == 13 def test_category_means_average_over_subdimensions(): """ Scenario: Category-level aggregation for the paper's 5-dimension plots. Why it matters: Figures 5/6 plot 5 categories, not 13 dimensions. Safety has 2 sub-dimensions, so a mean must weight them equally. """ state = NeedState( current={d: 0.0 for d in ALL_NEEDS}, expected={d: 8.0 for d in ALL_NEEDS}, ) state.current["psychological safety"] = 4.0 state.current["emotional safety"] = 6.0 means = state.category_means() assert set(means) == set(NEED_TAXONOMY) assert means["Safety"] == pytest.approx(5.0) def test_qualitative_description_is_monotone(): """ Scenario: Textualizing v_t must preserve order (higher value -> better band). Why it matters: Appendix E.1's qualitative-description step exists because LLMs read text better than numbers. If the mapping were not monotone, the planner would receive an actively misleading picture of the agent's state. """ bands = [qualitative_description(v) for v in (0.0, 3.0, 5.0, 7.0, 9.0)] assert bands == [ "severely unmet", "largely unmet", "partially met", "mostly met", "fully satisfied", ] assert len(set(bands)) == 5 def test_describe_mentions_every_dimension_and_category(): """ Scenario: The planner prompt block must expose the whole need state. Why it matters: Any dimension missing from the prompt is invisible to the planner and therefore cannot influence behaviour -- it would exist in the bookkeeping but not in the architecture. """ state = init_need_state(random.Random(5)) text = state.describe() for cat in NEED_TAXONOMY: assert cat in text for dim in ALL_NEEDS: assert dim in text # ----------------------------------------------------------------------------- # Social Value System (Eq. 2). # ----------------------------------------------------------------------------- def test_four_svo_profiles(): """ Scenario: SVO has exactly the paper's four stable orientations. Why it matters: Sec 3.3 names Altruistic, Prosocial, Individualistic, Competitive. Case Study 2 assigns agents these profiles. """ assert set(SVO_PROFILES) == {"Altruistic", "Prosocial", "Individualistic", "Competitive"} def test_theta_bounded_to_zero_pi_over_two(): """ Scenario: theta_t must stay in [0, pi/2] for any non-negative signals. Why it matters: Eq. (2) asserts this bound, and describe_orientation's bands assume it. A theta outside the range would map to a nonsensical stance. """ for s_self in (0.0, 0.5, 3.0, 10.0): for s_other in (0.0, 0.5, 3.0, 10.0): theta = orientation_angle(s_self, s_other) assert 0.0 <= theta <= math.pi / 2 + 1e-9 def test_theta_direction_self_vs_other(): """ Scenario: High own-need + low other-effect => self-dominant (small theta); the reverse => other-regarding (large theta). Why it matters: This is the semantic content of Eq. (2) -- "smaller values indicate self-dominant preferences and larger values indicate other-regarding preferences". A sign flip here would invert every SVO conclusion in Claim 5. """ self_dominant = orientation_angle(s_self=10.0, s_other=0.0) other_regarding = orientation_angle(s_self=0.0, s_other=10.0) assert theta_to_degrees(self_dominant) < 5.0 assert theta_to_degrees(other_regarding) > 85.0 assert self_dominant < other_regarding def test_theta_balanced_when_both_signals_vanish(): """ Scenario: A fully satisfied agent with no effect on others (0/0 case). Why it matters: eps exists precisely to keep this defined. arctan(eps/eps) = arctan(1) = 45 degrees, i.e. perfectly balanced -- the right default for an agent with nothing at stake. Without eps this would be a ZeroDivisionError mid-simulation. """ theta = orientation_angle(0.0, 0.0) assert theta_to_degrees(theta) == pytest.approx(45.0, abs=1e-3) def test_bounded_sum_is_nonnegative_and_clipped(): """ Scenario: S(t) = clip(sum_d Delta_t(d)) over all 13 dimensions. Why it matters: Non-negativity is what constrains theta to [0, pi/2]; the clip is what stops a many-unmet-needs agent from saturating the ratio. """ assert bounded_sum({d: 0.0 for d in ALL_NEEDS}) == 0.0 assert bounded_sum({d: 9.0 for d in ALL_NEEDS}) == 10.0 # 117 raw -> clipped assert bounded_sum({d: -5.0 for d in ALL_NEEDS}) == 0.0 # negatives floored def test_svo_rejects_unknown_orientation(): """ Scenario: A typo'd orientation name must fail fast. Why it matters: Silently accepting "prosocial" (lowercase) would give an agent no SVO guidance while still appearing configured -- a silent experimental error in exactly the arm Claim 5 depends on. """ with pytest.raises(ValueError, match="Unknown SVO target"): SocialValueSystem("prosocial") def test_svo_target_is_stable_while_effective_orientation_moves(): """ Scenario: Across steps with varying need states, the stable target must not drift even though the effective theta does. Why it matters: This is Case Study 2's central premise -- "stable internal personalities that remain consistent across external environments". The architecture must separate the fixed trait from the dynamic state, or the "stable traits" claim is untestable by construction. """ svo = SocialValueSystem("Competitive") gaps_hi = {d: 9.0 for d in ALL_NEEDS} gaps_lo = {d: 0.0 for d in ALL_NEEDS} t1 = svo.step(gaps_hi, {d: 0.0 for d in ALL_NEEDS}) t2 = svo.step(gaps_lo, {d: 9.0 for d in ALL_NEEDS}) assert svo.target == "Competitive" # unchanged assert t1 != t2 # effective orientation did move assert len(svo.history) == 2 def test_svo_prompt_condition_carries_both_halves(): """ Scenario: The planner's SVO block must state the stable profile AND the dynamic stance. Why it matters: Sec 3.3 says candidate actions are evaluated for "consistency with the agent's SVO profile" using theta as a prompt-level condition. Both pieces must reach the prompt or the mechanism is only half-implemented. """ svo = SocialValueSystem("Altruistic") text = svo.prompt_condition(orientation_angle(1.0, 9.0)) assert "Altruistic" in text assert "theta" in text def test_claim1_concordia_scope(): """ Scenario: Documents the limit of what this test file can prove about Claim 1. Why it matters: Claim 1 has two halves. The Need System half is fully verifiable from the paper's text and is verified above. The "built on the Concordia simulation engine" half is an assertion about the authors' unreleased implementation -- no released artifact exists to inspect, so no test can confirm it. We reimplement the GM's described contract instead. Encoding this as an explicit test keeps the limitation visible in the test report rather than buried in prose, so nobody reads a green suite as confirming the Concordia half. """ from edumirror import gm assert "Concordia" in (gm.__doc__ or ""), "GM must document the Concordia scope limit" # Assert the honest negative: we do not depend on Concordia, and do not claim to. with pytest.raises(ImportError): __import__("concordia")