From a59cd590bcbf36b9d2f79971eda25d4da0e0f681 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Wed, 30 Jul 2025 20:20:56 -0400 Subject: [PATCH] Deletes these very confusing mocks into account viewmodels for the test --- .../loggedIn/home/ShortNotePostViewModel.kt | 12 +- .../ui/actions/NewPostViewModelTest.kt | 132 ------------------ 2 files changed, 7 insertions(+), 137 deletions(-) delete mode 100644 amethyst/src/test/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModelTest.kt diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt index 30d88edf5..5e1366a3b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/home/ShortNotePostViewModel.kt @@ -281,8 +281,10 @@ open class ShortNotePostViewModel : pTags = null } - canAddInvoice = accountViewModel.userProfile().info?.lnAddress() != null - canAddZapRaiser = accountViewModel.userProfile().info?.lnAddress() != null + val user = account.userProfile() + + canAddInvoice = user.info?.lnAddress() != null + canAddZapRaiser = user.info?.lnAddress() != null canUsePoll = originalNote == null multiOrchestrator = null @@ -290,12 +292,12 @@ open class ShortNotePostViewModel : message = TextFieldValue(message.text + "\nnostr:${quotedNote.toNEvent()}") quotedNote.author?.let { quotedUser -> - if (quotedUser.pubkeyHex != accountViewModel.userProfile().pubkeyHex) { + if (quotedUser.pubkeyHex != user.pubkeyHex) { if (forwardZapTo.value.items.none { it.key.pubkeyHex == quotedUser.pubkeyHex }) { forwardZapTo.value.addItem(quotedUser) } - if (forwardZapTo.value.items.none { it.key.pubkeyHex == accountViewModel.userProfile().pubkeyHex }) { - forwardZapTo.value.addItem(accountViewModel.userProfile()) + if (forwardZapTo.value.items.none { it.key.pubkeyHex == user.pubkeyHex }) { + forwardZapTo.value.addItem(user) } val pos = forwardZapTo.value.items.indexOfFirst { it.key.pubkeyHex == quotedUser.pubkeyHex } diff --git a/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModelTest.kt b/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModelTest.kt deleted file mode 100644 index 47e668662..000000000 --- a/amethyst/src/test/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModelTest.kt +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Copyright (c) 2025 Vitor Pamplona - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to use, - * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the - * Software, and to permit persons to whom the Software is furnished to do so, - * subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS - * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR - * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN - * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.vitorpamplona.amethyst.ui.actions - -import com.vitorpamplona.amethyst.model.Account -import com.vitorpamplona.amethyst.model.LocalCache -import com.vitorpamplona.amethyst.model.Note -import com.vitorpamplona.amethyst.model.User -import com.vitorpamplona.amethyst.model.nip30CustomEmojis.EmojiPackState.EmojiMedia -import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel -import com.vitorpamplona.amethyst.ui.screen.loggedIn.home.ShortNotePostViewModel -import com.vitorpamplona.quartz.nip01Core.core.HexKey -import com.vitorpamplona.quartz.nip01Core.tags.people.PTag -import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent -import io.mockk.MockKAnnotations -import io.mockk.every -import io.mockk.impl.annotations.MockK -import io.mockk.mockk -import io.mockk.mockkObject -import io.mockk.unmockkAll -import io.mockk.verify -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.flow.StateFlow -import kotlinx.coroutines.test.runTest -import org.junit.After -import org.junit.Before -import org.junit.Test - -@ExperimentalCoroutinesApi -class NewPostViewModelTest { - @MockK - lateinit var accountViewModel: AccountViewModel - - @MockK(relaxed = true) - lateinit var replyingTo: Note - - private lateinit var shortNoteViewModelUnderTest: ShortNotePostViewModel - - @Before - fun setup() { - mockkObject(LocalCache) - every { LocalCache.getOrCreateUser(any()) } returns mockk() - shortNoteViewModelUnderTest = ShortNotePostViewModel() - MockKAnnotations.init(this) - } - - @After - fun tearDown() { - unmockkAll() - } - - @Test - fun `test load with mentions`() = - runTest { - // Arrange: Setup with non empty mentions - every { accountViewModel.account } returns mockk() - - val textNoteEvent = mockk(relaxed = true) - every { textNoteEvent.mentions() } returns listOf(PTag("user1"), PTag("user2")) - every { replyingTo.event } returns textNoteEvent - - every { accountViewModel.userProfile() } returns mockk(relaxed = true) - every { accountViewModel.account.userProfile() } returns mockk(relaxed = true) - every { accountViewModel.account.emoji.myEmojis } returns mockk>>(relaxed = true) - - // Act: Call load with mentions - shortNoteViewModelUnderTest.init(accountViewModel) - shortNoteViewModelUnderTest.load(replyingTo, quote = null, fork = null, version = null, draft = null) - - // Assert - // Two mentions should call LocalCache.getOrCreateUser twice - verify(exactly = 2) { LocalCache.getOrCreateUser(any()) } - } - - @Test - fun `test load with zero mentions`() = - runTest { - every { accountViewModel.account } returns mockk() - - // Arrange: Setup Note with zero mentions - val textNoteEvent = mockk(relaxed = true) - every { textNoteEvent.mentions() } returns emptyList() - every { replyingTo.event } returns textNoteEvent - - every { accountViewModel.userProfile() } returns mockk(relaxed = true) - - // Act: Call load with empty mentions - shortNoteViewModelUnderTest.load(replyingTo, quote = null, fork = null, version = null, draft = null) - - // Assert - // With no mentions LocalCache.getOrCreateUser should not be called - verify(exactly = 0) { LocalCache.getOrCreateUser(any()) } - } - - @Test - fun `test load with empty mentions`() = - runTest { - every { accountViewModel.account } returns mockk() - - // Arrange: Setup empty mentions - val textNoteEvent = mockk(relaxed = true) - every { textNoteEvent.mentions() } returns emptyList() - every { replyingTo.event } returns textNoteEvent - - every { accountViewModel.userProfile() } returns mockk(relaxed = true) - - // Act: Call load with empty mentions - shortNoteViewModelUnderTest.load(replyingTo, quote = null, fork = null, version = null, draft = null) - - // Assert - // Verify LocalCache.getOrCreateUser(it) is not called with empty hex, it will crash the app - verify(exactly = 0) { LocalCache.getOrCreateUser(any()) } - } -}