From 7611ad379c8a3afed30c0a5d050c9c78f3c039c3 Mon Sep 17 00:00:00 2001 From: Corina Date: Wed, 1 Jul 2026 10:59:35 +0000 Subject: [PATCH 1/2] Add an endpoint for Bob --- .../main/kotlin/com/example/chatapp/data/RecipientsRepository.kt | 1 + .../main/kotlin/com/example/chatapp/data/RecipientsRepository.kt | 1 + 2 files changed, 2 insertions(+) diff --git a/ChatApp/app/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt b/ChatApp/app/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt index 224715d..d1e3375 100644 --- a/ChatApp/app/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt +++ b/ChatApp/app/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt @@ -39,6 +39,7 @@ class RecipientsRepository Recipient(id = "4", name = "David Wilson", email = "david@example.com"), Recipient(id = "5", name = "Eve Davis", email = "eve@example.com"), Recipient(id = "6", name = "Frank Miller", email = "frank@example.com"), + Recipient(id = "7", name = "Bob Johnson", email = "bob2@example.com"), ) private val groups = diff --git a/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt b/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt index 224715d..d1e3375 100644 --- a/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt +++ b/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt @@ -39,6 +39,7 @@ class RecipientsRepository Recipient(id = "4", name = "David Wilson", email = "david@example.com"), Recipient(id = "5", name = "Eve Davis", email = "eve@example.com"), Recipient(id = "6", name = "Frank Miller", email = "frank@example.com"), + Recipient(id = "7", name = "Bob Johnson", email = "bob2@example.com"), ) private val groups = From a96e78f85033ed8bcd9f4c936011712c08d74430 Mon Sep 17 00:00:00 2001 From: Corina Date: Thu, 2 Jul 2026 10:51:08 +0000 Subject: [PATCH 2/2] Repository reeturns all recipients with same name --- .../appfunctions/AppFunctionInstrumentationTest.kt | 2 +- .../example/chatapp/appfunctions/AppFunctionsTest.kt | 8 ++++++++ .../kotlin/com/example/chatapp/data/CallManager.kt | 4 +++- .../com/example/chatapp/data/RecipientsRepository.kt | 2 +- .../main/kotlin/com/example/chatapp/ChatViewModel.kt | 4 +++- .../com/example/chatapp/appfunctions/AppFunctions.kt | 12 ++++++++++-- .../kotlin/com/example/chatapp/data/CallManager.kt | 4 +++- .../com/example/chatapp/data/RecipientsRepository.kt | 2 +- 8 files changed, 30 insertions(+), 8 deletions(-) diff --git a/ChatApp/app/src/androidTest/kotlin/com/example/chatapp/appfunctions/AppFunctionInstrumentationTest.kt b/ChatApp/app/src/androidTest/kotlin/com/example/chatapp/appfunctions/AppFunctionInstrumentationTest.kt index c7f0011..3703558 100644 --- a/ChatApp/app/src/androidTest/kotlin/com/example/chatapp/appfunctions/AppFunctionInstrumentationTest.kt +++ b/ChatApp/app/src/androidTest/kotlin/com/example/chatapp/appfunctions/AppFunctionInstrumentationTest.kt @@ -95,7 +95,7 @@ class AppFunctionInstrumentationTest { .getAppFunctionData(ExecuteAppFunctionResponse.Success.PROPERTY_RETURN_VALUE) ?.getString("message"), ) - .isEqualTo("Message sent to recipient: Alice Smith.") + .isEqualTo("Message sent to: Alice Smith.") // Verify that the message was actually saved in the repository val messages = messageRepository.getMessages(testRecipient.id).first() assertThat(messages).isNotEmpty() diff --git a/ChatApp/app/src/androidTest/kotlin/com/example/chatapp/appfunctions/AppFunctionsTest.kt b/ChatApp/app/src/androidTest/kotlin/com/example/chatapp/appfunctions/AppFunctionsTest.kt index 03cf74d..11ce049 100644 --- a/ChatApp/app/src/androidTest/kotlin/com/example/chatapp/appfunctions/AppFunctionsTest.kt +++ b/ChatApp/app/src/androidTest/kotlin/com/example/chatapp/appfunctions/AppFunctionsTest.kt @@ -237,4 +237,12 @@ class AppFunctionsTest { appFunctions.makeCall(testContext, endpointValue = "nonexistent_id") } } + + @Test(expected = AppFunctionInvalidArgumentException::class) + fun makeCall_duplicateContactName_fails() { + runBlocking { + // "Bob Johnson" has two entries (ID 2 and ID 7) + appFunctions.makeCall(testContext, contactName = "Bob Johnson") + } + } } diff --git a/ChatApp/app/src/main/kotlin/com/example/chatapp/data/CallManager.kt b/ChatApp/app/src/main/kotlin/com/example/chatapp/data/CallManager.kt index 7c506c9..3b45185 100644 --- a/ChatApp/app/src/main/kotlin/com/example/chatapp/data/CallManager.kt +++ b/ChatApp/app/src/main/kotlin/com/example/chatapp/data/CallManager.kt @@ -41,7 +41,9 @@ class CallManager ?: recipientsRepository.getGroupById(recipientId)?.let { Recipient(it.id, it.name, "") } - ?: recipientsRepository.getRecipientByName(recipientId) + ?: recipientsRepository.getRecipientByName(recipientId).let { list -> + if (list.size == 1) list.first() else null + } ?: Recipient(recipientId, recipientId, "") startCall(recipient) } diff --git a/ChatApp/app/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt b/ChatApp/app/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt index d1e3375..fdd153e 100644 --- a/ChatApp/app/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt +++ b/ChatApp/app/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt @@ -160,7 +160,7 @@ class RecipientsRepository fun getRecipientById(id: String): Recipient? = recipients.singleOrNull { it.id == id } - fun getRecipientByName(name: String): Recipient? = recipients.singleOrNull { it.name == name } + fun getRecipientByName(name: String): List = recipients.filter { it.name == name } fun getGroupById(id: String): ChatGroup? = groups.singleOrNull { it.id == id } } diff --git a/ChatApp/shared/src/main/kotlin/com/example/chatapp/ChatViewModel.kt b/ChatApp/shared/src/main/kotlin/com/example/chatapp/ChatViewModel.kt index 5fdd7d7..85011c7 100644 --- a/ChatApp/shared/src/main/kotlin/com/example/chatapp/ChatViewModel.kt +++ b/ChatApp/shared/src/main/kotlin/com/example/chatapp/ChatViewModel.kt @@ -85,7 +85,9 @@ class ChatViewModel ?: recipientsRepository.getGroupById(recipientId)?.let { Recipient(it.id, it.name, "") } - ?: recipientsRepository.getRecipientByName(recipientId) + ?: recipientsRepository.getRecipientByName(recipientId).let { list -> + if (list.size == 1) list.first() else null + } ?: if (recipientId == "bot") { Recipient("bot", "Assistant", "bot@example.com") } else { diff --git a/ChatApp/shared/src/main/kotlin/com/example/chatapp/appfunctions/AppFunctions.kt b/ChatApp/shared/src/main/kotlin/com/example/chatapp/appfunctions/AppFunctions.kt index d1f79f8..8849f6f 100644 --- a/ChatApp/shared/src/main/kotlin/com/example/chatapp/appfunctions/AppFunctions.kt +++ b/ChatApp/shared/src/main/kotlin/com/example/chatapp/appfunctions/AppFunctions.kt @@ -158,10 +158,18 @@ class AppFunctions val recipient = with(recipientsRepository) { if (endpointValue == null) { - getRecipientByName(checkNotNull(contactName)) - ?: throw AppFunctionElementNotFoundException( + val matches = getRecipientByName(checkNotNull(contactName)) + if (matches.isEmpty()) { + throw AppFunctionElementNotFoundException( "No recipient exists for contactName: $contactName", ) + } + if (matches.size > 1) { + throw AppFunctionInvalidArgumentException( + "Multiple contacts found with name $contactName. Please be more specific.", + ) + } + matches.first() } else { getRecipientById(endpointValue) ?: throw AppFunctionElementNotFoundException( diff --git a/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/CallManager.kt b/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/CallManager.kt index 7c506c9..3b45185 100644 --- a/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/CallManager.kt +++ b/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/CallManager.kt @@ -41,7 +41,9 @@ class CallManager ?: recipientsRepository.getGroupById(recipientId)?.let { Recipient(it.id, it.name, "") } - ?: recipientsRepository.getRecipientByName(recipientId) + ?: recipientsRepository.getRecipientByName(recipientId).let { list -> + if (list.size == 1) list.first() else null + } ?: Recipient(recipientId, recipientId, "") startCall(recipient) } diff --git a/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt b/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt index d1e3375..fdd153e 100644 --- a/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt +++ b/ChatApp/shared/src/main/kotlin/com/example/chatapp/data/RecipientsRepository.kt @@ -160,7 +160,7 @@ class RecipientsRepository fun getRecipientById(id: String): Recipient? = recipients.singleOrNull { it.id == id } - fun getRecipientByName(name: String): Recipient? = recipients.singleOrNull { it.name == name } + fun getRecipientByName(name: String): List = recipients.filter { it.name == name } fun getGroupById(id: String): ChatGroup? = groups.singleOrNull { it.id == id } }