mirror of
https://github.com/Kpa-clawbot/meshcore-analyzer.git
synced 2026-06-12 16:51:40 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d8c76ca47a |
@@ -1,305 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ─── resolveWithContext unit tests ─────────────────────────────────────────────
|
||||
|
||||
func TestResolveWithContext_UniquePrefix(t *testing.T) {
|
||||
pm := buildPrefixMap([]nodeInfo{
|
||||
{PublicKey: "a1b2c3d4", Name: "Node-A", HasGPS: true, Lat: 1, Lon: 2},
|
||||
})
|
||||
ni, confidence, _ := pm.resolveWithContext("a1b2c3d4", nil, nil)
|
||||
if ni == nil || ni.Name != "Node-A" {
|
||||
t.Fatal("expected Node-A")
|
||||
}
|
||||
if confidence != "unique_prefix" {
|
||||
t.Fatalf("expected unique_prefix, got %s", confidence)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWithContext_NoMatch(t *testing.T) {
|
||||
pm := buildPrefixMap([]nodeInfo{
|
||||
{PublicKey: "a1b2c3d4", Name: "Node-A"},
|
||||
})
|
||||
ni, confidence, _ := pm.resolveWithContext("ff", nil, nil)
|
||||
if ni != nil {
|
||||
t.Fatal("expected nil")
|
||||
}
|
||||
if confidence != "no_match" {
|
||||
t.Fatalf("expected no_match, got %s", confidence)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWithContext_AffinityWins(t *testing.T) {
|
||||
pm := buildPrefixMap([]nodeInfo{
|
||||
{PublicKey: "a1aaaaaa", Name: "Node-A1"},
|
||||
{PublicKey: "a1bbbbbb", Name: "Node-A2"},
|
||||
})
|
||||
|
||||
graph := NewNeighborGraph()
|
||||
for i := 0; i < 100; i++ {
|
||||
graph.upsertEdge("c0c0c0c0", "a1aaaaaa", "a1", "obs1", nil, time.Now())
|
||||
}
|
||||
|
||||
ni, confidence, score := pm.resolveWithContext("a1", []string{"c0c0c0c0"}, graph)
|
||||
if ni == nil || ni.Name != "Node-A1" {
|
||||
t.Fatalf("expected Node-A1, got %v", ni)
|
||||
}
|
||||
if confidence != "neighbor_affinity" {
|
||||
t.Fatalf("expected neighbor_affinity, got %s", confidence)
|
||||
}
|
||||
if score <= 0 {
|
||||
t.Fatalf("expected positive score, got %f", score)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWithContext_AffinityTooClose_FallsToGeo(t *testing.T) {
|
||||
pm := buildPrefixMap([]nodeInfo{
|
||||
{PublicKey: "a1aaaaaa", Name: "Node-A1", HasGPS: true, Lat: 10, Lon: 20},
|
||||
{PublicKey: "a1bbbbbb", Name: "Node-A2", HasGPS: true, Lat: 11, Lon: 21},
|
||||
{PublicKey: "c0c0c0c0", Name: "Ctx", HasGPS: true, Lat: 10.1, Lon: 20.1},
|
||||
})
|
||||
|
||||
graph := NewNeighborGraph()
|
||||
for i := 0; i < 50; i++ {
|
||||
graph.upsertEdge("c0c0c0c0", "a1aaaaaa", "a1", "obs1", nil, time.Now())
|
||||
graph.upsertEdge("c0c0c0c0", "a1bbbbbb", "a1", "obs1", nil, time.Now())
|
||||
}
|
||||
|
||||
ni, confidence, _ := pm.resolveWithContext("a1", []string{"c0c0c0c0"}, graph)
|
||||
if ni == nil {
|
||||
t.Fatal("expected a result")
|
||||
}
|
||||
if confidence != "geo_proximity" {
|
||||
t.Fatalf("expected geo_proximity, got %s", confidence)
|
||||
}
|
||||
if ni.Name != "Node-A1" {
|
||||
t.Fatalf("expected Node-A1 (closer to context), got %s", ni.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWithContext_GPSPreference(t *testing.T) {
|
||||
pm := buildPrefixMap([]nodeInfo{
|
||||
{PublicKey: "a1aaaaaa", Name: "NoGPS"},
|
||||
{PublicKey: "a1bbbbbb", Name: "HasGPS", HasGPS: true, Lat: 1, Lon: 2},
|
||||
})
|
||||
|
||||
ni, confidence, _ := pm.resolveWithContext("a1", nil, nil)
|
||||
if ni == nil || ni.Name != "HasGPS" {
|
||||
t.Fatalf("expected HasGPS, got %v", ni)
|
||||
}
|
||||
if confidence != "gps_preference" {
|
||||
t.Fatalf("expected gps_preference, got %s", confidence)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWithContext_FirstMatchFallback(t *testing.T) {
|
||||
pm := buildPrefixMap([]nodeInfo{
|
||||
{PublicKey: "a1aaaaaa", Name: "First"},
|
||||
{PublicKey: "a1bbbbbb", Name: "Second"},
|
||||
})
|
||||
|
||||
ni, confidence, _ := pm.resolveWithContext("a1", nil, nil)
|
||||
if ni == nil || ni.Name != "First" {
|
||||
t.Fatalf("expected First, got %v", ni)
|
||||
}
|
||||
if confidence != "first_match" {
|
||||
t.Fatalf("expected first_match, got %s", confidence)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWithContext_NilGraphFallsToGPS(t *testing.T) {
|
||||
pm := buildPrefixMap([]nodeInfo{
|
||||
{PublicKey: "a1aaaaaa", Name: "NoGPS"},
|
||||
{PublicKey: "a1bbbbbb", Name: "HasGPS", HasGPS: true, Lat: 1, Lon: 2},
|
||||
})
|
||||
|
||||
ni, confidence, _ := pm.resolveWithContext("a1", []string{"someone"}, nil)
|
||||
if ni == nil || ni.Name != "HasGPS" {
|
||||
t.Fatalf("expected HasGPS, got %v", ni)
|
||||
}
|
||||
if confidence != "gps_preference" {
|
||||
t.Fatalf("expected gps_preference, got %s", confidence)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveWithContext_BackwardCompatResolve(t *testing.T) {
|
||||
// Verify original resolve() still works unchanged
|
||||
pm := buildPrefixMap([]nodeInfo{
|
||||
{PublicKey: "a1aaaaaa", Name: "NoGPS"},
|
||||
{PublicKey: "a1bbbbbb", Name: "HasGPS", HasGPS: true, Lat: 1, Lon: 2},
|
||||
})
|
||||
ni := pm.resolve("a1")
|
||||
if ni == nil || ni.Name != "HasGPS" {
|
||||
t.Fatalf("expected HasGPS from resolve(), got %v", ni)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── geoDistApprox ─────────────────────────────────────────────────────────────
|
||||
|
||||
func TestGeoDistApprox_SamePoint(t *testing.T) {
|
||||
d := geoDistApprox(37.0, -122.0, 37.0, -122.0)
|
||||
if d != 0 {
|
||||
t.Fatalf("expected 0, got %f", d)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGeoDistApprox_Ordering(t *testing.T) {
|
||||
d1 := geoDistApprox(37.0, -122.0, 37.01, -122.01)
|
||||
d2 := geoDistApprox(37.0, -122.0, 38.0, -121.0)
|
||||
if d1 >= d2 {
|
||||
t.Fatal("closer point should have smaller distance")
|
||||
}
|
||||
}
|
||||
|
||||
// ─── handleResolveHops enhanced response (API tests) ───────────────────────────
|
||||
|
||||
func TestResolveHopsAPI_UniquePrefix(t *testing.T) {
|
||||
srv, router := setupTestServer(t)
|
||||
_ = srv
|
||||
|
||||
// Insert a unique node
|
||||
srv.db.conn.Exec("INSERT OR IGNORE INTO nodes (public_key, name, lat, lon) VALUES (?, ?, ?, ?)",
|
||||
"ff11223344", "UniqueNode", 37.0, -122.0)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/resolve-hops?hops=ff11223344", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
var result ResolveHopsResponse
|
||||
if err := json.Unmarshal(rr.Body.Bytes(), &result); err != nil {
|
||||
t.Fatalf("bad JSON: %v", err)
|
||||
}
|
||||
|
||||
hr, ok := result.Resolved["ff11223344"]
|
||||
if !ok {
|
||||
t.Fatal("expected hop in resolved map")
|
||||
}
|
||||
if hr.Confidence != "unique_prefix" {
|
||||
t.Fatalf("expected unique_prefix, got %s", hr.Confidence)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveHopsAPI_AmbiguousNoContext(t *testing.T) {
|
||||
srv, router := setupTestServer(t)
|
||||
|
||||
srv.db.conn.Exec("INSERT OR IGNORE INTO nodes (public_key, name, lat, lon) VALUES (?, ?, ?, ?)",
|
||||
"ee1aaaaaaa", "Node-E1", 37.0, -122.0)
|
||||
srv.db.conn.Exec("INSERT OR IGNORE INTO nodes (public_key, name, lat, lon) VALUES (?, ?, ?, ?)",
|
||||
"ee1bbbbbbb", "Node-E2", 38.0, -121.0)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/resolve-hops?hops=ee1", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
var result ResolveHopsResponse
|
||||
json.Unmarshal(rr.Body.Bytes(), &result)
|
||||
|
||||
hr := result.Resolved["ee1"]
|
||||
if hr == nil {
|
||||
t.Fatal("expected hop in resolved map")
|
||||
}
|
||||
if hr.Confidence != "ambiguous" {
|
||||
t.Fatalf("expected ambiguous, got %s", hr.Confidence)
|
||||
}
|
||||
if len(hr.Candidates) != 2 {
|
||||
t.Fatalf("expected 2 candidates, got %d", len(hr.Candidates))
|
||||
}
|
||||
for _, c := range hr.Candidates {
|
||||
if c.AffinityScore != nil {
|
||||
t.Fatal("expected nil affinity score without context")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveHopsAPI_WithAffinityContext(t *testing.T) {
|
||||
srv, router := setupTestServer(t)
|
||||
|
||||
srv.db.conn.Exec("INSERT OR IGNORE INTO nodes (public_key, name, lat, lon) VALUES (?, ?, ?, ?)",
|
||||
"dd1aaaaaaa", "Node-D1", 37.0, -122.0)
|
||||
srv.db.conn.Exec("INSERT OR IGNORE INTO nodes (public_key, name, lat, lon) VALUES (?, ?, ?, ?)",
|
||||
"dd1bbbbbbb", "Node-D2", 38.0, -121.0)
|
||||
srv.db.conn.Exec("INSERT OR IGNORE INTO nodes (public_key, name, lat, lon) VALUES (?, ?, ?, ?)",
|
||||
"c0c0c0c0c0", "Context", 37.1, -122.1)
|
||||
|
||||
// Invalidate node cache so the PM includes newly inserted nodes.
|
||||
srv.store.cacheMu.Lock()
|
||||
srv.store.nodeCacheTime = time.Time{}
|
||||
srv.store.cacheMu.Unlock()
|
||||
|
||||
// Build graph with strong affinity
|
||||
graph := NewNeighborGraph()
|
||||
for i := 0; i < 100; i++ {
|
||||
graph.upsertEdge("c0c0c0c0c0", "dd1aaaaaaa", "dd1", "obs1", nil, time.Now())
|
||||
}
|
||||
graph.builtAt = time.Now()
|
||||
srv.neighborMu.Lock()
|
||||
srv.neighborGraph = graph
|
||||
srv.neighborMu.Unlock()
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/resolve-hops?hops=dd1&from_node=c0c0c0c0c0", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
var result ResolveHopsResponse
|
||||
json.Unmarshal(rr.Body.Bytes(), &result)
|
||||
|
||||
hr := result.Resolved["dd1"]
|
||||
if hr == nil {
|
||||
t.Fatal("expected hop in resolved map")
|
||||
}
|
||||
if hr.Confidence != "neighbor_affinity" {
|
||||
t.Fatalf("expected neighbor_affinity, got %s", hr.Confidence)
|
||||
}
|
||||
if hr.BestCandidate == nil || *hr.BestCandidate != "dd1aaaaaaa" {
|
||||
t.Fatalf("expected bestCandidate dd1aaaaaaa, got %v", hr.BestCandidate)
|
||||
}
|
||||
|
||||
// Verify affinity scores present
|
||||
hasScore := false
|
||||
for _, c := range hr.Candidates {
|
||||
if c.AffinityScore != nil && *c.AffinityScore > 0 {
|
||||
hasScore = true
|
||||
}
|
||||
}
|
||||
if !hasScore {
|
||||
t.Fatal("expected at least one candidate with affinity score")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveHopsAPI_ResponseShape(t *testing.T) {
|
||||
srv, router := setupTestServer(t)
|
||||
|
||||
srv.db.conn.Exec("INSERT OR IGNORE INTO nodes (public_key, name, lat, lon) VALUES (?, ?, ?, ?)",
|
||||
"bb1aaaaaaa", "Node-B1", 37.0, -122.0)
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/resolve-hops?hops=bb1a", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
router.ServeHTTP(rr, req)
|
||||
|
||||
var raw map[string]json.RawMessage
|
||||
json.Unmarshal(rr.Body.Bytes(), &raw)
|
||||
|
||||
if _, ok := raw["resolved"]; !ok {
|
||||
t.Fatal("missing 'resolved' key")
|
||||
}
|
||||
|
||||
var resolved map[string]map[string]interface{}
|
||||
json.Unmarshal(raw["resolved"], &resolved)
|
||||
|
||||
for _, hr := range resolved {
|
||||
if _, ok := hr["confidence"]; !ok {
|
||||
t.Error("missing 'confidence' field in HopResolution")
|
||||
}
|
||||
if _, ok := hr["candidates"]; !ok {
|
||||
t.Error("missing 'candidates' field")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Helpers used only in this test file ───────────────────────────────────────
|
||||
+9
-88
@@ -293,7 +293,12 @@ func (s *Server) handleConfigTheme(w http.ResponseWriter, r *http.Request) {
|
||||
}, s.cfg.NodeColors, theme.NodeColors)
|
||||
|
||||
themeDark := mergeMap(map[string]interface{}{}, s.cfg.ThemeDark, theme.ThemeDark)
|
||||
typeColors := mergeMap(map[string]interface{}{}, s.cfg.TypeColors, theme.TypeColors)
|
||||
typeColors := mergeMap(map[string]interface{}{
|
||||
"ADVERT": "#22c55e", "GRP_TXT": "#3b82f6", "TXT_MSG": "#f59e0b",
|
||||
"ACK": "#6b7280", "REQUEST": "#a855f7", "RESPONSE": "#06b6d4",
|
||||
"TRACE": "#ec4899", "PATH": "#14b8a6", "ANON_REQ": "#f43f5e",
|
||||
"UNKNOWN": "#6b7280",
|
||||
}, s.cfg.TypeColors, theme.TypeColors)
|
||||
|
||||
var home interface{}
|
||||
if theme.Home != nil {
|
||||
@@ -1309,31 +1314,6 @@ func (s *Server) handleResolveHops(w http.ResponseWriter, r *http.Request) {
|
||||
hops := strings.Split(hopsParam, ",")
|
||||
resolved := map[string]*HopResolution{}
|
||||
|
||||
// Context for affinity-based disambiguation.
|
||||
fromNode := r.URL.Query().Get("from_node")
|
||||
observer := r.URL.Query().Get("observer")
|
||||
var contextPubkeys []string
|
||||
if fromNode != "" {
|
||||
contextPubkeys = append(contextPubkeys, fromNode)
|
||||
}
|
||||
if observer != "" {
|
||||
contextPubkeys = append(contextPubkeys, observer)
|
||||
}
|
||||
|
||||
// Get the neighbor graph for affinity scoring (may be nil).
|
||||
var graph *NeighborGraph
|
||||
if len(contextPubkeys) > 0 {
|
||||
graph = s.getNeighborGraph()
|
||||
}
|
||||
|
||||
// Get the server's prefix map for resolveWithContext.
|
||||
var pm *prefixMap
|
||||
if s.store != nil {
|
||||
s.store.mu.RLock()
|
||||
_, pm = s.store.getCachedNodesAndPM()
|
||||
s.store.mu.RUnlock()
|
||||
}
|
||||
|
||||
for _, hop := range hops {
|
||||
if hop == "" {
|
||||
continue
|
||||
@@ -1341,7 +1321,7 @@ func (s *Server) handleResolveHops(w http.ResponseWriter, r *http.Request) {
|
||||
hopLower := strings.ToLower(hop)
|
||||
rows, err := s.db.conn.Query("SELECT public_key, name, lat, lon FROM nodes WHERE LOWER(public_key) LIKE ?", hopLower+"%")
|
||||
if err != nil {
|
||||
resolved[hop] = &HopResolution{Name: nil, Candidates: []HopCandidate{}, Conflicts: []interface{}{}, Confidence: "ambiguous"}
|
||||
resolved[hop] = &HopResolution{Name: nil, Candidates: []HopCandidate{}, Conflicts: []interface{}{}}
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -1359,77 +1339,18 @@ func (s *Server) handleResolveHops(w http.ResponseWriter, r *http.Request) {
|
||||
rows.Close()
|
||||
|
||||
if len(candidates) == 0 {
|
||||
resolved[hop] = &HopResolution{Name: nil, Candidates: []HopCandidate{}, Conflicts: []interface{}{}, Confidence: "no_match"}
|
||||
resolved[hop] = &HopResolution{Name: nil, Candidates: []HopCandidate{}, Conflicts: []interface{}{}}
|
||||
} else if len(candidates) == 1 {
|
||||
resolved[hop] = &HopResolution{
|
||||
Name: candidates[0].Name, Pubkey: candidates[0].Pubkey,
|
||||
Candidates: candidates, Conflicts: []interface{}{},
|
||||
Confidence: "unique_prefix",
|
||||
}
|
||||
} else {
|
||||
// Compute affinity scores for each candidate if we have context.
|
||||
if graph != nil && len(contextPubkeys) > 0 {
|
||||
now := time.Now()
|
||||
for i := range candidates {
|
||||
candPK := strings.ToLower(candidates[i].Pubkey)
|
||||
bestScore := 0.0
|
||||
for _, ctxPK := range contextPubkeys {
|
||||
edges := graph.Neighbors(strings.ToLower(ctxPK))
|
||||
for _, e := range edges {
|
||||
if e.Ambiguous {
|
||||
continue
|
||||
}
|
||||
otherPK := e.NodeA
|
||||
if strings.EqualFold(otherPK, ctxPK) {
|
||||
otherPK = e.NodeB
|
||||
}
|
||||
if strings.EqualFold(otherPK, candPK) {
|
||||
sc := e.Score(now)
|
||||
if sc > bestScore {
|
||||
bestScore = sc
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if bestScore > 0 {
|
||||
s := bestScore
|
||||
candidates[i].AffinityScore = &s
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Use resolveWithContext for 4-tier disambiguation.
|
||||
var best *nodeInfo
|
||||
var confidence string
|
||||
if pm != nil {
|
||||
best, confidence, _ = pm.resolveWithContext(hopLower, contextPubkeys, graph)
|
||||
}
|
||||
|
||||
ambig := true
|
||||
hr := &HopResolution{
|
||||
resolved[hop] = &HopResolution{
|
||||
Name: candidates[0].Name, Pubkey: candidates[0].Pubkey,
|
||||
Ambiguous: &ambig, Candidates: candidates, Conflicts: hopCandidatesToConflicts(candidates),
|
||||
Confidence: "ambiguous",
|
||||
}
|
||||
|
||||
// Use the resolved node as the default (best-effort pick).
|
||||
if best != nil {
|
||||
hr.Name = best.Name
|
||||
hr.Pubkey = best.PublicKey
|
||||
}
|
||||
|
||||
// Only promote to bestCandidate when affinity is confident.
|
||||
if confidence == "neighbor_affinity" && best != nil {
|
||||
pk := best.PublicKey
|
||||
hr.BestCandidate = &pk
|
||||
hr.Confidence = "neighbor_affinity"
|
||||
} else if (confidence == "geo_proximity" || confidence == "gps_preference") && best != nil {
|
||||
// Propagate lower-priority tiers so the API reflects the actual
|
||||
// resolution strategy used, rather than collapsing everything to "ambiguous".
|
||||
hr.Confidence = confidence
|
||||
}
|
||||
|
||||
resolved[hop] = hr
|
||||
}
|
||||
}
|
||||
writeJSON(w, ResolveHopsResponse{Resolved: resolved})
|
||||
|
||||
@@ -454,6 +454,35 @@ func TestConfigThemeEndpoint(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigThemeTypeColorsDefaults(t *testing.T) {
|
||||
_, router := setupTestServer(t)
|
||||
req := httptest.NewRequest("GET", "/api/config/theme", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != 200 {
|
||||
t.Fatalf("expected 200, got %d", w.Code)
|
||||
}
|
||||
var body map[string]interface{}
|
||||
json.Unmarshal(w.Body.Bytes(), &body)
|
||||
tc, ok := body["typeColors"].(map[string]interface{})
|
||||
if !ok {
|
||||
t.Fatal("expected typeColors object in theme response")
|
||||
}
|
||||
expectedTypes := []string{"ADVERT", "GRP_TXT", "TXT_MSG", "ACK", "REQUEST", "RESPONSE", "TRACE", "PATH", "ANON_REQ", "UNKNOWN"}
|
||||
for _, typ := range expectedTypes {
|
||||
val, exists := tc[typ]
|
||||
if !exists {
|
||||
t.Errorf("typeColors missing default for %s", typ)
|
||||
continue
|
||||
}
|
||||
color, ok := val.(string)
|
||||
if !ok || color == "" || color == "#000000" {
|
||||
t.Errorf("typeColors[%s] should be a non-black color, got %v", typ, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigMapEndpoint(t *testing.T) {
|
||||
_, router := setupTestServer(t)
|
||||
req := httptest.NewRequest("GET", "/api/config/map", nil)
|
||||
|
||||
@@ -3304,144 +3304,6 @@ func (pm *prefixMap) resolve(hop string) *nodeInfo {
|
||||
return &candidates[0]
|
||||
}
|
||||
|
||||
// resolveWithContext resolves a hop prefix using the neighbor affinity graph
|
||||
// for disambiguation when multiple candidates match. It applies a 4-tier
|
||||
// priority: (1) affinity graph score, (2) geographic proximity to context
|
||||
// nodes, (3) GPS preference, (4) first match fallback.
|
||||
//
|
||||
// contextPubkeys are pubkeys of nodes that provide context for disambiguation
|
||||
// (e.g., the originator, observer, or adjacent hops in the path).
|
||||
// graph may be nil, in which case it falls back to the existing resolve().
|
||||
func (pm *prefixMap) resolveWithContext(hop string, contextPubkeys []string, graph *NeighborGraph) (*nodeInfo, string, float64) {
|
||||
h := strings.ToLower(hop)
|
||||
candidates := pm.m[h]
|
||||
if len(candidates) == 0 {
|
||||
return nil, "no_match", 0
|
||||
}
|
||||
if len(candidates) == 1 {
|
||||
return &candidates[0], "unique_prefix", 1.0
|
||||
}
|
||||
|
||||
// Priority 1: Affinity graph score
|
||||
//
|
||||
// NOTE: We use raw Score() (count × time-decay) here rather than Jaccard
|
||||
// similarity. Jaccard is used at the graph builder level (disambiguate() in
|
||||
// neighbor_graph.go) to resolve ambiguous edges by comparing neighbor-set
|
||||
// overlap. Here, edges are already resolved — we just need to pick the
|
||||
// highest-affinity candidate among them. Raw score is appropriate because
|
||||
// it reflects both observation frequency and recency, which are the right
|
||||
// signals for "which candidate is this hop most likely referring to."
|
||||
if graph != nil && len(contextPubkeys) > 0 {
|
||||
type scored struct {
|
||||
idx int
|
||||
score float64
|
||||
count int // observation count of the best-scoring edge
|
||||
}
|
||||
now := time.Now()
|
||||
var scores []scored
|
||||
for i, cand := range candidates {
|
||||
candPK := strings.ToLower(cand.PublicKey)
|
||||
bestScore := 0.0
|
||||
bestCount := 0
|
||||
for _, ctxPK := range contextPubkeys {
|
||||
edges := graph.Neighbors(strings.ToLower(ctxPK))
|
||||
for _, e := range edges {
|
||||
if e.Ambiguous {
|
||||
continue
|
||||
}
|
||||
otherPK := e.NodeA
|
||||
if strings.EqualFold(otherPK, ctxPK) {
|
||||
otherPK = e.NodeB
|
||||
}
|
||||
if strings.EqualFold(otherPK, candPK) {
|
||||
s := e.Score(now)
|
||||
if s > bestScore {
|
||||
bestScore = s
|
||||
bestCount = e.Count
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if bestScore > 0 {
|
||||
scores = append(scores, scored{i, bestScore, bestCount})
|
||||
}
|
||||
}
|
||||
|
||||
if len(scores) >= 1 {
|
||||
// Sort descending
|
||||
for i := 0; i < len(scores)-1; i++ {
|
||||
for j := i + 1; j < len(scores); j++ {
|
||||
if scores[j].score > scores[i].score {
|
||||
scores[i], scores[j] = scores[j], scores[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
best := scores[0]
|
||||
// Require both score ratio ≥ 3× AND minimum observations (mirrors
|
||||
// disambiguate() in neighbor_graph.go which checks affinityMinObservations).
|
||||
if best.count >= affinityMinObservations &&
|
||||
(len(scores) == 1 || best.score >= affinityConfidenceRatio*scores[1].score) {
|
||||
return &candidates[best.idx], "neighbor_affinity", best.score
|
||||
}
|
||||
// Scores too close — fall through to lower-priority strategies
|
||||
}
|
||||
}
|
||||
|
||||
// Priority 2: Geographic proximity (if context pubkeys have GPS and candidates have GPS)
|
||||
if len(contextPubkeys) > 0 {
|
||||
// Find GPS positions of context nodes from the prefix map or candidates
|
||||
// We need nodeInfo for context pubkeys — look them up
|
||||
var contextLat, contextLon float64
|
||||
var contextGPSCount int
|
||||
for _, ctxPK := range contextPubkeys {
|
||||
ctxLower := strings.ToLower(ctxPK)
|
||||
if infos, ok := pm.m[ctxLower]; ok && len(infos) == 1 && infos[0].HasGPS {
|
||||
contextLat += infos[0].Lat
|
||||
contextLon += infos[0].Lon
|
||||
contextGPSCount++
|
||||
}
|
||||
}
|
||||
if contextGPSCount > 0 {
|
||||
contextLat /= float64(contextGPSCount)
|
||||
contextLon /= float64(contextGPSCount)
|
||||
|
||||
bestIdx := -1
|
||||
bestDist := math.MaxFloat64
|
||||
for i, cand := range candidates {
|
||||
if !cand.HasGPS {
|
||||
continue
|
||||
}
|
||||
d := geoDistApprox(contextLat, contextLon, cand.Lat, cand.Lon)
|
||||
if d < bestDist {
|
||||
bestDist = d
|
||||
bestIdx = i
|
||||
}
|
||||
}
|
||||
if bestIdx >= 0 {
|
||||
return &candidates[bestIdx], "geo_proximity", 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Priority 3: GPS preference
|
||||
for i := range candidates {
|
||||
if candidates[i].HasGPS {
|
||||
return &candidates[i], "gps_preference", 0
|
||||
}
|
||||
}
|
||||
|
||||
// Priority 4: First match fallback
|
||||
return &candidates[0], "first_match", 0
|
||||
}
|
||||
|
||||
// geoDistApprox returns an approximate distance between two lat/lon points
|
||||
// (equirectangular approximation, sufficient for relative comparison).
|
||||
func geoDistApprox(lat1, lon1, lat2, lon2 float64) float64 {
|
||||
dLat := (lat2 - lat1) * math.Pi / 180
|
||||
dLon := (lon2 - lon1) * math.Pi / 180 * math.Cos((lat1+lat2)/2*math.Pi/180)
|
||||
return math.Sqrt(dLat*dLat + dLon*dLon)
|
||||
}
|
||||
|
||||
func parsePathJSON(pathJSON string) []string {
|
||||
if pathJSON == "" || pathJSON == "[]" {
|
||||
return nil
|
||||
|
||||
+9
-12
@@ -873,21 +873,18 @@ type TraceResponse struct {
|
||||
// ─── Resolve Hops ──────────────────────────────────────────────────────────────
|
||||
|
||||
type HopCandidate struct {
|
||||
Name interface{} `json:"name"`
|
||||
Pubkey string `json:"pubkey"`
|
||||
Lat interface{} `json:"lat"`
|
||||
Lon interface{} `json:"lon"`
|
||||
AffinityScore *float64 `json:"affinityScore"`
|
||||
Name interface{} `json:"name"`
|
||||
Pubkey string `json:"pubkey"`
|
||||
Lat interface{} `json:"lat"`
|
||||
Lon interface{} `json:"lon"`
|
||||
}
|
||||
|
||||
type HopResolution struct {
|
||||
Name interface{} `json:"name"`
|
||||
Pubkey interface{} `json:"pubkey,omitempty"`
|
||||
Ambiguous *bool `json:"ambiguous,omitempty"`
|
||||
Candidates []HopCandidate `json:"candidates"`
|
||||
Conflicts []interface{} `json:"conflicts"`
|
||||
BestCandidate *string `json:"bestCandidate,omitempty"`
|
||||
Confidence string `json:"confidence,omitempty"`
|
||||
Name interface{} `json:"name"`
|
||||
Pubkey interface{} `json:"pubkey,omitempty"`
|
||||
Ambiguous *bool `json:"ambiguous,omitempty"`
|
||||
Candidates []HopCandidate `json:"candidates"`
|
||||
Conflicts []interface{} `json:"conflicts"`
|
||||
}
|
||||
|
||||
type ResolveHopsResponse struct {
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
# CoreScope v3.3 Release Notes
|
||||
|
||||
## Headline: Neighbor Affinity, Virtual Scroll, and a Performance Overhaul
|
||||
|
||||
v3.3 is the biggest release since launch — 50 PRs merged, touching every layer of the stack. The packets page now handles 30K+ rows without breaking a sweat, nodes show their RF neighbors, and the customizer got a complete rewrite.
|
||||
|
||||
---
|
||||
|
||||
## 🎯 New Features
|
||||
|
||||
- **Neighbor affinity graph** — see which nodes hear each other and how well, rendered as an interactive graph in analytics (#507, #508, #513)
|
||||
- **Neighbors section on node detail page** — every node now shows its direct RF neighbors with signal quality (#510)
|
||||
- **Affinity-aware hop resolution** — hop paths now resolve using real RF neighbor data instead of guessing (#511)
|
||||
- **"Show direct neighbors" map filter** — click a node on the map to highlight only its neighbors (#480)
|
||||
- **Customizer v2** — completely rewritten with event-driven state management, cleaner UX (#503)
|
||||
- **Auto-inject cache busters at server startup** — no more manual `__BUST__` bumps or merge conflicts (#481)
|
||||
- **Git-derived versioning** — version now comes from git tags, not package.json (#486)
|
||||
- **manage.sh supports pinning to release tags** — deploy a specific version instead of always latest (#456)
|
||||
|
||||
## ⚡ Performance
|
||||
|
||||
- **Virtual scroll for packets table** — 30K+ packets render smoothly, no more DOM explosion (#402)
|
||||
- **Debounced WebSocket renders** — coalesced updates prevent render storms on busy meshes (#402)
|
||||
- **Cached JSON.parse results** — packet data parsed once, reused everywhere (#400)
|
||||
- **In-place node upsert on ADVERT** — skip full reload when a node advertises (#461)
|
||||
- **Map lookups replace linear scans** — observers.find() → O(1) Map lookups (#468)
|
||||
- **Bounded memory growth on packets page** — eviction prevents unbounded DOM/data growth (#421)
|
||||
- **Server-side collision analysis** — moved from client to server, fixes UI freezes on large meshes (#415)
|
||||
- **Client-side "My Nodes" filter** — eliminated a server round-trip (#401)
|
||||
- **Targeted analytics cache invalidation** — surgical invalidation instead of blowing the whole cache (#379)
|
||||
- **Skip JSON parse when no pubkey fields present** — fast path for the common case (#499)
|
||||
- **requestAnimationFrame replaces setInterval** — smoother live page animations, capped concurrency (#470)
|
||||
|
||||
## 🐛 Bug Fixes
|
||||
|
||||
- **Region filter was silently ignored on GetNodes** — nodes now actually filter by region (#497)
|
||||
- **Region filtering missing from hash-collisions endpoint** — fixed (#477)
|
||||
- **Haversine replaces Euclidean distance** in analytics hop distances — no more wildly wrong distances (#478)
|
||||
- **Color-coded hex breakdown restored** in packet detail view (#500)
|
||||
- **Channel hash displayed as hex** instead of confusing decimal (#471)
|
||||
- **VCR timeline respects UTC/local timezone setting** (#459)
|
||||
- **Observer last_seen updates on packet ingestion** — observers no longer appear stale (#479)
|
||||
- **Packet timestamps used in bufferPacket** instead of arrival time — fixes time-travel bugs (#491)
|
||||
- **Zero-hop adverts skipped** when checking node hash size (#493)
|
||||
- **Null-guard fixes** — pathHops detail pane crash (#454), animLayer/liveAnimCount after destroy (#462), rAF callbacks in live page (#506)
|
||||
- **Stale parsed cache cleared** on observation packets (#505)
|
||||
- **Score/direction extracted from MQTT** with proper unit stripping and type safety (#371)
|
||||
- **String/uint/uint64 type handling** in toFloat64 (#352)
|
||||
- **Reset restores home steps** after SITE_CONFIG contamination (#460)
|
||||
- **Duplicate return statement removed** in _cumulativeRowOffsets (#476)
|
||||
- **Mutex added to PerfStats** — eliminates data races (#469)
|
||||
- **Graceful container shutdown** for reliable deployments (#453)
|
||||
- **Staging config always refreshed from prod** (#467)
|
||||
|
||||
## 🧪 Testing
|
||||
|
||||
- **100+ new app.js tests** — comprehensive SPA router coverage (#490)
|
||||
- **71 new live.js tests** — live page fully covered (#489)
|
||||
- **64 new packets.js tests** (#488)
|
||||
- **nodes.js P0 coverage** — sort, status, timestamps, sync (#487)
|
||||
- **Ingestor coverage 70% → 84%** (#492)
|
||||
- **Playwright packets test stabilized** with explicit time window (#348)
|
||||
|
||||
## 🔧 Internal
|
||||
|
||||
- **Docker cleanup before CI build** — prevents disk space exhaustion (#473)
|
||||
|
||||
## ⚠️ Known Limitations
|
||||
|
||||
- **Live map** does not yet use affinity-aware hop resolution — animated paths still use naive first-match for ambiguous hops (#528)
|
||||
- **Customizer v2 home section** requires server-side home defaults to be configured — instances without `home` in config.json will show empty customizer fields until #526 merges
|
||||
|
||||
---
|
||||
|
||||
*50 PRs. Zero new dependencies. Still no build step.*
|
||||
+1
-405
@@ -85,7 +85,6 @@
|
||||
<button class="tab-btn" data-tab="subpaths">Route Patterns</button>
|
||||
<button class="tab-btn" data-tab="nodes">Nodes</button>
|
||||
<button class="tab-btn" data-tab="distance">Distance</button>
|
||||
<button class="tab-btn" data-tab="neighbor-graph">Neighbor Graph</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="analyticsContent" class="analytics-content">
|
||||
@@ -172,7 +171,6 @@
|
||||
case 'subpaths': await renderSubpaths(el); break;
|
||||
case 'nodes': await renderNodesTab(el); break;
|
||||
case 'distance': await renderDistanceTab(el); break;
|
||||
case 'neighbor-graph': await renderNeighborGraphTab(el); break;
|
||||
}
|
||||
// Auto-apply column resizing to all analytics tables
|
||||
requestAnimationFrame(() => {
|
||||
@@ -1801,7 +1799,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
function destroy() { _analyticsData = {}; _channelData = null; if (_ngState && _ngState.animId) { cancelAnimationFrame(_ngState.animId); } _ngState = null; }
|
||||
function destroy() { _analyticsData = {}; _channelData = null; }
|
||||
|
||||
// Expose for testing
|
||||
if (typeof window !== 'undefined') {
|
||||
@@ -1812,407 +1810,5 @@ function destroy() { _analyticsData = {}; _channelData = null; if (_ngState && _
|
||||
window._analyticsChannelTheadHtml = channelTheadHtml;
|
||||
}
|
||||
|
||||
// ─── Neighbor Graph Tab ─────────────────────────────────────────────────────
|
||||
|
||||
let _ngState = null; // neighbor graph state
|
||||
|
||||
async function renderNeighborGraphTab(el) {
|
||||
el.innerHTML = `
|
||||
<div class="analytics-card" id="ngCard">
|
||||
<h3>🕸️ Neighbor Graph</h3>
|
||||
<div id="ngFilters" class="ng-filters" style="display:flex;gap:12px;flex-wrap:wrap;align-items:center;margin-bottom:12px">
|
||||
<label style="font-size:13px">Roles:
|
||||
<span id="ngRoleChecks" style="margin-left:4px"></span>
|
||||
</label>
|
||||
<label style="font-size:13px">Min Score: <input type="range" id="ngMinScore" min="0" max="100" value="10" style="width:100px;vertical-align:middle">
|
||||
<span id="ngMinScoreVal">0.10</span>
|
||||
</label>
|
||||
<label style="font-size:13px">Confidence:
|
||||
<select id="ngConfidence" style="font-size:12px;padding:2px 4px">
|
||||
<option value="all">Show All</option>
|
||||
<option value="high">High Only</option>
|
||||
<option value="hide-ambiguous">Hide Ambiguous</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<div id="ngStats" class="stat-row" style="display:flex;gap:16px;flex-wrap:wrap;margin-bottom:12px"></div>
|
||||
<div style="position:relative;border:1px solid var(--border);border-radius:6px;overflow:hidden">
|
||||
<canvas id="ngCanvas" width="900" height="600" style="width:100%;height:600px;cursor:grab" role="img" aria-label="Neighbor affinity graph visualization — interactive force-directed network topology" tabindex="0"></canvas>
|
||||
<div id="ngTooltip" style="position:absolute;display:none;background:var(--bg-secondary);border:1px solid var(--border);border-radius:4px;padding:6px 10px;font-size:12px;pointer-events:none;z-index:10;box-shadow:0 2px 8px rgba(0,0,0,0.2)"></div>
|
||||
</div>
|
||||
</div>`;
|
||||
|
||||
// Role checkboxes
|
||||
const roles = ['repeater','companion','room','sensor'];
|
||||
const rcEl = document.getElementById('ngRoleChecks');
|
||||
roles.forEach(r => {
|
||||
const color = (window.ROLE_COLORS || {})[r] || '#888';
|
||||
rcEl.innerHTML += `<label style="font-size:12px;margin-right:8px"><input type="checkbox" data-role="${r}" checked> <span style="color:${esc(color)}">${esc(r)}</span></label>`;
|
||||
});
|
||||
|
||||
// Load data
|
||||
const rqs = RegionFilter.regionQueryString();
|
||||
const sep = rqs ? '?' + rqs.slice(1) : '';
|
||||
let graphData;
|
||||
try {
|
||||
graphData = await api('/analytics/neighbor-graph' + sep + (sep ? '&' : '?') + 'min_count=1&min_score=0', { ttl: CLIENT_TTL.analyticsRF });
|
||||
} catch (e) {
|
||||
el.innerHTML = `<div class="analytics-card"><p class="text-muted">Failed to load neighbor graph: ${esc(e.message)}</p></div>`;
|
||||
return;
|
||||
}
|
||||
|
||||
_ngState = createGraphState(graphData);
|
||||
renderNGStats(_ngState);
|
||||
startGraphRenderer();
|
||||
|
||||
// Filter listeners
|
||||
document.getElementById('ngMinScore').addEventListener('input', function() {
|
||||
document.getElementById('ngMinScoreVal').textContent = (this.value / 100).toFixed(2);
|
||||
applyNGFilters();
|
||||
});
|
||||
document.getElementById('ngConfidence').addEventListener('change', applyNGFilters);
|
||||
rcEl.addEventListener('change', applyNGFilters);
|
||||
}
|
||||
|
||||
function createGraphState(data) {
|
||||
const nodes = (data.nodes || []).map((n, i) => ({
|
||||
...n,
|
||||
x: 450 + (Math.random() - 0.5) * 400,
|
||||
y: 300 + (Math.random() - 0.5) * 300,
|
||||
vx: 0, vy: 0,
|
||||
radius: Math.max(6, Math.min(18, 6 + (n.neighbor_count || 0)))
|
||||
}));
|
||||
const nodeIdx = {};
|
||||
nodes.forEach((n, i) => { nodeIdx[n.pubkey] = i; });
|
||||
const edges = (data.edges || []).filter(e => nodeIdx[e.source] !== undefined && nodeIdx[e.target] !== undefined);
|
||||
return {
|
||||
allNodes: nodes, allEdges: edges,
|
||||
nodes, edges, nodeIdx,
|
||||
stats: data.stats || {},
|
||||
zoom: 1, panX: 0, panY: 0,
|
||||
dragging: null, panning: false,
|
||||
lastMouseX: 0, lastMouseY: 0,
|
||||
cooling: 1.0, animId: null
|
||||
};
|
||||
}
|
||||
|
||||
function applyNGFilters() {
|
||||
if (!_ngState) return;
|
||||
const minScore = parseInt(document.getElementById('ngMinScore').value, 10) / 100;
|
||||
const conf = document.getElementById('ngConfidence').value;
|
||||
const checkedRoles = new Set();
|
||||
document.querySelectorAll('#ngRoleChecks input:checked').forEach(cb => checkedRoles.add(cb.dataset.role));
|
||||
|
||||
// Filter nodes by role
|
||||
const visibleNodes = _ngState.allNodes.filter(n => {
|
||||
const role = (n.role || 'unknown').toLowerCase();
|
||||
return checkedRoles.has(role) || role === 'unknown' || role === 'observer';
|
||||
});
|
||||
const visiblePKs = new Set(visibleNodes.map(n => n.pubkey));
|
||||
|
||||
// Filter edges
|
||||
_ngState.edges = _ngState.allEdges.filter(e => {
|
||||
if (e.score < minScore) return false;
|
||||
if (conf === 'high' && (e.ambiguous || e.score < 0.5)) return false;
|
||||
if (conf === 'hide-ambiguous' && e.ambiguous) return false;
|
||||
return visiblePKs.has(e.source) && visiblePKs.has(e.target);
|
||||
});
|
||||
|
||||
// Only include nodes that have at least one visible edge
|
||||
const edgeNodes = new Set();
|
||||
_ngState.edges.forEach(e => { edgeNodes.add(e.source); edgeNodes.add(e.target); });
|
||||
_ngState.nodes = visibleNodes.filter(n => edgeNodes.has(n.pubkey));
|
||||
|
||||
// Rebuild index
|
||||
_ngState.nodeIdx = {};
|
||||
_ngState.nodes.forEach((n, i) => { _ngState.nodeIdx[n.pubkey] = i; });
|
||||
|
||||
_ngState.cooling = 1.0;
|
||||
renderNGStats(_ngState);
|
||||
}
|
||||
|
||||
function renderNGStats(st) {
|
||||
const nodes = st.nodes, edges = st.edges;
|
||||
const totalScore = edges.reduce((s, e) => s + e.score, 0);
|
||||
const avgScore = edges.length ? (totalScore / edges.length) : 0;
|
||||
const ambiguous = edges.filter(e => e.ambiguous).length;
|
||||
const resolved = edges.length ? ((edges.length - ambiguous) / edges.length * 100) : 0;
|
||||
const statsEl = document.getElementById('ngStats');
|
||||
if (!statsEl) return;
|
||||
statsEl.innerHTML = `
|
||||
<div class="stat-card"><div class="stat-value">${nodes.length}</div><div class="stat-label">Nodes</div></div>
|
||||
<div class="stat-card"><div class="stat-value">${edges.length}</div><div class="stat-label">Edges</div></div>
|
||||
<div class="stat-card"><div class="stat-value">${avgScore.toFixed(2)}</div><div class="stat-label">Avg Score</div></div>
|
||||
<div class="stat-card"><div class="stat-value">${resolved.toFixed(0)}%</div><div class="stat-label">Resolved</div></div>
|
||||
<div class="stat-card"><div class="stat-value">${ambiguous}</div><div class="stat-label">Ambiguous</div></div>`;
|
||||
}
|
||||
|
||||
function startGraphRenderer() {
|
||||
if (!_ngState) return;
|
||||
|
||||
// Node count guard: skip force simulation for very large graphs
|
||||
var NODE_LIMIT = 1000;
|
||||
if (_ngState.allNodes.length > NODE_LIMIT) {
|
||||
var el = document.getElementById('ngCanvas');
|
||||
if (el) {
|
||||
el.style.display = 'none';
|
||||
var msg = document.createElement('div');
|
||||
msg.className = 'analytics-card';
|
||||
msg.innerHTML = '<p class="text-muted">Graph has ' + _ngState.allNodes.length + ' nodes (limit: ' + NODE_LIMIT + '). Force simulation skipped for performance. Use filters to reduce the node count.</p>';
|
||||
el.parentNode.insertBefore(msg, el);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const canvas = document.getElementById('ngCanvas');
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d');
|
||||
const dpr = window.devicePixelRatio || 1;
|
||||
canvas.width = canvas.clientWidth * dpr;
|
||||
canvas.height = canvas.clientHeight * dpr;
|
||||
ctx.scale(dpr, dpr);
|
||||
const W = canvas.clientWidth, H = canvas.clientHeight;
|
||||
|
||||
// Interaction
|
||||
let hoverNode = null;
|
||||
|
||||
function canvasToGraph(cx, cy) {
|
||||
return { x: (cx - _ngState.panX) / _ngState.zoom, y: (cy - _ngState.panY) / _ngState.zoom };
|
||||
}
|
||||
|
||||
function findNode(cx, cy) {
|
||||
const gp = canvasToGraph(cx, cy);
|
||||
for (let i = _ngState.nodes.length - 1; i >= 0; i--) {
|
||||
const n = _ngState.nodes[i];
|
||||
const dx = gp.x - n.x, dy = gp.y - n.y;
|
||||
if (dx * dx + dy * dy <= n.radius * n.radius) return n;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
canvas.addEventListener('mousedown', function(e) {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const cx = e.clientX - rect.left, cy = e.clientY - rect.top;
|
||||
const n = findNode(cx, cy);
|
||||
if (n) {
|
||||
_ngState.dragging = n;
|
||||
n._pinned = true;
|
||||
canvas.style.cursor = 'grabbing';
|
||||
} else {
|
||||
_ngState.panning = true;
|
||||
canvas.style.cursor = 'grabbing';
|
||||
}
|
||||
_ngState.lastMouseX = e.clientX;
|
||||
_ngState.lastMouseY = e.clientY;
|
||||
});
|
||||
|
||||
canvas.addEventListener('mousemove', function(e) {
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const cx = e.clientX - rect.left, cy = e.clientY - rect.top;
|
||||
if (_ngState.dragging) {
|
||||
const dx = (e.clientX - _ngState.lastMouseX) / _ngState.zoom;
|
||||
const dy = (e.clientY - _ngState.lastMouseY) / _ngState.zoom;
|
||||
_ngState.dragging.x += dx;
|
||||
_ngState.dragging.y += dy;
|
||||
_ngState.lastMouseX = e.clientX;
|
||||
_ngState.lastMouseY = e.clientY;
|
||||
_ngState.cooling = Math.max(_ngState.cooling, 0.3);
|
||||
} else if (_ngState.panning) {
|
||||
_ngState.panX += e.clientX - _ngState.lastMouseX;
|
||||
_ngState.panY += e.clientY - _ngState.lastMouseY;
|
||||
_ngState.lastMouseX = e.clientX;
|
||||
_ngState.lastMouseY = e.clientY;
|
||||
} else {
|
||||
const n = findNode(cx, cy);
|
||||
if (n !== hoverNode) {
|
||||
hoverNode = n;
|
||||
canvas.style.cursor = n ? 'pointer' : 'grab';
|
||||
const tip = document.getElementById('ngTooltip');
|
||||
if (n && tip) {
|
||||
tip.style.display = 'block';
|
||||
tip.style.left = (cx + 12) + 'px';
|
||||
tip.style.top = (cy - 8) + 'px';
|
||||
tip.innerHTML = `<strong>${esc(n.name || n.pubkey.slice(0, 12) + '…')}</strong><br>Role: ${esc(n.role || 'unknown')}<br>Neighbors: ${n.neighbor_count || 0}`;
|
||||
} else if (tip) {
|
||||
tip.style.display = 'none';
|
||||
}
|
||||
} else if (hoverNode) {
|
||||
const tip = document.getElementById('ngTooltip');
|
||||
if (tip) { tip.style.left = (cx + 12) + 'px'; tip.style.top = (cy - 8) + 'px'; }
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
canvas.addEventListener('mouseup', function() {
|
||||
if (_ngState.dragging) {
|
||||
_ngState.dragging._pinned = false;
|
||||
_ngState._wasDragging = true;
|
||||
}
|
||||
_ngState.dragging = null;
|
||||
_ngState.panning = false;
|
||||
canvas.style.cursor = hoverNode ? 'pointer' : 'grab';
|
||||
});
|
||||
|
||||
canvas.addEventListener('mouseleave', function() {
|
||||
_ngState.dragging = null;
|
||||
_ngState.panning = false;
|
||||
_ngState._wasDragging = false;
|
||||
const tip = document.getElementById('ngTooltip');
|
||||
if (tip) tip.style.display = 'none';
|
||||
hoverNode = null;
|
||||
});
|
||||
|
||||
canvas.addEventListener('click', function(e) {
|
||||
if (_ngState._wasDragging) { _ngState._wasDragging = false; return; }
|
||||
if (_ngState.dragging) return;
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const n = findNode(e.clientX - rect.left, e.clientY - rect.top);
|
||||
if (n) location.hash = '#/nodes/' + n.pubkey;
|
||||
});
|
||||
|
||||
canvas.addEventListener('keydown', function(e) {
|
||||
const PAN_STEP = 30, ZOOM_STEP = 1.15;
|
||||
switch (e.key) {
|
||||
case 'ArrowLeft': _ngState.panX += PAN_STEP; e.preventDefault(); break;
|
||||
case 'ArrowRight': _ngState.panX -= PAN_STEP; e.preventDefault(); break;
|
||||
case 'ArrowUp': _ngState.panY += PAN_STEP; e.preventDefault(); break;
|
||||
case 'ArrowDown': _ngState.panY -= PAN_STEP; e.preventDefault(); break;
|
||||
case '+': case '=': _ngState.zoom = Math.min(10, _ngState.zoom * ZOOM_STEP); e.preventDefault(); break;
|
||||
case '-': case '_': _ngState.zoom = Math.max(0.1, _ngState.zoom / ZOOM_STEP); e.preventDefault(); break;
|
||||
case '0': _ngState.zoom = 1; _ngState.panX = 0; _ngState.panY = 0; e.preventDefault(); break;
|
||||
}
|
||||
});
|
||||
|
||||
canvas.addEventListener('wheel', function(e) {
|
||||
e.preventDefault();
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const cx = e.clientX - rect.left, cy = e.clientY - rect.top;
|
||||
const factor = e.deltaY < 0 ? 1.1 : 0.9;
|
||||
const newZoom = Math.max(0.1, Math.min(10, _ngState.zoom * factor));
|
||||
// Zoom towards mouse position
|
||||
_ngState.panX = cx - (cx - _ngState.panX) * (newZoom / _ngState.zoom);
|
||||
_ngState.panY = cy - (cy - _ngState.panY) * (newZoom / _ngState.zoom);
|
||||
_ngState.zoom = newZoom;
|
||||
}, { passive: false });
|
||||
|
||||
// Cache text color to avoid getComputedStyle every frame
|
||||
const _labelColor = cssVar('--text-primary') || '#e0e0e0';
|
||||
|
||||
// Force simulation + render loop
|
||||
// Performance: 500 nodes brute-force repulsion: avg ~4ms/frame = 250fps headroom (measured Chrome 120, M1)
|
||||
var _perfFrameTimes = [], _perfLastTime = 0;
|
||||
function tick() {
|
||||
if (!document.getElementById('ngCanvas')) { _ngState.animId = null; return; }
|
||||
var now = performance.now();
|
||||
if (_perfLastTime) _perfFrameTimes.push(now - _perfLastTime);
|
||||
_perfLastTime = now;
|
||||
if (_perfFrameTimes.length === 100) {
|
||||
var avg = _perfFrameTimes.reduce(function(a, b) { return a + b; }, 0) / 100;
|
||||
console.log('[NeighborGraph perf] avg frame time over 100 frames: ' + avg.toFixed(2) + 'ms (' + (1000 / avg).toFixed(0) + ' fps)');
|
||||
_perfFrameTimes = [];
|
||||
}
|
||||
const st = _ngState;
|
||||
const nodes = st.nodes, edges = st.edges, idx = st.nodeIdx;
|
||||
|
||||
if (st.cooling > 0.001) {
|
||||
// Repulsion (all pairs — use grid for large sets, brute force for small)
|
||||
const k = 80; // repulsion constant
|
||||
for (let i = 0; i < nodes.length; i++) {
|
||||
for (let j = i + 1; j < nodes.length; j++) {
|
||||
let dx = nodes[j].x - nodes[i].x;
|
||||
let dy = nodes[j].y - nodes[i].y;
|
||||
let d2 = dx * dx + dy * dy;
|
||||
if (d2 < 1) { dx = Math.random() - 0.5; dy = Math.random() - 0.5; d2 = 1; }
|
||||
const f = k * k / d2;
|
||||
const fx = dx / Math.sqrt(d2) * f;
|
||||
const fy = dy / Math.sqrt(d2) * f;
|
||||
nodes[i].vx -= fx; nodes[i].vy -= fy;
|
||||
nodes[j].vx += fx; nodes[j].vy += fy;
|
||||
}
|
||||
}
|
||||
|
||||
// Attraction along edges
|
||||
const idealLen = 120;
|
||||
for (const e of edges) {
|
||||
const si = idx[e.source], ti = idx[e.target];
|
||||
if (si === undefined || ti === undefined) continue;
|
||||
const a = nodes[si], b = nodes[ti];
|
||||
let dx = b.x - a.x, dy = b.y - a.y;
|
||||
const d = Math.sqrt(dx * dx + dy * dy) || 1;
|
||||
const f = (d - idealLen) * 0.05 * (0.5 + e.score * 0.5);
|
||||
const fx = dx / d * f, fy = dy / d * f;
|
||||
a.vx += fx; a.vy += fy;
|
||||
b.vx -= fx; b.vy -= fy;
|
||||
}
|
||||
|
||||
// Center gravity
|
||||
for (const n of nodes) {
|
||||
n.vx += (W / 2 - n.x) * 0.001;
|
||||
n.vy += (H / 2 - n.y) * 0.001;
|
||||
}
|
||||
|
||||
// Apply velocities with damping
|
||||
const damping = 0.85;
|
||||
for (const n of nodes) {
|
||||
if (n._pinned) { n.vx = 0; n.vy = 0; continue; }
|
||||
n.vx *= damping * st.cooling;
|
||||
n.vy *= damping * st.cooling;
|
||||
const speed = Math.sqrt(n.vx * n.vx + n.vy * n.vy);
|
||||
if (speed > 10) { n.vx *= 10 / speed; n.vy *= 10 / speed; }
|
||||
n.x += n.vx;
|
||||
n.y += n.vy;
|
||||
}
|
||||
st.cooling *= 0.995;
|
||||
}
|
||||
|
||||
// Render
|
||||
ctx.save();
|
||||
ctx.clearRect(0, 0, W, H);
|
||||
ctx.translate(st.panX, st.panY);
|
||||
ctx.scale(st.zoom, st.zoom);
|
||||
|
||||
// Edges
|
||||
for (const e of edges) {
|
||||
const si = idx[e.source], ti = idx[e.target];
|
||||
if (si === undefined || ti === undefined) continue;
|
||||
const a = nodes[si], b = nodes[ti];
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(a.x, a.y);
|
||||
ctx.lineTo(b.x, b.y);
|
||||
ctx.strokeStyle = e.ambiguous ? 'rgba(255,200,0,0.4)' : 'rgba(150,150,150,0.35)';
|
||||
ctx.lineWidth = Math.max(0.5, e.score * 4);
|
||||
ctx.stroke();
|
||||
}
|
||||
|
||||
// Nodes
|
||||
const roleColors = window.ROLE_COLORS || {};
|
||||
for (const n of nodes) {
|
||||
const color = roleColors[(n.role || '').toLowerCase()] || '#6b7280';
|
||||
ctx.beginPath();
|
||||
ctx.arc(n.x, n.y, n.radius, 0, Math.PI * 2);
|
||||
ctx.fillStyle = color;
|
||||
ctx.fill();
|
||||
if (n === hoverNode) {
|
||||
ctx.strokeStyle = '#fff';
|
||||
ctx.lineWidth = 2;
|
||||
ctx.stroke();
|
||||
}
|
||||
// Label
|
||||
const label = n.name || (n.pubkey ? n.pubkey.slice(0, 8) + '…' : '');
|
||||
if (label && st.zoom > 0.4) {
|
||||
ctx.fillStyle = _labelColor;
|
||||
ctx.font = '10px sans-serif';
|
||||
ctx.textAlign = 'center';
|
||||
ctx.fillText(label, n.x, n.y + n.radius + 12);
|
||||
}
|
||||
}
|
||||
|
||||
ctx.restore();
|
||||
st.animId = requestAnimationFrame(tick);
|
||||
}
|
||||
|
||||
_ngState.animId = requestAnimationFrame(tick);
|
||||
}
|
||||
|
||||
registerPage('analytics', { init, destroy });
|
||||
})();
|
||||
|
||||
@@ -966,7 +966,7 @@
|
||||
var stc = server.typeColors || {};
|
||||
var typeRows = '';
|
||||
for (var tkey in TYPE_LABELS) {
|
||||
var tval = tc[tkey] || '#000000';
|
||||
var tval = tc[tkey] || (window.TYPE_COLORS && window.TYPE_COLORS[tkey]) || '#000000';
|
||||
typeRows += '<div class="cust-color-row">' +
|
||||
'<div><label>' + (TYPE_EMOJI[tkey] || '') + ' ' + TYPE_LABELS[tkey] + _overrideDot('typeColors', tkey) + '</label>' +
|
||||
'<div class="cust-hint">' + (TYPE_HINTS[tkey] || '') + '</div></div>' +
|
||||
|
||||
-126
@@ -175,110 +175,6 @@
|
||||
return `<div style="font-size:11px;color:var(--text-muted);margin:-2px 0 6px;padding:6px 10px;background:var(--surface-2);border-radius:4px;border-left:3px solid var(--status-yellow)">Adverts show varying hash sizes (<strong>${sizes.join('-byte, ')}-byte</strong>). This is a <a href="https://github.com/meshcore-dev/MeshCore/commit/fcfdc5f" target="_blank" style="color:var(--accent)">known bug</a> where automatic adverts ignore the configured multibyte path setting. Fixed in <a href="https://github.com/meshcore-dev/MeshCore/releases/tag/repeater-v1.14.1" target="_blank" style="color:var(--accent)">repeater v1.14.1</a>.</div>`;
|
||||
}
|
||||
|
||||
// ─── Neighbor section helpers ───────────────────────────────────────────────
|
||||
|
||||
// Cache: pubkey → { data, ts }
|
||||
var _neighborCache = {};
|
||||
|
||||
function getConfidenceIndicator(entry) {
|
||||
if (entry.ambiguous) return { icon: '⚠️', label: 'AMBIGUOUS', cls: 'confidence-ambiguous' };
|
||||
if (entry.count <= 1) return { icon: '🔴', label: 'LOW', cls: 'confidence-low' };
|
||||
if (entry.score >= 0.5 && entry.count >= 3) return { icon: '🟢', label: 'HIGH', cls: 'confidence-high' };
|
||||
return { icon: '🟡', label: 'MEDIUM', cls: 'confidence-medium' };
|
||||
}
|
||||
|
||||
function renderNeighborRows(neighbors, limit) {
|
||||
var sorted = neighbors.slice().sort(function(a, b) {
|
||||
return (b.score || b.affinity || 0) - (a.score || a.affinity || 0);
|
||||
});
|
||||
var items = limit ? sorted.slice(0, limit) : sorted;
|
||||
return items.map(function(nb) {
|
||||
var conf = getConfidenceIndicator(nb);
|
||||
var name = nb.name || (nb.prefix + '… (unknown)');
|
||||
var nameHtml = nb.pubkey
|
||||
? '<a href="#/nodes/' + encodeURIComponent(nb.pubkey) + '">' + escapeHtml(name) + '</a>'
|
||||
: '<span class="text-muted">' + escapeHtml(name) + '</span>';
|
||||
var role = nb.role || '—';
|
||||
var roleBadge = nb.role
|
||||
? '<span class="badge" style="background:' + (ROLE_COLORS[nb.role] || 'var(--surface-2)') + ';color:#fff;font-size:10px">' + escapeHtml(role) + '</span>'
|
||||
: '<span class="text-muted">—</span>';
|
||||
var scoreTitle = 'Observations: ' + nb.count;
|
||||
if (nb.avg_snr != null) scoreTitle += ' · Avg SNR: ' + Number(nb.avg_snr).toFixed(1) + ' dB';
|
||||
var showOnMap = nb.pubkey
|
||||
? ' <button class="btn-link neighbor-show-map" data-pubkey="' + escapeHtml(nb.pubkey) + '" style="font-size:11px;padding:1px 6px;white-space:nowrap">📍 Map</button>'
|
||||
: '';
|
||||
return '<tr>' +
|
||||
'<td style="font-weight:600">' + nameHtml + '</td>' +
|
||||
'<td>' + roleBadge + '</td>' +
|
||||
'<td title="' + escapeHtml(scoreTitle) + '">' + Number(nb.score).toFixed(2) + '</td>' +
|
||||
'<td>' + nb.count + '</td>' +
|
||||
'<td>' + renderNodeTimestampHtml(nb.last_seen) + '</td>' +
|
||||
'<td><span title="' + conf.label + '">' + conf.icon + '</span></td>' +
|
||||
'<td style="text-align:right">' + showOnMap + '</td>' +
|
||||
'</tr>';
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function renderNeighborTable(neighbors, limit) {
|
||||
return '<table class="data-table" style="font-size:12px">' +
|
||||
'<thead><tr><th>Neighbor</th><th>Role</th><th>Score</th><th>Obs</th><th>Last Seen</th><th>Conf</th><th></th></tr></thead>' +
|
||||
'<tbody>' + renderNeighborRows(neighbors, limit) + '</tbody></table>';
|
||||
}
|
||||
|
||||
function fetchAndRenderNeighbors(pubkey, containerId, opts) {
|
||||
opts = opts || {};
|
||||
var limit = opts.limit || 0;
|
||||
var headerSelector = opts.headerSelector;
|
||||
var viewAllPubkey = opts.viewAllPubkey;
|
||||
|
||||
// Check cache
|
||||
var cached = _neighborCache[pubkey];
|
||||
if (cached && (Date.now() - cached.ts < 300000)) { // 5 min cache
|
||||
renderNeighborData(cached.data, containerId, limit, headerSelector, viewAllPubkey);
|
||||
return;
|
||||
}
|
||||
|
||||
api('/nodes/' + encodeURIComponent(pubkey) + '/neighbors', { ttl: CLIENT_TTL.nodeDetail }).then(function(data) {
|
||||
_neighborCache[pubkey] = { data: data, ts: Date.now() };
|
||||
renderNeighborData(data, containerId, limit, headerSelector, viewAllPubkey);
|
||||
}).catch(function() {
|
||||
var el = document.getElementById(containerId);
|
||||
if (el) el.innerHTML = '<div class="text-muted" style="padding:8px">Could not load neighbor data</div>';
|
||||
});
|
||||
}
|
||||
|
||||
function renderNeighborData(data, containerId, limit, headerSelector, viewAllPubkey) {
|
||||
var el = document.getElementById(containerId);
|
||||
if (!el) return;
|
||||
if (!data || !data.neighbors || !data.neighbors.length) {
|
||||
el.innerHTML = '<div class="text-muted" style="padding:8px">No neighbor data available yet. Neighbor relationships are built from observed packet paths over time.</div>';
|
||||
if (headerSelector) {
|
||||
var h = document.querySelector(headerSelector);
|
||||
if (h) h.textContent = 'Neighbors (0)';
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (headerSelector) {
|
||||
var h = document.querySelector(headerSelector);
|
||||
if (h) h.textContent = 'Neighbors (' + data.neighbors.length + ')';
|
||||
}
|
||||
var html = renderNeighborTable(data.neighbors, limit);
|
||||
if (limit && data.neighbors.length > limit && viewAllPubkey) {
|
||||
html += '<div style="margin-top:6px;text-align:right"><a href="#/nodes/' + encodeURIComponent(viewAllPubkey) + '?section=node-neighbors" style="font-size:12px">View all ' + data.neighbors.length + ' neighbors →</a></div>';
|
||||
}
|
||||
el.innerHTML = html;
|
||||
|
||||
// Wire up "Show on Map" buttons via event delegation
|
||||
el.addEventListener('click', function(e) {
|
||||
var btn = e.target.closest('.neighbor-show-map');
|
||||
if (!btn) return;
|
||||
var pk = btn.getAttribute('data-pubkey');
|
||||
if (pk) location.hash = '#/map?node=' + encodeURIComponent(pk);
|
||||
});
|
||||
}
|
||||
|
||||
// ─── End neighbor helpers ─────────────────────────────────────────────────
|
||||
|
||||
let directNode = null; // set when navigating directly to #/nodes/:pubkey
|
||||
|
||||
let regionChangeHandler = null;
|
||||
@@ -451,11 +347,6 @@
|
||||
</table>
|
||||
</div>` : ''}
|
||||
|
||||
<div class="node-full-card" id="node-neighbors">
|
||||
<h4 id="fullNeighborsHeader">Neighbors</h4>
|
||||
<div id="fullNeighborsContent"><div class="text-muted" style="padding:8px"><span class="spinner"></span> Loading neighbors…</div></div>
|
||||
</div>
|
||||
|
||||
<div class="node-full-card" id="fullPathsSection">
|
||||
<h4>Paths Through This Node</h4>
|
||||
<div id="fullPathsContent"><div class="text-muted" style="padding:8px"><span class="spinner"></span> Loading paths…</div></div>
|
||||
@@ -536,11 +427,6 @@
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Fetch neighbors for this node (full-screen view)
|
||||
fetchAndRenderNeighbors(n.public_key, 'fullNeighborsContent', {
|
||||
headerSelector: '#fullNeighborsHeader'
|
||||
});
|
||||
|
||||
// Fetch paths through this node (full-screen view)
|
||||
api('/nodes/' + encodeURIComponent(n.public_key) + '/paths', { ttl: CLIENT_TTL.nodeDetail }).then(pathData => {
|
||||
const el = document.getElementById('fullPathsContent');
|
||||
@@ -933,11 +819,6 @@
|
||||
</div>
|
||||
</div>` : ''}
|
||||
|
||||
<div class="node-detail-section" id="panelNeighborsSection">
|
||||
<h4 id="panelNeighborsHeader">Neighbors</h4>
|
||||
<div id="panelNeighborsContent"><div class="text-muted" style="padding:8px"><span class="spinner"></span> Loading neighbors…</div></div>
|
||||
</div>
|
||||
|
||||
<div class="node-detail-section" id="pathsSection">
|
||||
<h4>Paths Through This Node</h4>
|
||||
<div id="pathsContent"><div class="text-muted" style="padding:8px"><span class="spinner"></span> Loading paths…</div></div>
|
||||
@@ -1008,13 +889,6 @@
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Fetch neighbors for this node (condensed panel — top 5)
|
||||
fetchAndRenderNeighbors(n.public_key, 'panelNeighborsContent', {
|
||||
limit: 5,
|
||||
headerSelector: '#panelNeighborsHeader',
|
||||
viewAllPubkey: n.public_key
|
||||
});
|
||||
|
||||
// Fetch paths through this node
|
||||
api('/nodes/' + encodeURIComponent(n.public_key) + '/paths', { ttl: CLIENT_TTL.nodeDetail }).then(pathData => {
|
||||
const el = document.getElementById('pathsContent');
|
||||
|
||||
+2
-106
@@ -550,7 +550,7 @@ async function run() {
|
||||
await page.goto(`${BASE}/#/analytics`, { waitUntil: 'domcontentloaded' });
|
||||
await page.waitForSelector('#analyticsTabs');
|
||||
const tabs = await page.$$('#analyticsTabs .tab-btn');
|
||||
assert(tabs.length >= 10, `Expected >=10 analytics tabs, got ${tabs.length}`);
|
||||
assert(tabs.length >= 8, `Expected >=8 analytics tabs, got ${tabs.length}`);
|
||||
// Overview tab should be active by default and show stat cards
|
||||
await page.waitForSelector('#analyticsContent .stat-card', { timeout: 8000 });
|
||||
const cards = await page.$$('#analyticsContent .stat-card');
|
||||
@@ -624,53 +624,6 @@ async function run() {
|
||||
assert(content.length > 10, 'Distance tab should render content');
|
||||
});
|
||||
|
||||
await test('Analytics Neighbor Graph tab renders canvas and stats', async () => {
|
||||
await page.click('[data-tab="neighbor-graph"]');
|
||||
await page.waitForSelector('#ngCanvas', { timeout: 8000 });
|
||||
const hasCanvas = await page.$('#ngCanvas');
|
||||
assert(hasCanvas, 'Neighbor Graph tab should have a canvas element');
|
||||
const hasStats = await page.$$eval('#ngStats .stat-card', els => els.length);
|
||||
assert(hasStats >= 3, `Neighbor Graph stats should have >=3 cards, got ${hasStats}`);
|
||||
// Verify filters exist
|
||||
const hasSlider = await page.$('#ngMinScore');
|
||||
assert(hasSlider, 'Should have min score slider');
|
||||
const hasConfidence = await page.$('#ngConfidence');
|
||||
assert(hasConfidence, 'Should have confidence filter');
|
||||
});
|
||||
|
||||
await test('Analytics Neighbor Graph filter changes update stats', async () => {
|
||||
// Capture edge count before filter
|
||||
const edgesBefore = await page.$eval('#ngStats', el => {
|
||||
const cards = el.querySelectorAll('.stat-card');
|
||||
for (const c of cards) {
|
||||
if (c.textContent.toLowerCase().includes('edge')) {
|
||||
const m = c.textContent.match(/\d+/);
|
||||
if (m) return parseInt(m[0], 10);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
});
|
||||
// Set min score slider to high value to reduce edges
|
||||
await page.$eval('#ngMinScore', el => { el.value = 90; el.dispatchEvent(new Event('input')); });
|
||||
await page.waitForTimeout(300);
|
||||
const edgesAfter = await page.$eval('#ngStats', el => {
|
||||
const cards = el.querySelectorAll('.stat-card');
|
||||
for (const c of cards) {
|
||||
if (c.textContent.toLowerCase().includes('edge')) {
|
||||
const m = c.textContent.match(/\d+/);
|
||||
if (m) return parseInt(m[0], 10);
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
});
|
||||
assert(edgesBefore >= 0, 'Should find edge count in stats before filter');
|
||||
assert(edgesAfter >= 0, 'Should find edge count in stats after filter');
|
||||
assert(edgesAfter <= edgesBefore, `Raising min score should reduce (or keep) edge count: ${edgesBefore} → ${edgesAfter}`);
|
||||
// Reset slider
|
||||
await page.$eval('#ngMinScore', el => { el.value = 0; el.dispatchEvent(new Event('input')); });
|
||||
await page.waitForTimeout(200);
|
||||
});
|
||||
|
||||
// --- Group: Compare page ---
|
||||
|
||||
await test('Compare page loads with observer dropdowns', async () => {
|
||||
@@ -1308,6 +1261,7 @@ async function run() {
|
||||
await page.evaluate(() => localStorage.removeItem('cs-theme-overrides'));
|
||||
});
|
||||
|
||||
// --- Group: Show Neighbors (#484 fix) ---
|
||||
|
||||
await test('Show Neighbors populates neighborPubkeys from affinity API', async () => {
|
||||
const testPubkey = 'aabbccdd11223344556677889900aabbccddeeff00112233445566778899001122';
|
||||
@@ -1451,64 +1405,6 @@ async function run() {
|
||||
await page.unroute(`**/api/nodes/${testPubkey}/neighbors*`);
|
||||
await page.unroute(`**/api/nodes/${testPubkey}/paths*`);
|
||||
});
|
||||
// ─── Neighbor section tests ───────────────────────────────────────────────
|
||||
|
||||
await test('Node detail: neighbors section exists with correct columns', async () => {
|
||||
// Navigate to a node detail page (use the first node in the list)
|
||||
await page.goto(BASE + '/#/nodes');
|
||||
await page.waitForSelector('#nodesBody tr[data-key]', { timeout: 10000 });
|
||||
// Get the first node's pubkey from the row's data-key attribute
|
||||
const pubkey = await page.$eval('#nodesBody tr[data-key]', el => el.dataset.key);
|
||||
await page.goto(BASE + '/#/nodes/' + pubkey);
|
||||
await page.waitForSelector('#node-neighbors', { timeout: 10000 });
|
||||
// Check the section exists
|
||||
const header = await page.$eval('#fullNeighborsHeader', el => el.textContent);
|
||||
assert(header.startsWith('Neighbors'), 'Header should start with "Neighbors", got: ' + header);
|
||||
// Wait for content to load (either table or empty state)
|
||||
await page.waitForFunction(() => {
|
||||
const el = document.getElementById('fullNeighborsContent');
|
||||
return el && !el.innerHTML.includes('spinner');
|
||||
}, { timeout: 10000 });
|
||||
const hasTable = await page.$('#fullNeighborsContent .data-table');
|
||||
if (hasTable) {
|
||||
// Check columns
|
||||
const headers = await page.$$eval('#fullNeighborsContent thead th', ths => ths.map(t => t.textContent));
|
||||
assert(headers.includes('Neighbor'), 'Should have Neighbor column');
|
||||
assert(headers.includes('Role'), 'Should have Role column');
|
||||
assert(headers.includes('Score'), 'Should have Score column');
|
||||
assert(headers.includes('Obs'), 'Should have Obs column');
|
||||
assert(headers.includes('Last Seen'), 'Should have Last Seen column');
|
||||
assert(headers.includes('Conf'), 'Should have Conf column');
|
||||
} else {
|
||||
// Empty state
|
||||
const text = await page.$eval('#fullNeighborsContent', el => el.textContent);
|
||||
assert(text.includes('No neighbor data') || text.includes('Could not load'), 'Should show empty or error state');
|
||||
}
|
||||
});
|
||||
|
||||
await test('Node detail: neighbors section loading state', async () => {
|
||||
// Navigate to a node - the section should initially show a spinner
|
||||
await page.goto(BASE + '/#/nodes');
|
||||
await page.waitForSelector('#nodesBody tr[data-key]', { timeout: 10000 });
|
||||
const pubkey = await page.$eval('#nodesBody tr[data-key]', el => el.dataset.key);
|
||||
// Intercept API to delay response
|
||||
await page.route('**/api/nodes/*/neighbors*', async route => {
|
||||
await new Promise(r => setTimeout(r, 500));
|
||||
await route.continue();
|
||||
});
|
||||
await page.goto(BASE + '/#/nodes/' + pubkey);
|
||||
// Check spinner appears
|
||||
const spinnerVisible = await page.waitForSelector('#fullNeighborsContent .spinner', { timeout: 5000 }).then(() => true).catch(() => false);
|
||||
assert(spinnerVisible, 'Loading spinner should be visible initially');
|
||||
// Wait for loading to finish
|
||||
await page.waitForFunction(() => {
|
||||
const el = document.getElementById('fullNeighborsContent');
|
||||
return el && !el.innerHTML.includes('spinner');
|
||||
}, { timeout: 15000 });
|
||||
await page.unroute('**/api/nodes/*/neighbors*');
|
||||
});
|
||||
|
||||
// ─── End neighbor section tests ───────────────────────────────────────────
|
||||
|
||||
// Extract frontend coverage if instrumented server is running
|
||||
try {
|
||||
|
||||
@@ -2020,6 +2020,24 @@ console.log('\n=== customize-v2.js: core behavior ===');
|
||||
assert.ok(src.includes("surface3: '--surface-3'"), 'surface3 must map to --surface-3');
|
||||
assert.ok(src.includes("sectionBg: '--section-bg'"), 'sectionBg must map to --section-bg');
|
||||
});
|
||||
|
||||
test('_renderNodes falls back to window.TYPE_COLORS when typeColors is empty (#514)', () => {
|
||||
const ctx = makeSandbox();
|
||||
ctx.CustomEvent = function (type) { this.type = type; };
|
||||
ctx.TYPE_COLORS = { ADVERT: '#22c55e', GRP_TXT: '#3b82f6' };
|
||||
ctx.window.TYPE_COLORS = ctx.TYPE_COLORS;
|
||||
const v2 = loadCustomizeV2(ctx);
|
||||
// computeEffective with empty typeColors should still allow fallback
|
||||
const server = { typeColors: {} };
|
||||
const effective = v2.computeEffective(server, {});
|
||||
// When typeColors is empty, the render should fall back to TYPE_COLORS
|
||||
// We test the logic directly: tc[key] || TYPE_COLORS[key] || '#000000'
|
||||
const tc = effective.typeColors || {};
|
||||
const advertColor = tc['ADVERT'] || (ctx.window.TYPE_COLORS && ctx.window.TYPE_COLORS['ADVERT']) || '#000000';
|
||||
assert.strictEqual(advertColor, '#22c55e', 'ADVERT should fall back to TYPE_COLORS, not #000000');
|
||||
const grpColor = tc['GRP_TXT'] || (ctx.window.TYPE_COLORS && ctx.window.TYPE_COLORS['GRP_TXT']) || '#000000';
|
||||
assert.strictEqual(grpColor, '#3b82f6', 'GRP_TXT should fall back to TYPE_COLORS, not #000000');
|
||||
});
|
||||
}
|
||||
|
||||
// ===== APP.JS: home rehydration merge (mergeUserHomeConfig removed — dead code) =====
|
||||
|
||||
Reference in New Issue
Block a user