package com.example.volunteer;

import com.example.volunteer.repository.EventRepository;
import com.example.volunteer.web.EventController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

import java.util.List;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * ============================================================================
 * 附：MVC 切片测试（样卷只考数据层，这个作对照）
 * ============================================================================
 *
 * EN — @WebMvcTest boots only the web layer; the repository is replaced by a mock so the
 *      test never touches a database. Useful for asserting status codes and view names.
 * 中 — @WebMvcTest 只启动 Web 层，Repository 用 mock 替代，不碰数据库；
 *      适合断言状态码与视图名。
 */
@WebMvcTest(EventController.class)
class EventControllerTest {

    @Autowired private MockMvc mockMvc;

    @MockBean private EventRepository eventRepository;

    @Test
    void listPageRendersEventsView() throws Exception {
        given(eventRepository.findAllWithSignupsAndVolunteers()).willReturn(List.of());

        mockMvc.perform(get("/events"))
                .andExpect(status().isOk())
                .andExpect(view().name("events"))
                .andExpect(model().attributeExists("events"));
    }
}
