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 sections migration on retain onEnd event #1209

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,32 @@ extension SecureConversations.TranscriptModel {
func migrate(
from chatModel: ChatViewModel
) {
sections = chatModel.sections
clearSections(sections)
// Filter out items that are not reflected in transcript
let items = chatModel.sections.flatMap(\.items).filter {
switch $0.kind {
case .callUpgrade,
.operatorConnected,
.queueOperator,
.transferring,
.unreadMessageDivider:
return false
case .outgoingMessage,
.visitorMessage,
.choiceCard,
.customCard,
.gvaGallery,
.gvaPersistentButton,
.gvaQuickReply,
.gvaResponseText,
.operatorMessage,
.systemMessage:
return true
}
}
historySection.set(items)
action?(.refreshAll)

// There's a possibility where migration to SC (this actually doesn't seem to work ATM, needs checking (MOB-3988)).
// happens when there are awaiting uploads.
// For that case we need to make sure that theses uploads
Expand Down Expand Up @@ -862,4 +887,8 @@ extension SecureConversations.TranscriptModel {
// we skip chat transcript loading.
start(isTranscriptFetchNeeded: false)
}

func clearSections(_ sections: [Section<ChatItem>]) {
sections.forEach { $0.set([]) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,38 @@ final class ChatCoordinatorTests: XCTestCase {
return
}

// Place some visitor message into chat model to
// Place some messages into chat model to
// make sure that all section are migrated from chat
// to transcript model.
let visitorMessage = ChatItem(kind: .mock(kind: .visitorMessage))
chatModel.pendingSection.append(visitorMessage)

let operatorMessage = ChatItem(kind: .mock(kind: .operatorMessage))
chatModel.messagesSection.append(operatorMessage)
let choiceCard = ChatItem(kind: .mock(kind: .choiceCard))
chatModel.messagesSection.append(choiceCard)
let customCard = ChatItem(kind: .mock(kind: .customCard))
chatModel.messagesSection.append(customCard)
let gvaGallery = ChatItem(kind: .mock(kind: .gvaGallery))
chatModel.messagesSection.append(gvaGallery)
let gvaPersistentButton = ChatItem(kind: .mock(kind: .gvaPersistentButton))
chatModel.messagesSection.append(gvaPersistentButton)
let gvaQuickReply = ChatItem(kind: .mock(kind: .gvaQuickReply))
chatModel.messagesSection.append(gvaQuickReply)
let gvaResponseText = ChatItem(kind: .mock(kind: .gvaResponseText))
chatModel.messagesSection.append(gvaResponseText)
let outgoingMessage = ChatItem(kind: .mock(kind: .outgoingMessage))
chatModel.messagesSection.append(outgoingMessage)
let systemMessage = ChatItem(kind: .mock(kind: .systemMessage))
chatModel.messagesSection.append(systemMessage)

// Append items that will not be migrated
chatModel.queueOperatorSection.append(.init(kind: .mock(kind: .operatorConnected)))
chatModel.messagesSection.append(.init(kind: .mock(kind: .callUpgrade)))
chatModel.messagesSection.append(.init(kind: .mock(kind: .queueOperator)))
chatModel.messagesSection.append(.init(kind: .mock(kind: .transferring)))
chatModel.messagesSection.append(.init(kind: .mock(kind: .unreadMessageDivider)))

chatModel.delegate?(.liveChatEngagementUpgradedToSecureMessaging(chatModel))

let transcriptModel: SecureConversations.TranscriptModel
Expand All @@ -463,14 +489,30 @@ final class ChatCoordinatorTests: XCTestCase {
// some extra checks are required to make sure
// that migration happens as expected.
XCTAssertFalse(chatModel.sections.isEmpty)
XCTAssertEqual(chatModel.pendingSection.itemCount, 1)
XCTAssertEqual(transcriptModel.pendingSection.itemCount, chatModel.pendingSection.itemCount)
for sectionIdx in chatModel.sections.indices {
XCTAssertTrue(transcriptModel.sections[sectionIdx] === transcriptModel.sections[sectionIdx])
for idx in chatModel.sections[sectionIdx].items.indices {
XCTAssertTrue(chatModel.sections[sectionIdx].items[idx] === transcriptModel.sections[sectionIdx].items[idx])
}

let expectedSectionItems = [
visitorMessage,
operatorMessage,
choiceCard,
customCard,
gvaGallery,
gvaPersistentButton,
gvaQuickReply,
gvaResponseText,
outgoingMessage,
systemMessage
]

// Check whether sections except historySection are empty.
for section in transcriptModel.sections where section !== transcriptModel.historySection {
XCTAssertTrue(section.items.isEmpty)
}

// Check whether historySection contains only items reflected in chat transcript.
for idx in transcriptModel.historySection.items.indices {
XCTAssertTrue(transcriptModel.historySection.items[idx] === expectedSectionItems[idx])
}

XCTAssertEqual(chatModel.isViewLoaded, transcriptModel.isViewLoaded)
}

Expand Down
Loading