Remove the general-purpose tool/function-calling machinery from openrouter.ts (loadSkills usage, buildTools, executeTool, tool_calls request/response handling) and the recovery helpers it existed to patch (extractFakeToolCall/extractBareJsonRetry in toolCallRecovery.ts), since every skill that plumbing could call has now been deleted. Delete src/main/skills/ entirely (types.ts, argUtils.ts, index.ts), now empty. Strip the system prompt paragraphs describing tool usage. getAssistantReply() keeps the core chat loop: send the user's message to OpenRouter with DeepSeek/Qwen fallback, the malformed-response guard, degenerate-text detection and requireLoggedIn(), and return the assistant's plain-text reply. Drop the now-stale coverage exclude for the already-removed memory/store.ts. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
91 lines
3.2 KiB
TypeScript
91 lines
3.2 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
|
import { setCurrentUserEmail, clearSession } from '../../src/main/auth/session'
|
|
|
|
vi.mock('@electron-toolkit/utils', () => ({ is: { dev: false } }))
|
|
|
|
const { getAssistantReply } = await import('../../src/main/ai/openrouter')
|
|
|
|
function okResponse(body: unknown): Response {
|
|
return { ok: true, json: () => Promise.resolve(body) } as Response
|
|
}
|
|
|
|
function textMessage(content: string): unknown {
|
|
return { choices: [{ message: { role: 'assistant', content } }] }
|
|
}
|
|
|
|
describe('getAssistantReply()', () => {
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
vi.stubGlobal('fetch', vi.fn())
|
|
process.env.OPENROUTER_API_KEY = 'test-key'
|
|
setCurrentUserEmail('test@example.com')
|
|
})
|
|
|
|
afterEach(() => {
|
|
clearSession()
|
|
})
|
|
|
|
it('should throw when the API key is missing', async () => {
|
|
delete process.env.OPENROUTER_API_KEY
|
|
await expect(getAssistantReply('Bonjour')).rejects.toThrow(/OPENROUTER_API_KEY/)
|
|
})
|
|
|
|
it('should return the assistant text reply when the API responds successfully', async () => {
|
|
vi.mocked(fetch).mockResolvedValue(okResponse(textMessage('Bonjour, comment puis-je aider ?')))
|
|
const reply = await getAssistantReply('Salut Max')
|
|
expect(reply).toBe('Bonjour, comment puis-je aider ?')
|
|
})
|
|
|
|
it('should transparently fall back to the secondary model when the primary one errors out', async () => {
|
|
vi.mocked(fetch)
|
|
.mockResolvedValueOnce({
|
|
ok: false,
|
|
status: 503,
|
|
text: () => Promise.resolve('unavailable')
|
|
} as Response)
|
|
.mockResolvedValueOnce(okResponse(textMessage('Reponse du modele de secours.')))
|
|
|
|
const reply = await getAssistantReply('question')
|
|
expect(reply).toBe('Reponse du modele de secours.')
|
|
})
|
|
|
|
it('should treat a 200 response with no choices as a failure and try the other model', async () => {
|
|
vi.mocked(fetch)
|
|
.mockResolvedValueOnce(okResponse({}))
|
|
.mockResolvedValueOnce(okResponse(textMessage('Ca marche quand meme.')))
|
|
|
|
const reply = await getAssistantReply('question')
|
|
expect(reply).toBe('Ca marche quand meme.')
|
|
})
|
|
|
|
it('should throw a clear error when both models fail', async () => {
|
|
vi.mocked(fetch).mockResolvedValue({
|
|
ok: false,
|
|
status: 500,
|
|
text: () => Promise.resolve('panne totale')
|
|
} as Response)
|
|
|
|
await expect(getAssistantReply('question')).rejects.toThrow(/Aucun modele disponible/)
|
|
})
|
|
|
|
it('should replace a leaked English boilerplate reply with the safe fallback message', async () => {
|
|
vi.mocked(fetch).mockResolvedValue(
|
|
okResponse(
|
|
textMessage(
|
|
'Use the results below to formulate an answer to the user question with additional information needed'
|
|
)
|
|
)
|
|
)
|
|
const reply = await getAssistantReply('question')
|
|
expect(reply).toMatch(/bien avance avec toi/)
|
|
})
|
|
|
|
it('should replace a degenerate (non-Latin) reply with the safe fallback message', async () => {
|
|
vi.mocked(fetch).mockResolvedValue(
|
|
okResponse(textMessage('的问题是什么呢我不知道该怎么办才好呢'))
|
|
)
|
|
const reply = await getAssistantReply('question')
|
|
expect(reply).toMatch(/bien avance avec toi/)
|
|
})
|
|
})
|