diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt index cca33575..389aa714 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/post/Post.kt @@ -184,6 +184,7 @@ class Post( } fun restore(content: PostContent, overview: PostOverview?, mediaIds: List) { + require(deleted) deleted = false this.content = content this.overview = overview diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt index 8487e528..7c7be023 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt @@ -6,6 +6,7 @@ import dev.usbharu.hideout.core.domain.model.actor.ActorPublicKey import dev.usbharu.hideout.core.domain.model.actor.TestActorFactory import dev.usbharu.hideout.core.domain.model.emoji.EmojiId import dev.usbharu.hideout.core.domain.model.media.MediaId +import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertDoesNotThrow import org.junit.jupiter.api.assertThrows @@ -14,6 +15,7 @@ import utils.AssertDomainEvent.assertEmpty import java.net.URI import java.time.Instant import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertNull class PostTest { @@ -550,4 +552,136 @@ class PostTest { assertEquals(mediaIds, post.mediaIds) } + + @Test + fun `delete deleteイベントが発生する`() { + + val actor = TestActorFactory.create() + val post = TestPostFactory.create(deleted = false, actorId = actor.id.id) + post.delete(actor) + assertContainsEvent(post, PostEvent.DELETE.eventName) + } + + @Test + fun `delete すでにdeletedがtrueの時deleteイベントは発生しない`() { + + val actor = TestActorFactory.create() + val post = TestPostFactory.create(deleted = true, actorId = actor.id.id) + post.delete(actor) + assertEmpty(post) + } + + @Test + fun `delete contentがemptyにoverviewがnullにmediaIdsがemptyになる`() { + val actor = TestActorFactory.create() + val post = TestPostFactory.create(deleted = false, actorId = actor.id.id) + post.delete(actor) + assertEquals(PostContent.empty, post.content) + assertNull(post.overview) + assertEquals(emptyList(), post.mediaIds) + assertTrue(post.deleted) + } + + @Test + fun `checkUpdate CHECKUPDATEイベントが発生する`() { + val post = TestPostFactory.create() + + post.checkUpdate() + + assertContainsEvent(post, PostEvent.CHECK_UPDATE.eventName) + } + + @Test + fun `restore 指定された引数で再構成されCHECKUPDATEイベントが発生する`() { + val post = TestPostFactory.create(deleted = true) + + val postContent = PostContent("aiueo", "aiueo", listOf(EmojiId(1))) + val overview = PostOverview("overview") + val mediaIds = listOf(MediaId(1)) + post.restore( + postContent, + overview, + mediaIds + ) + + assertContainsEvent(post, PostEvent.CHECK_UPDATE.eventName) + assertEquals(postContent, post.content) + assertEquals(overview, post.overview) + assertEquals(mediaIds, post.mediaIds) + } + + @Test + fun deletedがfalseの時失敗する() { + val post = TestPostFactory.create(deleted = false) + + val postContent = PostContent("aiueo", "aiueo", listOf(EmojiId(1))) + val overview = PostOverview("overview") + val mediaIds = listOf(MediaId(1)) + assertThrows { + post.restore( + postContent, + overview, + mediaIds + ) + } + } + + @Test + fun `addMediaIds deletedがtrueの時失敗する`() { + val post = TestPostFactory.create(deleted = true) + val actor = TestActorFactory.create(id = post.actorId.id) + + assertThrows { + post.addMediaIds(listOf(MediaId(1)), actor) + } + } + + @Test + fun `addMediaIds updateイベントが発生する`() { + val post = TestPostFactory.create(deleted = false) + val actor = TestActorFactory.create(id = post.actorId.id) + + post.addMediaIds(listOf(MediaId(2)), actor) + assertContainsEvent(post, PostEvent.UPDATE.eventName) + } + + @Test + fun `hide hideがtrueになる`() { + val post = TestPostFactory.create(hide = false) + + post.hide() + + assertTrue(post.hide) + } + + @Test + fun `show hideがfalseになる`() { + val post = TestPostFactory.create(hide = true) + + post.show() + + assertFalse(post.hide) + } + + @Test + fun `moveTo すでに設定されている場合は失敗する`() { + val post = TestPostFactory.create(moveTo = 100) + val actor = TestActorFactory.create(post.actorId.id) + + assertThrows { + post.moveTo(PostId(2), actor) + } + } + + @Test + fun `moveTo moveToが設定される`() { + val post = TestPostFactory.create(moveTo = null) + val actor = TestActorFactory.create(post.actorId.id) + + assertDoesNotThrow { + post.moveTo(PostId(2), actor) + } + + assertEquals(PostId(2), post.moveTo) + } }