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

Fix integration tests #177

Merged
merged 4 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Example/AblyChatExample/Mocks/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class MockMessagesPaginatedResult: PaginatedResult {
Array(repeating: 0, count: numberOfMockMessages).map { _ in
Message(
serial: "\(Date().timeIntervalSince1970)",
latestAction: .create,
action: .create,
clientID: self.clientID,
roomID: self.roomID,
text: MockStrings.randomPhrase(),
Expand Down
4 changes: 2 additions & 2 deletions Example/AblyChatExample/Mocks/MockClients.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ actor MockMessages: Messages {
let subscription = MockSubscription<Message>(randomElement: {
Message(
serial: "\(Date().timeIntervalSince1970)",
latestAction: .create,
action: .create,
clientID: MockStrings.names.randomElement()!,
roomID: self.roomID,
text: MockStrings.randomPhrase(),
Expand All @@ -130,7 +130,7 @@ actor MockMessages: Messages {
func send(params: SendMessageParams) async throws -> Message {
let message = Message(
serial: "\(Date().timeIntervalSince1970)",
latestAction: .create,
action: .create,
clientID: clientID,
roomID: roomID,
text: params.text,
Expand Down
2 changes: 1 addition & 1 deletion Sources/AblyChat/ChatAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal final class ChatAPI: Sendable {

let message = Message(
serial: response.serial,
latestAction: .create,
action: .create,
clientID: clientId,
roomID: roomId,
text: params.text,
Expand Down
2 changes: 1 addition & 1 deletion Sources/AblyChat/DefaultMessages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ internal final class DefaultMessages: Messages, EmitsDiscontinuities {

let message = Message(
serial: serial,
latestAction: action,
action: action,
clientID: clientID,
roomID: self.roomID,
text: text,
Expand Down
8 changes: 4 additions & 4 deletions Sources/AblyChat/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ public struct Message: Sendable, Codable, Identifiable, Equatable {
public var id: String { serial }

public var serial: String
public var latestAction: MessageAction
public var action: MessageAction
public var clientID: String
public var roomID: String
public var text: String
public var createdAt: Date?
public var metadata: MessageMetadata
public var headers: MessageHeaders

public init(serial: String, latestAction: MessageAction, clientID: String, roomID: String, text: String, createdAt: Date?, metadata: MessageMetadata, headers: MessageHeaders) {
public init(serial: String, action: MessageAction, clientID: String, roomID: String, text: String, createdAt: Date?, metadata: MessageMetadata, headers: MessageHeaders) {
self.serial = serial
self.latestAction = latestAction
self.action = action
self.clientID = clientID
self.roomID = roomID
self.text = text
Expand All @@ -30,7 +30,7 @@ public struct Message: Sendable, Codable, Identifiable, Equatable {

internal enum CodingKeys: String, CodingKey {
case serial
case latestAction
case action
case clientID = "clientId"
case roomID = "roomId"
case text
Expand Down
6 changes: 3 additions & 3 deletions Tests/AblyChatTests/ChatAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct ChatAPITests {
// Then
let expectedMessage = Message(
serial: "3446456",
latestAction: .create,
action: .create,
clientID: "mockClientId",
roomID: roomId,
text: "hello",
Expand Down Expand Up @@ -91,7 +91,7 @@ struct ChatAPITests {
items: [
Message(
serial: "3446456",
latestAction: .create,
action: .create,
clientID: "random",
roomID: roomId,
text: "hello",
Expand All @@ -101,7 +101,7 @@ struct ChatAPITests {
),
Message(
serial: "3446457",
latestAction: .create,
action: .create,
clientID: "random",
roomID: roomId,
text: "hello response",
Expand Down
31 changes: 30 additions & 1 deletion Tests/AblyChatTests/IntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,36 @@ struct IntegrationTests {
#expect(rxMessageFromSubscription == txMessageAfterRxSubscribe)

// (7) Fetch historical messages from before subscribing, and check we get txMessageBeforeRxSubscribe
let rxMessagesBeforeSubscribing = try await rxMessageSubscription.getPreviousMessages(params: .init())

/*
TODO: This line should just be

let messages = try await rxMessageSubscription.getPreviousMessages(params: .init())

but sometimes `messages.items` is coming back empty. Andy said in
https://ably-real-time.slack.com/archives/C03JDBVM5MY/p1733220395208909
that

> new materialised history system doesn’t currently support “live”
> history (realtime implementation detail) - so we’re approximating the
> behaviour

and indicated that the right workaround for now is to introduce a
wait. So we retry the fetching of history until we get a non-empty
result.

Revert this (https://github.com/ably/ably-chat-swift/issues/175) once it’s fixed in Realtime.
*/
let rxMessagesBeforeSubscribing = try await {
while true {
let messages = try await rxMessageSubscription.getPreviousMessages(params: .init())
if !messages.items.isEmpty {
return messages
}
// Wait 1 second before retrying the history fetch
try await Task.sleep(nanoseconds: NSEC_PER_SEC)
}
}()
try #require(rxMessagesBeforeSubscribing.items.count == 1)
#expect(rxMessagesBeforeSubscribing.items[0] == txMessageBeforeRxSubscribe)

Expand Down
2 changes: 1 addition & 1 deletion Tests/AblyChatTests/MessageSubscriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private final class MockPaginatedResult<T: Equatable>: PaginatedResult {

struct MessageSubscriptionTests {
let messages = ["First", "Second"].map { text in
Message(serial: "", latestAction: .create, clientID: "", roomID: "", text: text, createdAt: .init(), metadata: [:], headers: [:])
Message(serial: "", action: .create, clientID: "", roomID: "", text: text, createdAt: .init(), metadata: [:], headers: [:])
}

@Test
Expand Down
8 changes: 4 additions & 4 deletions Tests/AblyChatTests/MessageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Testing
struct MessageTests {
let earlierMessage = Message(
serial: "ABC123@1631840000000-5:2",
latestAction: .create,
action: .create,
clientID: "testClientID",
roomID: "roomId",
text: "hello",
Expand All @@ -15,7 +15,7 @@ struct MessageTests {

let laterMessage = Message(
serial: "ABC123@1631840000001-5:2",
latestAction: .create,
action: .create,
clientID: "testClientID",
roomID: "roomId",
text: "hello",
Expand All @@ -26,7 +26,7 @@ struct MessageTests {

let invalidMessage = Message(
serial: "invalid",
latestAction: .create,
action: .create,
clientID: "testClientID",
roomID: "roomId",
text: "hello",
Expand Down Expand Up @@ -70,7 +70,7 @@ struct MessageTests {
func isEqual_whenMessageIsEqual_ReturnsTrue() async throws {
let duplicateOfEarlierMessage = Message(
serial: "ABC123@1631840000000-5:2",
latestAction: .create,
action: .create,
clientID: "random",
roomID: "",
text: "",
Expand Down
4 changes: 2 additions & 2 deletions Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ extension MockHTTPPaginatedResponse {
[
"clientId": "random",
"serial": "3446456",
"latestAction": "message.create",
"action": "message.create",
"createdAt": 1_730_943_049_269,
"roomId": "basketball::$chat::$chatMessages",
"text": "hello",
Expand All @@ -109,7 +109,7 @@ extension MockHTTPPaginatedResponse {
[
"clientId": "random",
"serial": "3446457",
"latestAction": "message.create",
"action": "message.create",
"roomId": "basketball::$chat::$chatMessages",
"text": "hello response",
"metadata": [:],
Expand Down
Loading