56 lines
3.1 KiB
TypeScript
56 lines
3.1 KiB
TypeScript
import { test } from 'node:test'
|
||
import assert from 'node:assert/strict'
|
||
import { analyzeMail, parseAgentDecision, AgentResponseError } from '../src/main/services/mail-agent-client.ts'
|
||
import type { AgentRequest } from '../src/main/services/mail-agent-client.ts'
|
||
|
||
const request: AgentRequest = {
|
||
url: 'http://localhost:11434/v1/', model: 'local-model', apiKey: 'secret',
|
||
email: 'user@example.org', name: 'Пользователь', text: 'Ответьте до завтра',
|
||
envelope: {
|
||
id: '1', folderId: 'INBOX', subject: 'Вопрос', from: { name: 'Коллега', email: 'other@example.org' },
|
||
to: [{ name: 'Пользователь', email: 'user@example.org' }], cc: [], date: '2026-11-16',
|
||
flags: { seen: false, flagged: false, answered: false, deleted: false },
|
||
hasAttachments: false, snippet: ''
|
||
}
|
||
}
|
||
|
||
test('разбирает решение с причиной и JSON в блоке кода', (): void => {
|
||
assert.deepEqual(parseAgentDecision('```json\n{"notify":true,"reason":" Нужен ответ "}\n```'), {
|
||
notify: true, reason: 'Нужен ответ'
|
||
})
|
||
})
|
||
|
||
test('принимает решение без уведомления', (): void => {
|
||
assert.deepEqual(parseAgentDecision('{"notify":false,"reason":"Рассылка"}'), { notify: false, reason: 'Рассылка' })
|
||
})
|
||
|
||
test('отклоняет неполные решения и неверные типы', (): void => {
|
||
for (const content of ['null', '{}', '{"notify":"false","reason":"x"}', '{"notify":true,"reason":""}']) {
|
||
assert.throws(() => parseAgentDecision(content), AgentResponseError)
|
||
}
|
||
assert.throws(() => parseAgentDecision('не JSON'), SyntaxError)
|
||
})
|
||
|
||
test('передаёт текст, адрес пользователя и ключ в локальный API', async (): Promise<void> => {
|
||
const send: typeof fetch = async (url, options): Promise<Response> => {
|
||
assert.equal(url, 'http://localhost:11434/v1/chat/completions')
|
||
assert.equal(new Headers(options?.headers).get('Authorization'), 'Bearer secret')
|
||
assert.equal(options?.redirect, 'error')
|
||
const payload: { model: string; messages: { content: string }[] } = JSON.parse(String(options?.body))
|
||
assert.equal(payload.model, 'local-model')
|
||
assert.match(payload.messages[1].content, /user@example.org/)
|
||
assert.match(payload.messages[1].content, /Ответьте до завтра/)
|
||
return Response.json({ choices: [{ message: { content: '{"notify":true,"reason":"Нужен ответ"}' } }] })
|
||
}
|
||
assert.deepEqual(await analyzeMail(request, send), { notify: true, reason: 'Нужен ответ' })
|
||
})
|
||
|
||
test('явно сообщает об ошибке сервера', async (): Promise<void> => {
|
||
const send: typeof fetch = async (): Promise<Response> => new Response('', { status: 503 })
|
||
await assert.rejects(analyzeMail(request, send), /HTTP 503/)
|
||
})
|
||
|
||
test('отклоняет ответ без решения', async (): Promise<void> => {
|
||
const send: typeof fetch = async (): Promise<Response> => Response.json({ choices: [] })
|
||
await assert.rejects(analyzeMail(request, send), AgentResponseError)
|
||
})
|