Fix test compilation issues

Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2025-10-13 15:16:54 -07:00
parent 991a4a86e6
commit ab2c16288b
8 changed files with 50 additions and 34 deletions

View File

@@ -70,14 +70,15 @@ final class AuthIntegrationTests: XCTestCase {
} }
*/ */
func testAuthIntegrationRelayDamusIo() { @MainActor
func testAuthIntegrationRelayDamusIo() async {
// Create relay pool and connect to `wss://relay.damus.io` // Create relay pool and connect to `wss://relay.damus.io`
let relay_url = RelayURL("wss://relay.damus.io")! let relay_url = RelayURL("wss://relay.damus.io")!
var received_messages: [String] = [] var received_messages: [String] = []
var sent_messages: [String] = [] var sent_messages: [String] = []
let keypair: Keypair = generate_new_keypair().to_keypair() let keypair: Keypair = generate_new_keypair().to_keypair()
let pool = RelayPool(ndb: Ndb.test, keypair: keypair) let pool = RelayPool(ndb: Ndb.test, keypair: keypair)
pool.message_received_function = { obj in await pool.set_message_received_function({ obj in
let str = obj.0 let str = obj.0
let descriptor = obj.1 let descriptor = obj.1
@@ -86,8 +87,8 @@ final class AuthIntegrationTests: XCTestCase {
} }
received_messages.append(str) received_messages.append(str)
} })
pool.message_sent_function = { obj in await pool.set_message_sent_function({ obj in
let str = obj.0 let str = obj.0
let relay = obj.1 let relay = obj.1
@@ -96,10 +97,10 @@ final class AuthIntegrationTests: XCTestCase {
} }
sent_messages.append(str) sent_messages.append(str)
} })
XCTAssertEqual(pool.relays.count, 0) XCTAssertEqual(pool.relays.count, 0)
let relay_descriptor = RelayPool.RelayDescriptor.init(url: relay_url, info: .readWrite) let relay_descriptor = RelayPool.RelayDescriptor.init(url: relay_url, info: .readWrite)
try! pool.add_relay(relay_descriptor) try! await pool.add_relay(relay_descriptor)
XCTAssertEqual(pool.relays.count, 1) XCTAssertEqual(pool.relays.count, 1)
let connection_expectation = XCTestExpectation(description: "Waiting for connection") let connection_expectation = XCTestExpectation(description: "Waiting for connection")
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
@@ -114,14 +115,15 @@ final class AuthIntegrationTests: XCTestCase {
XCTAssertEqual(received_messages.count, 0) XCTAssertEqual(received_messages.count, 0)
} }
func testAuthIntegrationNostrWine() { @MainActor
func testAuthIntegrationNostrWine() async {
// Create relay pool and connect to `wss://nostr.wine` // Create relay pool and connect to `wss://nostr.wine`
let relay_url = RelayURL("wss://nostr.wine")! let relay_url = RelayURL("wss://nostr.wine")!
var received_messages: [String] = [] var received_messages: [String] = []
var sent_messages: [String] = [] var sent_messages: [String] = []
let keypair: Keypair = generate_new_keypair().to_keypair() let keypair: Keypair = generate_new_keypair().to_keypair()
let pool = RelayPool(ndb: Ndb.test, keypair: keypair) let pool = RelayPool(ndb: Ndb.test, keypair: keypair)
pool.message_received_function = { obj in await pool.set_message_received_function({ obj in
let str = obj.0 let str = obj.0
let descriptor = obj.1 let descriptor = obj.1
@@ -130,8 +132,8 @@ final class AuthIntegrationTests: XCTestCase {
} }
received_messages.append(str) received_messages.append(str)
} })
pool.message_sent_function = { obj in await pool.set_message_sent_function({ obj in
let str = obj.0 let str = obj.0
let relay = obj.1 let relay = obj.1
@@ -140,10 +142,10 @@ final class AuthIntegrationTests: XCTestCase {
} }
sent_messages.append(str) sent_messages.append(str)
} })
XCTAssertEqual(pool.relays.count, 0) XCTAssertEqual(pool.relays.count, 0)
let relay_descriptor = RelayPool.RelayDescriptor.init(url: relay_url, info: .readWrite) let relay_descriptor = RelayPool.RelayDescriptor.init(url: relay_url, info: .readWrite)
try! pool.add_relay(relay_descriptor) try! await pool.add_relay(relay_descriptor)
XCTAssertEqual(pool.relays.count, 1) XCTAssertEqual(pool.relays.count, 1)
let connection_expectation = XCTestExpectation(description: "Waiting for connection") let connection_expectation = XCTestExpectation(description: "Waiting for connection")
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
@@ -162,7 +164,7 @@ final class AuthIntegrationTests: XCTestCase {
let subscribe = NostrSubscribe(filters: [ let subscribe = NostrSubscribe(filters: [
NostrFilter(kinds: [.dm]) NostrFilter(kinds: [.dm])
], sub_id: uuid) ], sub_id: uuid)
pool.send(NostrRequest.subscribe(subscribe)) await pool.send(NostrRequest.subscribe(subscribe))
// Wait for AUTH message to have been received & sent // Wait for AUTH message to have been received & sent
let msg_expectation = XCTestExpectation(description: "Waiting for messages") let msg_expectation = XCTestExpectation(description: "Waiting for messages")
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
@@ -186,3 +188,13 @@ final class AuthIntegrationTests: XCTestCase {
} }
} }
extension RelayPool {
func set_message_received_function(_ newFunction: (((String, RelayDescriptor)) -> Void)?) {
self.message_received_function = newFunction
}
func set_message_sent_function(_ newFunction: (((String, Relay)) -> Void)? = nil) {
self.message_sent_function = newFunction
}
}

