damus

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

commit dafa1ba4debbe8055a1ee7e6900079fbc9a4224c
parent bb32d72903dead180dbafab22248435202fd649e
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 10 May 2023 16:23:54 -0700

test: add nwc and ephemeral relay test

This ensures that when paying with nwc, we get an ephemeral relay added
and a request queued in the postbox.

Diffstat:
Mdamus.xcodeproj/project.pbxproj | 4++++
AdamusTests/WalletConnectTests.swift | 84+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/damus.xcodeproj/project.pbxproj b/damus.xcodeproj/project.pbxproj @@ -149,6 +149,7 @@ 4C7D09742A0AEF9000943473 /* AlbyGradient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D09732A0AEF9000943473 /* AlbyGradient.swift */; }; 4C7D09762A0AF19E00943473 /* FillAndStroke.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D09752A0AF19E00943473 /* FillAndStroke.swift */; }; 4C7D09782A0B0CC900943473 /* WalletModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D09772A0B0CC900943473 /* WalletModel.swift */; }; + 4C7D097E2A0C58B900943473 /* WalletConnectTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7D097D2A0C58B900943473 /* WalletConnectTests.swift */; }; 4C7FF7D52823313F009601DB /* Mentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7FF7D42823313F009601DB /* Mentions.swift */; }; 4C8682872814DE470026224F /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C8682862814DE470026224F /* ProfileView.swift */; }; 4C8D00C829DF791C0036AF10 /* CompatibleAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C8D00C729DF791C0036AF10 /* CompatibleAttribute.swift */; }; @@ -569,6 +570,7 @@ 4C7D09732A0AEF9000943473 /* AlbyGradient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbyGradient.swift; sourceTree = "<group>"; }; 4C7D09752A0AF19E00943473 /* FillAndStroke.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FillAndStroke.swift; sourceTree = "<group>"; }; 4C7D09772A0B0CC900943473 /* WalletModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WalletModel.swift; sourceTree = "<group>"; }; + 4C7D097D2A0C58B900943473 /* WalletConnectTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WalletConnectTests.swift; sourceTree = "<group>"; }; 4C7FF7D42823313F009601DB /* Mentions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mentions.swift; sourceTree = "<group>"; }; 4C8682862814DE470026224F /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = "<group>"; }; 4C8D00C729DF791C0036AF10 /* CompatibleAttribute.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CompatibleAttribute.swift; sourceTree = "<group>"; }; @@ -1275,6 +1277,7 @@ 4CE6DEF627F7A08200C66700 /* damusTests */ = { isa = PBXGroup; children = ( + 4C7D097D2A0C58B900943473 /* WalletConnectTests.swift */, F944F56C29EA9CB20067B3BF /* Models */, 50A50A8C29A09E1C00C01BE7 /* RequestTests.swift */, DD597CBC2963D85A00C64D32 /* MarkdownTests.swift */, @@ -1851,6 +1854,7 @@ 3A3040EF29A8FEE9008A0F29 /* EventDetailBarTests.swift in Sources */, 4C3EA67B28FF7B3900C48A62 /* InvoiceTests.swift in Sources */, 4C363A9E2828A822006E126D /* ReplyTests.swift in Sources */, + 4C7D097E2A0C58B900943473 /* WalletConnectTests.swift in Sources */, 4CB883AA297612FF00DC99E7 /* ZapTests.swift in Sources */, 4CB8839A297322D200DC99E7 /* DMTests.swift in Sources */, F944F56E29EA9CCC0067B3BF /* DamusParseContentTests.swift in Sources */, diff --git a/damusTests/WalletConnectTests.swift b/damusTests/WalletConnectTests.swift @@ -0,0 +1,84 @@ +// +// WalletConnectTests.swift +// damusTests +// +// Created by William Casarin on 2023-04-02. +// + +import XCTest +@testable import damus + +final class WalletConnectTests: XCTestCase { + + override func setUpWithError() throws { + // Put setup code here. This method is called before the invocation of each test method in the class. + } + + override func tearDownWithError() throws { + // Put teardown code here. This method is called after the invocation of each test method in the class. + } + + func testWalletBalanceRequest() throws { + // This is an example of a functional test case. + // Use XCTAssert and related functions to verify your tests produce the correct results. + // Any test you write for XCTest can be annotated as throws and async. + // Mark your test throws to produce an unexpected failure when your test encounters an uncaught error. + // Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards. + } + + func get_test_nwc() -> WalletConnectURL { + let pk = "9d088f4760422443d4699b485e2ac66e565a2f5da1198c55ddc5679458e3f67a" + let sec = "ff2eefd57196d42089e1b42acc39916d7ecac52e0625bd70597bbd5be14aff18" + let relay = "wss://relay.getalby.com/v1" + let str = "nostrwalletconnect://\(pk)?relay=\(relay)&secret=\(sec)" + + return WalletConnectURL(str: str)! + } + + func testDoesNWCParse() { + let pk = "9d088f4760422443d4699b485e2ac66e565a2f5da1198c55ddc5679458e3f67a" + let sec = "ff2eefd57196d42089e1b42acc39916d7ecac52e0625bd70597bbd5be14aff18" + let relay = "wss://relay.getalby.com/v1" + let str = "nostrwalletconnect://\(pk)?relay=\(relay)&secret=\(sec)&lud16=jb55@jb55.com" + + let url = WalletConnectURL(str: str) + XCTAssertNotNil(url) + guard let url else { + return + } + XCTAssertEqual(url.pubkey, pk) + XCTAssertEqual(url.keypair.privkey, sec) + XCTAssertEqual(url.keypair.pubkey, privkey_to_pubkey(privkey: sec)) + XCTAssertEqual(url.relay.id, relay) + XCTAssertEqual(url.lud16, "jb55@jb55.com") + } + + func testNWCEphemeralRelay() { + let sec = "8ba3a6b3b57d0f4211bb1ea4d8d1e351a367e9b4ea694746e0a4a452b2bc4d37" + let pk = "89446b900c70d62438dcf66756405eea6225ad94dc61f3856f62f9699111a9a6" + let nwc = WalletConnectURL(str: "nostrwalletconnect://\(pk)?relay=ws://127.0.0.1&secret=\(sec)&lud16=jb55@jb55.com")! + + let pool = RelayPool() + let box = PostBox(pool: pool) + + nwc_pay(url: nwc, pool: pool, post: box, invoice: "invoice") + + XCTAssertEqual(pool.our_descriptors.count, 0) + XCTAssertEqual(pool.all_descriptors.count, 1) + XCTAssertEqual(pool.all_descriptors[0].info.ephemeral, true) + XCTAssertEqual(pool.all_descriptors[0].url.id, "ws://127.0.0.1") + XCTAssertEqual(box.events.count, 1) + let ev = box.events.first!.value + XCTAssertEqual(ev.skip_ephemeral, false) + XCTAssertEqual(ev.remaining.count, 1) + XCTAssertEqual(ev.remaining[0].relay, "ws://127.0.0.1") + } + + func testPerformanceExample() throws { + // This is an example of a performance test case. + self.measure { + // Put the code you want to measure the time of here. + } + } + +}