import type { JsonAgentHistoryRepository } from '../../src/main/services/mail-agent-history.ts' import { fileURLToPath } from 'node:url' import { createServer } from 'vite' import type { ViteDevServer } from 'vite' import type { MailAgentService } from '../../src/main/services/mail-agent-service.ts' export interface AgentTestModule { server: ViteDevServer Service: typeof MailAgentService History: typeof JsonAgentHistoryRepository failRequests: () => void restoreRequests: () => void } export async function loadAgentTestModule(): Promise { const server: ViteDevServer = await createServer({ configFile: false, ssr: { noExternal: ['electron'] }, logLevel: 'silent', resolve: { alias: { electron: '\0test-electron', '@shared': fileURLToPath(new URL('../../src/shared', import.meta.url)) } }, server: { middlewareMode: true, hmr: false, ws: false, watch: null }, plugins: [{ name: 'test-electron-network', resolveId(id: string): string | undefined { return id === '\0test-electron' ? id : undefined }, load(id: string): string | undefined { return id === '\0test-electron' ? `let unavailable = false; export function failRequests() { unavailable = true; } export function restoreRequests() { unavailable = false; } export const net = { async fetch(input, options) { if (unavailable) throw new TypeError("Сервер модели недоступен"); const payload = JSON.parse(options.body); const mail = JSON.parse(payload.messages[1].content); const content = mail.messages ? {decisions: mail.messages.map(item => ({id:item.id,notify:true,reason:"Требуется ответ"}))} : {notify:true,reason:"Требуется ответ"}; return Response.json({usage:{prompt_tokens:10,completion_tokens:5,total_tokens:15},choices:[{message:{content:JSON.stringify(content)}}]}); } }` : undefined } }] }) try { const module = await server.ssrLoadModule('/src/main/services/mail-agent-service.ts') as { MailAgentService: typeof MailAgentService } const historyModule = await server.ssrLoadModule('/src/main/services/mail-agent-history.ts') as { JsonAgentHistoryRepository: typeof JsonAgentHistoryRepository } const network = await server.ssrLoadModule('\0test-electron') as { failRequests: () => void; restoreRequests: () => void } return { server, Service: module.MailAgentService, History: historyModule.JsonAgentHistoryRepository, ...network } } catch (error: unknown) { await server.close() throw error } }