Files
vvvomitandClaude Opus 5.5 f2823ad950 Host stats read each peer's latency and who dialled whom
ipfs swarm peers --latency --direction, for the big map's peer table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 19:55:09 +03:30

66 lines
2.7 KiB
Go

package deploy
import (
"testing"
"time"
)
func TestParseHostStats(t *testing.T) {
s := parseHostStats(`installed yes
active active
follow activating
result success
next 1793400000
parts images
last fetching bafyoci
free 120000000000
peers 7
repo {"RepoSize":5000,"StorageMax":1}
bw Bandwidth TotalIn: 900 B TotalOut: 1.5 kB RateIn: 12.5 B/s RateOut: 3 MB/s
bitswap {"BlocksReceived":40,"DataReceived":4000,"BlocksSent":2,"DataSent":200}
index { "version": 9, "created": "2026-10-29T23:18:50Z" }
pins bafygit bafyother
links {"Data":{"/":{"bytes":"CAE"}},"Links":[{"Hash":{"/":"bafypkg"},"Name":"packages","Tsize":3000},{"Hash":{"/":"bafyoci"},"Name":"oci","Tsize":10000},{"Hash":{"/":"bafygit"},"Name":"git","Tsize":70},{"Hash":{"/":"bafyidx"},"Name":"index.json","Tsize":9}]}
`)
if !s.Active || !s.Following || s.Failed || s.Peers != 7 || s.RateIn != 12.5 || s.TotalOut != 1500 || s.RateOut != 3e6 || s.DataSent != 200 || s.Published != "2026-10-29T23:18:50Z" {
t.Fatalf("%+v", s)
}
// Images only: the OCI part is wanted, and what's in the repo counts as arriving.
if s.Wanted != 10000 || s.Received != 5000 || len(s.PublishedParts) != 3 || !s.PublishedParts[2].Pinned || s.PublishedParts[1].Pinned {
t.Fatalf("wanted %d received %d parts %v", s.Wanted, s.Received, s.PublishedParts)
}
// interzone-mirror's node pins only the root: every part under it is kept.
m := parseHostStats(`installed yes
mirror interzone-mirror-ipfs-1
active active
parts all
root bafyroot
pins bafyroot
links {"Links":[{"Hash":{"/":"bafypkg"},"Name":"packages","Tsize":3000},{"Hash":{"/":"bafyoci"},"Name":"oci","Tsize":10000}]}
`)
if m.Mirror != "interzone-mirror-ipfs-1" || len(m.Parts) != 4 || m.Wanted != 13000 || m.Received != 13000 {
t.Fatalf("%+v", m)
}
if s := parseHostStats("installed no\n"); s.Installed || s.Active {
t.Fatalf("%+v", s)
}
}
func TestParseHostStatsPeers(t *testing.T) {
s := parseHostStats(`installed yes
active active
self /ip4/10.0.0.5/tcp/4001/p2p/me /ip4/2.176.0.9/tcp/4001/p2p/me /dns4/mirror.example/tcp/4001/p2p/me
peer /ip4/5.160.0.1/tcp/4001/p2p/12D3a 300 9000
peer /ip6/2a01::1/tcp/4001/p2p/12D3b
swarm {"Peers":[{"Addr":"/ip4/5.160.0.1/tcp/4001","Peer":"12D3a","Latency":"41.5ms","Muxer":"","Direction":1,"Streams":null}]}
`)
if len(s.Self) != 1 || s.Self[0].String() != "2.176.0.9" || len(s.selfNames) != 1 || s.selfNames[0] != "mirror.example" {
t.Fatalf("self %v names %v", s.Self, s.selfNames)
}
if len(s.PeerList) != 2 || s.PeerList[0].ID != "12D3a" || s.PeerList[0].Sent != 300 || s.PeerList[0].Recv != 9000 ||
s.PeerList[1].Addr.String() != "2a01::1" || s.PeerList[1].Sent != 0 ||
s.PeerList[0].Latency != 41500*time.Microsecond || !s.PeerList[0].Inbound || s.PeerList[1].Inbound {
t.Fatalf("%+v", s.PeerList)
}
}