damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

RelayPoolTests.swift (2242B)


      1 //
      2 //  RelayPoolTests.swift
      3 //  damusTests
      4 //
      5 //  Created by kernelkind on 12/16/23.
      6 //
      7 
      8 import Foundation
      9 
     10 import XCTest
     11 @testable import damus
     12 
     13 final class RelayPoolTests: XCTestCase {
     14     
     15     override func setUpWithError() throws {
     16     }
     17 
     18     override func tearDownWithError() throws {
     19     }
     20     
     21     func testAddRelay_ValidRelayURL_NoErrors() {
     22         testAddRelays(urls: [
     23             "wss://relay.damus.io"
     24         ])
     25     }
     26     
     27     func testAddRelay_TwoSameURLs_ThrowsRelayAlreadyExists() {
     28         testAddRelays(urls: [
     29             "wss://relay.damus.io",
     30             "wss://relay.damus.io"
     31         ], expectedError: .RelayAlreadyExists)
     32     }
     33     
     34     func testAddRelay_OneExtraneousSlashURL_ThrowsRelayAlreadyExists() {
     35         testAddRelays(urls: [
     36             "wss://relay.damus.io",
     37             "wss://relay.damus.io/"
     38         ], expectedError: .RelayAlreadyExists)
     39     }
     40 
     41     func testAddRelay_MultipleExtraneousSlashURL_ThrowsRelayAlreadyExists() {
     42         testAddRelays(urls: [
     43             "wss://relay.damus.io",
     44             "wss://relay.damus.io///"
     45         ], expectedError: .RelayAlreadyExists)
     46     }
     47     
     48     func testAddRelay_ExtraSlashURLFirst_ThrowsRelayAlreadyExists() {
     49         testAddRelays(urls: [
     50             "wss://relay.damus.io///",
     51             "wss://relay.damus.io"
     52         ], expectedError: .RelayAlreadyExists)
     53     }
     54 }
     55 
     56 func testAddRelays(urls: [String], expectedError: RelayError? = nil) {
     57     let ndb = Ndb()!
     58     let relayPool = RelayPool(ndb: ndb)
     59     let info = RelayInfo(read: true, write: true)
     60 
     61     do {
     62         for relay in urls {
     63             guard let url = RelayURL(relay) else {
     64                 XCTFail("Invalid URL encountered: \(relay)")
     65                 return
     66             }
     67 
     68             let descriptor = RelayDescriptor(url: url, info: info)
     69             try relayPool.add_relay(descriptor)
     70         }
     71 
     72         if expectedError != nil {
     73             XCTFail("Expected \(expectedError!) error, but no error was thrown.")
     74         }
     75     } catch let error as RelayError where expectedError == .RelayAlreadyExists {
     76         XCTAssertEqual(error, expectedError!, "Expected RelayAlreadyExists error, got \(error)")
     77     } catch {
     78         XCTFail("An unexpected error was thrown: \(error)")
     79     }
     80 }
     81 
     82