feat: 实现情绪记录功能和聊天历史查看
- 完成情绪记录生成功能,支持AI分析聊天内容生成情绪记录 - 实现聊天页面历史记录查看,支持分页和搜索 - 修改日记页面展示情绪记录而非普通日记 - 添加情绪记录的增删改查API - 优化前端UI,添加情绪强度显示和详细信息展示 - 修复SCSS变量缺失问题
This commit is contained in:
+121
@@ -0,0 +1,121 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.emotion.service.AIChatService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
/**
|
||||
* 情绪总结控制器测试
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @date 2025-07-25
|
||||
*/
|
||||
@Slf4j
|
||||
@WebMvcTest(EmotionSummaryController.class)
|
||||
public class EmotionSummaryControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockBean
|
||||
private AIChatService aiChatService;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Test
|
||||
public void testGenerateEmotionSummarySuccess() throws Exception {
|
||||
// 准备测试数据
|
||||
Map<String, Object> mockResult = new HashMap<>();
|
||||
mockResult.put("success", true);
|
||||
mockResult.put("message", "情绪记录总结生成成功");
|
||||
|
||||
Map<String, Object> emotionRecord = new HashMap<>();
|
||||
emotionRecord.put("emotionType", "开心");
|
||||
emotionRecord.put("intensity", 0.8);
|
||||
emotionRecord.put("triggers", "与AI的愉快对话");
|
||||
|
||||
mockResult.put("emotionRecord", emotionRecord);
|
||||
mockResult.put("summary", "用户今天表现出积极的情绪状态");
|
||||
mockResult.put("messageCount", 10);
|
||||
|
||||
// 模拟服务调用
|
||||
when(aiChatService.generateEmotionSummary(anyString())).thenReturn(mockResult);
|
||||
|
||||
// 执行测试
|
||||
mockMvc.perform(post("/api/emotion-summary/generate/test_user_123")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
.andExpect(jsonPath("$.message").value("情绪记录总结生成成功"))
|
||||
.andExpect(jsonPath("$.data.emotionRecord.emotionType").value("开心"))
|
||||
.andExpect(jsonPath("$.data.emotionRecord.intensity").value(0.8))
|
||||
.andExpect(jsonPath("$.data.summary").value("用户今天表现出积极的情绪状态"));
|
||||
|
||||
log.info("✅ 情绪记录总结生成成功测试通过");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerateEmotionSummaryNoMessages() throws Exception {
|
||||
// 准备测试数据 - 没有聊天记录的情况
|
||||
Map<String, Object> mockResult = new HashMap<>();
|
||||
mockResult.put("success", false);
|
||||
mockResult.put("message", "今天还没有聊天记录,无法生成情绪总结");
|
||||
|
||||
// 模拟服务调用
|
||||
when(aiChatService.generateEmotionSummary(anyString())).thenReturn(mockResult);
|
||||
|
||||
// 执行测试
|
||||
mockMvc.perform(post("/api/emotion-summary/generate/test_user_456")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.success").value(false))
|
||||
.andExpect(jsonPath("$.message").value("今天还没有聊天记录,无法生成情绪总结"));
|
||||
|
||||
log.info("✅ 无聊天记录情况测试通过");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGenerateEmotionSummaryError() throws Exception {
|
||||
// 模拟服务异常
|
||||
when(aiChatService.generateEmotionSummary(anyString()))
|
||||
.thenThrow(new RuntimeException("AI服务暂时不可用"));
|
||||
|
||||
// 执行测试
|
||||
mockMvc.perform(post("/api/emotion-summary/generate/test_user_789")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.success").value(false))
|
||||
.andExpect(jsonPath("$.message").exists());
|
||||
|
||||
log.info("✅ 服务异常情况测试通过");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetEmotionSummaryStatus() throws Exception {
|
||||
// 执行测试
|
||||
mockMvc.perform(get("/api/emotion-summary/status/test_user_123")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.success").value(true))
|
||||
.andExpect(jsonPath("$.data.userId").value("test_user_123"))
|
||||
.andExpect(jsonPath("$.data.canGenerate").value(true));
|
||||
|
||||
log.info("✅ 情绪记录状态查询测试通过");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user