package com.njcn.rdms.module.project.service.project.task.worklog;

import com.njcn.rdms.framework.common.exception.ServiceException;
import com.njcn.rdms.framework.test.core.ut.BaseMockitoUnitTest;
import com.njcn.rdms.module.project.enums.ErrorCodeConstants;
import com.njcn.rdms.module.project.enums.ProjectDictTypeConstants;
import com.njcn.rdms.module.system.api.dict.DictDataApi;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

/**
 * 仅测 {@link TaskWorklogServiceImpl#validateDifficulty(String)} 的三个分支。
 * createWorklog / updateWorklog 端到端单测留待后续单测建设独立立项。
 */
class TaskWorklogServiceImplValidateDifficultyTest extends BaseMockitoUnitTest {

    @InjectMocks
    private TaskWorklogServiceImpl taskWorklogService;

    @Mock
    private DictDataApi dictDataApi;

    @Test
    void validateDifficulty_whenDictReturnsTrue_shouldNotThrow() {
        when(dictDataApi.validateDictDataList(eq(ProjectDictTypeConstants.WORKLOG_DIFFICULTY), any()))
                .thenReturn(success(true));

        assertDoesNotThrow(() -> taskWorklogService.validateDifficulty("2"));
    }

    @Test
    void validateDifficulty_whenDictReturnsFalse_shouldThrowInvalid() {
        when(dictDataApi.validateDictDataList(eq(ProjectDictTypeConstants.WORKLOG_DIFFICULTY), any()))
                .thenReturn(success(false));

        ServiceException ex = assertThrows(ServiceException.class,
                () -> taskWorklogService.validateDifficulty("99"));

        assertEquals(ErrorCodeConstants.PROJECT_TASK_WORKLOG_DIFFICULTY_INVALID.getCode(), ex.getCode());
    }

    @Test
    void validateDifficulty_whenDictThrowsRuntimeException_shouldThrowInvalid() {
        when(dictDataApi.validateDictDataList(eq(ProjectDictTypeConstants.WORKLOG_DIFFICULTY), any()))
                .thenThrow(new RuntimeException("非法字典值"));

        ServiceException ex = assertThrows(ServiceException.class,
                () -> taskWorklogService.validateDifficulty("99"));

        assertEquals(ErrorCodeConstants.PROJECT_TASK_WORKLOG_DIFFICULTY_INVALID.getCode(), ex.getCode());
    }
}
