package cn.novalon.manage.file.core.service.impl;

import cn.novalon.manage.file.core.domain.SysFile;
import cn.novalon.manage.file.core.repository.ISysFileRepository;
import cn.novalon.manage.file.core.service.ISysFileService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class SysFileServiceTest {

    @Mock
    private ISysFileRepository fileRepository;

    private ISysFileService fileService;

    private SysFile testFile;

    @BeforeEach
    void setUp() {
        fileService = new SysFileServiceImpl(fileRepository, "/tmp/uploads");
        testFile = new SysFile();
        testFile.setId(1L);
        testFile.setFileName("test.txt");
        testFile.setFilePath("/tmp/uploads/test.txt");
        testFile.setFileType("text/plain");
        testFile.setFileSize(1024L);
        testFile.setCreateBy("testuser");
        testFile.setStorageType("LOCAL");
    }

    @Test
    void testGetAllFiles_Success() {
        when(fileRepository.findByDeletedAtIsNullOrderByCreatedAtDesc()).thenReturn(Flux.just(testFile));
        
        Flux<SysFile> result = fileService.getAllFiles();
        
        StepVerifier.create(result)
                .expectNext(testFile)
                .verifyComplete();
        
        verify(fileRepository).findByDeletedAtIsNullOrderByCreatedAtDesc();
    }

    @Test
    void testGetFileById_Success() {
        when(fileRepository.findById(1L)).thenReturn(Mono.just(testFile));
        
        Mono<SysFile> result = fileService.getFileById(1L);
        
        StepVerifier.create(result)
                .expectNext(testFile)
                .verifyComplete();
        
        verify(fileRepository).findById(1L);
    }

    @Test
    void testGetFileById_NotFound() {
        when(fileRepository.findById(999L)).thenReturn(Mono.empty());
        
        Mono<SysFile> result = fileService.getFileById(999L);
        
        StepVerifier.create(result)
                .verifyComplete();
        
        verify(fileRepository).findById(999L);
    }

    @Test
    void testDeleteFile_NotFound() {
        when(fileRepository.findById(999L)).thenReturn(Mono.empty());
        
        Mono<Void> result = fileService.deleteFile(999L);
        
        StepVerifier.create(result)
                .verifyComplete();
        
        verify(fileRepository).findById(999L);
        verify(fileRepository, never()).deleteByIdAndDeletedAtIsNull(any());
    }
}
