diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5f020d54..634c5da2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,10 +33,6 @@ jobs: --health-timeout 5s --health-retries 5 - defaults: - run: - working-directory: core - env: DB_HOST: 127.0.0.1 DB_PORT: 5432 @@ -62,4 +58,11 @@ jobs: gradle-version: "9.4.1" - name: Run tests - run: gradle clean test -x :db:test --no-daemon + run: gradle :core:clean :worker:clean :core:test :worker:test --no-daemon + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v5 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: core/build/reports/jacoco/test/jacocoTestReport.xml,worker/build/reports/jacoco/test/jacocoTestReport.xml + fail_ci_if_error: false diff --git a/core/build.gradle b/core/build.gradle index af3fffd7..567b3df9 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java' + id 'jacoco' id 'org.springframework.boot' version '4.0.5' id 'io.spring.dependency-management' version '1.1.7' } @@ -56,4 +57,21 @@ dependencies { tasks.named('test') { useJUnitPlatform() + finalizedBy jacocoTestReport +} + +jacocoTestReport { + dependsOn test + reports { + xml.required = true + html.required = true + } + classDirectories.setFrom( + files(classDirectories.files.collect { + fileTree(dir: it, include: [ + '**/facade/**', + '**/service/**', + ]) + }) + ) } diff --git a/worker/build.gradle b/worker/build.gradle index 51bce6a6..dbab1e38 100644 --- a/worker/build.gradle +++ b/worker/build.gradle @@ -1,5 +1,6 @@ plugins { id 'java' + id 'jacoco' id 'org.springframework.boot' version '4.0.5' id 'io.spring.dependency-management' version '1.1.7' } @@ -56,4 +57,22 @@ dependencies { tasks.named('test') { useJUnitPlatform() + finalizedBy jacocoTestReport +} + +jacocoTestReport { + dependsOn test + reports { + xml.required = true + html.required = true + } + classDirectories.setFrom( + files(classDirectories.files.collect { + fileTree(dir: it, include: [ + '**/facade/**', + '**/service/**', + '**/handler/**', + ]) + }) + ) } diff --git a/worker/src/test/java/com/mailsangja/worker/service/mail/GmailNewMessageApplyCommandServiceTest.java b/worker/src/test/java/com/mailsangja/worker/service/mail/GmailNewMessageApplyCommandServiceTest.java index e5819d30..7e0ad0de 100644 --- a/worker/src/test/java/com/mailsangja/worker/service/mail/GmailNewMessageApplyCommandServiceTest.java +++ b/worker/src/test/java/com/mailsangja/worker/service/mail/GmailNewMessageApplyCommandServiceTest.java @@ -19,9 +19,11 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDateTime; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -95,7 +97,16 @@ void applyNewMessageSync_whenThreadIsDeleted_restoresOnlyThreadAndInsertsNewMess .thenReturn(Optional.of(oldMessage)); when(messageRepositoryPort.findByThreadIdAndGmailMessageId(deletedThread.getId(), "message-new")) .thenReturn(Optional.empty()); - when(messageRepositoryPort.save(any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0)); + AtomicReference savedNewMessage = new AtomicReference<>(); + when(messageRepositoryPort.save(any(Message.class))).thenAnswer(invocation -> { + Message m = invocation.getArgument(0); + savedNewMessage.set(m); + return m; + }); + when(messageRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccount.getId(), "thread-1")) + .thenAnswer(invocation -> savedNewMessage.get() != null ? List.of(savedNewMessage.get()) : List.of()); + when(threadRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccount.getId(), "thread-1")) + .thenReturn(List.of(deletedThread)); GmailHistoryEvent event = new GmailHistoryEvent( GmailHistoryEventType.MESSAGE_ADDED, @@ -115,7 +126,7 @@ void applyNewMessageSync_whenThreadIsDeleted_restoresOnlyThreadAndInsertsNewMess assertFalse(deletedThread.isDeleted()); assertTrue(oldMessage.isDeleted()); assertEquals("message-new", messageCaptor.getValue().getGmailMessageId()); - assertEquals(2, deletedThread.getMessageCount()); + assertEquals(1, deletedThread.getMessageCount()); assertEquals("new subject", deletedThread.getLatestSubject()); assertEquals("new snippet", deletedThread.getLatestSnippet()); assertEquals(LocalDateTime.of(2026, 4, 14, 10, 0), deletedThread.getLastMessageAt()); @@ -150,7 +161,20 @@ void applyNewMessageSync_whenThreadIsNotDeleted_insertsNewMessageAndUpdatesThrea .thenReturn(Optional.of(existingMessage)); when(messageRepositoryPort.findByThreadIdAndGmailMessageId(activeThread.getId(), "message-new")) .thenReturn(Optional.empty()); - when(messageRepositoryPort.save(any(Message.class))).thenAnswer(invocation -> invocation.getArgument(0)); + AtomicReference savedNewMessage = new AtomicReference<>(); + when(messageRepositoryPort.save(any(Message.class))).thenAnswer(invocation -> { + Message m = invocation.getArgument(0); + savedNewMessage.set(m); + return m; + }); + when(messageRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccount.getId(), "thread-2")) + .thenAnswer(invocation -> { + List msgs = new ArrayList<>(List.of(existingMessage)); + if (savedNewMessage.get() != null) msgs.add(savedNewMessage.get()); + return msgs; + }); + when(threadRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccount.getId(), "thread-2")) + .thenReturn(List.of(activeThread)); GmailHistoryEvent event = new GmailHistoryEvent( GmailHistoryEventType.MESSAGE_ADDED, diff --git a/worker/src/test/java/com/mailsangja/worker/service/mail/InitialMailSyncCommandServiceTest.java b/worker/src/test/java/com/mailsangja/worker/service/mail/InitialMailSyncCommandServiceTest.java index f0128232..53bdc8db 100644 --- a/worker/src/test/java/com/mailsangja/worker/service/mail/InitialMailSyncCommandServiceTest.java +++ b/worker/src/test/java/com/mailsangja/worker/service/mail/InitialMailSyncCommandServiceTest.java @@ -64,6 +64,10 @@ void saveThreadBatch_doesNotIncreaseMessageCountForUpdatedMessages() { storedMessage.set(message); return message; }); + when(messageRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccount.getId(), "thread-1")) + .thenAnswer(invocation -> storedMessage.get() != null ? List.of(storedMessage.get()) : List.of()); + when(threadRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccount.getId(), "thread-1")) + .thenReturn(List.of(thread)); service.saveThreadBatch(mailAccount, java.util.List.of(new InitialMailSyncThreadSaveCommand( "thread-1", @@ -132,6 +136,7 @@ void saveThreadBatch_doesNotReplaceLatestMessageWhenSentAtIsNull() { MailAccount mailAccount = createMailAccount(); Thread thread = createThread(mailAccount, "thread-1", Direction.INBOUND); AtomicReference firstMessage = new AtomicReference<>(); + List activeMessages = new ArrayList<>(); when(threadRepositoryPort.findByMailAccountIdAndGmailThreadIdAndDirectionAndDeletedAtIsNull( mailAccount.getId(), "thread-1", Direction.INBOUND @@ -145,8 +150,13 @@ void saveThreadBatch_doesNotReplaceLatestMessageWhenSentAtIsNull() { if ("message-1".equals(message.getGmailMessageId())) { firstMessage.set(message); } + activeMessages.add(message); return message; }); + when(messageRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccount.getId(), "thread-1")) + .thenAnswer(invocation -> List.copyOf(activeMessages)); + when(threadRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccount.getId(), "thread-1")) + .thenReturn(List.of(thread)); service.saveThreadBatch(mailAccount, java.util.List.of(new InitialMailSyncThreadSaveCommand( "thread-1", diff --git a/worker/src/test/java/com/mailsangja/worker/service/mail/ReplyDraftSuggestionQueryServiceTest.java b/worker/src/test/java/com/mailsangja/worker/service/mail/ReplyDraftSuggestionQueryServiceTest.java index 5f1b367d..1ca65f2e 100644 --- a/worker/src/test/java/com/mailsangja/worker/service/mail/ReplyDraftSuggestionQueryServiceTest.java +++ b/worker/src/test/java/com/mailsangja/worker/service/mail/ReplyDraftSuggestionQueryServiceTest.java @@ -287,8 +287,8 @@ class ReplyDraftSuggestionQueryServiceTest { LocalDateTime.now() ); ReplyDraftSuggestionQueryService service = createService(); - when(messageRepositoryPort.findByIdIncludingDeleted(latestMessageId)).thenReturn(Optional.of(latestMessage)); - when(messageRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNull(mailAccountId, "gmail-thread-1")) + when(messageRepositoryPort.findByIdIncludingDeletedAndSensitiveLabelsExcluded(latestMessageId)).thenReturn(Optional.of(latestMessage)); + when(messageRepositoryPort.findAllByMailAccountIdAndGmailThreadIdAndDeletedAtIsNullAndSensitiveLabelsExcluded(mailAccountId, "gmail-thread-1")) .thenReturn(List.of(latestMessage)); when(referenceQueryPort.findWrittenMessagesByHints(any(), any(), any(), any(Integer.class))).thenReturn(List.of()); when(referenceQueryPort.findRecentWrittenMessages(userId, mailAccountId, 4)).thenReturn(List.of());