aero-mail/tests/mail-agent-settings-persistence.test.ts

49 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { test } from 'node:test'
import assert from 'node:assert/strict'
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import Conf from 'conf'
import { loadAgentTestModule } from './helpers/agent-module.ts'
import { DEFAULT_AGENT_PROMPT } from '../src/shared/constants/agent.ts'
import type { MailAgentService, AgentStoreSchema } from '../src/main/services/mail-agent-service.ts'
test('сохраняет пользовательский промпт на диск и загружает его после повторного создания сервиса', async (): Promise<void> => {
const directory: string = await mkdtemp(join(tmpdir(), 'aero-agent-settings-'))
const { server, Service, History } = await loadAgentTestModule()
try {
const defaults: AgentStoreSchema = {
settings: { enabled: false, notifyMutedFolders: false, prompt: DEFAULT_AGENT_PROMPT, pageSize: 100, batchSize: 1, concurrency: 2, url: 'http://localhost:11434/v1', model: 'model' },
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0, measuredResponses: 0, unmeasuredResponses: 0 },
processed: {}, baselines: {}
}
const createStore = (): Conf<AgentStoreSchema> => new Conf<AgentStoreSchema>({ cwd: directory, configName: 'mail-agent', defaults })
const createService = (store: Conf<AgentStoreSchema>): MailAgentService => new Service({
getAccounts: (): never => { throw new Error('Аккаунты в тесте не используются') },
getConnectedAccountIds: (): string[] => [],
listFolders: async (): Promise<never> => { throw new Error('IMAP в тесте запрещён') },
listMessagesAfterUid: async (): Promise<never> => { throw new Error('IMAP в тесте запрещён') },
listMessages: async (): Promise<never> => { throw new Error('IMAP в тесте запрещён') },
fetchMessageBody: async (): Promise<never> => { throw new Error('IMAP в тесте запрещён') }
}, store, {
load: async (): Promise<void> => {},
get: (): undefined => undefined,
save: async (): Promise<never> => { throw new Error('Ключ в тесте не изменяется') }
}, (): void => {}, (message: string): never => { throw new Error(message) }, (): void => {}, new History(store))
const store: Conf<AgentStoreSchema> = createStore()
const first: MailAgentService = createService(store)
const customPrompt: string = 'Уведомляй о задачах для меня. Верни JSON с notify и reason.'
const saved = await first.saveSettings({ ...defaults.settings, prompt: customPrompt, apiKey: null })
assert.equal(saved.prompt, customPrompt)
store.set('processed', { example: { notify: true, reason: 'Задача' } })
const file: { settings: { prompt: string } } = JSON.parse(await readFile(join(directory, 'mail-agent.json'), 'utf8'))
assert.equal(file.settings.prompt, customPrompt)
const reopened: MailAgentService = createService(createStore())
assert.equal(reopened.getSettings().prompt, customPrompt)
await reopened.saveSettings({ ...reopened.getSettings(), apiKey: null, prompt: DEFAULT_AGENT_PROMPT })
assert.equal(createService(createStore()).getSettings().prompt, DEFAULT_AGENT_PROMPT)
} finally {
await server.close()
await rm(directory, { recursive: true, force: true })
}
})