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 f1846382..90cbcb2c 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 @@ -54,6 +54,7 @@ class Post( var visibility = visibility set(value) { + require(visibility != Visibility.DIRECT) require(value != Visibility.DIRECT) require(field.ordinal >= value.ordinal) diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorsTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorsTest.kt index d35fb0f8..a0127a35 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorsTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorsTest.kt @@ -12,7 +12,7 @@ import kotlin.test.assertNull class ActorsTest { @Test fun suspendがtrueのときactorSuspendイベントが発生する() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) actor.suspend = true @@ -21,7 +21,7 @@ class ActorsTest { @Test fun suspendがfalseになったときactorUnsuspendイベントが発生する() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), suspend = true) + val actor = TestActorFactory.create(publicKey = ActorPublicKey(""), suspend = true) actor.suspend = false @@ -30,7 +30,7 @@ class ActorsTest { @Test fun alsoKnownAsに自分自身が含まれない場合更新される() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) val actorIds = setOf(ActorId(100), ActorId(200)) actor.alsoKnownAs = actorIds @@ -40,7 +40,7 @@ class ActorsTest { @Test fun moveToに自分自身が設定された場合moveイベントが発生し更新される() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) actor.moveTo = ActorId(100) @@ -50,7 +50,7 @@ class ActorsTest { @Test fun alsoKnownAsに自分自身が含まれてはいけない() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) assertThrows { actor.alsoKnownAs = setOf(actor.id) @@ -59,7 +59,7 @@ class ActorsTest { @Test fun moveToに自分自身が設定されてはいけない() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) assertThrows { actor.moveTo = actor.id @@ -68,7 +68,7 @@ class ActorsTest { @Test fun descriptionが更新されたときupdateイベントが発生する() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) actor.description = ActorDescription("hoge fuga") @@ -77,7 +77,7 @@ class ActorsTest { @Test fun screenNameが更新されたときupdateイベントが発生する() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) actor.screenName = ActorScreenName("fuga hoge") @@ -86,7 +86,7 @@ class ActorsTest { @Test fun deleteが実行されたときすでにdeletedがtrueなら何もしない() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), deleted = true) + val actor = TestActorFactory.create(publicKey = ActorPublicKey(""), deleted = true) actor.delete() @@ -95,7 +95,7 @@ class ActorsTest { @Test fun deleteが実行されたときdeletedがfalseならdeleteイベントが発生する() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) actor.delete() @@ -111,7 +111,7 @@ class ActorsTest { @Test fun restoreが実行されたときcheckUpdateイベントが発生する() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), deleted = true) + val actor = TestActorFactory.create(publicKey = ActorPublicKey(""), deleted = true) actor.restore() @@ -121,7 +121,7 @@ class ActorsTest { @Test fun checkUpdateが実行されたときcheckUpdateイベントがh() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) actor.checkUpdate() diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActor2Factory.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActorFactory.kt similarity index 99% rename from hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActor2Factory.kt rename to hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActorFactory.kt index fa0662c9..c0919feb 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActor2Factory.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/TestActorFactory.kt @@ -8,7 +8,7 @@ import kotlinx.coroutines.runBlocking import java.net.URI import java.time.Instant -object TestActor2Factory { +object TestActorFactory { private val idGenerateService = TwitterSnowflakeIdGenerateService fun create( 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 b14d9bbe..83c632ad 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 @@ -1,10 +1,145 @@ package dev.usbharu.hideout.core.domain.model.post +import dev.usbharu.hideout.core.domain.event.post.PostEvent +import dev.usbharu.hideout.core.domain.model.actor.ActorId import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow +import org.junit.jupiter.api.assertThrows +import utils.AssertDomainEvent.assertContainsEvent +import utils.AssertDomainEvent.assertEmpty +import kotlin.test.assertEquals class PostTest { @Test fun deletedがtrueのときghostのidが返される() { + val post = TestPostFactory.create(deleted = true) + + assertEquals(ActorId.ghost, post.actorId) + } + + @Test + fun deletedがfalseの時actorのIDが返される() { + val post = TestPostFactory.create(deleted = false, actorId = 100) + + assertEquals(ActorId(100), post.actorId) + } + + @Test + fun visibilityがDIRECTのとき変更できない() { + val post = TestPostFactory.create(visibility = Visibility.DIRECT) + + assertThrows { + post.visibility = Visibility.PUBLIC + } + assertThrows { + post.visibility = Visibility.UNLISTED + } + assertThrows { + post.visibility = Visibility.FOLLOWERS + } + } + + @Test + fun visibilityを小さくすることはできないPUBLIC() { + val post = TestPostFactory.create(visibility = Visibility.PUBLIC) + + assertThrows { + post.visibility = Visibility.DIRECT + } + assertThrows { + post.visibility = Visibility.UNLISTED + } + assertThrows { + post.visibility = Visibility.FOLLOWERS + } + } + + @Test + fun visibilityを小さくすることはできないUNLISTED() { + val post = TestPostFactory.create(visibility = Visibility.UNLISTED) + + assertThrows { + post.visibility = Visibility.DIRECT + } + assertThrows { + post.visibility = Visibility.FOLLOWERS + } + } + + @Test + fun visibilityを小さくすることはできないFOLLOWERS() { + val post = TestPostFactory.create(visibility = Visibility.FOLLOWERS) + + assertThrows { + post.visibility = Visibility.DIRECT + } + } + + @Test + fun visibilityをDIRECTにあとからすることはできない() { + val post = TestPostFactory.create(visibility = Visibility.DIRECT) + + assertThrows { + post.visibility = Visibility.DIRECT + } + } + + @Test + fun visibilityを大きくすることができるFOLLOWERS() { + val post = TestPostFactory.create(visibility = Visibility.FOLLOWERS) + + assertDoesNotThrow { + post.visibility = Visibility.UNLISTED + } + + val post2 = TestPostFactory.create(visibility = Visibility.FOLLOWERS) + + assertDoesNotThrow { + post2.visibility = Visibility.PUBLIC + } + } + + @Test + fun visibilityを大きくすることができるUNLISTED() { + val post = TestPostFactory.create(visibility = Visibility.UNLISTED) + + assertDoesNotThrow { + post.visibility = Visibility.PUBLIC + } + } + + @Test + fun deletedがtrueのときvisibilityを変更できない() { + val post = TestPostFactory.create(visibility = Visibility.UNLISTED, deleted = true) + + assertThrows { + post.visibility = Visibility.PUBLIC + } + } + + @Test + fun visibilityが変更されない限りドメインイベントは発生しない() { + val post = TestPostFactory.create(visibility = Visibility.UNLISTED) + + post.visibility = Visibility.UNLISTED + assertEmpty(post) } + + @Test + fun visibilityが変更されるとupdateイベントが発生する() { + val post = TestPostFactory.create(visibility = Visibility.UNLISTED) + post.visibility = Visibility.PUBLIC + + assertContainsEvent(post, PostEvent.update.eventName) + } + + @Test + fun deletedがtrueのときvisibleActorsを変更できない() { + val post = TestPostFactory.create(deleted = true) + + assertThrows { + post.visibleActors = listOf(ActorId(100)) + } + } } \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/TestPostFactory.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/TestPostFactory.kt new file mode 100644 index 00000000..e9dcc61e --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/TestPostFactory.kt @@ -0,0 +1,54 @@ +package dev.usbharu.hideout.core.domain.model.post + +import dev.usbharu.hideout.core.domain.model.actor.ActorId +import dev.usbharu.hideout.core.domain.model.media.MediaId +import dev.usbharu.hideout.core.infrastructure.other.TwitterSnowflakeIdGenerateService +import kotlinx.coroutines.runBlocking +import java.net.URI +import java.time.Instant + +object TestPostFactory { + private val idGenerateService = TwitterSnowflakeIdGenerateService + + fun create( + id: Long = generateId(), + actorId: Long = 1, + overview: String? = null, + content: String = "This is test content", + createdAt: Instant = Instant.now(), + visibility: Visibility = Visibility.PUBLIC, + url: URI = URI.create("https://example.com/$actorId/posts/$id"), + repostId: Long? = null, + replyId: Long? = null, + sensitive: Boolean = false, + apId: URI = URI.create("https://example.com/$actorId/posts/$id"), + deleted: Boolean = false, + mediaIds: List = emptyList(), + visibleActors: List = emptyList(), + hide: Boolean = false, + moveTo: Long? = null, + ): Post { + return Post( + PostId(id), + ActorId(actorId), + overview = overview?.let { PostOverview(it) }, + content = PostContent(content, content, emptyList()), + createdAt = createdAt, + visibility = visibility, + url = url, + repostId = repostId?.let { PostId(it) }, + replyId?.let { PostId(it) }, + sensitive = sensitive, + apId = apId, + deleted = deleted, + mediaIds.map { MediaId(it) }, + visibleActors.map { ActorId(it) }, + hide = hide, + moveTo?.let { PostId(it) } + ) + } + + private fun generateId(): Long = runBlocking { + idGenerateService.generateId() + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/actor/RemoteActorCheckDomainServiceTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/actor/RemoteActorCheckDomainServiceTest.kt index a1185837..0b7acd6d 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/actor/RemoteActorCheckDomainServiceTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/service/actor/RemoteActorCheckDomainServiceTest.kt @@ -2,7 +2,7 @@ package dev.usbharu.hideout.core.domain.service.actor import dev.usbharu.hideout.core.config.ApplicationConfig import dev.usbharu.hideout.core.domain.model.actor.ActorPublicKey -import dev.usbharu.hideout.core.domain.model.actor.TestActor2Factory +import dev.usbharu.hideout.core.domain.model.actor.TestActorFactory import org.junit.jupiter.api.Test import java.net.URI import kotlin.test.assertFalse @@ -11,7 +11,7 @@ import kotlin.test.assertTrue class RemoteActorCheckDomainServiceTest { @Test fun リモートのドメインならtrueを返す() { - val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(publicKey = ActorPublicKey("")) val remoteActor = RemoteActorCheckDomainService( ApplicationConfig( @@ -26,7 +26,7 @@ class RemoteActorCheckDomainServiceTest { @Test fun ローカルのActorならfalseを返す() { - val actor = TestActor2Factory.create(domain = "local.example.com", publicKey = ActorPublicKey("")) + val actor = TestActorFactory.create(domain = "local.example.com", publicKey = ActorPublicKey("")) val localActor = RemoteActorCheckDomainService( ApplicationConfig(