View File

@@ -35,7 +35,7 @@ final class MutingTests: XCTestCase {
} }
await test_damus_state.mutelist_manager.set_mutelist(mutelist) await test_damus_state.mutelist_manager.set_mutelist(mutelist)
test_damus_state.nostrNetwork.postbox.send(mutelist) await test_damus_state.nostrNetwork.postbox.send(mutelist)
let spammy_note_muted = await test_damus_state.mutelist_manager.is_event_muted(spammy_test_note) let spammy_note_muted = await test_damus_state.mutelist_manager.is_event_muted(spammy_test_note)
XCTAssert(spammy_note_muted) XCTAssert(spammy_note_muted)

View File

@@ -147,7 +147,7 @@ final class NIP10Tests: XCTestCase {
XCTAssertEqual(tr.is_reply_to_root, true) XCTAssertEqual(tr.is_reply_to_root, true)
} }
func test_marker_reply() { func test_marker_reply() async {
let note_json = """ let note_json = """
{ {
"pubkey": "5b0183ab6c3e322bf4d41c6b3aef98562a144847b7499543727c5539a114563e", "pubkey": "5b0183ab6c3e322bf4d41c6b3aef98562a144847b7499543727c5539a114563e",
@@ -181,7 +181,7 @@ final class NIP10Tests: XCTestCase {
let pk = Pubkey(hex: "5b0183ab6c3e322bf4d41c6b3aef98562a144847b7499543727c5539a114563e")! let pk = Pubkey(hex: "5b0183ab6c3e322bf4d41c6b3aef98562a144847b7499543727c5539a114563e")!
//let last_reply_hex = "1bb940ce0ba0d4a3b2a589355d908498dcd7452f941cf520072218f7e6ede75e" //let last_reply_hex = "1bb940ce0ba0d4a3b2a589355d908498dcd7452f941cf520072218f7e6ede75e"
let note = decode_nostr_event_json(json: note_json)! let note = decode_nostr_event_json(json: note_json)!
let reply = build_post(state: test_damus_state, post: .init(string: "hello"), action: .replying_to(note), uploadedMedias: [], pubkeys: [pk] + note.referenced_pubkeys.map({pk in pk})) let reply = await build_post(state: test_damus_state, post: .init(string: "hello"), action: .replying_to(note), uploadedMedias: [], pubkeys: [pk] + note.referenced_pubkeys.map({pk in pk}))
let root_hex = "00152d2945459fb394fed2ea95af879c903c4ec42d96327a739fa27c023f20e0" let root_hex = "00152d2945459fb394fed2ea95af879c903c4ec42d96327a739fa27c023f20e0"
XCTAssertEqual(reply.tags, XCTAssertEqual(reply.tags,

View File

@@ -15,8 +15,6 @@ class NostrNetworkManagerTests: XCTestCase {
override func setUpWithError() throws { override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class. // Put setup code here. This method is called before the invocation of each test method in the class.
damusState = generate_test_damus_state(mock_profile_info: nil) damusState = generate_test_damus_state(mock_profile_info: nil)
try! damusState?.nostrNetwork.userRelayList.set(userRelayList: NIP65.RelayList())
damusState?.nostrNetwork.connect()
let notesJSONL = getTestNotesJSONL() let notesJSONL = getTestNotesJSONL()
@@ -59,9 +57,10 @@ class NostrNetworkManagerTests: XCTestCase {
gotAtLeastExpectedCount.fulfill() gotAtLeastExpectedCount.fulfill()
} }
case .eose: case .eose:
continue
case .ndbEose:
// End of stream, break out of the loop // End of stream, break out of the loop
endOfStream.fulfill() endOfStream.fulfill()
case .ndbEose:
continue continue
case .networkEose: case .networkEose:
continue continue
@@ -83,7 +82,10 @@ class NostrNetworkManagerTests: XCTestCase {
/// ``` /// ```
/// nak req --kind 1 ws://localhost:10547 | wc -l /// nak req --kind 1 ws://localhost:10547 | wc -l
/// ``` /// ```
func testNdbSubscription() { func testNdbSubscription() async {
try! await damusState?.nostrNetwork.userRelayList.set(userRelayList: NIP65.RelayList())
await damusState?.nostrNetwork.connect()
ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text]), expectedCount: 57) ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text]), expectedCount: 57)
ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(authors: [Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!]), expectedCount: 22) ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(authors: [Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!]), expectedCount: 22)
ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.boost], referenced_ids: [NoteId(hex: "64b26d0a587f5f894470e1e4783756b4d8ba971226de975ee30ac1b69970d5a1")!]), expectedCount: 5) ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.boost], referenced_ids: [NoteId(hex: "64b26d0a587f5f894470e1e4783756b4d8ba971226de975ee30ac1b69970d5a1")!]), expectedCount: 5)

