Fix automated test issues

Closes: https://github.com/damus-io/damus/issues/3275
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2025-10-20 17:33:40 -07:00
parent 690f8b891e
commit 9555145359
8 changed files with 65 additions and 49 deletions

View File

@@ -103,13 +103,16 @@ final class AuthIntegrationTests: XCTestCase {
try! await pool.add_relay(relay_descriptor)
XCTAssertEqual(pool.relays.count, 1)
let connection_expectation = XCTestExpectation(description: "Waiting for connection")
await pool.connect()
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
if pool.num_connected == 1 {
connection_expectation.fulfill()
timer.invalidate()
Task {
if await pool.num_connected == 1 {
connection_expectation.fulfill()
timer.invalidate()
}
}
}
wait(for: [connection_expectation], timeout: 30.0)
await fulfillment(of: [connection_expectation], timeout: 30.0)
XCTAssertEqual(pool.num_connected, 1)
// Assert that no AUTH messages have been received
XCTAssertEqual(received_messages.count, 0)
@@ -148,13 +151,16 @@ final class AuthIntegrationTests: XCTestCase {
try! await pool.add_relay(relay_descriptor)
XCTAssertEqual(pool.relays.count, 1)
let connection_expectation = XCTestExpectation(description: "Waiting for connection")
await pool.connect()
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
if pool.num_connected == 1 {
connection_expectation.fulfill()
timer.invalidate()
Task {
if pool.num_connected == 1 {
connection_expectation.fulfill()
timer.invalidate()
}
}
}
wait(for: [connection_expectation], timeout: 30.0)
await fulfillment(of: [connection_expectation], timeout: 30.0)
XCTAssertEqual(pool.num_connected, 1)
// Assert that no AUTH messages have been received
XCTAssertEqual(received_messages.count, 0)
@@ -173,13 +179,15 @@ final class AuthIntegrationTests: XCTestCase {
timer.invalidate()
}
}
wait(for: [msg_expectation], timeout: 30.0)
await fulfillment(of: [msg_expectation], timeout: 30.0)
// Assert that AUTH message has been received
XCTAssertTrue(received_messages.count >= 1, "expected recieved_messages to be >= 1")
if received_messages.count < 1 { return } // End test early
let json_received = try! JSONSerialization.jsonObject(with: received_messages[0].data(using: .utf8)!, options: []) as! [Any]
XCTAssertEqual(json_received[0] as! String, "AUTH")
// Assert that we've replied with the AUTH response
XCTAssertEqual(sent_messages.count, 2)
if sent_messages.count < 2 { return }
let json_sent = try! JSONSerialization.jsonObject(with: sent_messages[1].data(using: .utf8)!, options: []) as! [Any]
XCTAssertEqual(json_sent[0] as! String, "AUTH")
let sent_msg = json_sent[1] as! [String: Any]

View File

@@ -12,12 +12,12 @@ import EmojiPicker
// Generates a test damus state with configurable mock parameters
func generate_test_damus_state(
mock_profile_info: [Pubkey: Profile]?,
home: HomeModel? = nil
home: HomeModel? = nil,
addNdbToRelayPool: Bool = true
) -> DamusState {
// Create a unique temporary directory
let ndb = Ndb.test
let our_pubkey = test_pubkey
let pool = RelayPool(ndb: ndb)
let settings = UserSettingsStore()
let profiles: Profiles = {
@@ -51,7 +51,8 @@ func generate_test_damus_state(
ndb: ndb,
quote_reposts: .init(our_pubkey: our_pubkey),
emoji_provider: DefaultEmojiProvider(showAllVariations: false),
favicon_cache: .init()
favicon_cache: .init(),
addNdbToRelayPool: addNdbToRelayPool
)
home?.damus_state = damus

View File

@@ -14,7 +14,10 @@ class NostrNetworkManagerTests: XCTestCase {
override func setUpWithError() throws {
// 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,
addNdbToRelayPool: false // Don't give RelayPool any access to Ndb. This will prevent incoming notes from affecting our test
)
let notesJSONL = getTestNotesJSONL()
@@ -37,13 +40,12 @@ class NostrNetworkManagerTests: XCTestCase {
return try! String(contentsOf: fileURL, encoding: .utf8)
}
func ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter, expectedCount: Int) {
func ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter, expectedCount: Int) async {
let endOfStream = XCTestExpectation(description: "Stream should receive EOSE")
let gotAtLeastExpectedCount = XCTestExpectation(description: "Stream should receive at least the expected number of items")
var receivedCount = 0
var eventIds: Set<NoteId> = []
Task {
for await item in self.damusState!.nostrNetwork.reader.advancedStream(filters: [filter], streamMode: .ndbFirst) {
for await item in self.damusState!.nostrNetwork.reader.advancedStream(filters: [filter], streamMode: .ndbOnly) {
switch item {
case .event(let lender):
try? lender.borrow { event in
@@ -53,9 +55,6 @@ class NostrNetworkManagerTests: XCTestCase {
}
eventIds.insert(event.id)
}
if receivedCount == expectedCount {
gotAtLeastExpectedCount.fulfill()
}
case .eose:
continue
case .ndbEose:
@@ -67,7 +66,7 @@ class NostrNetworkManagerTests: XCTestCase {
}
}
}
wait(for: [endOfStream, gotAtLeastExpectedCount], timeout: 10.0)
await fulfillment(of: [endOfStream], timeout: 15.0)
XCTAssertEqual(receivedCount, expectedCount, "Event IDs: \(eventIds.map({ $0.hex() }))")
}
@@ -83,14 +82,11 @@ class NostrNetworkManagerTests: XCTestCase {
/// nak req --kind 1 ws://localhost:10547 | wc -l
/// ```
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(authors: [Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!]), expectedCount: 22)
ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.boost], referenced_ids: [NoteId(hex: "64b26d0a587f5f894470e1e4783756b4d8ba971226de975ee30ac1b69970d5a1")!]), expectedCount: 5)
ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text, .boost, .zap], referenced_ids: [NoteId(hex: "64b26d0a587f5f894470e1e4783756b4d8ba971226de975ee30ac1b69970d5a1")!], limit: 500), expectedCount: 5)
ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text], limit: 10), expectedCount: 10)
ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text], until: UInt32(Date.now.timeIntervalSince1970), limit: 10), expectedCount: 10)
await ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text]), expectedCount: 57)
await ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(authors: [Pubkey(hex: "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245")!]), expectedCount: 22)
await ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.boost], referenced_ids: [NoteId(hex: "64b26d0a587f5f894470e1e4783756b4d8ba971226de975ee30ac1b69970d5a1")!]), expectedCount: 5)
await ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text, .boost, .zap], referenced_ids: [NoteId(hex: "64b26d0a587f5f894470e1e4783756b4d8ba971226de975ee30ac1b69970d5a1")!], limit: 500), expectedCount: 5)
await ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text], limit: 10), expectedCount: 10)
await ensureSubscribeGetsAllExpectedNotes(filter: NostrFilter(kinds: [.text], until: UInt32(Date.now.timeIntervalSince1970), limit: 10), expectedCount: 10)
}
}