import { describe, it, expect, beforeAll, afterAll } from 'vitest'; import { Hono } from 'hono'; import { createServer } from '../../src/server.js'; import { Logger } from '../../src/logger.js'; import { DetailLogger } from '../../src/detail-logger.js'; import type { ProviderConfig, ApiKey, ProxyConfig } from '../../src/config.js'; import { tmpdir } from 'os'; import { join } from 'path'; import { writeFileSync, rmSync, readFileSync } from 'fs'; describe('Admin Models Form E2E', () => { let app: Hono; let testLogDir: string; let testConfigPath: string; let originalFetch: typeof fetch; beforeAll(() => { testLogDir = join(tmpdir(), 'test-models-form-' + Date.now()); testConfigPath = join(testLogDir, 'config.json'); const logger = new Logger(testLogDir); const detailLogger = new DetailLogger(testLogDir); // 创建测试模型配置 const testModels: ProviderConfig[] = [ { customModel: 'test-gpt4', realModel: 'gpt-4', apiKey: 'sk-test-key', baseUrl: 'https://api.openai.com/v1', provider: 'openai' } ]; // 创建测试 API Keys const testApiKeys: ApiKey[] = [ { id: 'key-1', name: 'My OpenAI Key', key: 'sk-openai-123', createdAt: 1700000000000, updatedAt: 1700000000000 }, { id: 'key-2', name: 'My Anthropic Key', key: 'sk-anthropic-456', createdAt: 1700000000000, updatedAt: 1700000000000 } ]; // 创建测试 ProxyConfig 对象 const testConfig: ProxyConfig = { models: testModels, apiKeys: testApiKeys }; // 创建配置文件(包含 models 和 apiKeys) writeFileSync( testConfigPath, JSON.stringify(testConfig, null, 2) ); app = createServer(testConfig, logger, detailLogger, 30000, testLogDir); originalFetch = globalThis.fetch; }); afterAll(() => { globalThis.fetch = originalFetch; rmSync(testLogDir, { recursive: true, force: true }); }); describe('新增模型页面', () => { it('应显示已配置的 API Key 下拉选项', async () => { const response = await app.request('/admin/models/new'); expect(response.status).toBe(200); const html = await response.text(); // 应该包含两个 API Key 选项 expect(html).toContain('My OpenAI Key'); expect(html).toContain('My Anthropic Key'); }); it('API Provider 下拉应包含 response-api (Responses API) 选项', async () => { const response = await app.request('/admin/models/new'); expect(response.status).toBe(200); const html = await response.text(); // provider 下拉应同时包含 openai / response-api / anthropic 三类 expect(html).toContain('value="openai"'); expect(html).toContain('value="response-api"'); expect(html).toContain('value="anthropic"'); }); it('API Key 下拉选项应只显示名称,不包含 provider', async () => { const response = await app.request('/admin/models/new'); expect(response.status).toBe(200); const html = await response.text(); // 选项文本中不应包含 provider 信息 // 检查