5835799b69
- 新增 vitest 配置和测试基础设施 - 添加 adapter.test.ts: 测试 Core 模块初始化和 Agent 管理 (18 tests) - 添加 token.test.ts: 测试 Token 生成、验证和中间件 (25 tests) - 添加 handler.test.ts: 测试权限处理器 (18 tests) - 添加 ws.test.ts: 测试 WebSocket 连接和消息处理 (19 tests) - 添加 sse.test.ts: 测试 SSE 事件发送 (14 tests) - 添加 sessions.test.ts: 测试会话路由 (16 tests) - 添加 config.test.ts: 测试配置路由 (10 tests) - 添加 context.test.ts: 测试上下文压缩路由 (9 tests) - 添加 providers.test.ts: 测试 Provider 管理路由 (18 tests) - 添加 manager.test.ts: 测试 SessionManager (48 tests) 总计 195 个测试,覆盖率从 0% 提升至 29.59%
144 lines
4.1 KiB
TypeScript
144 lines
4.1 KiB
TypeScript
/**
|
|
* 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');
|
|
});
|
|
});
|
|
});
|