forked from mtusi2026/mtusi2026lab2
Move lab1 to lab2
This commit is contained in:
+43
@@ -0,0 +1,43 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.chat
|
||||
|
||||
import com.mtusi.messenger.main.extensions.ErrorBody
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Chat
|
||||
import com.mtusi.messenger.main.support.createChat
|
||||
import com.mtusi.messenger.main.support.createUser
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class ChatControllerTest {
|
||||
|
||||
@Test
|
||||
fun `foreign chat returns 404 for non-member`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val bob = client.createUser("bob")
|
||||
val chat = client.createChat(alice, title = "secret", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.get("/api/v1/chats/${chat.id}") {
|
||||
header("X-User-Id", bob.id)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.NotFound, response.status)
|
||||
assertEquals("not_found", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `member can read own chat`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val created = client.createChat(alice, title = "notes", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.get("/api/v1/chats/${created.id}") {
|
||||
header("X-User-Id", alice.id)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
assertEquals(created.id, response.body<Chat>().id)
|
||||
}
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.health
|
||||
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class HealthControllerTest {
|
||||
|
||||
@Test
|
||||
fun `live returns 200`() = runApp { client ->
|
||||
val response = client.get("/health/live")
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `ready returns 200`() = runApp { client ->
|
||||
val response = client.get("/health/ready")
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
}
|
||||
}
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.message
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.message.responses.MessagePageResponse
|
||||
import com.mtusi.messenger.main.support.createChat
|
||||
import com.mtusi.messenger.main.support.createUser
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
class MessageConcurrencyTest {
|
||||
|
||||
@Test
|
||||
fun `parallel senders produce exactly N unique messages`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val chat = client.createChat(alice, title = "load", memberIds = listOf(alice.id))
|
||||
|
||||
val workers = 10
|
||||
val perWorker = 10
|
||||
val total = workers * perWorker
|
||||
|
||||
coroutineScope {
|
||||
(0 until workers).map { w ->
|
||||
async {
|
||||
repeat(perWorker) { i ->
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "w$w-i$i"))
|
||||
}
|
||||
assertEquals(HttpStatusCode.Created, response.status)
|
||||
}
|
||||
}
|
||||
}.awaitAll()
|
||||
}
|
||||
|
||||
val collected = mutableListOf<String>()
|
||||
var cursor: String? = null
|
||||
|
||||
do {
|
||||
val response = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
url {
|
||||
parameters.append("limit", "100")
|
||||
if (cursor != null) parameters.append("cursor", cursor!!)
|
||||
}
|
||||
}
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
|
||||
val page = response.body<MessagePageResponse>()
|
||||
collected += page.items.map { it.text }
|
||||
cursor = page.nextCursor
|
||||
} while (cursor != null)
|
||||
|
||||
assertEquals(total, collected.size)
|
||||
assertEquals(total, collected.toSet().size)
|
||||
|
||||
val expected = buildSet {
|
||||
for (w in 0 until workers) for (i in 0 until perWorker) add("w$w-i$i")
|
||||
}
|
||||
assertEquals(expected, collected.toSet())
|
||||
}
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.message
|
||||
|
||||
import com.mtusi.messenger.main.extensions.ErrorBody
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.message.requests.CreateMessageRequest
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.message.responses.MessagePageResponse
|
||||
import com.mtusi.messenger.main.support.createChat
|
||||
import com.mtusi.messenger.main.support.createUser
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
import kotlin.test.assertNull
|
||||
|
||||
class MessageControllerTest {
|
||||
|
||||
@Test
|
||||
fun `empty text returns 400 invalid_request`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = ""))
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status)
|
||||
assertEquals("invalid_request", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `foreign chat returns 404 when sending`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val bob = client.createUser("bob")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", bob.id)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "hi"))
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.NotFound, response.status)
|
||||
assertEquals("not_found", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `foreign chat returns 404 when listing`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val bob = client.createUser("bob")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", bob.id)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.NotFound, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `pagination walks all messages exactly once`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val total = 25
|
||||
repeat(total) { i ->
|
||||
val r = client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "m$i"))
|
||||
}
|
||||
assertEquals(HttpStatusCode.Created, r.status)
|
||||
}
|
||||
|
||||
val collected = mutableListOf<String>()
|
||||
var cursor: String? = null
|
||||
var pages = 0
|
||||
|
||||
do {
|
||||
val response = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
url {
|
||||
parameters.append("limit", "10")
|
||||
if (cursor != null) parameters.append("cursor", cursor!!)
|
||||
}
|
||||
}
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
|
||||
val page = response.body<MessagePageResponse>()
|
||||
collected += page.items.map { it.text }
|
||||
cursor = page.nextCursor
|
||||
pages++
|
||||
} while (cursor != null)
|
||||
|
||||
assertEquals(3, pages)
|
||||
assertEquals(total, collected.size)
|
||||
assertEquals(total, collected.toSet().size)
|
||||
assertEquals((0 until total).map { "m$it" }, collected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `history that is an exact multiple of limit ends with null next_cursor`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
repeat(20) { i ->
|
||||
client.post("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "m$i"))
|
||||
}
|
||||
}
|
||||
|
||||
val first = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
url { parameters.append("limit", "10") }
|
||||
}.body<MessagePageResponse>()
|
||||
|
||||
val cursor = first.nextCursor
|
||||
assertNotNull(cursor)
|
||||
|
||||
val second = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
url {
|
||||
parameters.append("limit", "10")
|
||||
parameters.append("cursor", cursor)
|
||||
}
|
||||
}.body<MessagePageResponse>()
|
||||
|
||||
assertEquals((10 until 20).map { "m$it" }, second.items.map { it.text })
|
||||
assertNull(second.nextCursor)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `last page has null next_cursor`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val chat = client.createChat(alice, title = "t", memberIds = listOf(alice.id))
|
||||
|
||||
val response = client.get("/api/v1/chats/${chat.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.OK, response.status)
|
||||
val page = response.body<MessagePageResponse>()
|
||||
assertEquals(0, page.items.size)
|
||||
assertNull(page.nextCursor)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `foreign cursor returns 400`() = runApp { client ->
|
||||
val alice = client.createUser("alice")
|
||||
val chatA = client.createChat(alice, title = "a", memberIds = listOf(alice.id))
|
||||
val chatB = client.createChat(alice, title = "b", memberIds = listOf(alice.id))
|
||||
|
||||
repeat(2) { i ->
|
||||
client.post("/api/v1/chats/${chatA.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateMessageRequest(text = "a$i"))
|
||||
}
|
||||
}
|
||||
|
||||
val firstA = client.get("/api/v1/chats/${chatA.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
url { parameters.append("limit", "1") }
|
||||
}.body<MessagePageResponse>()
|
||||
|
||||
val cursor = firstA.nextCursor
|
||||
assertNotNull(cursor)
|
||||
|
||||
val response = client.get("/api/v1/chats/${chatB.id}/messages") {
|
||||
header("X-User-Id", alice.id)
|
||||
url {
|
||||
parameters.append("limit", "1")
|
||||
parameters.append("cursor", cursor)
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status)
|
||||
assertEquals("invalid_request", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package com.mtusi.messenger.main.infrastructure.controllers.user
|
||||
|
||||
import com.mtusi.messenger.main.extensions.ErrorBody
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest
|
||||
import com.mtusi.messenger.main.support.createUser
|
||||
import com.mtusi.messenger.main.support.runApp
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotNull
|
||||
|
||||
class UserControllerTest {
|
||||
|
||||
@Test
|
||||
fun `create user returns 201 and echoes fields`() = runApp { client ->
|
||||
val response = client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateUserRequest(username = "alice", displayName = "Alice"))
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Created, response.status)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `invalid JSON body returns 400 invalid_request`() = runApp { client ->
|
||||
val response = client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody("{not json")
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.BadRequest, response.status)
|
||||
|
||||
val body = response.body<ErrorBody>()
|
||||
assertEquals("invalid_request", body.error.code)
|
||||
assertNotNull(body.error.requestId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `duplicate username returns 409 conflict`() = runApp { client ->
|
||||
client.createUser(username = "bob")
|
||||
|
||||
val response = client.post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateUserRequest(username = "bob", displayName = "Bob 2"))
|
||||
}
|
||||
|
||||
assertEquals(HttpStatusCode.Conflict, response.status)
|
||||
assertEquals("conflict", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `missing X-User-Id on protected endpoint returns 401`() = runApp { client ->
|
||||
val response = client.get("/api/v1/users/me")
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
assertEquals("unauthorized", response.body<ErrorBody>().error.code)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unknown X-User-Id returns 401`() = runApp { client ->
|
||||
val response = client.get("/api/v1/users/me") {
|
||||
header("X-User-Id", "00000000-0000-0000-0000-000000000000")
|
||||
}
|
||||
assertEquals(HttpStatusCode.Unauthorized, response.status)
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.mtusi.messenger.main.infrastructure.repositories.message
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Message
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
private const val CHAT_ID = "chat"
|
||||
|
||||
class InMemoryMessageRepositoryTest {
|
||||
|
||||
private fun repositoryWith(size: Int): InMemoryMessageRepository {
|
||||
|
||||
val repository = InMemoryMessageRepository()
|
||||
|
||||
repeat(size) { i ->
|
||||
repository.append(
|
||||
CHAT_ID,
|
||||
Message(
|
||||
id = "m$i",
|
||||
chatId = CHAT_ID,
|
||||
senderId = "u",
|
||||
text = "t$i",
|
||||
createdAt = "2026-01-01T00:00:00.000Z",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return repository
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `returns requested window in append order`() {
|
||||
val range = repositoryWith(10).findRangeByChatId(CHAT_ID, offset = 3, count = 4)
|
||||
assertEquals(listOf("m3", "m4", "m5", "m6"), range.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `window is clipped at the end of history`() {
|
||||
val range = repositoryWith(10).findRangeByChatId(CHAT_ID, offset = 8, count = 5)
|
||||
assertEquals(listOf("m8", "m9"), range.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `offset past the end returns empty`() {
|
||||
assertTrue(repositoryWith(10).findRangeByChatId(CHAT_ID, offset = 10, count = 5).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `huge offset does not overflow`() {
|
||||
assertTrue(repositoryWith(10).findRangeByChatId(CHAT_ID, offset = Int.MAX_VALUE - 1, count = 51).isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `unknown chat returns empty`() {
|
||||
assertTrue(InMemoryMessageRepository().findRangeByChatId("missing", offset = 0, count = 50).isEmpty())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.mtusi.messenger.main.support
|
||||
|
||||
import com.mtusi.messenger.main.infrastructure.dto.Chat
|
||||
import com.mtusi.messenger.main.infrastructure.dto.User
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.chat.requests.CreateChatRequest
|
||||
import com.mtusi.messenger.main.infrastructure.controllers.user.requests.CreateUserRequest
|
||||
import com.mtusi.messenger.main.module
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.body
|
||||
import io.ktor.client.plugins.contentnegotiation.ContentNegotiation
|
||||
import io.ktor.client.request.header
|
||||
import io.ktor.client.request.post
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.ContentType
|
||||
import io.ktor.http.HttpHeaders
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.contentType
|
||||
import io.ktor.serialization.kotlinx.json.json
|
||||
import io.ktor.server.testing.ApplicationTestBuilder
|
||||
import io.ktor.server.testing.testApplication
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonNamingStrategy
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
val testJson = Json {
|
||||
ignoreUnknownKeys = true
|
||||
encodeDefaults = true
|
||||
namingStrategy = JsonNamingStrategy.SnakeCase
|
||||
}
|
||||
|
||||
fun runApp(block: suspend ApplicationTestBuilder.(HttpClient) -> Unit) = testApplication {
|
||||
application {
|
||||
module()
|
||||
}
|
||||
val client = createClient {
|
||||
install(ContentNegotiation) {
|
||||
json(testJson)
|
||||
}
|
||||
}
|
||||
block(client)
|
||||
}
|
||||
|
||||
suspend fun HttpClient.createUser(username: String, displayName: String = username): User {
|
||||
val response = post("/api/v1/users") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateUserRequest(username = username, displayName = displayName))
|
||||
}
|
||||
check(response.status == HttpStatusCode.Created) { "createUser failed: ${response.status}" }
|
||||
return response.body()
|
||||
}
|
||||
|
||||
suspend fun HttpClient.createChat(
|
||||
creator: User,
|
||||
title: String,
|
||||
memberIds: List<String>,
|
||||
): Chat {
|
||||
val response = post("/api/v1/chats") {
|
||||
header("X-User-Id", creator.id)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(CreateChatRequest(title = title, memberIds = memberIds))
|
||||
}
|
||||
check(response.status == HttpStatusCode.Created) { "createChat failed: ${response.status}" }
|
||||
return response.body()
|
||||
}
|
||||
|
||||
fun HttpResponse.header(name: String): String? = headers[name]
|
||||
|
||||
val HttpResponse.requestId: String?
|
||||
get() = headers[HttpHeaders.XRequestId]
|
||||
Reference in New Issue
Block a user