determineLatestMessagesPerThread currently has a subtle bug due to the fact that it only considers the last message index. (It iterates through all of them, but only the last one matters because it overwrites the results from earlier ones.)
latestMessage is getting set correctly in all cases, but the bug is that latestReadMessage can be wrong in some cases. In particular, if the most recent message doesn't set latestReadMessage, but an earlier one does, then the earlier one will be ignored and latestReadMessage will not be set.
This is a pretty extreme edge case because it only applies in the case that we have messages from different authors in the createMessages call, which I don't believe happens at any current callsites. But it still makes sense to fix the bug, and the issue becomes more relevant in future diffs where we introduce code to set latestMessageForUnreadCheck as well.
Depends on D14331