View File

@@ -15,8 +15,6 @@ final class ThreadModelTests: XCTestCase {
override func setUpWithError() throws { override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class. // Put setup code here. This method is called before the invocation of each test method in the class.
damusState = generate_test_damus_state(mock_profile_info: nil) damusState = generate_test_damus_state(mock_profile_info: nil)
try! damusState?.nostrNetwork.userRelayList.set(userRelayList: NIP65.RelayList())
damusState?.nostrNetwork.connect()
let notesJSONL = getTestNotesJSONL() let notesJSONL = getTestNotesJSONL()
@@ -40,7 +38,10 @@ final class ThreadModelTests: XCTestCase {
} }
/// Tests loading up a thread and checking if the repost count loads as expected. /// Tests loading up a thread and checking if the repost count loads as expected.
func testActionBarModel() throws { func testActionBarModel() async throws {
try! await damusState?.nostrNetwork.userRelayList.set(userRelayList: NIP65.RelayList())
await damusState?.nostrNetwork.connect()
let testNoteJson = """ let testNoteJson = """
{"content":"https://smartflowsocial.s3.us-east-1.amazonaws.com/clients/cm7kdrwdk0000qyu6fwtd96ui/0cab65a9-0142-48e3-abd7-94d20e30d3b2.jpg\n\n","pubkey":"71ecabd8b6b33548e075ff01b31568ffda19d0ac2788067d99328c6de4885975","tags":[["t","meme"],["t","memes"],["t","memestr"],["t","plebchain"]],"created_at":1755694800,"id":"64b26d0a587f5f894470e1e4783756b4d8ba971226de975ee30ac1b69970d5a1","kind":1,"sig":"c000794da8c4f7549b546630b16ed17f6edc0af0269b8c46ce14f5b1937431e7575b78351bc152007ebab5720028e5fe4b738f99e8887f273d35dd2217d1cc3d"} {"content":"https://smartflowsocial.s3.us-east-1.amazonaws.com/clients/cm7kdrwdk0000qyu6fwtd96ui/0cab65a9-0142-48e3-abd7-94d20e30d3b2.jpg\n\n","pubkey":"71ecabd8b6b33548e075ff01b31568ffda19d0ac2788067d99328c6de4885975","tags":[["t","meme"],["t","memes"],["t","memestr"],["t","plebchain"]],"created_at":1755694800,"id":"64b26d0a587f5f894470e1e4783756b4d8ba971226de975ee30ac1b69970d5a1","kind":1,"sig":"c000794da8c4f7549b546630b16ed17f6edc0af0269b8c46ce14f5b1937431e7575b78351bc152007ebab5720028e5fe4b738f99e8887f273d35dd2217d1cc3d"}
""" """
@@ -52,7 +53,7 @@ final class ThreadModelTests: XCTestCase {
let actionBarModel = make_actionbar_model(ev: note.id, damus: damusState!) let actionBarModel = make_actionbar_model(ev: note.id, damus: damusState!)
while true { while true {
try await Task.sleep(nanoseconds: 500_000_000) try await Task.sleep(nanoseconds: 500_000_000)
actionBarModel.update(damus: damusState!, evid: note.id) await actionBarModel.update(damus: damusState!, evid: note.id)
if actionBarModel.boosts >= 5 { if actionBarModel.boosts >= 5 {
break break
} }

View File

@@ -171,13 +171,13 @@ final class PostViewTests: XCTestCase {
nonAlphaNumerics.forEach { testAddingStringAfterLink(str: $0)} nonAlphaNumerics.forEach { testAddingStringAfterLink(str: $0)}
} }
func testQuoteRepost() { func testQuoteRepost() async {
let post = build_post(state: test_damus_state, post: .init(), action: .quoting(test_note), uploadedMedias: [], pubkeys: []) let post = await build_post(state: test_damus_state, post: .init(), action: .quoting(test_note), uploadedMedias: [], pubkeys: [])
XCTAssertEqual(post.tags, [["q", test_note.id.hex(), "", jack_keypair.pubkey.hex()], ["p", jack_keypair.pubkey.hex()]]) XCTAssertEqual(post.tags, [["q", test_note.id.hex(), "", jack_keypair.pubkey.hex()], ["p", jack_keypair.pubkey.hex()]])
} }
func testBuildPostRecognizesStringsAsNpubs() throws { func testBuildPostRecognizesStringsAsNpubs() async throws {
// given // given
let expectedLink = "nostr:\(test_pubkey.npub)" let expectedLink = "nostr:\(test_pubkey.npub)"
let content = NSMutableAttributedString(string: "@test", attributes: [ let content = NSMutableAttributedString(string: "@test", attributes: [
@@ -185,7 +185,7 @@ final class PostViewTests: XCTestCase {
]) ])
// when // when
let post = build_post( let post = await build_post(
state: test_damus_state, state: test_damus_state,
post: content, post: content,
action: .posting(.user(test_pubkey)), action: .posting(.user(test_pubkey)),
@@ -197,7 +197,7 @@ final class PostViewTests: XCTestCase {
XCTAssertEqual(post.content, expectedLink) XCTAssertEqual(post.content, expectedLink)
} }
func testBuildPostRecognizesUrlsAsNpubs() throws { func testBuildPostRecognizesUrlsAsNpubs() async throws {
// given // given
guard let npubUrl = URL(string: "damus:nostr:\(test_pubkey.npub)") else { guard let npubUrl = URL(string: "damus:nostr:\(test_pubkey.npub)") else {
return XCTFail("Could not create URL") return XCTFail("Could not create URL")
@@ -207,7 +207,7 @@ final class PostViewTests: XCTestCase {
]) ])
// when // when
let post = build_post( let post = await build_post(
state: test_damus_state, state: test_damus_state,
post: content, post: content,
action: .posting(.user(test_pubkey)), action: .posting(.user(test_pubkey)),

View File

@@ -86,7 +86,7 @@ class ReplyTests: XCTestCase {
// XCTAssertEqual(post_blocks.count, 1) // XCTAssertEqual(post_blocks.count, 1)
// } // }
func testNewlineMentions() throws { func testNewlineMentions() async throws {
let bech32_pk = "npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s" let bech32_pk = "npub1xtscya34g58tk0z605fvr788k263gsu6cy9x0mhnm87echrgufzsevkk5s"
let pk = bech32_pubkey_decode(bech32_pk)! let pk = bech32_pubkey_decode(bech32_pk)!
@@ -96,7 +96,7 @@ class ReplyTests: XCTestCase {
post.append(user_tag_attr_string(profile: profile, pubkey: pk)) post.append(user_tag_attr_string(profile: profile, pubkey: pk))
post.append(.init(string: "\n")) post.append(.init(string: "\n"))
let post_note = build_post(state: test_damus_state, post: post, action: .posting(.none), uploadedMedias: [], pubkeys: [pk]) let post_note = await build_post(state: test_damus_state, post: post, action: .posting(.none), uploadedMedias: [], pubkeys: [pk])
let expected_render = "nostr:\(pk.npub)\nnostr:\(pk.npub)" let expected_render = "nostr:\(pk.npub)\nnostr:\(pk.npub)"
XCTAssertEqual(post_note.content, expected_render) XCTAssertEqual(post_note.content, expected_render)

View File

@@ -79,7 +79,8 @@ final class WalletConnectTests: XCTestCase {
XCTAssertEqual(url_2.relay.url.absoluteString, relay_2) XCTAssertEqual(url_2.relay.url.absoluteString, relay_2)
} }
func testNWCEphemeralRelay() { @MainActor
func testNWCEphemeralRelay() async {
let sec = "8ba3a6b3b57d0f4211bb1ea4d8d1e351a367e9b4ea694746e0a4a452b2bc4d37" let sec = "8ba3a6b3b57d0f4211bb1ea4d8d1e351a367e9b4ea694746e0a4a452b2bc4d37"
let pk = "89446b900c70d62438dcf66756405eea6225ad94dc61f3856f62f9699111a9a6" let pk = "89446b900c70d62438dcf66756405eea6225ad94dc61f3856f62f9699111a9a6"
let nwc = WalletConnectURL(str: "nostrwalletconnect://\(pk)?relay=ws://127.0.0.1&secret=\(sec)&lud16=jb55@jb55.com")! let nwc = WalletConnectURL(str: "nostrwalletconnect://\(pk)?relay=ws://127.0.0.1&secret=\(sec)&lud16=jb55@jb55.com")!
@@ -87,7 +88,7 @@ final class WalletConnectTests: XCTestCase {
let pool = RelayPool(ndb: .empty) let pool = RelayPool(ndb: .empty)
let box = PostBox(pool: pool) let box = PostBox(pool: pool)
WalletConnect.pay(url: nwc, pool: pool, post: box, invoice: "invoice", zap_request: nil) await WalletConnect.pay(url: nwc, pool: pool, post: box, invoice: "invoice", zap_request: nil)
XCTAssertEqual(pool.our_descriptors.count, 0) XCTAssertEqual(pool.our_descriptors.count, 0)
XCTAssertEqual(pool.all_descriptors.count, 1) XCTAssertEqual(pool.all_descriptors.count, 1)