more consistent view model display between switching chats

This commit is contained in:
2026-05-03 21:42:28 -07:00
parent 0c0226e37e
commit d1140d21d4
4 changed files with 540 additions and 87 deletions

View File

@@ -17,8 +17,11 @@ private actor MockSybilClient: SybilAPIClienting {
private let searchesResponse: [SearchSummary]
private let chatDetails: [String: ChatDetail]
private let searchDetails: [String: SearchDetail]
private let createChatResponse: ChatSummary?
private var snapshot = MockClientCallSnapshot()
private var getChatDelayNanoseconds: UInt64 = 0
private var getSearchDelayNanoseconds: UInt64 = 0
private var completionStreamNetworkErrorMessage: String?
private var completionStreamDelayNanoseconds: UInt64 = 0
private var searchStreamNetworkErrorMessage: String?
@@ -28,12 +31,14 @@ private actor MockSybilClient: SybilAPIClienting {
chatsResponse: [ChatSummary] = [],
searchesResponse: [SearchSummary] = [],
chatDetails: [String: ChatDetail] = [:],
searchDetails: [String: SearchDetail] = [:]
searchDetails: [String: SearchDetail] = [:],
createChatResponse: ChatSummary? = nil
) {
self.chatsResponse = chatsResponse
self.searchesResponse = searchesResponse
self.chatDetails = chatDetails
self.searchDetails = searchDetails
self.createChatResponse = createChatResponse
}
func currentSnapshot() -> MockClientCallSnapshot {
@@ -45,6 +50,14 @@ private actor MockSybilClient: SybilAPIClienting {
completionStreamDelayNanoseconds = delayNanoseconds
}
func setGetChatDelay(_ delayNanoseconds: UInt64) {
getChatDelayNanoseconds = delayNanoseconds
}
func setGetSearchDelay(_ delayNanoseconds: UInt64) {
getSearchDelayNanoseconds = delayNanoseconds
}
func setSearchStreamNetworkError(_ message: String, delayNanoseconds: UInt64 = 0) {
searchStreamNetworkErrorMessage = message
searchStreamDelayNanoseconds = delayNanoseconds
@@ -60,11 +73,17 @@ private actor MockSybilClient: SybilAPIClienting {
}
func createChat(title: String?) async throws -> ChatSummary {
if let createChatResponse {
return createChatResponse
}
throw UnexpectedClientCall()
}
func getChat(chatID: String) async throws -> ChatDetail {
snapshot.getChat += 1
if getChatDelayNanoseconds > 0 {
try await Task.sleep(nanoseconds: getChatDelayNanoseconds)
}
guard let detail = chatDetails[chatID] else {
throw UnexpectedClientCall()
}
@@ -90,6 +109,9 @@ private actor MockSybilClient: SybilAPIClienting {
func getSearch(searchID: String) async throws -> SearchDetail {
snapshot.getSearch += 1
if getSearchDelayNanoseconds > 0 {
try await Task.sleep(nanoseconds: getSearchDelayNanoseconds)
}
guard let detail = searchDetails[searchID] else {
throw UnexpectedClientCall()
}
@@ -293,6 +315,100 @@ private func makeSearchDetail(id: String, date: Date, answer: String) -> SearchD
#expect(viewModel.selectedSearch?.answerText == "fresh answer")
}
@MainActor
@Test func selectingChatClearsStaleTranscriptUntilNewDetailLoads() async throws {
let date = Date(timeIntervalSince1970: 1_700_000_210)
let staleDetail = makeChatDetail(id: "chat-old", date: date, body: "stale transcript")
let freshDetail = makeChatDetail(id: "chat-new", date: date, body: "fresh transcript")
let client = MockSybilClient(chatDetails: ["chat-new": freshDetail])
await client.setGetChatDelay(50_000_000)
let viewModel = SybilViewModel(settings: testSettings(named: #function)) { _ in client }
viewModel.isAuthenticated = true
viewModel.isCheckingSession = false
viewModel.selectedItem = .chat("chat-old")
viewModel.selectedChat = staleDetail
viewModel.select(.chat("chat-new"))
#expect(viewModel.displayedMessages.isEmpty)
#expect(viewModel.isLoadingSelection)
try await Task.sleep(nanoseconds: 90_000_000)
#expect(viewModel.displayedMessages.first?.content == "fresh transcript")
#expect(!viewModel.isLoadingSelection)
}
@MainActor
@Test func navigationSelectionWaitsForFastTranscriptLoad() async throws {
let date = Date(timeIntervalSince1970: 1_700_000_220)
let detail = makeChatDetail(id: "chat-fast", date: date, body: "loaded before push")
let client = MockSybilClient(chatDetails: ["chat-fast": detail])
await client.setGetChatDelay(20_000_000)
let viewModel = SybilViewModel(settings: testSettings(named: #function)) { _ in client }
viewModel.isAuthenticated = true
viewModel.isCheckingSession = false
await viewModel.selectForNavigation(.chat("chat-fast"), preloadTimeout: .milliseconds(500))
#expect(viewModel.selectedItem == .chat("chat-fast"))
#expect(viewModel.displayedMessages.first?.content == "loaded before push")
#expect(!viewModel.isLoadingSelection)
}
@MainActor
@Test func navigationSelectionTimesOutAndKeepsLoadingTranscript() async throws {
let date = Date(timeIntervalSince1970: 1_700_000_230)
let detail = makeChatDetail(id: "chat-slow", date: date, body: "loaded after push")
let client = MockSybilClient(chatDetails: ["chat-slow": detail])
await client.setGetChatDelay(100_000_000)
let viewModel = SybilViewModel(settings: testSettings(named: #function)) { _ in client }
viewModel.isAuthenticated = true
viewModel.isCheckingSession = false
await viewModel.selectForNavigation(.chat("chat-slow"), preloadTimeout: .milliseconds(10))
#expect(viewModel.selectedItem == .chat("chat-slow"))
#expect(viewModel.displayedMessages.isEmpty)
#expect(viewModel.isLoadingSelection)
try await Task.sleep(nanoseconds: 150_000_000)
#expect(viewModel.displayedMessages.first?.content == "loaded after push")
#expect(!viewModel.isLoadingSelection)
}
@MainActor
@Test func newDraftChatDoesNotShowTypingStateFromPreviousSend() async throws {
let date = Date(timeIntervalSince1970: 1_700_000_240)
let detail = makeChatDetail(id: "chat-typing", date: date, body: "existing transcript")
let client = MockSybilClient(chatDetails: ["chat-typing": detail])
await client.setCompletionStreamNetworkError(
"Network error -1005 while requesting POST: The network connection was lost.",
delayNanoseconds: 50_000_000
)
let viewModel = SybilViewModel(settings: testSettings(named: #function)) { _ in client }
viewModel.isAuthenticated = true
viewModel.isCheckingSession = false
viewModel.selectedItem = .chat("chat-typing")
viewModel.selectedChat = detail
viewModel.composer = "continue"
let sendTask = Task {
await viewModel.sendComposer()
}
try await Task.sleep(nanoseconds: 10_000_000)
#expect(viewModel.isSendingVisibleChat)
viewModel.startNewChat()
#expect(viewModel.displayedMessages.isEmpty)
#expect(!viewModel.isSendingVisibleChat)
await sendTask.value
}
@MainActor
@Test func backgroundChatStreamInterruptionIsSuppressedUntilForegroundRefresh() async throws {
let date = Date(timeIntervalSince1970: 1_700_000_300)