Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Notify] Integration tests refactor #1219

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 71 additions & 59 deletions Example/IntegrationTests/Push/NotifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class NotifyTests: XCTestCase {

let gmDappDomain = InputConfig.gmDappHost

let pk = try! EthereumPrivateKey()
var pk: EthereumPrivateKey!

var privateKey: Data {
return Data(pk.rawPrivateKey)
Expand Down Expand Up @@ -93,111 +93,122 @@ final class NotifyTests: XCTestCase {
}

override func setUp() {
pk = try! EthereumPrivateKey()
walletNotifyClientA = makeWalletClient()
}

func testWalletCreatesSubscription() async {
func testWalletCreatesSubscription() async throws {
let expectation = expectation(description: "expects to create notify subscription")
expectation.assertForOverFulfill = false

var subscription: NotifySubscription?

walletNotifyClientA.subscriptionsPublisher
.sink { [unowned self] subscriptions in
guard let subscription = subscriptions.first else { return }
Task(priority: .high) {
try await walletNotifyClientA.deleteSubscription(topic: subscription.topic)
expectation.fulfill()
}
.sink { subscriptions in
subscription = subscriptions.first
expectation.fulfill()
}.store(in: &publishers)

try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)
try await walletNotifyClientA.register(account: account, domain: gmDappDomain, onSign: sign)
try await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)
await fulfillment(of: [expectation], timeout: InputConfig.defaultTimeout)

if let subscription {
try await walletNotifyClientA.deleteSubscription(topic: subscription.topic)
}
}

func testNotifyWatchSubscriptions() async throws {
let expectation = expectation(description: "expects client B to receive subscription created by client A")
expectation.assertForOverFulfill = false

var subscription: NotifySubscription?

let clientB = makeWalletClient(prefix: "👐🏼 Wallet B: ")
clientB.subscriptionsPublisher.sink { subscriptions in
guard let subscription = subscriptions.first else { return }
Task(priority: .high) {
try await clientB.deleteSubscription(topic: subscription.topic)
expectation.fulfill()
}
subscription = subscriptions.first
expectation.fulfill()
}.store(in: &publishers)

try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)
try! await clientB.register(account: account, domain: gmDappDomain, onSign: sign)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)
await fulfillment(of: [expectation], timeout: InputConfig.defaultTimeout)

if let subscription {
try await clientB.deleteSubscription(topic: subscription.topic)
}
flypaper0 marked this conversation as resolved.
Show resolved Hide resolved
}

func testNotifySubscriptionChanged() async throws {
let expectation = expectation(description: "expects client B to receive subscription after both clients are registered and client A creates one")
expectation.assertForOverFulfill = false

var subscription: NotifySubscription!
var subscription: NotifySubscription?

let clientB = makeWalletClient(prefix: "👐🏼 Wallet B: ")
clientB.subscriptionsPublisher.sink { subscriptions in
guard let newSubscription = subscriptions.first else { return }
subscription = newSubscription
subscription = subscriptions.first
expectation.fulfill()
}.store(in: &publishers)

try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, onSign: sign)
try! await clientB.register(account: account, domain: gmDappDomain, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)
await fulfillment(of: [expectation], timeout: InputConfig.defaultTimeout)

try await clientB.deleteSubscription(topic: subscription.topic)
if let subscription {
try await clientB.deleteSubscription(topic: subscription.topic)
}
}

func testWalletCreatesAndUpdatesSubscription() async {
let expectation = expectation(description: "expects to create and update notify subscription")
expectation.assertForOverFulfill = false
func testWalletCreatesAndUpdatesSubscription() async throws {
let created = expectation(description: "Subscription created")

var updateScope: Set<String>!
var didUpdate = false

walletNotifyClientA.subscriptionsPublisher
.sink { [unowned self] subscriptions in
guard
let subscription = subscriptions.first,
let scope = subscription.scope.keys.first
else { return }
let updated = expectation(description: "Subscription Updated")

let updatedScope = Set(subscription.scope.filter { $0.value.enabled == true }.keys)
var isCreated = false
var isUpdated = false
var subscription: NotifySubscription!

if !didUpdate {
updateScope = Set([scope])
didUpdate = true
Task(priority: .high) {
try await walletNotifyClientA.update(topic: subscription.topic, scope: Set([scope]))
}
}
if updateScope == updatedScope {
Task(priority: .high) {
try await walletNotifyClientA.deleteSubscription(topic: subscription.topic)
expectation.fulfill()
}
walletNotifyClientA.subscriptionsPublisher
.sink { subscriptions in
subscription = subscriptions.first

if !isCreated {
isCreated = true
created.fulfill()
} else if !isUpdated {
isUpdated = true
updated.fulfill()
}
}.store(in: &publishers)

try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)
try await walletNotifyClientA.register(account: account, domain: gmDappDomain, onSign: sign)
try await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)

await fulfillment(of: [created], timeout: InputConfig.defaultTimeout)

let updateScope = Set([subscription.scope.keys.first!])
try await walletNotifyClientA.update(topic: subscription.topic, scope: updateScope)

await fulfillment(of: [updated], timeout: InputConfig.defaultTimeout)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)
let updatedScope = Set(subscription.scope.filter { $0.value.enabled == true }.keys)
XCTAssertEqual(updatedScope, updateScope)

try await walletNotifyClientA.deleteSubscription(topic: subscription.topic)
}

func testNotifyServerSubscribeAndNotifies() async throws {
let subscribeExpectation = expectation(description: "creates notify subscription")
let messageExpectation = expectation(description: "receives a notify message")

var notifyMessage: NotifyMessage!
var notifyMessageRecord: NotifyMessageRecord?

var didNotify = false
walletNotifyClientA.subscriptionsPublisher
Expand All @@ -222,20 +233,21 @@ final class NotifyTests: XCTestCase {
}.store(in: &publishers)

walletNotifyClientA.messagesPublisher
.sink { [unowned self] messages in
guard let notifyMessageRecord = messages.first else { return }
XCTAssertEqual(notifyMessageRecord.message, notifyMessage)

Task(priority: .high) {
try await walletNotifyClientA.deleteSubscription(topic: notifyMessageRecord.topic)
messageExpectation.fulfill()
}
.sink { messages in
guard let newNotifyMessageRecord = messages.first else { return }
XCTAssertEqual(newNotifyMessageRecord.message, notifyMessage)
notifyMessageRecord = newNotifyMessageRecord
messageExpectation.fulfill()
}.store(in: &publishers)

try! await walletNotifyClientA.register(account: account, domain: gmDappDomain, onSign: sign)
try! await walletNotifyClientA.subscribe(appDomain: gmDappDomain, account: account)

wait(for: [subscribeExpectation, messageExpectation], timeout: InputConfig.defaultTimeout)
await fulfillment(of: [subscribeExpectation, messageExpectation], timeout: InputConfig.defaultTimeout)

if let notifyMessageRecord {
try await walletNotifyClientA.deleteSubscription(topic: notifyMessageRecord.topic)
}
}

}
Expand Down