note-251110-barebones-test-peer-created

Barebones Test Peer Created

Date: 2025-11-10
Issue: - P2P Testing Infrastructure
Status: ✅ Complete

Problem

Testing P2P connections with two Electron instances is cumbersome:

  • Heavy overhead (2 full Electron apps)
  • Difficult to debug (two separate DevTools)
  • Slow iteration cycle
  • Hard to automate

Solution: Standalone Test Peer

Created a lightweight Node.js test peer (/test-peer) with:

  • Exact same libp2p config as Electron app
  • mDNS auto-discovery (finds Electron app automatically)
  • Interactive CLI for manual testing
  • Detailed logging for debugging
  • Fast startup (~1 second vs Electron's ~5 seconds)

Usage

Start Test Peer

cd test-peer
npm install
npm start

Start Electron App

cd app
npm run dev
# Navigate to "P2P Network" view

Test Connection

From test peer:

whatnext> list              # See discovered peers
whatnext> connect 1         # Connect to first peer
whatnext> connections       # Verify connection

From Electron app:

  • Click "Connect" button next to discovered test peer

CLI Commands

Command Description
list List discovered peers
connect <n> Connect to peer number <n>
connections Show active connections
status Show node status
help Show all commands
exit Shutdown and exit

Architecture

┌─────────────────────┐         ┌──────────────────────┐
│   Test Peer         │         │  Electron App        │
   (Node.js)  (Utility Process)   │
│                     │         │                      │
│  libp2p node        │◄───────►│  libp2p node         │
│  - WebRTC           │  mDNS   │  - WebRTC            │
│  - mDNS             │  P2P- mDNS              │
│  - Noise            │         │  - Noise             │
│  - yamux            │         │  - yamux             │
└─────────────────────┘         └──────────────────────┘

Key Point: Both use IDENTICAL libp2p configuration, ensuring parity.

Benefits

Development

  • Fast iteration: Restart in 1 second (vs 5-10 seconds for Electron)
  • Easy debugging: Single terminal, clear logs
  • No UI overhead: Pure P2P testing

Testing

  • Automated tests: Can spawn test peer programmatically
  • CI/CD ready: No Electron required for P2P tests
  • Multi-peer testing: Spawn 10+ peers easily

Documentation

  • Living example: Test peer code documents P2P usage
  • Onboarding: New devs can experiment without Electron

Example Session

$ npm start

╔════════════════════════════════════════════════════════════╗
║          WhatNext Barebones Test Peer v1.0                ║
╚════════════════════════════════════════════════════════════╝

🚀 Starting WhatNext Test Peer...

✅ Node started successfully!

Your Peer ID:
  12D3KooWSeGgUKtPNVcVy6423yWX4XMqoRoz8fw1jwMRNcpLqSHF

Listening on:
  /ip4/127.0.0.1/tcp/54321/p2p/12D3KooWSeGg...
  /ip4/192.168.1.100/tcp/54321/p2p/12D3KooWSeGg...

👂 Listening for mDNS peer discovery...

💬 Interactive CLI ready. Type "help" for commands.

whatnext>

🔍 Peer discovered!
   Peer ID: 12D3KooWElectronApp...
   Multiaddrs: 2 address(es)
   Type 'connect 1' to connect

whatnext> connect 1

📡 Connecting to peer 1...
   Peer ID: 12D3KooWElectronApp...

✅ CONNECTED to peer!
   Peer ID: 12D3KooWElectronApp...
   Total connections: 1

whatnext> status

📊 Node Status:

  Peer ID: 12D3KooWSeGgUKtPNVcVy6423yWX4XMqoRoz8fw1jwMRNcpLqSHF
  Multiaddrs: 2
    /ip4/127.0.0.1/tcp/54321/p2p/12D3KooWSeGg...
    /ip4/192.168.1.100/tcp/54321/p2p/12D3KooWSeGg...
  Discovered Peers: 1
  Active Connections: 1

whatnext> exit

👋 Shutting down...

File Structure

/test-peer
  /src
    index.js          # Main test peer implementation
  package.json        # Dependencies (same as Electron app)
  README.md           # Usage documentation

Configuration Parity

Test Peer (test-peer/src/index.js):

createLibp2p({
    connectionEncrypters: [noise()],
    streamMuxers: [yamux()],
    transports: [webRTC(), circuitRelayTransport()],
    peerDiscovery: [mdns()],
    services: { identify: identify() },
    connectionManager: { maxConnections: 10 }
})

Electron App (app/src/utility/p2p-service.ts):

createLibp2p({
    connectionEncrypters: [noise()],
    streamMuxers: [yamux()],
    transports: [webRTC(), circuitRelayTransport()],
    peerDiscovery: [mdns()],
    services: { identify: identify() },
    connectionManager: { maxConnections: 10 }
})

IDENTICAL - Ensures bugs found in one apply to the other.

Next Steps

Immediate

  • Test peer connects to Electron app
  • Verify bidirectional connection (both can dial each other)
  • Test connection stability (long-running)

Phase 2: Automated Testing

  • Spawn test peer programmatically in tests
  • Write integration tests using test peer
  • Add to CI/CD pipeline

Phase 3: Protocol Testing

  • Add custom protocol handlers to test peer
  • Test RxDB replication protocol
  • Simulate network conditions (latency, packet loss)

Phase 4: Load Testing

  • Spawn 10+ test peers simultaneously
  • Measure connection limits
  • Test mesh network scaling

Maintenance

⚠️ IMPORTANT: When updating Electron app's P2P config, always update test peer.

Files to keep in sync:

  • app/src/utility/p2p-service.ts
  • test-peer/src/index.js

Success Metrics

Test peer created: Standalone Node.js implementation
Dependencies installed: Same libp2p packages as Electron
Interactive CLI: Commands for testing
Documentation: Comprehensive README
Fast startup: <1 second to running node
mDNS discovery: Auto-finds Electron app

References

  • Issue: Handle whtnxt://connect Custom Protocol
  • /docs/notes/note-251109-custom-protocol-barebones-peer.md (Original design doc)
  • Test Peer README: /test-peer/README.md
  • libp2p docs: https://docs.libp2p.io/

Status: ✅ Test peer ready for use!

Next: Test connection between test peer and Electron app.