Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 0 additions & 9 deletions .github/workflows/android-releases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ on:
push:
branches: [main]

# Releases run one at a time and are never cancelled part way, so a release
# is not left created without its docs deployed
concurrency:
group: pages-${{ github.ref }}
cancel-in-progress: false
Expand Down Expand Up @@ -44,13 +42,6 @@ jobs:
- name: Tests
run: ./code/gradlew -p code test

- name: Upload test reports
if: failure()
uses: actions/upload-artifact@v7
with:
name: unit-test-reports
path: code/app/build/reports/tests/

- name: Build Release APK
run: ./code/gradlew -p code assembleRelease

Expand Down
8 changes: 1 addition & 7 deletions .github/workflows/pull-requests-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@ jobs:
- name: Tests
run: ./code/gradlew -p code test

- name: Upload test reports
if: failure()
uses: actions/upload-artifact@v7
with:
name: unit-test-reports
path: code/app/build/reports/tests/

- name: Build
run: ./code/gradlew -p code assembleDebug

Expand Down Expand Up @@ -92,6 +85,7 @@ jobs:
arch: x86_64
profile: pixel_7_pro
disable-animations: true
emulator-options: -no-window -gpu swangle_indirect -no-snapshot -noaudio -no-boot-anim
script: ./code/gradlew -p code connectedAndroidTest

- name: Upload UI test reports
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.lambda_bbb_events.controller
package com.example.lambda_bbb_events.controller.lottery

import com.example.lambda_bbb_events.model.lottery.Lottery
import com.example.lambda_bbb_events.model.lottery.LotteryStatus
Expand All @@ -18,6 +18,12 @@ import org.junit.Before
import org.junit.Test
import java.util.Date

/*
* Instrumented tests for LotteryController with its Android dependencies.
*
* Outstanding issues: None known.
*/

/** Verifies lottery controller workflows against their Android dependencies. */
class LotteryControllerTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.example.lambda_bbb_events.controller.messaging

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.lambda_bbb_events.controller.user.UserController
import com.example.lambda_bbb_events.model.message.ConversationType
import com.example.lambda_bbb_events.model.message.Message
import com.example.lambda_bbb_events.model.user.Entrant
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import com.example.lambda_bbb_events.support.FirestoreTestData
import com.example.lambda_bbb_events.support.OwnerMessagingFixture

/*
* Tests MessageController opening a chat, sending and deleting messages against the real
* Firestore database.
*
* Outstanding issues: Needs a network connection and the published Firestore rules. A test that
* stops before reading its data back can leave a "test-" document behind.
*/

