package org.dromara.web.controller;

import cn.dev33.satoken.stp.StpUtil;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.AccountCancelBody;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.web.service.AccountCancellationService;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
@Tag("dev")
class AuthControllerUnitTest {

    @Mock
    private AccountCancellationService accountCancellationService;

    @Test
    void cancelAccount_checksLoginCancelsAccountAndLogsOut() {
        AuthController controller = new AuthController(
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            null,
            accountCancellationService
        );

        try (MockedStatic<StpUtil> stpUtil = mockStatic(StpUtil.class);
             MockedStatic<LoginHelper> loginHelper = mockStatic(LoginHelper.class)) {
            AccountCancelBody body = new AccountCancelBody();
            body.setCode("123456");
            loginHelper.when(LoginHelper::getUserId).thenReturn(100L);

            R<Void> result = controller.cancelAccount(body);

            assertThat(result.getCode()).isEqualTo(200);
            verify(accountCancellationService).cancelCurrentAccount("123456");
            stpUtil.verify(StpUtil::checkLogin);
            stpUtil.verify(() -> StpUtil.logout(100L));
        }
    }
}
