const std = @import("std"); const testing = std.testing; const network = @import("../src/network.zig"); const discovery = @import("../src/network/discovery.zig"); const DiscoveryManager = discovery.DiscoveryManager; const DiscoveryConfig = discovery.DiscoveryConfig; const DiscoveryState = discovery.DiscoveryState; const DiscoveryRequest = discovery.DiscoveryRequest; const DiscoveryResponse = discovery.DiscoveryResponse; const ByteBuffer = discovery.ByteBuffer; const MulticastDiscovery = discovery.MulticastDiscovery; const peer_mod = @import("../src/network/peer.zig"); const PeerId = peer_mod.PeerId; const core = @import("core"); const Address = core.address.Address; test "discovery: create and destroy manager" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19998, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try testing.expectEqual(DiscoveryState.Stopped, manager.state); } test "discovery: start discovery" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19997, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); try testing.expectEqual(DiscoveryState.Scanning, manager.state); try testing.expect(manager.isRunning()); } test "discovery: stop discovery" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19996, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); manager.stop(); try testing.expectEqual(DiscoveryState.Stopped, manager.state); try testing.expect(!manager.isRunning()); } test "discovery: send discovery request" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address{ .family = .ipv4, .data = .{ .ipv4 = core.address.Ipv4Address.init([4]u8{ 255, 255, 255, 255 }) }, .port = 0, }, .discovery_port = 19995, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); const peer_id = PeerId.init(); try manager.sendDiscoveryRequest(peer_id, 8080); const stats = manager.getStats(); try testing.expectEqual(@as(u32, 1), stats.requests_sent); } test "discovery: timeout functionality" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19994, .timeout_ms = 500, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); const start_time = std.time.milliTimestamp(); const result = try manager.listen(500); const elapsed = std.time.milliTimestamp() - start_time; try testing.expectEqual(false, result); try testing.expect(elapsed >= 500); try testing.expect(elapsed < 1000); } test "discovery: listen without timeout uses polling" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19993, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); const start_time = std.time.milliTimestamp(); const result = try manager.listen(100); const elapsed = std.time.milliTimestamp() - start_time; try testing.expectEqual(false, result); try testing.expect(elapsed >= 100); } test "discovery: discovery request encoding and decoding" { const allocator = testing.allocator; var buffer = ByteBuffer.init(allocator); defer buffer.deinit(); const request = DiscoveryRequest{ .requester_id = PeerId.init(), .port = 8080, .protocol_version = 1, .metadata = [_]u8{1} ** 32, }; try request.encode(&buffer); const decoded = try DiscoveryRequest.decode(&buffer); try testing.expectEqual(request.port, decoded.port); try testing.expectEqual(request.protocol_version, decoded.protocol_version); } test "discovery: discovery response encoding and decoding" { const allocator = testing.allocator; var buffer = ByteBuffer.init(allocator); defer buffer.deinit(); const response = DiscoveryResponse{ .responder_id = PeerId.init(), .address = try Address.parse("192.168.1.100:8080"), .capabilities = [_]u8{1} ** 8, .protocol_version = 1, .timestamp = std.time.timestamp(), }; try response.encode(&buffer); const decoded = try DiscoveryResponse.decode(&buffer); try testing.expectEqual(response.protocol_version, decoded.protocol_version); try testing.expectEqual(response.address.port, decoded.address.port); } test "discovery: protocol version mismatch handling" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19992, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); // Create response with unsupported protocol version const response = DiscoveryResponse{ .responder_id = PeerId.init(), .address = try Address.parse("127.0.0.1:8080"), .capabilities = [_]u8{0} ** 8, .protocol_version = 99, // Unsupported version .timestamp = std.time.timestamp(), }; const source = try Address.parse("127.0.0.1:9999"); // Should not crash, just log warning try manager.handleDiscoveryResponse(response, source); // Peer should still be added despite version mismatch try testing.expectEqual(@as(u32, 1), manager.stats.responses_received); } test "discovery: max peers limit" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19991, .max_peers = 5, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); // Add more than max_peers var i: u16 = 0; while (i < 10) : (i += 1) { const response = DiscoveryResponse{ .responder_id = PeerId.init(), .address = Address{ .family = .ipv4, .data = .{ .ipv4 = core.address.Ipv4Address.init([4]u8{ 127, 0, 0, @intCast(i + 1) }) }, .port = 8000 + i, }, .capabilities = [_]u8{0} ** 8, .protocol_version = 1, .timestamp = std.time.timestamp(), }; const source = Address{ .family = .ipv4, .data = .{ .ipv4 = core.address.Ipv4Address.init([4]u8{ 127, 0, 0, @intCast(i + 1) }) }, .port = 8000 + i, }; try manager.handleDiscoveryResponse(response, source); } const discovered = manager.getDiscoveredPeers(); try testing.expect(discovered.len <= 5); } test "discovery: cleanup removes excess peers" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19990, .max_peers = 3, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); // Add 5 peers var i: u16 = 0; while (i < 5) : (i += 1) { const response = DiscoveryResponse{ .responder_id = PeerId.init(), .address = Address{ .family = .ipv4, .data = .{ .ipv4 = core.address.Ipv4Address.init([4]u8{ 192, 168, 1, @intCast(i + 1) }) }, .port = 8000 + i, }, .capabilities = [_]u8{0} ** 8, .protocol_version = 1, .timestamp = std.time.timestamp(), }; const source = Address{ .family = .ipv4, .data = .{ .ipv4 = core.address.Ipv4Address.init([4]u8{ 192, 168, 1, @intCast(i + 1) }) }, .port = 8000 + i, }; try manager.handleDiscoveryResponse(response, source); } manager.cleanup(); const discovered = manager.getDiscoveredPeers(); try testing.expect(discovered.len <= 3); } test "discovery: statistics tracking" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19989, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); const peer_id = PeerId.init(); try manager.sendDiscoveryRequest(peer_id, 8080); try manager.sendDiscoveryRequest(peer_id, 8080); const stats = manager.getStats(); try testing.expectEqual(@as(u32, 2), stats.requests_sent); } test "discovery: success rate calculation" { const allocator = testing.allocator; const config = DiscoveryConfig{ .broadcast_address = Address.init(), .discovery_port = 19988, }; var manager = try DiscoveryManager.init(allocator, config); defer manager.deinit(); try manager.start(); manager.stats.requests_sent = 10; manager.stats.responses_received = 7; // Trigger success rate calculation const response = DiscoveryResponse{ .responder_id = PeerId.init(), .address = try Address.parse("127.0.0.1:8080"), .capabilities = [_]u8{0} ** 8, .protocol_version = 1, .timestamp = std.time.timestamp(), }; const source = try Address.parse("127.0.0.1:8080"); try manager.handleDiscoveryResponse(response, source); const stats = manager.getStats(); try testing.expect(stats.success_rate >= 70); } test "discovery: multicast configuration" { const config = MulticastDiscovery.createConfig(.link_local, 8080); try testing.expectEqual(@as(u16, 8080), config.discovery_port); try testing.expectEqual(@as(u8, 1), config.multicast_ttl); } test "discovery: site-local multicast configuration" { const config = MulticastDiscovery.createConfig(.site_local, 9090); try testing.expectEqual(@as(u16, 9090), config.discovery_port); try testing.expectEqual(@as(u8, 4), config.multicast_ttl); } test "discovery: ByteBuffer operations" { const allocator = testing.allocator; var buffer = ByteBuffer.init(allocator); defer buffer.deinit(); try buffer.writeByte(0x42); try buffer.writeInt(u16, 8080, .big); try buffer.writeBytes(&[_]u8{ 1, 2, 3, 4 }); try testing.expectEqual(@as(u8, 0x42), try buffer.readByte()); try testing.expectEqual(@as(u16, 8080), try buffer.readInt(u16, .big)); var read_bytes: [4]u8 = undefined; try buffer.readBytes(&read_bytes); try testing.expectEqualSlices(u8, &[_]u8{ 1, 2, 3, 4 }, &read_bytes); }