Initial commit: NetworkCity — network traffic as a neon city
A native macOS network monitor that renders live traffic as a top-down neon city: your Mac is downtown, remote orgs are glowing districts, and traffic is cars of light driving the roads. Architecture: - NetworkCityCore: swappable data layer behind a ConnectionSource protocol (nettop-backed today), plus pure/tested logic — traffic diffing, org classification (reverse-DNS + CIDR), traffic classes, hub-and-spoke expansion policy, label anti-overlap, and latency→distance layout. - NetworkCityApp: SwiftUI + SpriteKit city — auto-expanding districts, protocol-coloured cars, click-to-inspect panel, and latency-as-distance (districts glide to their measured RTT). - nettop-probe: CLI proof of the data layer. 44 tests passing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import XCTest
|
||||
@testable import NetworkCityCore
|
||||
|
||||
final class TrafficDifferTests: XCTestCase {
|
||||
|
||||
private func conn(_ host: String, in bIn: UInt64, out bOut: UInt64) -> Connection {
|
||||
Connection(
|
||||
proto: .tcp4,
|
||||
local: Endpoint(host: "192.168.1.2", port: 5000),
|
||||
remote: Endpoint(host: host, port: 443),
|
||||
bytesIn: bIn, bytesOut: bOut,
|
||||
processName: "test", pid: 1
|
||||
)
|
||||
}
|
||||
|
||||
func testFirstSnapshotYieldsNoDeltas() {
|
||||
let differ = TrafficDiffer()
|
||||
let t0 = Date(timeIntervalSince1970: 1000)
|
||||
let deltas = differ.ingest(ConnectionSnapshot(timestamp: t0, connections: [conn("1.1.1.1", in: 100, out: 50)]))
|
||||
XCTAssertTrue(deltas.isEmpty) // need two readings to know a rate
|
||||
}
|
||||
|
||||
func testComputesDeltaAndRate() {
|
||||
let differ = TrafficDiffer()
|
||||
let t0 = Date(timeIntervalSince1970: 1000)
|
||||
let t1 = Date(timeIntervalSince1970: 1002) // +2s
|
||||
_ = differ.ingest(ConnectionSnapshot(timestamp: t0, connections: [conn("1.1.1.1", in: 100, out: 50)]))
|
||||
let deltas = differ.ingest(ConnectionSnapshot(timestamp: t1, connections: [conn("1.1.1.1", in: 1124, out: 50)]))
|
||||
|
||||
XCTAssertEqual(deltas.count, 1)
|
||||
XCTAssertEqual(deltas[0].bytesInDelta, 1024)
|
||||
XCTAssertEqual(deltas[0].bytesOutDelta, 0)
|
||||
XCTAssertEqual(deltas[0].inBytesPerSec, 512, accuracy: 0.001) // 1024 / 2s
|
||||
}
|
||||
|
||||
func testCounterResetTreatedAsZero() {
|
||||
let differ = TrafficDiffer()
|
||||
let t0 = Date(timeIntervalSince1970: 1000)
|
||||
let t1 = Date(timeIntervalSince1970: 1002)
|
||||
_ = differ.ingest(ConnectionSnapshot(timestamp: t0, connections: [conn("1.1.1.1", in: 9999, out: 0)]))
|
||||
let deltas = differ.ingest(ConnectionSnapshot(timestamp: t1, connections: [conn("1.1.1.1", in: 10, out: 0)]))
|
||||
XCTAssertTrue(deltas.isEmpty) // reused tuple, counter dropped -> no phantom delta
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user