Skip to content
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
9 changes: 7 additions & 2 deletions TypeTypeIOS/Features/Watch/Models/LiveChatBuffer.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import Foundation

// What the chat column shows: the newest messages and where the connection stands. Replayed messages (a
// reconnect may deliver the last batch again) are shown once.
// What the chat column shows: the newest messages and where the connection stands.
// The server numbers the messages of each connection from chat-1 and starts a new one with the chat's recent
// backlog, so a new connection starts the list over (the web client does the same): ids only identify a message
// within one connection, and keeping the old list would show the backlog twice.
struct LiveChatBuffer: Equatable {
enum Status: Equatable {
case connecting
Expand All @@ -23,6 +25,9 @@ struct LiveChatBuffer: Equatable {
for event in events {
switch event {
case .connected:
messages.removeAll()
remembered.removeAll()
rememberedSet.removeAll()
if status == .connecting { status = .connected }
case let .message(message):
append(message)
Expand Down
13 changes: 11 additions & 2 deletions TypeTypeIOSTests/Features/Watch/LiveChatBufferTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ final class LiveChatBufferTests: XCTestCase {
XCTAssertEqual(buffer.messages.last?.id, "45")
}

func testAReplayedMessageShowsOnce() {
func testAMessageSentTwiceInOneConnectionShowsOnce() {
var buffer = LiveChatBuffer()
buffer.apply([.connected, message("1"), message("2")])
buffer.apply([.connected, message("2"), message("3")])
buffer.apply([message("2"), message("3")])
XCTAssertEqual(buffer.messages.map(\.id), ["1", "2", "3"])
}

// Real server: every connection numbers its messages chat-1, chat-2, ... again.
func testANewConnectionStartsTheListOver() {
var buffer = LiveChatBuffer()
buffer.apply([.connected, message("chat-1"), message("chat-2")])
buffer.apply([.connected, message("chat-1"), message("chat-2"), message("chat-3")])
XCTAssertEqual(buffer.messages.map(\.id), ["chat-1", "chat-2", "chat-3"])
XCTAssertEqual(buffer.messages.count, 3, "the backlog of the new connection is not shown twice")
}

func testAnOldIdIsForgottenAfterManyMessages() {
var buffer = LiveChatBuffer()
buffer.apply((1...(LiveChatBuffer.rememberedIDs + 5)).map { message("\($0)") })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ final class LiveChatEventDecoderTests: XCTestCase {
XCTAssertEqual(event, .message(LiveChatMessage(id: "chat-1", author: "Mika", text: "Hello", moderator: true)))
}

// A YouTube live message as the server sends it: no moderator field, an avatar address.
func testReadsARealServerMessage() throws {
let event = try decode(
"message",
"{\"id\":\"chat-1\",\"text\":\"sei sette\",\"receivedAtMs\":1793994518651,\"authorName\":\"@Viewer\",\"authorAvatarUrl\":\"https://avatar.test/viewer=s64-c-k-c0x00ffffff-no-rj\"}"
)
XCTAssertEqual(event, .message(LiveChatMessage(id: "chat-1", author: "@Viewer", text: "sei sette", moderator: false)))
}

func testTheOptionalFieldsMayBeMissing() throws {
let event = try decode("message", "{\"id\":\"chat-2\",\"text\":\"Hi\",\"receivedAtMs\":1}")
XCTAssertEqual(event, .message(LiveChatMessage(id: "chat-2", author: "", text: "Hi", moderator: false)))
Expand Down