mirror of
https://github.com/Syngnat/GoNavi.git
synced 2026-06-21 22:14:02 +08:00
- 将连接能力、外部 SQL、本地查询资产探针测试拆入独立文件 - 继续缩减 aiLocalToolExecutor.test.ts,降低单文件维护成本 - 通过定向测试和生产构建验证
144 lines
4.4 KiB
TypeScript
144 lines
4.4 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
import type { AIToolCall, SavedConnection } from '../../types';
|
|
import { executeLocalAIToolCall } from './aiLocalToolExecutor';
|
|
|
|
const buildConnection = (): SavedConnection => ({
|
|
id: 'conn-1',
|
|
name: '主库',
|
|
config: {
|
|
type: 'mysql',
|
|
host: '127.0.0.1',
|
|
port: 3306,
|
|
user: 'root',
|
|
},
|
|
});
|
|
|
|
const buildToolCall = (name: string, args: Record<string, unknown>): AIToolCall => ({
|
|
id: `call-${name}`,
|
|
type: 'function',
|
|
function: {
|
|
name,
|
|
arguments: JSON.stringify(args),
|
|
},
|
|
});
|
|
|
|
describe('aiLocalToolExecutor local asset inspection tools', () => {
|
|
it('returns local saved queries so the model can reuse historical sql scripts', async () => {
|
|
const result = await executeLocalAIToolCall({
|
|
toolCall: buildToolCall('inspect_saved_queries', {
|
|
keyword: '支付',
|
|
connectionId: 'conn-1',
|
|
}),
|
|
connections: [buildConnection()],
|
|
mcpTools: [],
|
|
toolContextMap: new Map(),
|
|
savedQueries: [
|
|
{
|
|
id: 'saved-1',
|
|
name: '支付订单核对',
|
|
sql: 'SELECT * FROM orders WHERE status = \'paid\'',
|
|
connectionId: 'conn-1',
|
|
dbName: 'crm',
|
|
createdAt: 2,
|
|
},
|
|
{
|
|
id: 'saved-2',
|
|
name: '用户列表',
|
|
sql: 'SELECT * FROM users',
|
|
connectionId: 'conn-1',
|
|
dbName: 'crm',
|
|
createdAt: 1,
|
|
},
|
|
],
|
|
runtime: {
|
|
getDatabases: vi.fn(),
|
|
getTables: vi.fn(),
|
|
},
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.content).toContain('"totalMatched":1');
|
|
expect(result.content).toContain('支付订单核对');
|
|
expect(result.content).toContain('"connectionName":"主库"');
|
|
expect(result.content).toContain('status = \'paid\'');
|
|
});
|
|
|
|
it('returns local ai chat sessions so the model can locate previous conversations by title or preview', async () => {
|
|
const result = await executeLocalAIToolCall({
|
|
toolCall: buildToolCall('inspect_ai_sessions', {
|
|
keyword: '支付',
|
|
limit: 5,
|
|
}),
|
|
connections: [buildConnection()],
|
|
mcpTools: [],
|
|
toolContextMap: new Map(),
|
|
aiChatSessions: [
|
|
{ id: 'session-1', title: '支付异常排查', updatedAt: 200 },
|
|
{ id: 'session-2', title: '用户列表', updatedAt: 100 },
|
|
],
|
|
aiChatHistory: {
|
|
'session-1': [
|
|
{ id: 'msg-1', role: 'user', content: '帮我排查支付超时', timestamp: 101 },
|
|
{ id: 'msg-2', role: 'assistant', content: '先看最近错误日志', timestamp: 102 },
|
|
],
|
|
'session-2': [
|
|
{ id: 'msg-3', role: 'user', content: '列出最近注册用户', timestamp: 103 },
|
|
],
|
|
},
|
|
activeSessionId: 'session-2',
|
|
runtime: {
|
|
getDatabases: vi.fn(),
|
|
getTables: vi.fn(),
|
|
},
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.content).toContain('"totalMatched":1');
|
|
expect(result.content).toContain('支付异常排查');
|
|
expect(result.content).toContain('帮我排查支付超时');
|
|
expect(result.content).toContain('先看最近错误日志');
|
|
expect(result.content).not.toContain('列出最近注册用户');
|
|
});
|
|
|
|
it('returns sql snippets so the model can inspect local query templates', async () => {
|
|
const result = await executeLocalAIToolCall({
|
|
toolCall: buildToolCall('inspect_sql_snippets', {
|
|
keyword: '支付',
|
|
}),
|
|
connections: [buildConnection()],
|
|
mcpTools: [],
|
|
toolContextMap: new Map(),
|
|
sqlSnippets: [
|
|
{
|
|
id: 'snippet-1',
|
|
prefix: 'sel',
|
|
name: 'SELECT 模板',
|
|
body: 'SELECT * FROM ${1:table};',
|
|
isBuiltin: true,
|
|
createdAt: 1,
|
|
},
|
|
{
|
|
id: 'snippet-2',
|
|
prefix: 'pay',
|
|
name: '支付模板',
|
|
description: '支付对账',
|
|
body: 'SELECT * FROM pay_orders WHERE created_at >= ${1:start};',
|
|
isBuiltin: false,
|
|
createdAt: 2,
|
|
},
|
|
],
|
|
runtime: {
|
|
getDatabases: vi.fn(),
|
|
getTables: vi.fn(),
|
|
},
|
|
});
|
|
|
|
expect(result.success).toBe(true);
|
|
expect(result.content).toContain('"totalMatched":1');
|
|
expect(result.content).toContain('"prefix":"pay"');
|
|
expect(result.content).toContain('"customCount":1');
|
|
expect(result.content).toContain('pay_orders');
|
|
});
|
|
});
|