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

make canMessage method accessible on the Client class #200

Merged
merged 8 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions Sources/XMTP/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ public final class Client: Sendable {
public func canMessage(_ peerAddress: String) async throws -> Bool {
return try await query(topic: .contact(peerAddress)).envelopes.count > 0
}

public static func canMessage(_ peerAddress: String, options: ClientOptions? = nil) async throws -> Bool {
let options = options ?? ClientOptions()

let client = try await XMTPRust.create_client(GRPCApiClient.envToUrl(env: options.api.env), options.api.env != .local)
let apiClient = try GRPCApiClient(
environment: options.api.env,
secure: options.api.isSecure,
rustClient: client
)
return try await apiClient.query(topic: .contact(peerAddress)).envelopes.count > 0
}

public func importConversation(from conversationData: Data) throws -> Conversation? {
let jsonDecoder = JSONDecoder()
Expand Down
14 changes: 14 additions & 0 deletions Tests/XMTPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class ClientTests: XCTestCase {
XCTAssertFalse(cannotMessage)
}

func testStaticCanMessage() async throws {
try TestConfig.skip(because: "run manually against dev")
let opts = ClientOptions(api: ClientOptions.Api(env: .local, isSecure: false))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this is running against local not dev?

Suggested change
try TestConfig.skip(because: "run manually against dev")
let opts = ClientOptions(api: ClientOptions.Api(env: .local, isSecure: false))
try TestConfig.skip(because: "run manually against local")
let opts = ClientOptions(api: ClientOptions.Api(env: .local, isSecure: false))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you run the test locally you will get the following error:

error: -[XMTPTests.ClientTests testStaticCanMessage] : failed: caught error: "creationError("setup error")"

you should run it pointing to dev to pass the test

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're pointing at the local network here so it's definitely running on local. When I have the local network running in docker on my computer and run this test it passes when I kill the local server it fails. Just want to make it clear this is not running on dev (which is good we want it running on local since it's a test). We should still keep the test skipped as iOS CI can't run a local server but I think the wording of dev is misleading in this instance. I'm pushing up a fix for that and some spacing issues. 🙏


let aliceWallet = try PrivateKey.generate()
let notOnNetwork = try PrivateKey.generate()
let alice = try await Client.create(account: aliceWallet, options: opts)

let canMessage = try await Client.canMessage(alice.address, options: opts)
let cannotMessage = try await Client.canMessage(notOnNetwork.address, options: opts)
XCTAssertTrue(canMessage)
XCTAssertFalse(cannotMessage)
}

func testHasPrivateKeyBundleV1() async throws {
let fakeWallet = try PrivateKey.generate()
let client = try await Client.create(account: fakeWallet, apiClient: FakeApiClient())
Expand Down