package com.njcn.rdms.module.system.controller.admin.file;

import com.njcn.rdms.framework.test.core.ut.BaseMockitoUnitTest;
import com.njcn.rdms.module.system.dal.dataobject.file.FileDO;
import com.njcn.rdms.module.system.service.file.FileService;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.mock.web.MockHttpServletResponse;

import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class FileControllerTest extends BaseMockitoUnitTest {

    @Mock
    private FileService fileService;

    @InjectMocks
    private FileController fileController;

    @Test
    void downloadFile_shouldReadContentByFileIdAndWriteAttachment() throws Exception {
        Long fileId = 10001L;
        FileDO file = new FileDO()
                .setId(fileId)
                .setConfigId(100L)
                .setPath("task/20260509/report_1778307118261.txt")
                .setName("report.txt");
        byte[] content = "hello".getBytes(StandardCharsets.UTF_8);
        when(fileService.getFile(fileId)).thenReturn(file);
        when(fileService.getFileContent(file.getConfigId(), file.getPath())).thenReturn(content);
        MockHttpServletResponse response = new MockHttpServletResponse();

        fileController.downloadFile(fileId, response);

        verify(fileService).getFile(fileId);
        verify(fileService).getFileContent(100L, "task/20260509/report_1778307118261.txt");
        assertEquals("hello", response.getContentAsString(StandardCharsets.UTF_8));
        assertEquals("attachment;filename=report.txt", response.getHeader("Content-Disposition"));
    }

}