/**
* Tests [MessageController] as a camper uses it to chat with a campsite owner: opening the
* conversation, seeing sent messages arrive live, and deleting one. Only the signed-in camper is
* faked; the conversation and its messages are in the real Firestore database.
*
* Everything written uses "test-" IDs and is deleted after each test in [cleanUp].
*
* Reference:
* Anthropic. (2026). Claude Code (Claude Opus 5.5) [Large language model].
* https://claude.com/claude-code
* Written with the help of this AI tool.
* Prompts:
* - "Okay implement the missing tests."
*/
@RunWith(AndroidJUnit4::class)
class MessageControllerFirestoreTest {
private val testData = FirestoreTestData()
private val fixture = OwnerMessagingFixture(testData)
private val users = mockk<UserController>()
private val controller = MessageController(users, fixture.conversations)

@Before
fun setUp() = runBlocking {
val camper = Entrant(deviceId = fixture.entrant, name = "Ed")
every { users.currentUser } returns camper
every { users.user } returns camper
fixture.grantAccess()
fixture.book()
testData.add("conversations/" + fixture.ownerConversationId())
}

@After
fun cleanUp() {
controller.closeConversation()
testData.cleanUp()
}

/** Waits until the open chat's messages satisfy [ready], and returns them. */
private suspend fun awaitMessages(ready: (List<Message>) -> Boolean): List<Message> =
withTimeout(15_000) {
while (!ready(controller.messages)) delay(100)
controller.messages
}

/** Opens the camper's chat with the owner and sends [content], marking it for cleanup. */
private suspend fun openAndSend(content: String): Message {
val id = controller.createConversation(fixture.owner, ConversationType.OWNER)
controller.openConversation(id)
controller.sendMessage(content)
val sent = awaitMessages { list -> list.any { it.content == content } }.first { it.content == content }
testData.add("conversations/$id/messages/${sent.messageId}")
return sent
}

@Test
fun camperOpensTheirChatWithTheOwner() = runBlocking {
val id = controller.createConversation(fixture.owner, ConversationType.OWNER)

assertEquals(fixture.ownerConversationId(), id)
assertEquals(fixture.owner, controller.getConversation(id)?.ownerId)
assertEquals(listOf(id), controller.getUserConversations().map { it.conversationId })
}

@Test
fun sentMessageAppearsInTheOpenChat() = runBlocking {
val sent = openAndSend("Is there firewood?")

assertEquals(fixture.entrant, sent.senderId)
assertEquals(fixture.owner, sent.recipientId)
assertTrue(controller.canDelete(sent))
}

@Test
fun newestMessageIsFirst() = runBlocking {
openAndSend("First")
openAndSend("Second")

val settled = awaitMessages { list -> list.map { it.content } == listOf("Second", "First") }

assertEquals(listOf("Second", "First"), settled.map { it.content })
}

@Test
fun deletedMessageShowsAsDeletedInTheOpenChat() = runBlocking {
val sent = openAndSend("Wrong campsite, sorry")

controller.deleteMessage(sent.messageId)

val deleted = awaitMessages { list -> list.any { it.messageId == sent.messageId && it.deleted } }
.first { it.messageId == sent.messageId }
assertEquals("", deleted.content)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package com.example.lambda_bbb_events.controller.messaging

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.lambda_bbb_events.controller.user.UserController
import com.example.lambda_bbb_events.model.admin.AdminLog
import com.example.lambda_bbb_events.model.lottery.WaitlistEntry
import com.example.lambda_bbb_events.model.lottery.WaitlistStatus
import com.example.lambda_bbb_events.model.repository.AdminLogRepository
import com.example.lambda_bbb_events.model.repository.MessageRepository
import com.example.lambda_bbb_events.model.user.CampsiteOwner
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.UUID
import com.example.lambda_bbb_events.support.FirestoreTestData
import com.example.lambda_bbb_events.support.OwnerMessagingFixture

/*
* Tests the whole Message Waitlist flow against the real Firestore database.
*
* Outstanding issues: Needs a network connection and the published Firestore rules. A test that
* stops before reading its data back can leave a "test-" document behind.
*/

/**
* Tests the whole Message Waitlist flow against the real Firestore database: an owner sends
* one message to their lottery's waitlist, it lands in each reachable entrant's chat, and
* one log listing only those entrants is saved for admins and for the owner's sent list.
*
* Only the signed-in owner is faked; every read and write goes to Firestore. Everything
* written uses "test-" IDs and is deleted after each test in [cleanUp].
*
* Reference:
* Anthropic. (2026). Claude Code (Claude Opus 5.5) [Large language model].
* https://claude.com/claude-code
* Written with the help of this AI tool.
* Prompts:
* - "For whatever you are not confident on write more tests."
*/
@RunWith(AndroidJUnit4::class)
class WaitlistMessageFirestoreTest {
private val testData = FirestoreTestData()
private val fixture = OwnerMessagingFixture(testData)
private val stranger = "test-entrant-" + UUID.randomUUID()
private val logs = AdminLogRepository()
private val users = mockk<UserController>()
private val sender = GroupMessageSender(users)
private val content = "Waitlist test " + UUID.randomUUID()

/** Waitlist entries for the camper who booked, and for [stranger], who never did. */
private val waitlist = listOf(
WaitlistEntry(entryId = "test-entry-1", entrantId = fixture.entrant, status = WaitlistStatus.IN_WAITLIST),
WaitlistEntry(entryId = "test-entry-2", entrantId = stranger, status = WaitlistStatus.IN_WAITLIST)
)

@Before
fun setUp() = runBlocking {
every { users.user } returns CampsiteOwner(deviceId = fixture.owner, name = "Test Owner")
coEvery { users.getUserName(fixture.entrant) } returns "Test Entrant"
fixture.grantAccess()
fixture.book()
testData.add("conversations/" + fixture.ownerConversationId())
}

@After
fun cleanUp() {
testData.cleanUp()
}

/**
* Waits for the owner's log about the test campsite, and marks it and the messages in the
* entrant's chat for cleanup.
*/
private suspend fun awaitLog(): AdminLog {
val log = withTimeout(15_000) {
logs.observeLogsSentBy(fixture.owner, fixture.campsite).first { it.isNotEmpty() }
}.single()
testData.add("notificationLogs/${log.id}")
chat().forEach { testData.add("conversations/${fixture.ownerConversationId()}/messages/${it.messageId}") }
return log
}

/** The messages in the entrant's chat with the owner. */
private suspend fun chat() = MessageRepository(fixture.ownerConversationId()).getAll()

/** Sends the test message to [waitlist]. */
private suspend fun send() =
sender.sendToWaitlist(waitlist, content, fixture.campsite, "Test campsite")

@Test
fun messageReachesOnlyEntrantsWhoCanBeMessaged() = runBlocking {
val result = send()
awaitLog()

assertEquals(1, result.sentCount)
assertEquals(listOf(stranger), result.failedEntrantIds)
}

@Test
fun messageLandsInTheEntrantsChatFromTheOwner() = runBlocking {
send()
awaitLog()

val chat = chat()

assertEquals(listOf(content), chat.map { it.content })
assertEquals(fixture.owner, chat.single().senderId)
assertEquals(fixture.entrant, chat.single().recipientId)
}

@Test
fun oneLogListsOnlyTheEntrantsReached() = runBlocking {
send()
val log = awaitLog()

assertEquals(content, log.content)
assertEquals(GroupMessageSender.LOG_TITLE, log.title)
assertEquals("Test Owner", log.senderName)
assertEquals(listOf(fixture.entrant), log.recipientIds)
assertEquals(mapOf(fixture.entrant to "Test Entrant"), log.recipientNames)
assertEquals("Test campsite", log.campsiteName)
}
}
Loading
Loading