/** * Config Route 测试 * * 测试配置管理 REST API 端点 */ import { describe, it, expect, beforeEach, vi } from 'vitest'; import { Hono } from 'hono'; import { configRouter, getConfig, setConfig } from '../../../src/routes/config.js'; // Create test app const app = new Hono(); app.route('/config', configRouter); describe('Config Route', () => { beforeEach(() => { // Reset config before each test setConfig({ workdir: '/default/workdir' }); }); describe('GET /config - 获取配置', () => { it('返回当前配置', async () => { setConfig({ workdir: '/test/workdir' }); const res = await app.request('/config'); const json = await res.json(); expect(res.status).toBe(200); expect(json.success).toBe(true); expect(json.data.workdir).toBe('/test/workdir'); }); }); describe('PUT /config - 更新配置', () => { it('更新配置', async () => { const res = await app.request('/config', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ workdir: '/new/workdir' }), }); const json = await res.json(); expect(res.status).toBe(200); expect(json.success).toBe(true); expect(json.data.workdir).toBe('/new/workdir'); }); it('合并配置而不是完全替换', async () => { setConfig({ workdir: '/original' }); const res = await app.request('/config', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ workdir: '/updated' }), }); const json = await res.json(); expect(res.status).toBe(200); expect(json.data.workdir).toBe('/updated'); }); it('无效 JSON 返回 400', async () => { const res = await app.request('/config', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: 'invalid json', }); const json = await res.json(); expect(res.status).toBe(400); expect(json.success).toBe(false); }); }); describe('PATCH /config - 部分更新配置', () => { it('部分更新配置', async () => { setConfig({ workdir: '/original' }); const res = await app.request('/config', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ workdir: '/patched' }), }); const json = await res.json(); expect(res.status).toBe(200); expect(json.success).toBe(true); expect(json.data.workdir).toBe('/patched'); }); it('忽略不存在的字段', async () => { setConfig({ workdir: '/original' }); const res = await app.request('/config', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ unknownField: 'value' }), }); const json = await res.json(); expect(res.status).toBe(200); expect(json.data.workdir).toBe('/original'); expect(json.data.unknownField).toBeUndefined(); }); it('无效 JSON 返回 400', async () => { const res = await app.request('/config', { method: 'PATCH', headers: { 'Content-Type': 'application/json' }, body: 'invalid json', }); const json = await res.json(); expect(res.status).toBe(400); expect(json.success).toBe(false); }); }); describe('getConfig / setConfig - 内部函数', () => { it('getConfig 返回当前配置', () => { setConfig({ workdir: '/test' }); const config = getConfig(); expect(config.workdir).toBe('/test'); }); it('getConfig 返回副本,不影响原配置', () => { setConfig({ workdir: '/original' }); const config = getConfig(); config.workdir = '/modified'; const freshConfig = getConfig(); expect(freshConfig.workdir).toBe('/original'); }); it('setConfig 合并配置', () => { setConfig({ workdir: '/first' }); setConfig({ workdir: '/second' }); const config = getConfig(); expect(config.workdir).toBe('/second'); }); }); });