Files
nahodka-back/src/test/java/com/krylov/refound/service/OrphanFileCleanupServiceTest.java
SlimusMinus 871b16a038 Add video attachments to chat messages
- browser uploads the source straight to MinIO via presigned PUT (type and
  exact size are signed, up to 100 MB); upload is confirmed once by its owner
- message is created with a PROCESSING video, transcoded in the background
  (chatVideoExecutor): H.264/AAC, short side up to 720px, faststart,
  metadata (incl. location) stripped, poster frame; up to 60 s
- the updated message is re-broadcast over WebSocket when READY or FAILED;
  stuck processing is failed after 30 min
- message_attachments: status, poster_key, duration_ms, processing_started_at
- chat deletion and orphan cleanup handle video posters; rate limit chat-video

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:00:11 +03:00

77 lines
3.9 KiB
Java

package com.krylov.refound.service;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.krylov.refound.config.MinioProperties;
import com.krylov.refound.repository.ImageRepository;
import com.krylov.refound.repository.MessageAttachmentRepository;
import com.krylov.refound.repository.UserRepository;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Response;
import software.amazon.awssdk.services.s3.model.S3Object;
import software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable;
class OrphanFileCleanupServiceTest {
@Test
void deletesOnlyOldUnreferencedFiles() {
S3Client s3 = mock(S3Client.class);
Instant old = Instant.now().minus(Duration.ofDays(3));
ListObjectsV2Response page = ListObjectsV2Response.builder().contents(
S3Object.builder().key("used.jpg").lastModified(old).build(),
S3Object.builder().key("avatars/legacy.jpg").lastModified(old).build(),
S3Object.builder().key("orphan.jpg").lastModified(old).build(),
S3Object.builder().key("chat/5/photo.jpg").lastModified(old).build(),
S3Object.builder().key("chat/5/video-poster.jpg").lastModified(old).build(),
S3Object.builder().key("chat/5/deleted-chat.jpg").lastModified(old).build(),
S3Object.builder().key("fresh.jpg").lastModified(Instant.now()).build()).build();
when(s3.listObjectsV2Paginator(any(ListObjectsV2Request.class)))
.thenReturn(new ListObjectsV2Iterable(s3, ListObjectsV2Request.builder().build()));
when(s3.listObjectsV2(any(ListObjectsV2Request.class))).thenReturn(page);
MinioProperties properties = new MinioProperties("http://minio", "refound-images", "a", "b", "us-east-1");
ImageRepository images = mock(ImageRepository.class);
when(images.findAllUrls()).thenReturn(List.of("used.jpg"));
UserRepository users = mock(UserRepository.class);
// Старая запись аватара хранит полный URL
when(users.findAllAvatarUrls()).thenReturn(List.of("http://minio:9010/refound-images/avatars/legacy.jpg?x=1"));
MessageAttachmentRepository attachments = mock(MessageAttachmentRepository.class);
when(attachments.findAllObjectKeys()).thenReturn(List.of("chat/5/photo.jpg"));
when(attachments.findAllPosterKeys()).thenReturn(List.of("chat/5/video-poster.jpg"));
FileStorageService storage = mock(FileStorageService.class);
new OrphanFileCleanupService(s3, properties, images, users, attachments, storage, true, Duration.ofHours(24), 100)
.cleanupOrphanFiles();
verify(storage).deleteFile("orphan.jpg");
verify(storage, never()).deleteFile("used.jpg");
verify(storage, never()).deleteFile("avatars/legacy.jpg");
verify(storage, never()).deleteFile("fresh.jpg");
// Фото чата живёт, пока на него ссылается вложение; файл удалённого чата — сирота
verify(storage, never()).deleteFile("chat/5/photo.jpg");
verify(storage, never()).deleteFile("chat/5/video-poster.jpg");
verify(storage).deleteFile("chat/5/deleted-chat.jpg");
}
@Test
void disabledJobDoesNothing() {
S3Client s3 = mock(S3Client.class);
FileStorageService storage = mock(FileStorageService.class);
new OrphanFileCleanupService(s3, mock(MinioProperties.class), mock(ImageRepository.class),
mock(UserRepository.class), mock(MessageAttachmentRepository.class), storage, false, Duration.ofHours(24), 100).cleanupOrphanFiles();
verify(storage, never()).deleteFile(anyString());
}
}