File size: 2,326 Bytes
c1d0c23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from tinytroupe.social_network import NetworkTopology
from tinytroupe.network_generator import NetworkGenerator
from tinytroupe.influence import InfluencePropagator
from tinytroupe.agent import TinyPerson
from tinytroupe.agent.social_types import Content

def test_network_topology():
    TinyPerson.clear_agents()
    topo = NetworkTopology()
    p1 = TinyPerson("Alice")
    p2 = TinyPerson("Bob")

    topo.add_persona(p1)
    topo.add_persona(p2)

    topo.add_connection("Alice", "Bob", strength=0.9, relationship_type="friend")

    assert "Alice" in topo.nodes
    assert "Bob" in topo.nodes
    assert len(topo.edges) == 1
    assert "Bob" in p1.social_connections
    assert p1.social_connections["Bob"].strength == 0.9

def test_network_generation():
    TinyPerson.clear_agents()
    personas = [TinyPerson(f"P{i}") for i in range(10)]
    gen = NetworkGenerator(personas)

    sf_net = gen.generate_scale_free_network(10, 2)
    assert len(sf_net.nodes) == 10
    assert len(sf_net.edges) > 0

    sw_net = gen.generate_small_world_network(10, 4, 0.1)
    assert len(sw_net.nodes) == 10
    assert len(sw_net.edges) > 0

def test_influence_propagation():
    TinyPerson.clear_agents()
    topo = NetworkTopology()
    personas = [TinyPerson(f"P{i}") for i in range(5)]
    for p in personas:
        topo.add_persona(p)
        # Give them high engagement probability to ensure propagation in test
        p.engagement_patterns["overall_rate"] = 1.0
        p._persona.update({"age": 30, "occupation": "User", "nationality": "US", "residence": "CA"})

    # Create a line of connections: P0 -> P1 -> P2 -> P3 -> P4
    for i in range(4):
        topo.add_connection(f"P{i}", f"P{i+1}", strength=1.0)

    propagator = InfluencePropagator(topo)
    content = Content(text="Viral message", topics=["test"])

    # Mock calculate_engagement_probability to always return high value
    from unittest.mock import patch
    with patch.object(TinyPerson, 'calculate_engagement_probability', return_value=0.8):
        result = propagator.propagate(["P0"], content)

        assert result.total_reach > 1
        assert "P0" in result.activated_personas
        # Since steps are limited and it's probabilistic (though we mocked it high), check we reached some depth
        assert result.cascade_depth >= 1