diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescription.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescription.kt index 3d734d97..7bc487a1 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescription.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorDescription.kt @@ -16,8 +16,10 @@ package dev.usbharu.hideout.core.domain.model.actor -@JvmInline -value class ActorDescription(val description: String) { + +class ActorDescription(description: String) { + val description: String = description.take(length) + companion object { val length = 10000 val empty = ActorDescription("") diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorName.kt index d65cd986..6819fc8e 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorName.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorName.kt @@ -18,8 +18,14 @@ package dev.usbharu.hideout.core.domain.model.actor @JvmInline value class ActorName(val name: String) { + init { + require(name.isNotBlank()) + require(name.length <= length) + require(regex.matches(name)) + } companion object { val length = 300 + private val regex = Regex("^[a-zA-Z0-9_-]{1,$length}\$") } } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPostsCount.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPostsCount.kt index e24819e4..8c344c05 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPostsCount.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorPostsCount.kt @@ -22,9 +22,6 @@ value class ActorPostsCount(val postsCount: Int) { require(0 <= this.postsCount) { "Posts count must be greater than 0" } } - operator fun inc() = ActorPostsCount(postsCount + 1) - operator fun dec() = ActorPostsCount(postsCount - 1) - companion object { val ZERO = ActorPostsCount(0) } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorRelationshipCount.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorRelationshipCount.kt index 7543996d..e21b75f4 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorRelationshipCount.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorRelationshipCount.kt @@ -21,7 +21,4 @@ value class ActorRelationshipCount(val relationshipCount: Int) { init { require(0 <= relationshipCount) { "Followers count must be > 0" } } - - operator fun inc(): ActorRelationshipCount = ActorRelationshipCount(relationshipCount + 1) - operator fun dec(): ActorRelationshipCount = ActorRelationshipCount(relationshipCount - 1) } diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorScreenName.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorScreenName.kt index 343e8f8a..4f7a8aed 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorScreenName.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorScreenName.kt @@ -16,8 +16,11 @@ package dev.usbharu.hideout.core.domain.model.actor -@JvmInline -value class ActorScreenName(val screenName: String) { + +class ActorScreenName(screenName: String) { + + val screenName: String = screenName.take(length) + companion object { val length = 300 val empty = ActorScreenName("") diff --git a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/shared/Domain.kt b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/shared/Domain.kt index 7ac333af..963c2574 100644 --- a/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/shared/Domain.kt +++ b/hideout-core/src/main/kotlin/dev/usbharu/hideout/core/domain/model/shared/Domain.kt @@ -18,6 +18,10 @@ package dev.usbharu.hideout.core.domain.model.shared @JvmInline value class Domain(val domain: String) { + init { + require(domain.length <= length) + } + companion object { val length = 1000 } diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorNameTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorNameTest.kt new file mode 100644 index 00000000..477f799d --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorNameTest.kt @@ -0,0 +1,35 @@ +package dev.usbharu.hideout.core.domain.model.actor + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertDoesNotThrow +import org.junit.jupiter.api.assertThrows + +class ActorNameTest { + @Test + fun blankはダメ() { + assertThrows { + ActorName("") + } + } + + @Test + fun 長過ぎるとダメ() { + assertThrows { + ActorName("a".repeat(1000)) + } + } + + @Test + fun 指定外の文字は使えない() { + assertThrows { + ActorName("あ") + } + } + + @Test + fun 普通に作成できる() { + assertDoesNotThrow { + ActorName("test-user") + } + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorScreenNameTest.kt b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorScreenNameTest.kt index febff754..163dbaf3 100644 --- a/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorScreenNameTest.kt +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/actor/ActorScreenNameTest.kt @@ -1,5 +1,13 @@ package dev.usbharu.hideout.core.domain.model.actor -class ActorScreenNameTest { +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +class ActorScreenNameTest { + @Test + fun screenNameがlengthを超えると無視される() { + val actorScreenName = ActorScreenName("a".repeat(1000)) + + assertEquals(ActorScreenName.length, actorScreenName.screenName.length) + } } \ No newline at end of file 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 3a65d48f..d35fb0f8 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 @@ -1,9 +1,53 @@ package dev.usbharu.hideout.core.domain.model.actor +import dev.usbharu.hideout.core.domain.event.actor.ActorEvent import org.junit.jupiter.api.Test import org.junit.jupiter.api.assertThrows +import utils.AssertDomainEvent.assertContainsEvent +import utils.AssertDomainEvent.assertEmpty +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNull class ActorsTest { + @Test + fun suspendがtrueのときactorSuspendイベントが発生する() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + + actor.suspend = true + + assertContainsEvent(actor, ActorEvent.actorSuspend.eventName) + } + + @Test + fun suspendがfalseになったときactorUnsuspendイベントが発生する() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), suspend = true) + + actor.suspend = false + + assertContainsEvent(actor, ActorEvent.actorUnsuspend.eventName) + } + + @Test + fun alsoKnownAsに自分自身が含まれない場合更新される() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + + val actorIds = setOf(ActorId(100), ActorId(200)) + actor.alsoKnownAs = actorIds + + assertEquals(actorIds, actor.alsoKnownAs) + } + + @Test + fun moveToに自分自身が設定された場合moveイベントが発生し更新される() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + + + actor.moveTo = ActorId(100) + + assertContainsEvent(actor, ActorEvent.move.eventName) + } + @Test fun alsoKnownAsに自分自身が含まれてはいけない() { val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) @@ -22,5 +66,65 @@ class ActorsTest { } } + @Test + fun descriptionが更新されたときupdateイベントが発生する() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + actor.description = ActorDescription("hoge fuga") + + assertContainsEvent(actor, ActorEvent.update.eventName) + } + + @Test + fun screenNameが更新されたときupdateイベントが発生する() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + + actor.screenName = ActorScreenName("fuga hoge") + + assertContainsEvent(actor, ActorEvent.update.eventName) + } + + @Test + fun deleteが実行されたときすでにdeletedがtrueなら何もしない() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), deleted = true) + + actor.delete() + + assertEmpty(actor) + } + + @Test + fun deleteが実行されたときdeletedがfalseならdeleteイベントが発生する() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + + actor.delete() + + assertEquals(ActorScreenName.empty, actor.screenName) + assertEquals(ActorDescription.empty, actor.description) + assertEquals(emptySet(), actor.emojis) + assertNull(actor.lastPostAt) + assertEquals(ActorPostsCount.ZERO, actor.postsCount) + assertNull(actor.followersCount) + assertNull(actor.followingCount) + assertContainsEvent(actor, ActorEvent.delete.eventName) + } + + @Test + fun restoreが実行されたときcheckUpdateイベントが発生する() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey(""), deleted = true) + + actor.restore() + + assertFalse(actor.deleted) + assertContainsEvent(actor, ActorEvent.checkUpdate.eventName) + } + + @Test + fun checkUpdateが実行されたときcheckUpdateイベントがh() { + val actor = TestActor2Factory.create(publicKey = ActorPublicKey("")) + + actor.checkUpdate() + + assertContainsEvent(actor, ActorEvent.checkUpdate.eventName) + } } \ No newline at end of file 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/TestActor2Factory.kt index 220b4056..217fb36b 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/TestActor2Factory.kt @@ -54,8 +54,8 @@ object TestActor2Factory { keyId = ActorKeyId(keyId), followersEndpoint = followersEndpoint, followingEndpoint = followingEndpoint, - InstanceId(instanceId), - locked, + instance = InstanceId(instanceId), + locked = locked, followersCount = ActorRelationshipCount(followersCount), followingCount = ActorRelationshipCount(followingCount), postsCount = ActorPostsCount(postCount), 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 new file mode 100644 index 00000000..b14d9bbe --- /dev/null +++ b/hideout-core/src/test/kotlin/dev/usbharu/hideout/core/domain/model/post/PostTest.kt @@ -0,0 +1,10 @@ +package dev.usbharu.hideout.core.domain.model.post + +import org.junit.jupiter.api.Test + +class PostTest { + @Test + fun deletedがtrueのときghostのidが返される() { + + } +} \ No newline at end of file diff --git a/hideout-core/src/test/kotlin/utils/AssertDomainEvent.kt b/hideout-core/src/test/kotlin/utils/AssertDomainEvent.kt new file mode 100644 index 00000000..d1d2c049 --- /dev/null +++ b/hideout-core/src/test/kotlin/utils/AssertDomainEvent.kt @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2024 usbharu + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package utils + +import dev.usbharu.hideout.core.domain.shared.domainevent.DomainEventStorable + +object AssertDomainEvent { + fun assertContainsEvent(domainEventStorable: DomainEventStorable, eventName: String) { + val find = domainEventStorable.getDomainEvents().find { it.name == eventName } + + if (find == null) { + throw AssertionError("Domain Event not found: $eventName") + } + } + + fun assertEmpty(domainEventStorable: DomainEventStorable) { + if (domainEventStorable.getDomainEvents().isNotEmpty()) { + throw AssertionError("Domain Event found") + } + } +} \ No newline at end of file