fix SQL handling of threads with null values + add test for it

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2025-07-04 12:43:36 +02:00
parent 43292943e7
commit ae2d3d2a76
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
2 changed files with 81 additions and 3 deletions

View File

@ -174,6 +174,84 @@ class ChatBlocksDaoTest {
assertEquals(5, results.first().size) assertEquals(5, results.first().size)
} }
@Test
fun testGetConnectedChatBlocksWithThreadsScenario() =
runTest {
val user = createUserEntity("account1", "Account 1")
usersDao.saveUser(user)
val account1 = usersDao.getUserWithUserId("account1").blockingGet()
conversationsDao.upsertConversations(
listOf(
createConversationEntity(
accountId = account1.id,
"abc",
roomName = "Conversation One"
),
createConversationEntity(
accountId = account1.id,
"def",
roomName = "Conversation Two"
)
)
)
val conversation1 = conversationsDao.getConversationsForUser(account1.id).first()[0]
val searchedChatBlock = ChatBlockEntity(
internalConversationId = conversation1.internalId,
accountId = conversation1.accountId,
token = conversation1.token,
threadId = 123,
oldestMessageId = 50,
newestMessageId = 60,
hasHistory = true
)
val chatBlockOverlap1 = ChatBlockEntity(
internalConversationId = conversation1.internalId,
accountId = conversation1.accountId,
token = conversation1.token,
threadId = null,
oldestMessageId = 45,
newestMessageId = 55,
hasHistory = true
)
val chatBlockOverlap2 = ChatBlockEntity(
internalConversationId = conversation1.internalId,
accountId = conversation1.accountId,
token = conversation1.token,
threadId = 123,
oldestMessageId = 59,
newestMessageId = 70,
hasHistory = true
)
chatBlocksDao.upsertChatBlock(searchedChatBlock)
chatBlocksDao.upsertChatBlock(chatBlockOverlap1)
chatBlocksDao.upsertChatBlock(chatBlockOverlap2)
val resultsForThreadIdNull = chatBlocksDao.getConnectedChatBlocks(
internalConversationId = conversation1.internalId,
threadId = null,
oldestMessageId = searchedChatBlock.oldestMessageId,
newestMessageId = searchedChatBlock.newestMessageId
)
assertEquals(1, resultsForThreadIdNull.first().size)
val resultsForThreadId123 = chatBlocksDao.getConnectedChatBlocks(
internalConversationId = conversation1.internalId,
threadId = 123,
oldestMessageId = searchedChatBlock.oldestMessageId,
newestMessageId = searchedChatBlock.newestMessageId
)
assertEquals(2, resultsForThreadId123.first().size)
}
private fun createUserEntity(userId: String, userName: String) = private fun createUserEntity(userId: String, userName: String) =
UserEntity( UserEntity(
userId = userId, userId = userId,

View File

@ -52,7 +52,7 @@ interface ChatBlocksDao {
SELECT * SELECT *
FROM ChatBlocks FROM ChatBlocks
WHERE internalConversationId in (:internalConversationId) WHERE internalConversationId in (:internalConversationId)
AND (:threadId IS NULL OR threadId = :threadId) AND (threadId = :threadId OR (threadId IS NULL AND :threadId IS NULL))
AND oldestMessageId <= :messageId AND oldestMessageId <= :messageId
AND newestMessageId >= :messageId AND newestMessageId >= :messageId
ORDER BY newestMessageId ASC ORDER BY newestMessageId ASC
@ -69,7 +69,7 @@ interface ChatBlocksDao {
SELECT * SELECT *
FROM ChatBlocks FROM ChatBlocks
WHERE internalConversationId = :internalConversationId WHERE internalConversationId = :internalConversationId
AND (:threadId IS NULL OR threadId = :threadId) AND (threadId = :threadId OR (threadId IS NULL AND :threadId IS NULL))
AND( AND(
(oldestMessageId <= :oldestMessageId AND newestMessageId >= :oldestMessageId) (oldestMessageId <= :oldestMessageId AND newestMessageId >= :oldestMessageId)
OR OR
@ -92,7 +92,7 @@ interface ChatBlocksDao {
SELECT MAX(newestMessageId) as max_items SELECT MAX(newestMessageId) as max_items
FROM ChatBlocks FROM ChatBlocks
WHERE internalConversationId = :internalConversationId WHERE internalConversationId = :internalConversationId
AND (:threadId IS NULL OR threadId = :threadId) AND (threadId = :threadId OR (threadId IS NULL AND :threadId IS NULL))
""" """
) )
fun getNewestMessageIdFromChatBlocks(internalConversationId: String, threadId: Long?): Long fun getNewestMessageIdFromChatBlocks(internalConversationId: String, threadId: Long?): Long