"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
// src/controllers/__tests__/promptsController.test.ts
const promptsController_1 = require("../promptsController");
const promptService = __importStar(require("../../services/promptService"));
// 模拟 promptService 模块
jest.mock('../../services/promptService');
// 类型断言，便于 TypeScript 识别模拟函数的类型
const mockedPromptService = jest.mocked(promptService);
describe('promptsController', () => {
    beforeEach(() => {
        jest.clearAllMocks();
    });
    describe('handlePromptsList', () => {
        it('应该正确将服务层返回的框架列表映射为 prompts/list 响应格式', async () => {
            // 模拟服务层返回的数据
            const mockFrameworks = [
                {
                    name: 'scqa',
                    description: 'SCQA 框架',
                    parameters: [
                        { name: 'situation', description: '情境', required: true },
                        { name: 'complication', description: '复杂性', required: true }
                    ],
                    template: '...', // 这些字段不会被 list 使用，但模拟时可省略
                    systemPrompt: '...'
                },
                {
                    name: 'swot',
                    description: 'SWOT 分析',
                    parameters: [
                        { name: 'entity', description: '分析对象', required: true }
                    ],
                    template: '...'
                }
            ];
            mockedPromptService.listFrameworks.mockResolvedValue(mockFrameworks);
            const result = await (0, promptsController_1.handlePromptsList)();
            // 验证返回结构符合 MCP prompts/list 规范
            expect(result).toHaveProperty('prompts');
            expect(Array.isArray(result.prompts)).toBe(true);
            expect(result.prompts).toHaveLength(2);
            // 验证每个 prompt 的字段映射正确
            expect(result.prompts[0]).toEqual({
                name: 'scqa',
                description: 'SCQA 框架',
                arguments: [
                    { name: 'situation', description: '情境', required: true },
                    { name: 'complication', description: '复杂性', required: true }
                ]
            });
            expect(result.prompts[1]).toEqual({
                name: 'swot',
                description: 'SWOT 分析',
                arguments: [
                    { name: 'entity', description: '分析对象', required: true }
                ]
            });
            // 验证服务层被调用一次
            expect(mockedPromptService.listFrameworks).toHaveBeenCalledTimes(1);
        });
        it('当服务层返回空数组时，应该返回空列表', async () => {
            mockedPromptService.listFrameworks.mockResolvedValue([]);
            const result = await (0, promptsController_1.handlePromptsList)();
            expect(result).toEqual({ prompts: [] });
            expect(mockedPromptService.listFrameworks).toHaveBeenCalledTimes(1);
        });
        it('当服务层抛出异常时，应该传播异常（由调用方处理）', async () => {
            const error = new Error('加载框架失败');
            mockedPromptService.listFrameworks.mockRejectedValue(error);
            await expect((0, promptsController_1.handlePromptsList)()).rejects.toThrow('加载框架失败');
        });
    });
    describe('handlePromptsGet', () => {
        const mockName = 'scqa';
        const mockArgs = { situation: '市场增长放缓' };
        it('当服务层成功返回时，应该返回包含 description 和 messages 的响应', async () => {
            const mockServiceResult = {
                messages: [
                    {
                        role: 'system',
                        content: { type: "text", text: '你是一名战略顾问' }
                    },
                    {
                        role: 'user',
                        content: { type: "text", text: '请分析情境：市场增长放缓' }
                    }
                ]
            };
            mockedPromptService.getFramework.mockResolvedValue(mockServiceResult);
            const result = await (0, promptsController_1.handlePromptsGet)(mockName, mockArgs);
            // 验证返回结构
            expect(result).toHaveProperty('description', '框架: scqa');
            expect(result).toHaveProperty('messages');
            expect(result.messages).toEqual(mockServiceResult.messages);
            // 验证服务层被正确调用
            expect(mockedPromptService.getFramework).toHaveBeenCalledWith(mockName, mockArgs);
        });
        it('当 args 为 undefined 时，应传入空对象给服务层', async () => {
            const mockServiceResult = { messages: [] };
            mockedPromptService.getFramework.mockResolvedValue(mockServiceResult);
            // 调用时第二个参数为 undefined
            await (0, promptsController_1.handlePromptsGet)(mockName, undefined);
            expect(mockedPromptService.getFramework).toHaveBeenCalledWith(mockName, {} // 预期被转换为空对象
            );
        });
        it('当服务层抛出错误时，应重新抛出错误，并包装错误信息', async () => {
            const serviceError = new Error('框架不存在');
            mockedPromptService.getFramework.mockRejectedValue(serviceError);
            // 验证抛出的错误包含原始信息
            await expect((0, promptsController_1.handlePromptsGet)(mockName, mockArgs)).rejects.toThrow('获取框架失败: 框架不存在');
        });
        it('当服务层抛出非 Error 类型时，也应正确处理', async () => {
            // 模拟服务层抛出一个字符串
            mockedPromptService.getFramework.mockRejectedValue('some string error');
            await expect((0, promptsController_1.handlePromptsGet)(mockName, mockArgs)).rejects.toThrow('获取框架失败: some string error');
        });
    });
});
//# sourceMappingURL=promptsController.test.js.map