P5 ES 集成: - config/elasticsearch.ts: 惰性初始化 + ik_max_word→standard 回退 - shared/sync/es-sync.worker.ts: Kafka 消费 question 事件并索引 ES - questions search API: ES 优先, MySQL LIKE 降级 - ensureQuestionIndex() 幂等创建, IK 不可用回退 standard - main.ts: 启动 ensureQuestionIndex + esSyncWorker 生命周期管理 P6+ 审核工作流/可视化/可观测性: - Question 状态机: draft→pending_review→published→archived - 非法转换拦截 - 知识图谱可视化 API: Neo4j 优先 + MySQL 降级 - Cypher 返回标量避免 Node 包装对象问题 - 教材版本管理: GET /textbooks/versions + archive - 5 个 Prometheus 指标 + /readyz Outbox 积压检查 Docker 本地测试 (8 类全通过): - healthz/readyz (5 依赖 ok) - REST CRUD (textbook/chapter/kp/question) - ES 全文检索命中 - 审核工作流状态机 (合法/非法转换) - Outbox 事件驱动 (8 事件全 published) - Neo4j 同步 (KnowledgePoint 节点创建) - 可视化 (nodes/edges 正确) - Prometheus 指标 docs/nextstep.md: 上游 (MySQL/Neo4j/Kafka/Redis/ES/ai) + 下游 (teacher-bff/student-bff/parent-bff/data-ana/api-gateway/ai)
170 lines
5.6 KiB
TypeScript
170 lines
5.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { QuestionsController } from "./questions.controller.js";
|
|
|
|
vi.mock("./questions.service.js", () => ({
|
|
QuestionsService: vi.fn(),
|
|
}));
|
|
|
|
const mockService = {
|
|
createQuestion: vi.fn(),
|
|
list: vi.fn(),
|
|
listByKnowledgePoint: vi.fn(),
|
|
getQuestion: vi.fn(),
|
|
updateQuestion: vi.fn(),
|
|
deleteQuestion: vi.fn(),
|
|
searchQuestions: vi.fn(),
|
|
submitForReview: vi.fn(),
|
|
approveQuestion: vi.fn(),
|
|
rejectQuestion: vi.fn(),
|
|
archiveQuestion: vi.fn(),
|
|
};
|
|
|
|
describe("QuestionsController", () => {
|
|
let controller: QuestionsController;
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
controller = new QuestionsController(mockService as never);
|
|
});
|
|
|
|
describe("create", () => {
|
|
it("should parse input and call service.createQuestion", async () => {
|
|
mockService.createQuestion.mockResolvedValue({ id: "q-1" });
|
|
const result = await controller.create({
|
|
knowledgePointId: "kp-1",
|
|
type: "single_choice",
|
|
content: "What is 2+2?",
|
|
answer: "4",
|
|
createdBy: "user-1",
|
|
});
|
|
expect(mockService.createQuestion).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
knowledgePointId: "kp-1",
|
|
type: "single_choice",
|
|
answer: "4",
|
|
}),
|
|
);
|
|
expect(result).toEqual({ success: true, data: { id: "q-1" } });
|
|
});
|
|
|
|
it("should throw on invalid body", async () => {
|
|
await expect(
|
|
controller.create({ knowledgePointId: "kp-1", type: "invalid" }),
|
|
).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
describe("list", () => {
|
|
it("should parse query and call service.list", async () => {
|
|
const data = [{ id: "q-1" }];
|
|
mockService.list.mockResolvedValue(data);
|
|
const result = await controller.list({ page: "1", pageSize: "20" });
|
|
expect(mockService.list).toHaveBeenCalledWith(
|
|
expect.objectContaining({ page: 1, pageSize: 20 }),
|
|
);
|
|
expect(result).toEqual({ success: true, data });
|
|
});
|
|
});
|
|
|
|
describe("listByKnowledgePoint", () => {
|
|
it("should call service.listByKnowledgePoint", async () => {
|
|
const data = [{ id: "q-1" }];
|
|
mockService.listByKnowledgePoint.mockResolvedValue(data);
|
|
const result = await controller.listByKnowledgePoint("kp-1");
|
|
expect(mockService.listByKnowledgePoint).toHaveBeenCalledWith("kp-1");
|
|
expect(result).toEqual({ success: true, data });
|
|
});
|
|
});
|
|
|
|
describe("search", () => {
|
|
it("should call service.searchQuestions and return paginated data", async () => {
|
|
mockService.searchQuestions.mockResolvedValue({
|
|
items: [{ id: "q-1" }],
|
|
total: 1,
|
|
});
|
|
const result = await controller.search({ q: "math" });
|
|
expect(mockService.searchQuestions).toHaveBeenCalled();
|
|
expect(result.data.total).toBe(1);
|
|
expect(result.data.items).toHaveLength(1);
|
|
});
|
|
});
|
|
|
|
describe("getById", () => {
|
|
it("should call service.getQuestion", async () => {
|
|
const data = { id: "q-1", content: "Q" };
|
|
mockService.getQuestion.mockResolvedValue(data);
|
|
const result = await controller.getById("q-1");
|
|
expect(mockService.getQuestion).toHaveBeenCalledWith("q-1");
|
|
expect(result).toEqual({ success: true, data });
|
|
});
|
|
});
|
|
|
|
describe("update", () => {
|
|
it("should parse input and call service.updateQuestion", async () => {
|
|
const result = await controller.update("q-1", { content: "New content" });
|
|
expect(mockService.updateQuestion).toHaveBeenCalledWith("q-1", {
|
|
content: "New content",
|
|
});
|
|
expect(result).toEqual({ success: true, data: { success: true } });
|
|
});
|
|
|
|
it("should throw on invalid status", async () => {
|
|
await expect(
|
|
controller.update("q-1", { status: "invalid" }),
|
|
).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
describe("remove", () => {
|
|
it("should call service.deleteQuestion", async () => {
|
|
const result = await controller.remove("q-1");
|
|
expect(mockService.deleteQuestion).toHaveBeenCalledWith("q-1");
|
|
expect(result).toEqual({ success: true, data: { success: true } });
|
|
});
|
|
});
|
|
|
|
// P6.1: 状态机端点测试
|
|
describe("submitReview", () => {
|
|
it("should call service.submitForReview", async () => {
|
|
const result = await controller.submitReview("q-1");
|
|
expect(mockService.submitForReview).toHaveBeenCalledWith("q-1");
|
|
expect(result).toEqual({ success: true, data: { success: true } });
|
|
});
|
|
});
|
|
|
|
describe("approve", () => {
|
|
it("should call service.approveQuestion", async () => {
|
|
const result = await controller.approve("q-1");
|
|
expect(mockService.approveQuestion).toHaveBeenCalledWith("q-1");
|
|
expect(result).toEqual({ success: true, data: { success: true } });
|
|
});
|
|
});
|
|
|
|
describe("reject", () => {
|
|
it("should parse reason and call service.rejectQuestion", async () => {
|
|
const result = await controller.reject("q-1", { reason: "bad content" });
|
|
expect(mockService.rejectQuestion).toHaveBeenCalledWith(
|
|
"q-1",
|
|
"bad content",
|
|
);
|
|
expect(result).toEqual({ success: true, data: { success: true } });
|
|
});
|
|
|
|
it("should throw on missing reason", async () => {
|
|
await expect(controller.reject("q-1", {})).rejects.toThrow();
|
|
});
|
|
|
|
it("should throw on empty reason", async () => {
|
|
await expect(controller.reject("q-1", { reason: "" })).rejects.toThrow();
|
|
});
|
|
});
|
|
|
|
describe("archive", () => {
|
|
it("should call service.archiveQuestion", async () => {
|
|
const result = await controller.archive("q-1");
|
|
expect(mockService.archiveQuestion).toHaveBeenCalledWith("q-1");
|
|
expect(result).toEqual({ success: true, data: { success: true } });
|
|
});
|
|
});
|
|
});
|