RelayURLTests.swift (4563B)
1 // 2 // RelayURLTests.swift 3 // damusTests 4 // 5 // Created by Daniel D’Aquino on 2024-03-20. 6 // 7 8 import Foundation 9 import XCTest 10 @testable import damus 11 12 final class RelayURLTests : XCTestCase { 13 func testRelayURLTrailingSlash() { 14 let relay_url_1: RelayURL = RelayURL("wss://relay.damus.io")! 15 let relay_url_2: RelayURL = RelayURL("wss://relay.damus.io/")! 16 17 XCTAssertEqual(relay_url_1.id, relay_url_2.id, "Relays with the same address should have the same ID even if one of them was initialized with a trailing slash") 18 XCTAssertEqual(relay_url_1, relay_url_2, "Relays with the same address should be equal even if one of them was initialized with a trailing slash") 19 20 var relays: [RelayURL: Int] = [:] 21 relays[relay_url_1] = 1 22 relays[relay_url_2] = 2 23 24 XCTAssertEqual(relays[relay_url_1], 2, "RelayURL with a trailing slash should evaluate to the same hash in a dictionary as an equivalent one without trailing slashes") 25 } 26 27 func testRelayURLDifferentProtocols() { 28 let relay_url_1: RelayURL = RelayURL("wss://relay.damus.io")! 29 let relay_url_2: RelayURL = RelayURL("ws://relay.damus.io")! 30 31 XCTAssertNotEqual(relay_url_1.id, relay_url_2.id, "Relays with different protocols should not have the same ID") 32 XCTAssertNotEqual(relay_url_1, relay_url_2, "Relays with different protocols should not be equal") 33 34 var relays: [RelayURL: Int] = [:] 35 relays[relay_url_1] = 1 36 relays[relay_url_2] = 2 37 38 XCTAssertNotEqual(relays[relay_url_1], relays[relay_url_2], "RelayURL with different protocols should not evaluate to the same hash in a dictionary") 39 } 40 41 func testRelayURLDifferentDomains() { 42 let relay_url_1: RelayURL = RelayURL("wss://relay.damus.io")! 43 let relay_url_3: RelayURL = RelayURL("wss://example.com")! 44 45 XCTAssertNotEqual(relay_url_1, relay_url_3, "Relays with different domains should not be equal") 46 47 var relays: [RelayURL: Int] = [:] 48 relays[relay_url_1] = 1 49 relays[relay_url_3] = 3 50 51 XCTAssertNotEqual(relays[relay_url_1], relays[relay_url_3], "RelayURL with different domains should not evaluate to the same hash in a dictionary") 52 } 53 54 func testRelayURLDifferentPaths() { 55 let relay_url_1: RelayURL = RelayURL("wss://relay.damus.io")! 56 let relay_url_2: RelayURL = RelayURL("wss://relay.damus.io/")! 57 let relay_url_3: RelayURL = RelayURL("wss://relay.damus.io/v1")! 58 let relay_url_4: RelayURL = RelayURL("wss://relay.damus.io/v2")! 59 let relay_url_5: RelayURL = RelayURL("wss://relay.damus.io/v2/beta")! 60 let relay_url_6: RelayURL = RelayURL("wss://relay.damus.io/v2/beta/")! 61 62 XCTAssertEqual(relay_url_1.id, relay_url_2.id, "Relays with the same address should have the same ID even if one of them was initialized with a trailing slash") 63 XCTAssertEqual(relay_url_1, relay_url_2, "Relays with the same address should be equal even if one of them was initialized with a trailing slash") 64 65 XCTAssertNotEqual(relay_url_1, relay_url_3, "Relays with different paths should not be equal") 66 XCTAssertNotEqual(relay_url_3, relay_url_4, "Relays with different paths should not be equal") 67 XCTAssertNotEqual(relay_url_4, relay_url_5, "Relays with different subpaths should not be equal") 68 XCTAssertEqual(relay_url_5, relay_url_6, "Relays with the same address should be equal if one of them is initialized with a trailing slash") 69 70 var relays: [RelayURL: Int] = [:] 71 relays[relay_url_1] = 1 72 relays[relay_url_2] = 2 73 relays[relay_url_3] = 3 74 relays[relay_url_4] = 4 75 relays[relay_url_5] = 5 76 relays[relay_url_6] = 6 77 78 XCTAssertEqual(relays[relay_url_1], relays[relay_url_2], "RelayURL with the same path should evaluate to the same hash in a dictionary") 79 XCTAssertNotEqual(relays[relay_url_1], relays[relay_url_3], "RelayURLs with different pathsshould not evaluate to the same hash in a dictionary") 80 XCTAssertNotEqual(relays[relay_url_3], relays[relay_url_4], "RelayURLs with different paths should not evaluate to the same hash in a dictionary") 81 XCTAssertNotEqual(relays[relay_url_4], relays[relay_url_5], "RelayURLs with different subpaths should not evaluate to the same hash in a dictionary") 82 XCTAssertEqual(relays[relay_url_5], relays[relay_url_6], "RelayURL with the same subpath should evaluate to the same hash in a dictionary even if one of them is initialized with a trailing slash") 83 } 84 }