-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_coparenting.py
More file actions
150 lines (132 loc) · 4.97 KB
/
Copy pathtest_coparenting.py
File metadata and controls
150 lines (132 loc) · 4.97 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env python3
"""Tests for cross-tendency co-parenting via sprout_child edge discovery.
Under the post-and-coparent refactor, when a node is sprouted at a
coordinate that falls within the locality bandwidth of another
tendency's anchor, sprout_child appends a parent edge into that
tendency's tree automatically. This is what creates "work items":
emergent multi-parented nodes, no explicit type.
"""
from __future__ import annotations
import sys
from world_model.generalized.tendency import GeneralizedTendency
from world_model.generalized.world import World
from world_model.models.tree import Position
def _make_world_two_tendencies() -> tuple[World, GeneralizedTendency, GeneralizedTendency]:
world = World()
t_a = GeneralizedTendency(
id="T_a",
thesis="Tendency A",
anchor=(1.0, 0.0, 0.0),
polarity_axis=(1.0, 0.0, 0.0),
bandwidth=0.7,
)
t_b = GeneralizedTendency(
id="T_b",
thesis="Tendency B",
anchor=(0.0, 1.0, 0.0),
polarity_axis=(0.0, 1.0, 0.0),
bandwidth=0.7,
)
world.add_tendency(t_a)
world.add_tendency(t_b)
return world, t_a, t_b
def test_sprout_far_from_other_no_coparent() -> None:
"""A node sprouted far from another tendency's anchor stays
single-parented."""
world, t_a, t_b = _make_world_two_tendencies()
# Anchor at (1, 0, 0) -- on top of A, far from B at (0, 1, 0)
# (Euclidean distance sqrt(2) ~ 1.41, while B's bandwidth*1.5 = 1.05).
new_node = t_a.sprout_child(
parent_node_id=t_a.tree.root_node.id,
position=Position.PRO,
anchor=(1.0, 0.0, 0.0),
polarity_axis=(1.0, 0.0, 0.0),
world=world,
)
parent_tendencies = {p.tendency_id for p in new_node.parents}
assert parent_tendencies == {"T_a"}, (
f"expected only T_a parent, got {parent_tendencies}"
)
def test_sprout_near_other_acquires_coparent() -> None:
"""A node sprouted at a coordinate inside another tendency's
locality bandwidth gets a parent edge in that tendency's tree."""
world, t_a, t_b = _make_world_two_tendencies()
# Anchor at (0.6, 0.6, 0) -- close to both anchors. Distance to
# A = sqrt(0.16 + 0.36) ~ 0.72; to B = sqrt(0.36 + 0.16) ~ 0.72.
# Both are within bandwidth*1.5 = 1.05.
new_node = t_a.sprout_child(
parent_node_id=t_a.tree.root_node.id,
position=Position.PRO,
anchor=(0.6, 0.6, 0.0),
polarity_axis=(1.0, 1.0, 0.0),
world=world,
)
parent_tendencies = {p.tendency_id for p in new_node.parents}
assert "T_a" in parent_tendencies and "T_b" in parent_tendencies, (
f"expected co-parent in T_a and T_b, got {parent_tendencies}"
)
def test_coparented_node_appears_in_both_trees() -> None:
"""A co-parented node is reachable from both tendencies' trees
via tree.get_node, not just the tendency that sprouted it."""
world, t_a, t_b = _make_world_two_tendencies()
new_node = t_a.sprout_child(
parent_node_id=t_a.tree.root_node.id,
position=Position.PRO,
anchor=(0.6, 0.6, 0.0),
polarity_axis=(1.0, 1.0, 0.0),
world=world,
)
assert t_a.tree.get_node(new_node.id) is not None, "missing from T_a"
assert t_b.tree.get_node(new_node.id) is not None, "missing from T_b"
def test_coparenting_is_idempotent() -> None:
"""Calling sprout_child twice with the same world doesn't create
duplicate parent links."""
world, t_a, t_b = _make_world_two_tendencies()
new_node = t_a.sprout_child(
parent_node_id=t_a.tree.root_node.id,
position=Position.PRO,
anchor=(0.6, 0.6, 0.0),
polarity_axis=(1.0, 1.0, 0.0),
world=world,
)
parents_first = list(new_node.parents)
# Sprout again -- should hit the existing-node branch, no new
# parent links should be appended.
same = t_a.sprout_child(
parent_node_id=t_a.tree.root_node.id,
position=Position.PRO,
anchor=(0.6, 0.6, 0.0),
polarity_axis=(1.0, 1.0, 0.0),
world=world,
)
assert same.id == new_node.id
assert list(same.parents) == parents_first, (
f"parent set should be stable; got {same.parents} vs {parents_first}"
)
def main() -> int:
tests = [
test_sprout_far_from_other_no_coparent,
test_sprout_near_other_acquires_coparent,
test_coparented_node_appears_in_both_trees,
test_coparenting_is_idempotent,
]
failed = 0
for t in tests:
name = t.__name__
try:
t()
print(f" OK {name}")
except AssertionError as exc:
failed += 1
print(f" FAIL {name}: {exc}")
except Exception as exc: # pragma: no cover
failed += 1
print(f" ERROR {name}: {type(exc).__name__}: {exc}")
print()
if failed:
print(f" {failed}/{len(tests)} test(s) failed")
return 1
print(f" {len(tests)}/{len(tests)} tests passed")
return 0
if __name__ == "__main__":
sys.exit(main())